<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.code" clearable placeholder="系统自动生成" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="要求回复日期" prop="reply_date">
                            <el-date-picker class="flex-1 !flex" v-model="formData.reply_date" 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="notify_user">
                            <el-input v-model="formData.notify_user" clearable placeholder="请输入通知人员" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="下达日期" prop="issue_date">
                            <el-date-picker class="flex-1 !flex" v-model="formData.issue_date" clearable
                                value-format="YYYY-MM-DD" placeholder="选择时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <createUserLable :formData="formData" flag></createUserLable>

                    <el-col :span="8">
                        <el-form-item label="附件" prop="annex">
                            <uploadAnnex :formData="formData"></uploadAnnex>

                        </el-form-item>
                    </el-col>
                </el-row>
                <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, unit_type: 6 }" @customEvent="customEvent1">
                        </dialogTable>
                    </el-dialog>
                </div>
            </el-form>
            <formTable :formData="formData.problem" :config="tableConfig"></formTable>
        </popup>
    </div>
</template>

<script lang="ts" setup name="supervisionNoticeEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiSupervisionNoticeAdd, apiSupervisionNoticeEdit, apiSupervisionNoticeDetail, apisupervision_notice_problem, apisupervision_notice_problem_delete } from '@/api/supervision_notice'
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 showDialog2 = ref(false)

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑工程监理--监理通知单' : '新增工程监理--监理通知单'
})

// 表单数据
const formData = reactive({
    id: '',
    project_id: 0,
    project_name: '',
    code: '',
    reply_date: '',
    company_id: '',
    company_name: '',
    notify_user: '',
    issue_date: '',
    create_user: "",
    create_time: "",
    annex: [],
    problem: [{
        "problem_description": "",
        "content_description": "",
        "demand_description": ""
    }]
})

const tableConfig = reactive(
    {
        title: "通知问题",
        tableConfig: [
            {
                label: "事由描述",
                value: 'problem_description',

            },
            {
                label: "内容描述",
                value: 'content_description'
            },
            {
                label: "要求描述",
                value: 'demand_description',
            },
        ],
        deleteApi: apisupervision_notice_problem_delete,
    }
)



const customEvent = (e) => {
    formData.project_id = e.id
    formData.project_name = e.project_name
    showDialog.value = false
    formData.company_id = ''
    formData.company_name = ''
}
const customEvent1 = (e) => {
    formData.company_id = e.id
    formData.company_name = e.project_name
    showDialog2.value = false
}
// 表单验证
const formRules = reactive<any>({
    project_id: [{
        required: true,
        message: '请输入项目',
        trigger: ['blur']
    }],
    code: [{
        required: true,
        message: '请输入项目通知编号',
        trigger: ['blur']
    }],
    reply_date: [{
        required: true,
        message: '请输入要求回复日期',
        trigger: ['blur']
    }],
    company_id: [{
        required: true,
        message: '请输入施工单位',
        trigger: ['blur']
    }],
    notify_user: [{
        required: true,
        message: '请输入通知人员',
        trigger: ['blur']
    }],
    issue_date: [{
        required: true,
        message: '请输入下达日期',
        trigger: ['blur']
    }],
    rectification_reply_time: [{
        required: true,
        message: '请输入整改回复时间',
        trigger: ['blur']
    }],
    rectification_reply_user: [{
        required: true,
        message: '请输入整改回复人',
        trigger: ['blur']
    }],
    rectification_reply_opinions: [{
        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]
        }
    }

    let res = await apisupervision_notice_problem({ notice_id: formData.id })
    formData.problem = res.lists
}

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


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