<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="80vw" @confirm="handleSubmit"
            @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="120px" :rules="formRules">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="项目名称" prop="project_name">
                            <el-input v-model="formData.project_name" clearable placeholder="点击选择项目" readonly
                                @click="showDialog = true" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="序号">
                            <el-input v-model="formData.sn" clearable placeholder="系统自动生成" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">

                        <el-form-item label="大型机械设备及仪器具" prop="name" label-width="200px">
                            <el-input v-model="formData.name" clearable placeholder="请输入大型机械设备及仪器具" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="使用部位" prop="use_site">
                            <el-input v-model="formData.use_site" clearable placeholder="请输入使用部位" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="进场时间" prop="entry_time">
                            <el-date-picker class="flex-1 !flex" v-model="formData.entry_time" clearable
                                value-format="YYYY-MM-DD" placeholder="选择进场时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="报审单位名称" prop="company_name">
                            <el-input v-model="formData.company_name" clearable placeholder="请输入报审单位名称" readonly
                                @click="showDialog2 = true" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="报审日期" prop="approval_date">
                            <el-date-picker class="flex-1 !flex" v-model="formData.approval_date" clearable
                                value-format="YYYY-MM-DD" placeholder="选择报审日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="报审结果">
                            <el-input v-model="formData.approval_result" clearable placeholder="请输入报审结果" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="报审表编号">
                            <el-input v-model="formData.approval_form_code" clearable placeholder="请输入报审表编号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="报审类型">
                            <el-select class="flex-1" v-model="formData.approval_type" clearable placeholder="请选择报审类型">
                                <el-option v-for="(item, index) in dictData.approval_type" :key="index"
                                    :label="item.name" :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="专监初审意见">
                            <el-select class="flex-1" v-model="formData.first_audit_opinion" clearable
                                placeholder="请选择专监初审意见">
                                <el-option v-for="(item, index) in dictData.first_audit_opinion" :key="index"
                                    :label="item.name" :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="初审人">
                            <el-input v-model="formData.first_auditor" clearable placeholder="请输入初审人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="初审时间">
                            <el-date-picker class="flex-1 !flex" v-model="formData.first_audit_time" clearable
                                value-format="YYYY-MM-DD" placeholder="选择初审时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="总监审定结论">
                            <el-select class="flex-1" v-model="formData.final_audit_opinion" clearable
                                placeholder="请选择总监审定结论">
                                <el-option v-for="(item, index) in dictData.final_audit_opinion" :key="index"
                                    :label="item.name" :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="审定时间">
                            <el-date-picker class="flex-1 !flex" v-model="formData.final_audit_time" clearable
                                value-format="YYYY-MM-DD" placeholder="选择审定时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="备注">
                            <el-input v-model="formData.remark" clearable placeholder="请输入备注" type="textarea" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="附件">
                            <uploadAnnex :formData="formData"></uploadAnnex>

                        </el-form-item>
                    </el-col>
                </el-row>

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

            </el-dialog>
            <div v-if="showDialog2">
                <el-dialog v-model="showDialog2" width=" 70%" title="选择施工单位">

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

<script lang="ts" setup name="supervisionLargeMechanicalEquipmentEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiSupervisionLargeMechanicalEquipmentAdd, apiSupervisionLargeMechanicalEquipmentEdit, apisupervision_large_mechanical_equipment_maintenance_record_delete, apiSupervisionLargeMechanicalEquipmentDetail, apisupervision_large_mechanical_equipment_maintenance_record } from '@/api/supervision_large_mechanical_equipment'
import { supervision_project } from "@/components/dialogTable/dialogTableConfig"
import type { PropType } from 'vue'
import { supervision_participating_units } from "@/components/dialogTable/dialogTableConfig"


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 showDialog2 = ref(false)


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑工程监理--大型机械设备及仪器台账' : '新增工程监理--大型机械设备及仪器台账'
})

// 表单数据
const formData = reactive({
    id: '',
    project_id: 0,
    project_name: "",
    sn: '',
    name: '',
    use_site: '',
    entry_time: '',
    company_id: '',
    company_name: '',
    approval_date: '',
    approval_result: '',
    approval_form_code: '',
    approval_type: '',
    first_audit_opinion: '',
    first_auditor: '',
    first_audit_time: '',
    final_audit_opinion: '',
    final_audit_time: '',
    remark: '',
    annex: [],
    maintenance_record: []
})

const tableConfig = reactive(
    {
        title: "机械维修保养记录",
        tableConfig: [
            {
                label: "登记日期",
                value: 'reg_date',
                date: true
            },
            {
                label: "机械每月维修保养附件",
                value: 'month_warranty_file'
            },
            {
                label: "设备维保有效期",
                value: 'maintenance_period',
                date: true
            },
            {
                label: "附配件1有效期",
                value: 'accessory_one_validity',
                date: true
            },
            {
                label: "附配件2有效期",
                value: 'accessory_two_validity',
                date: true
            },

        ],
        deleteApi: apisupervision_large_mechanical_equipment_maintenance_record_delete,
    }
)




const customEvent = (e) => {
    formData.project_id = e.id
    formData.project_name = e.project_name
    showDialog.value = false
    formData.company_name = ''
    formData.company_id = ''
}
const customEvent1 = (e) => {
    formData.company_id = e.id
    formData.company_name = e.unit_name
    showDialog2.value = false
}


// 表单验证
const formRules = reactive<any>({
    project_id: [{
        required: true,
        message: '请输入项目id',
        trigger: ['blur']
    }],
    name: [{
        required: true,
        message: '请输入大型机械设备及仪器具',
        trigger: ['blur']
    }],
    use_site: [{
        required: true,
        message: '请输入使用部位',
        trigger: ['blur']
    }],
    entry_time: [{
        required: true,
        message: '请输入进场时间',
        trigger: ['blur']
    }],
    company_id: [{
        required: true,
        message: '请输入报审单位名称',
        trigger: ['blur']
    }],
    approval_date: [{
        required: true,
        message: '请输入报审日期',
        trigger: ['blur']
    }],
    approval_result: [{
        required: true,
        message: '请输入报审结果',
        trigger: ['blur']
    }],
    approval_form_code: [{
        required: true,
        message: '请输入报审表编号',
        trigger: ['blur']
    }],
    approval_type: [{
        required: true,
        message: '请输入报审类型',
        trigger: ['blur']
    }],
    first_audit_opinion: [{
        required: true,
        message: '请输入专监初审意见',
        trigger: ['blur']
    }],
    first_auditor: [{
        required: true,
        message: '请输入初审人',
        trigger: ['blur']
    }],
    first_audit_time: [{
        required: true,
        message: '请输入初审时间',
        trigger: ['blur']
    }],
    final_audit_opinion: [{
        required: true,
        message: '请输入总监审定结论',
        trigger: ['blur']
    }],
    final_audit_time: [{
        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 apisupervision_large_mechanical_equipment_maintenance_record({ large_mechanical_equipment_id: formData.id })
    formData.maintenance_record = res.lists

}

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


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiSupervisionLargeMechanicalEquipmentEdit(data)
        : await apiSupervisionLargeMechanicalEquipmentAdd(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>
