<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.accept_code" disabled clearable placeholder="系统自动生成" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="验收类型" prop="accept_type">
                            <el-select class="flex-1" v-model="formData.accept_type" clearable placeholder="请选择巡视类型">
                                <el-option v-for="(item, index) in dictData.accept_type" :key="index" :label="item.name"
                                    :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="单位工程" prop="check_item_name">
                            <el-input v-model="formData.check_item_name" clearable placeholder="点击选择单位工程" readonly
                                @click="showDialog1 = true, flag = false" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="验收内容" prop="accept_content">
                            <el-input v-model="formData.accept_content" clearable placeholder="请输入巡视部位" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="验收位置" prop="accept_position">
                            <el-input v-model="formData.accept_position" clearable placeholder="请输入施工人数" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="验收时间" prop="accept_time" label-width="120px">
                            <el-date-picker class="flex-1 !flex" v-model="formData.accept_time" clearable
                                type="datetime" value-format="YYYY-MM-DD HH:mm:ss" 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="accept_result">
                            <el-select v-model="formData.accept_result" clearable placeholder="请选择" class="flex-1">
                                <el-option label="合格" value="0" />
                                <el-option label="不合格" value="1" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="检查表单选择" prop="check_item_detail_ids">
                            <el-input v-model="formData.check_item_detail_name" clearable placeholder="点击选择"
                                @click="showDialog1 = true, flag = true" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="formData.remark" clearable placeholder="请输入备注" type="textarea" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="附件" prop="annex">
                            <uploadAnnex :formData="formData"></uploadAnnex>

                        </el-form-item>
                    </el-col>
                    <createUserLable :formData="formData" flag></createUserLable>

                </el-row>
                <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" width="70%" :title="flag ? '检查表单' : '选择施工单位'">
                        <dialogTable :config="supervision_check_item" @customEvent="customEvents" :multipleChoice="flag"
                            :query="{ project_id: formData.project_id, node_type: flag ? '' : 2 }"></dialogTable>
                    </el-dialog>
                </div>
                <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.accept_check_result" :config="tableConfig"></formTable>
            <formTable :formData="formData.accept_check_problem" :config="tableConfig1" :dictData="dictData">
            </formTable>
        </popup>
    </div>
</template>

<script lang="ts" setup name="supervisionInspectionEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apisupervision_problem_delete, apiSupervisionInspectionDetail, apisupervision_problem } from '@/api/supervision_inspection'
import { apisupervision_check_item_detailLists } from '@/api/supervision_check_item'
import { apiSupervisionAcceptAdd, apiSupervisionAcceptEdit, apisupervision_accept_result, apisupervision_accept_result_delete } from '@/api/supervision_accept'
import type { PropType } from 'vue'
import { supervision_project, supervision_participating_units, supervision_check_item } 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 showDialog1 = ref(false)
const showDialog2 = ref(false)
const flag = ref(false)


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


// 表单数据
const formData = reactive({
    project_id: '',
    accept_code: '',
    accept_type: '',
    check_item_id: '',
    accept_content: '',
    accept_position: '',
    accept_time: '',
    company_id: '',
    accept_result: '',
    check_item_detail_ids: [],
    remark: '',
    create_user: "",
    create_time: "",
    check_item_name: "",
    company_name: '',
    check_item_detail_name: '',
    id: '',
    project_name: '',
    annex: [],
    accept_check_result: [],
    accept_check_problem: []
})



const tableConfig = reactive(
    {
        title: "巡视结果",
        tableConfig: [
            {
                label: "检查类别",
                value: 'check_type'

            },
            {
                label: "问题",
                value: 'check_problem'

            },
            {
                label: "检查类容",
                value: 'check_content'
            },
            {
                label: "是否必检",
                value: 'must_check',
                select: [
                    {
                        name: "否",
                        value: 0
                    },
                    {
                        name: "是",
                        value: 1
                    }
                ]
            },
            {
                label: "检查结果",
                value: 'check_result',
                select: [
                    {
                        name: "合格",
                        value: 0
                    },
                    {
                        name: "不合格",
                        value: 1
                    }
                ]
            },

        ],
        deleteApi: apisupervision_accept_result_delete,
    }
)

const tableConfig1 = reactive(
    {
        title: "巡视问题",
        tableConfig: [
            {
                label: "问题分类",
                value: 'problem_cate',
                select: 'problem_cate'

            },
            {
                label: "问题说明",
                value: 'problem_description'
            },
            {
                label: "问题名称",
                value: 'problem_name',
            },

        ],
        deleteApi: apisupervision_problem_delete,
    }
)


const customEvent = (e) => {
    formData.project_id = e.id
    formData.project_name = e.project_name
    showDialog.value = false
    formData.company_name = ''
    formData.company_id = ''
    formData.check_item_detail_ids = []
    formData.check_item_detail_name = ''
    formData.accept_check_result = []
}

const customEvents = (e) => {
    if (flag.value) {
        formData.check_item_detail_ids = e.map(item => item.id)
        formData.check_item_detail_name = e.map(item => item.project_name)
        apisupervision_check_item_detailLists({ node_ids: formData.check_item_detail_ids.join(',') }).then(res => {
            let lists = res.lists.map(item => ({
                check_type: item.check_type,
                check_content: item.check_content,
                check_problem: item.check_problem,
                must_check: item.must_check,
                check_result: item.check_result
            }))
            formData.accept_check_result = lists
        })
    } else {
        formData.check_item_id = e.id
        formData.check_item_name = e.project_name
    }
    showDialog1.value = false
}

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: '请选择项目',
        trigger: ['blur']
    }],
    accept_code: [{
        required: true,
        message: '请输入验收编号',
        trigger: ['blur']
    }],
    accept_type: [{
        required: true,
        message: '请输入验收类型',
        trigger: ['blur']
    }],
    check_item_id: [{
        required: true,
        message: '请输入单位工程',
        trigger: ['blur']
    }],
    accept_content: [{
        required: true,
        message: '请输入验收内容',
        trigger: ['blur']
    }],
    accept_position: [{
        required: true,
        message: '请输入验收位置',
        trigger: ['blur']
    }],
    accept_time: [{
        required: true,
        message: '请输入验收时间',
        trigger: ['blur']
    }],
    company_id: [{
        required: true,
        message: '请输入施工单位',
        trigger: ['blur']
    }],
    accept_result: [{
        required: true,
        message: '请输入验收结果 0-合格 1-不合格',
        trigger: ['blur']
    }],
    remark: [{
        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]
        }
    }
    formData.accept_result = String(formData.accept_result)
    formData.accept_type = String(formData.accept_type)
    formData.company_id == 0 ? formData.company_id = '' : ""
    apisupervision_accept_result({ accept_id: formData.id }).then(res => {
        formData.accept_check_result = res.lists
    })
    apisupervision_problem({ data_id: formData.id, data_type: 3 }).then(res => {
        formData.accept_check_problem = res.lists
    })
}

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


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