<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="分部分项工程" 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-col>
                    <el-col :span="8">
                        <el-form-item label="归属年月" prop="attribution_date">

                            <el-date-picker class="flex-1 !flex" v-model="formData.attribution_date" clearable
                                type="month" value-format="YYYY-MM" placeholder="请选择日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="本项目特征">
                            <el-input v-model="formData.project_characteristics" 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.planned_construction_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-date-picker class="flex-1 !flex" v-model="formData.actual_construction_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="is_discuss" label-width="180px">
                            <el-radio-group v-model="formData.is_discuss">
                                <el-radio :label="0">
                                    否
                                </el-radio>
                                <el-radio :label="1">
                                    是
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="是否已按专家意见修改" prop="is_modify" label-width="200px">
                            <el-radio-group v-model="formData.is_modify">
                                <el-radio :label="0">
                                    否
                                </el-radio>
                                <el-radio :label="1">
                                    是
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="监理审批是否同意" prop="is_agree" label-width="160px">
                            <el-radio-group v-model="formData.is_agree">
                                <el-radio :label="0">
                                    否
                                </el-radio>
                                <el-radio :label="1">
                                    是
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="是否实施验收" prop="is_accept">
                            <!-- <el-input v-model="formData.is_accept" clearable placeholder="请输入是否实施验收 0-否 1-是" /> -->
                            <el-radio-group v-model="formData.is_accept">
                                <el-radio :label="0">
                                    否
                                </el-radio>
                                <el-radio :label="1">
                                    是
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="是否按方案实施验收" prop="is_accept_to_plan" label-width="180px">
                            <el-radio-group v-model="formData.is_accept_to_plan">
                                <el-radio :label="0">
                                    否
                                </el-radio>
                                <el-radio :label="1">
                                    是
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="检查人">
                            <el-input v-model="formData.check_user" 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.check_time" clearable
                                value-format="YYYY-MM-DD" placeholder="选择检查时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="存在问题和整改情况" label-width="180px">
                            <el-input v-model="formData.problems_rectification" clearable placeholder="请输入存在问题和整改情况" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="监控责任人">
                            <el-input v-model="formData.responsible_person" clearable placeholder="请输入监控责任人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="是否超控制值">
                            <el-radio-group v-model="formData.exceed_the_control_value">
                                <el-radio :label="0">
                                    否
                                </el-radio>
                                <el-radio :label="1">
                                    是
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="是否超预警值" prop="exceed_the_warning_value">
                            <el-radio-group v-model="formData.exceed_the_warning_value">
                                <el-radio :label="0">
                                    否
                                </el-radio>
                                <el-radio :label="1">
                                    是
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="控制措施">
                            <el-input v-model="formData.control_measures" clearable placeholder="请输入控制措施" />
                        </el-form-item>
                    </el-col>
                    <createUserLable :formData="formData" flag></createUserLable>

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

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

<script lang="ts" setup name="supervisionExcessRiskSecurityMonitoringEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiSupervisionExcessRiskSecurityMonitoringAdd, apiSupervisionExcessRiskSecurityMonitoringEdit, apiSupervisionExcessRiskSecurityMonitoringDetail } from '@/api/supervision_excess_risk_security_monitoring'
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: "",
    partial_project: '',
    attribution_date: '',
    project_characteristics: '',
    planned_construction_time: '',
    actual_construction_time: '',
    is_discuss: '',
    is_modify: '',
    is_agree: '',
    is_accept: '',
    is_accept_to_plan: '',
    check_user: '',
    check_time: '',
    problems_rectification: '',
    responsible_person: '',
    exceed_the_control_value: '',
    exceed_the_warning_value: '',
    control_measures: '',
    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']
    }],
    partial_project: [{
        required: true,
        message: '请输入分部分项工程',
        trigger: ['blur']
    }],
    attribution_date: [{
        required: true,
        message: '请输入归属年月',
        trigger: ['blur']
    }],
    project_characteristics: [{
        required: true,
        message: '请输入本项目特征',
        trigger: ['blur']
    }],
    planned_construction_time: [{
        required: true,
        message: '请输入计划施工时间',
        trigger: ['blur']
    }],
    actual_construction_time: [{
        required: true,
        message: '请输入实际施工时间',
        trigger: ['blur']
    }],
    is_discuss: [{
        required: true,
        message: '请输入是否已经专家组论证 0-否 1-是',
        trigger: ['blur']
    }],
    is_modify: [{
        required: true,
        message: '请输入是否已按专家意见修改 0-否 1-是',
        trigger: ['blur']
    }],
    is_agree: [{
        required: true,
        message: '请输入监理审批是否同意 0-否 1-是',
        trigger: ['blur']
    }],
    is_accept: [{
        required: true,
        message: '请输入是否实施验收 0-否 1-是',
        trigger: ['blur']
    }],
    is_accept_to_plan: [{
        required: true,
        message: '请输入是否按方案实施验收 0-否 1-是',
        trigger: ['blur']
    }],
    check_user: [{
        required: true,
        message: '请输入检查人',
        trigger: ['blur']
    }],
    check_time: [{
        required: true,
        message: '请输入检查时间',
        trigger: ['blur']
    }],
    problems_rectification: [{
        required: true,
        message: '请输入存在问题和整改情况',
        trigger: ['blur']
    }],
    responsible_person: [{
        required: true,
        message: '请输入监控责任人',
        trigger: ['blur']
    }],
    exceed_the_control_value: [{
        required: true,
        message: '请输入是否超控制值 0-否 1-是',
        trigger: ['blur']
    }],
    exceed_the_warning_value: [{
        required: true,
        message: '请输入是否超预警值 0-否 1-是',
        trigger: ['blur']
    }],
    control_measures: [{
        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 getDetail = async (row: Record<string, any>) => {
    const data = await apiSupervisionExcessRiskSecurityMonitoringDetail({
        id: row.id
    })
    setFormData(data)
}


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