<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_id">
                    <el-input v-model="formData.project_name" clearable placeholder="点击选择项目" readonly
                        @click="showDialog = true" />
                </el-form-item>
                <el-form-item label="文档名称">
                    <el-input v-model="formData.doc_name" clearable placeholder="请输入文档名称" />
                </el-form-item>
                <el-form-item label="文档简介">
                    <el-input v-model="formData.doc_desc" clearable placeholder="请输入文档简介" type="textarea" />
                </el-form-item>
                <el-form-item label="附件">
                    <uploadAnnex :formData="formData"></uploadAnnex>

                </el-form-item>

                <createUserLable :formData="formData"></createUserLable>
                <el-dialog v-model="showDialog" title="选择项目" width="70%">
                    <dialogTable @customEvent="customEvent" :config="supervision_project" />

                </el-dialog>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="supervisionFirstMeetingEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiSupervisionDesignDisclosureEdit, apiSupervisionDesignDisclosureAdd } from '@/api/supervision_design_disclosure'
import { supervision_project } from "@/components/dialogTable/dialogTableConfig"
import type { PropType } from 'vue'


defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
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: "",
    doc_name: '',
    doc_desc: '',
    annex: [],
    create_user: '',
    create_time: '',


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

// 表单验证
const formRules = reactive<any>({
    project_id: [{
        required: true,
        message: '请选择项目',
        trigger: ['blur']
    }],
    doc_name: [{
        required: true,
        message: '请输入文档名称',
        trigger: ['blur']
    }],
    doc_desc: [{
        required: true,
        message: '请输入文档简介',
        trigger: ['blur']
    }],
    annex: [{
        required: true,
        message: '请输入附件',
        trigger: ['blur']
    }],
    create_user: [{
        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]
        }
    }
}




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

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

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



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