<template>
    <div class="edit-popup">
        <popup ref="popupRef" :async="true" width="80vw" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="100px">
                <el-descriptions column="3" title="工程监理--巡视登记详情" border>
                    <el-descriptions-item label="项目名称" label-align="left" align="left">
                        {{ formData.project_name }}
                    </el-descriptions-item>
                    <el-descriptions-item label="编号" label-align="left" align="left">
                        {{ formData.inspection_code }}
                    </el-descriptions-item>
                    <el-descriptions-item label="巡视类型" label-align="left" align="left">
                        {{ formData.inspection_type_text }}
                    </el-descriptions-item>
                    <el-descriptions-item label="单位工程" label-align="left" align="left">
                        {{ formData.check_item_name }}
                    </el-descriptions-item>
                    <el-descriptions-item label="巡视部位" label-align="left" align="left">
                        {{ formData.inspection_position }}
                    </el-descriptions-item>
                    <el-descriptions-item label="施工人数" label-align="left" align="left">
                        {{ formData.workers }}
                    </el-descriptions-item>
                    <el-descriptions-item label="管理人数" label-align="left" align="left">
                        {{ formData.managers }}
                    </el-descriptions-item>
                    <el-descriptions-item label="巡视开始时间" label-align="left" align="left">
                        {{ formData.start_time }}
                    </el-descriptions-item>
                    <el-descriptions-item label="巡视结束时间" label-align="left" align="left">
                        {{ formData.end_time }}
                    </el-descriptions-item>
                    <el-descriptions-item label="巡视人员" label-align="left" align="left">
                        {{ formData.inspection_user }}
                    </el-descriptions-item>
                    <el-descriptions-item label="施工单位" label-align="left" align="left">
                        {{ formData.company_name }}
                    </el-descriptions-item>
                    <el-descriptions-item label="巡视内容" label-align="left" align="left">
                        {{ formData.inspection_content }}
                    </el-descriptions-item>
                    <el-descriptions-item label="设置重点关注" label-align="left" align="left">
                        {{ formData.is_important_text }}
                    </el-descriptions-item>
                    <el-descriptions-item label="关注人" label-align="left" align="left">
                        {{ formData.follow_user }}
                    </el-descriptions-item>
                    <el-descriptions-item label="检查表单选择" label-align="left" align="left">
                        {{ formData.check_item_detail_name }}
                    </el-descriptions-item>
                    <el-descriptions-item label="附件" label-align="left" align="left">
                        <el-link class="mr-5" type="primary" v-for="item in formData.annex" :href="item.uri">
                            {{ item.name }}
                        </el-link>
                    </el-descriptions-item>
                </el-descriptions>
            </el-form>
            <el-card class="mt-5">
                <template #header>
                    巡视结果列表
                </template>
                <el-table :data="formData.inspection_result">
                    <el-table-column label="检查类别" prop="check_type" show-overflow-tooltip />
                    <el-table-column label="检查类容" prop="check_content" show-overflow-tooltip />
                    <el-table-column label="是否必检" prop="must_check_text" show-overflow-tooltip />
                    <el-table-column label="是否检查结果必检" prop="check_result_text" show-overflow-tooltip />
                </el-table>
            </el-card>
            <el-card>
                <template #header>
                    巡视问题列表
                </template>
                <el-table :data="formData.inspection_problem">
                    <el-table-column label="问题分类" prop="problem_cate_text" show-overflow-tooltip />
                    <el-table-column label="问题说明" prop="problem_description" show-overflow-tooltip />
                    <el-table-column label="问题名称" prop="problem_name" show-overflow-tooltip />
                </el-table>
            </el-card>
        </popup>
    </div>
</template>

<script lang="ts" setup name="supervisionInspectionEdit">
import Popup from '@/components/popup/index.vue'
import { apisupervision_inspection_result, apisupervision_problem } from '@/api/supervision_inspection'
import { timeFormat } from '@/utils/util'


const emit = defineEmits(['success', 'close'])
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')

// 表单数据
const formData = reactive({
    id: '',
    project_id: '',
    project_name: '',
    inspection_code: '',
    is_important: "",
    inspection_type: '',
    check_item_id: '',
    check_item_name: "",
    inspection_type_text: "",
    check_item_name_text: "",
    inspection_position: '',
    workers: '',
    managers: '',
    start_time: timeFormat('', 'yyyy-mm-dd hh:MM:ss'),
    end_time: '',
    inspection_user: '',
    company_id: 0,
    company_name: '',
    inspection_content: '',
    create_time: "",
    follow_user: '',
    check_item_detail_ids: [],
    check_item_detail_name: '',
    annex: [],
    create_user: '',
    inspection_result: [],
    is_important_text: "",
    check_item_detail_name_text: "",
    inspection_problem: []
})


// 获取详情
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]
        }
    }
    apisupervision_inspection_result({ inspection_id: formData.id }).then(res => {
        formData.inspection_result = res.lists
    })
    apisupervision_problem({ data_id: formData.id, data_type: 1 }).then(res => {
        formData.inspection_problem = res.lists
    })
}


//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}



defineExpose({
    open,
    setFormData,
})
</script>
