<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="120px" :rules="formRules">
                <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="type">
                    <el-select class="flex-1" v-model="formData.type" clearable placeholder="请选择危大工程类型">
                        <el-option label="超规模工程" :value="parseInt(0)" />
                        <el-option label="危险性较大工程" :value="parseInt(1)" />
                    </el-select>
                </el-form-item>
                <el-form-item label="分部分项工程" prop="partial_project">
                    <el-select class="flex-1" v-model="formData.partial_project" clearable placeholder="请选择分部分项工程">
                        <el-option v-for="(item, index) in dictData.partial_project" :key="index" :label="item.name"
                            :value="parseInt(item.value)" />
                    </el-select>
                </el-form-item>
                <el-form-item label="计划施工时间" prop="planned_construction_time">
                    <el-date-picker class="flex-1 !flex" v-model="formData.planned_construction_time" clearable
                        value-format="YYYY-MM-DD" placeholder="选择计划施工时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="单位工程" prop="node_name">
                    <el-input v-model="formData.node_name" clearable placeholder="请输入单位工程" readonly
                        @click="showDialog1 = true" />
                </el-form-item>
                <el-form-item label="施工部位">
                    <el-input v-model="formData.position" clearable placeholder="请输入施工部位" />
                </el-form-item>
                <el-form-item label="本项目特征">
                    <el-input v-model="formData.project_characteristics" clearable placeholder="请输入本项目特征"
                        type="textarea" />
                </el-form-item>
                <el-form-item label="上级监管人员">
                    <el-input v-model="formData.supervisor" clearable placeholder="请输入上级监管人员" />
                </el-form-item>
                <el-form-item label="负责人">
                    <el-input v-model="formData.responsible_person" clearable placeholder="请输入负责人" />
                </el-form-item>
            </el-form>
            <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_check_item" @customEvent="customEvents"
                        :query="{ project_id: formData.project_id, node_type: 2 }"></dialogTable>
                </el-dialog>
            </div>
        </popup>
    </div>
</template>

<script lang="ts" setup name="supervisionDangerousEngineeringMonitoringEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiSupervisionDangerousEngineeringMonitoringAdd, apiSupervisionDangerousEngineeringMonitoringEdit, apiSupervisionDangerousEngineeringMonitoringDetail } from '@/api/supervision_dangerous_engineering_monitoring'
import { supervision_project, supervision_check_item } 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: "",
    type: '',
    partial_project: '',
    planned_construction_time: '',
    check_item_id: '',
    // check_item_name: "",
    node_name: "",
    position: '',
    project_characteristics: '',
    supervisor: '',
    responsible_person: '',
})
const customEvent = (e) => {
    formData.project_id = e.id
    formData.project_name = e.project_name
    showDialog.value = false
}
const customEvents = (e) => {
    formData.check_item_id = e.id
    formData.node_name = e.node_name
    showDialog1.value = false
}

// 表单验证
const formRules = reactive<any>({
    project_id: [{
        required: true,
        message: '请输入项目id',
        trigger: ['blur']
    }],
    type: [{
        required: true,
        message: '请输入危大工程类型',
        trigger: ['blur']
    }],
    partial_project: [{
        required: true,
        message: '请输入分部分项工程',
        trigger: ['blur']
    }],
    planned_construction_time: [{
        required: true,
        message: '请输入计划施工时间',
        trigger: ['blur']
    }],
    check_item_id: [{
        required: true,
        message: '请输入单位工程',
        trigger: ['blur']
    }],
    position: [{
        required: true,
        message: '请输入施工部位',
        trigger: ['blur']
    }],
    project_characteristics: [{
        required: true,
        message: '请输入本项目特征',
        trigger: ['blur']
    }],
    supervisor: [{
        required: true,
        message: '请输入上级监管人员',
        trigger: ['blur']
    }],
    responsible_person: [{
        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 apiSupervisionDangerousEngineeringMonitoringDetail({
        id: row.id
    })
    setFormData(data)
}


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