<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" :disabled="mode == 'detail'">
                <el-form-item label="项目名称" prop="project_name">
                    <el-input v-model="formData.project_name" clearable placeholder="点击选择项目" readonly
                        @click="showDialog = true" />
                </el-form-item>
                <el-form-item label="所属单位" prop="company_name">
                    <el-input v-model="formData.company_name" clearable placeholder="请输入所属单位"
                        @click="showDialog1 = true" readonly />
                </el-form-item>
                <el-form-item label="资质名称">
                    <el-input v-model="formData.qualification_name" clearable placeholder="请输入资质名称" />
                </el-form-item>
                <el-form-item label="资质编号">
                    <el-input v-model="formData.qualification_number" clearable placeholder="请输入资质编号" type="number" />
                </el-form-item>
                <el-form-item label="发证日期">
                    <el-date-picker class="flex-1 !flex" v-model="formData.get_date" clearable
                        value-format="YYYY-MM-DD " placeholder="选择签订日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="有效日期">
                    <el-date-picker class="flex-1 !flex" v-model="formData.effective_date" clearable
                        value-format="YYYY-MM-DD " placeholder="选择签订日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="资质状态 ">
                    <el-select class="flex-1" v-model="formData.qualification_status" clearable placeholder="请选择部门">
                        <el-option label="有效" :value="0" />
                        <el-option label="无效" :value="1" />
                    </el-select>
                </el-form-item>
                <el-form-item label="备注">
                    <el-input v-model="formData.remark" clearable placeholder="请输入备注" />
                </el-form-item>
                <el-form-item label="附件">
                    <uploadAnnex :formData="formData"></uploadAnnex>
                </el-form-item>
                <el-dialog v-model="showDialog" title="选择项目" width="70%">
                    <dialogTable @customEvent="customEvent" :config="supervision_project" />
                </el-dialog>
                <div v-if="showDialog1">
                    <el-dialog v-model="showDialog1" title="选择单位" width="70%">

                        <dialogTable :config="supervision_participating_units"
                            :query="{ project_id: formData.project_id }" @customEvent="customEvent1">
                        </dialogTable>
                    </el-dialog>
                </div>

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

<script lang="ts" setup name="supervisionParticipatingUnitsQualificationsEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiSupervisionParticipatingUnitsQualificationsAdd, apiSupervisionParticipatingUnitsQualificationsEdit, } from '@/api/supervision_participating_units_qualifications'
import { supervision_project, supervision_participating_units } 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 showDialog1 = ref(false)


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑工程监理--参建单位资质' : '工程监理--参建单位资质'
})

// 表单数据
const formData = reactive({
    id: '',
    project_id: 0,
    project_name: "",
    company_id: '',
    company_name: '',
    qualification_name: '',
    qualification_number: '',
    get_date: '',
    effective_date: '',
    qualification_status: '',
    remark: '',
    annex: [],
})
const customEvent = (e) => {
    formData.project_id = e.id
    formData.project_name = e.project_name
    showDialog.value = false
}
const customEvent1 = (e) => {
    formData.company_id = e.id
    formData.company_name = e.unit_name
    showDialog1.value = false
}

// 表单验证
const formRules = reactive<any>({
    project_id: [{
        required: true,
        message: '请选择项目',
        trigger: ['blur']
    }],
    company_id: [{
        required: true,
        message: '请输入所属单位',
        trigger: ['blur']
    }],
    qualification_name: [{
        required: true,
        message: '请输入资质名称',
        trigger: ['blur']
    }],
    qualification_number: [{
        required: true,
        message: '请输入资质编号',
        trigger: ['blur']
    }],
    get_date: [{
        required: true,
        message: '请输入发证日期',
        trigger: ['blur']
    }],
    effective_date: [{
        required: true,
        message: '请输入有效日期',
        trigger: ['blur']
    }],
    qualification_status: [{
        required: true,
        message: '请输入资质状态 0-有效 1-无效',
        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]
        }
    }


}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiSupervisionParticipatingUnitsQualificationsDetail({
        id: row.id
    })
    setFormData(data)
}


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

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

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



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