<template>
    <el-dialog v-model="show" v-loading="loading" :show-close="false" :close-on-press-escape="false" :close-on-click-modal="false" style="max-width:400px;">
        <template #header>
            <div class="text-title">{{folderId==0?'新建':'修改'}}文件夹</div>
        </template>
        <div>
            <el-input v-model="name" placeholder="请输入文件夹名称" class="full-width" />
        </div>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="()=>closedhandler()">取消</el-button>
                <el-button type="primary" @click="()=>saveFolder()"> {{folderId==0?'立即创建':'保存修改'}} </el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { ApiResult } from '@/configs/axios';
import FolderService from '@/services/FolderService';
import { ElMessage } from 'element-plus';
import { ref } from 'vue';
//FolderType 文件夹类型(1-行程，2-广告)
const props = defineProps({
    folderId:{
        type:Number,
        default:0
    },
    parentId:{
        type:Number,
        default:0
    },
    folderName:{
        type:String,
        default:''
    },
    folderType:{
        type:String,
        default:1
    }
})
    const emit = defineEmits<{
  (event: 'close'): void,
  (event: 'success'): void
}>()
const show=ref(true)
const loading = ref(false)
const name = ref(props.folderName)

const closedhandler=()=>{
    emit('close')
}
const saveFolder=async ()=>{
    let errmsg = ''
    if(name.value=='') errmsg='请输入文件夹名称'
    if(errmsg==''){
        try {
            let response = await FolderService.SetFolderAsync(props.folderId,name.value,props.parentId,props.folderType)
            if(response.data.resultCode==ApiResult.SUCCESS){
                emit('close')
                emit('success')
                ElMessage.success({
                    message:'操作成功'
                })
            } else errmsg='操作失败，请重试'
        } catch (error) {
            errmsg = '操作失败，请重试'
        } 
    } 
    if(errmsg!='') {
        ElMessage.error({
            message:errmsg
        })
    }
    loading.value=false
}
</script>