<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit"
            @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <el-form-item label="项目名称" prop="project_name">
                    <el-input v-model="formData.project_name" clearable placeholder="点击选择项目" @click="showDialog = true"
                        readonly />
                </el-form-item>
                <el-form-item label="文档名称" prop="title">
                    <el-input v-model="formData.title" clearable placeholder="请输入文档名称" />
                </el-form-item>
                <el-form-item label="目录" prop="directory_id">
                    <el-select class="flex-1" v-model="formData.directory_id" clearable placeholder="请选择目录"
                        @change="getSubdirectory">
                        <el-option v-for="(item, index) in DirectoryList" :key="index" :label="item.name"
                            :value="item.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="子目录" prop="subdirectory_id">
                    <el-select class="flex-1" v-model="formData.subdirectory_id" clearable placeholder="请选择子目录">
                        <el-option v-for="(item, index) in SubdirectoryList" :key="index" :label="item.name"
                            :value="item.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="备注">
                    <el-input v-model="formData.remark" clearable placeholder="请输入备注" type="textarea" />
                </el-form-item>
                <el-form-item label="附件">
                    <annexUpload :annex="formData.annex" @handleAvatarSuccess="handleAvatarSuccess_four"
                        @delFile="delFileFn" />
                </el-form-item>
            </el-form>
            <el-dialog v-model="showDialog" title="选择项目" width="70%">
                <dialogTable @customEvent="customEvent" :config="consult_project" />
            </el-dialog>
        </popup>
    </div>
</template>

<script lang="ts" setup name="consultDemandEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiConsultContractAdd, apiConsultContractEdit, apiConsultContractDetail } from '@/api/consult_contract'
import type { PropType } from 'vue'
import { consult_project } from "@/components/dialogTable/dialogTableConfig"
import { apiConsultSubdirectoryLists } from '@/api/consult_subdirectory'

defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    },
    DirectoryList: Array

})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const showDialog = ref(false)


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑项目咨询--需求研究及管理' : '新增项目咨询--需求研究及管理'
})

// 表单数据
const formData = reactive({
    id: '',
    project_id: '',
    project_name: "",
    title: '',
    directory_id: '',
    subdirectory_id: '',
    remark: '',
    annex: [],
})


const handleAvatarSuccess_four = (response: any) => {
    formData.annex ||= []
    // @ts-ignore
    response.code != 0 ? formData.annex.push({ uri: response.data.uri, name: response.data.name }) : ElMessage.error(response.msg);
};
// 删除上传的文件
const delFileFn = (index: number) => { formData.annex.splice(index, 1) }

const customEvent = (e: any) => {
    formData.project_id = e.id
    formData.project_name = e.project_name
    showDialog.value = false
}


// 获取目录

const SubdirectoryList = ref([])
// 根据目录获取子目录
const getSubdirectory = async () => {
    formData.subdirectory_id = ''
    let res = await apiConsultSubdirectoryLists({ directory_id: formData.directory_id })
    SubdirectoryList.value = res.lists
}


// 表单验证
const formRules = reactive<any>({
    project_id: [{
        required: true,
        message: '请输入项目id',
        trigger: ['blur']
    }],
    title: [{
        required: true,
        message: '请输入文档名称',
        trigger: ['blur']
    }],
    directory_id: [{
        required: true,
        message: '请输入目录id',
        trigger: ['blur']
    }],
    subdirectory_id: [{
        required: true,
        message: '请输入子目录',
        trigger: ['blur']
    }],
    remark: [{
        required: true,
        message: '请输入备注',
        trigger: ['blur']
    }]
})


// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
    let res = await apiConsultSubdirectoryLists({ directory_id: formData.directory_id })
    SubdirectoryList.value = res.lists

}



// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiConsultContractEdit(data)
        : await apiConsultContractAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}



defineExpose({
    open,
    setFormData,
})
</script>
