
<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.side_station_code" disabled clearable placeholder="系统自动生成" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="旁站类型" prop="side_station_type">
                            <el-select class="flex-1" v-model="formData.side_station_type" clearable placeholder="请输入巡视类型">
                                <el-option v-for="(item, index) in dictData.side_station_type" :key="index"
                                    :label="item.name" :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <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="position">
                            <el-input v-model="formData.position" clearable placeholder="请输入巡视部位" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="施工人数" prop="workers">
                            <el-input v-model="formData.workers" clearable placeholder="请输入施工人数" type="number" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="管理人数" prop="managers">
                            <el-input v-model="formData.managers" clearable placeholder="请输入管理人数" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="旁站开始时间" prop="start_time" label-width="120px">
                            <el-date-picker class="flex-1 !flex" v-model="formData.start_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="end_time">
                            <el-date-picker class="flex-1 !flex" v-model="formData.end_time" clearable type="datetime"
                                value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择巡视结束时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <!-- <el-col :span="8">
                        <el-form-item label="巡视人员" prop="inspection_user">
                            <el-input v-model="formData.inspection_user" clearable placeholder="请输入巡视人员" />
                        </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="situation">
                            <el-input v-model="formData.situation" clearable placeholder="请输入巡视内容" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="旁站" prop="side_station_result">
                            <el-select v-model="formData.side_station_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="side_station_user">
                            <el-input v-model="formData.side_station_user" clearable placeholder="请输入关注人" />
                        </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-row>
                <el-row>
                    <!-- <el-col :span="8">
                        <el-form-item label="创建人" prop="create_user">
                            <el-input v-model="formData.create_user" clearable placeholder="请输入创建人" />
                        </el-form-item>
                    </el-col> -->
                    <el-col :span="8">
                        <el-form-item label="附件" prop="annex">
                            <annexUpload :annex="formData.annex" @handleAvatarSuccess="handleAvatarSuccess_four"
                                @delFile="delFileFn" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-dialog v-model="showDialog" title="选择项目" width="70%">
                    <supervision_project @customEvent="customEvent"></supervision_project>
                </el-dialog>
                <div v-if="showDialog1">
                    <el-dialog v-model="showDialog1" title="选择单位工程" width="70%" :title="flag ? '检查表单' : '选择施工单位'">
                        <supervision_check_item @customEvent="customEvents" :project_id="formData.project_id"
                            :node_type="flag ? '' : 2">
                        </supervision_check_item>
                    </el-dialog>
                </div>
                <div v-if="showDialog2">
                    <el-dialog v-model="showDialog2" width=" 70%" title="选择施工单位">
                        <supervision_participating_units @customEvent="customEvent1" :project_id="formData.project_id"
                            :unit_type="6">
                        </supervision_participating_units>
                    </el-dialog>
                </div>
            </el-form>

            <div style="margin-bottom: 30px;">巡视结果</div>
            <div style="margin-bottom: 30px;">
                <el-table :data="formData.side_result">
                    <el-table-column label="序号" width="150px">
                        <template #default="{ row }">
                            <el-button @click="handleAdd()">+</el-button>
                            <el-button @click="handleDelete(row)">-</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column label="检查类别" prop="check_type">
                        <template #default="scope">
                            <el-input v-model="scope.row.check_type" />
                        </template>
                    </el-table-column>
                    <el-table-column label="检查类容" prop="check_content">
                        <template #default="{ row }">
                            <el-input v-model="row.check_content" />
                        </template>
                    </el-table-column>

                    <el-table-column label="是否必检" prop="must_check">
                        <template #default="{ row }">
                            <el-select class="flex-1" v-model="row.must_check" clearable placeholder="请选择">
                                <el-option label="否" :value="parseInt(0)" />
                                <el-option label="是" :value="parseInt(1)" />
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column label="检查结果" prop="must_check">
                        <template #default="{ row }">
                            <el-select class="flex-1" v-model="row.check_result" clearable placeholder="请选择">
                                <el-option label="合格" :value="parseInt(0)" />
                                <el-option label="不合格" :value="parseInt(1)" />
                            </el-select>
                        </template>
                    </el-table-column>
                </el-table>
            </div>

            <div style="margin-bottom: 30px;">巡视问题</div>
            <div style="margin-bottom: 30px;">
                <el-table :data="formData.side_problem">
                    <el-table-column label="序号" width="150px">
                        <template #default="{ row }">
                            <el-button @click="handleAdd1()">+</el-button>
                            <el-button @click="handleDelete1(row)">-</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column label="问题分类" prop="problem_cate">
                        <template #default="scope">
                            <el-select class="flex-1" v-model="scope.row.problem_cate" clearable placeholder="请输入问题分类">
                                <el-option v-for="( item, index  ) in  dictData.problem_cate" :key="index"
                                    :label="item.name" :value="item.value" />
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column label="问题说明" prop="check_content">
                        <template #default="{ row }">
                            <el-input v-model="row.problem_description" />
                        </template>
                    </el-table-column>

                    <el-table-column label="问题名称" prop="must_check">
                        <template #default="{ row }">
                            <el-input v-model="row.problem_name" />
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </popup>
    </div>
</template>

<script lang="ts" setup name="supervisionInspectionEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiSupervisionInspectionDetail, apisupervision_inspection_result, apisupervision_problem, apisupervision_problem_delete } from '@/api/supervision_inspection'
import { apisupervision_check_item_detailLists } from '@/api/supervision_check_item'
import { apiSupervisionSideStationAdd, apiSupervisionSideStationEdit, apiSupervisionSideStationDetail, apisupervision_side_station_result, apisupervision_side_station_resultDelete } from '@/api/supervision_side_station'
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 showDialog2 = ref(false)
const showDialog3 = ref(false)
const flag = ref(false)


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑工程监理--巡视登记' : '新增工程监理--巡视登记'
})
const handleAvatarSuccess_four = (response: any) => {
    Array.isArray(formData.annex) ? '' : formData.annex = []
    // @ts-ignore
    response.code != 0 ? formData.annex.push({ uri: response.data.uri, name: response.data.name }) : ElMessage.error(response.msg);
};

// 删除上传的文件
const delFileFn = (index: number) => { formData.annex.splice(index, 1) }

const handleAdd = () => {
    formData.side_result.push({
        "check_type": "",
        "check_content": "",
        "must_check": 0,
        "check_result": 0
    })
}

const handleDelete = async (row: any) => {
    if (formData.side_result.length == 1) return
    if (row.id) {
        await apisupervision_side_station_resultDelete({ id: row.id })
        const index = formData.side_result.indexOf(row);
        formData.side_result.splice(index, 1);
    } else {
        const index = formData.side_result.indexOf(row);
        formData.side_result.splice(index, 1);
    }

}


const handleAdd1 = () => {
    formData.side_problem.push({
        "problem_cate": "",
        "problem_description": "",
        "problem_name": ""
    })
}

const handleDelete1 = async (row: any) => {
    if (formData.side_problem.length == 1) return
    if (row.id) {
        await apisupervision_problem_delete({ id: row.id })
        const index = formData.side_problem.indexOf(row);
        formData.side_problem.splice(index, 1);

    }
    else {
        const index = formData.side_problem.indexOf(row);
        formData.side_problem.splice(index, 1);
    }

}


// 表单数据
const formData = reactive({
    id: '',
    project_id: '',
    side_station_code: '',
    side_station_type: '',
    check_item_id: '',
    position: '',
    company_id: '',
    start_time: '',
    end_time: '',
    workers: '',
    managers: '',
    situation: '',
    side_station_result: '',
    side_station_user: '',
    check_item_detail_ids: '',
    annex: '',
    create_user: '',
    project_name: '',
    inspection_code: '',
    is_important: "",
    inspection_type: '',
    check_item_name: "",
    inspection_position: '',
    inspection_user: '',
    company_name: '',
    inspection_content: '',
    is_importent: '',
    follow_user: '',
    check_item_detail_name: '',
    side_result: [
        {
            "check_type": "",
            "check_content": "",
            "must_check": 1,
            "check_result": 1
        }
    ],
    side_problem: [
        {
            "problem_cate": "",
            "problem_description": "",
            "problem_name": ""
        }
    ]
    // inspection_result: [{
    //     "check_type": "",
    //     "check_content": "",
    //     "must_check": 0,
    //     "check_result": 0
    // },],
    // inspection_problem: [{
    //     "problem_cate": "",
    //     "problem_description": "",
    //     "problem_name": ""
    // }]
})
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.side_result = [{
        "check_type": "",
        "check_content": "",
        "must_check": 0,
        "check_result": 0
    },]
}

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,
                must_check: item.must_check,
                check_result: item.check_result
            }))
            formData.side_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.project_name
    showDialog2.value = false
}


// 表单验证
const formRules = reactive<any>({
    project_id: [{
        required: true,
        message: '请选择项目',
        trigger: ['blur']
    }],
    side_station_code: [{
        required: true,
        message: '请输入旁站编号',
        trigger: ['blur']
    }],
    side_station_type: [{
        required: true,
        message: '请输入旁站类型',
        trigger: ['blur']
    }],
    check_item_id: [{
        required: true,
        message: '请输入单位工程',
        trigger: ['blur']
    }],
    position: [{
        required: true,
        message: '请输入部位',
        trigger: ['blur']
    }],
    company_id: [{
        required: true,
        message: '请输入施工单位',
        trigger: ['blur']
    }],
    start_time: [{
        required: true,
        message: '请输入旁站开始时间',
        trigger: ['blur']
    }],
    end_time: [{
        required: true,
        message: '请输入旁站结束时间',
        trigger: ['blur']
    }],
    workers: [{
        required: true,
        message: '请输入施工人数',
        trigger: ['blur']
    }],
    managers: [{
        required: true,
        message: '请输入管理人数',
        trigger: ['blur']
    }],
    situation: [{
        required: true,
        message: '请输入现场施工情况',
        trigger: ['blur']
    }],
    side_station_result: [{
        required: true,
        message: '请输入旁站结果 0-完成 1-交班',
        trigger: ['blur']
    }],
    side_station_user: [{
        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]
        }
    }
    apisupervision_side_station_result({ side_station_id: formData.id }).then(res => {
        formData.side_result = res.lists
    })
    apisupervision_problem({ data_id: formData.id }).then(res => {
        formData.side_problem = res.lists
    })
    // if (formData)
    formData.company_id == 0 ? formData.company_id = '' : ""
    formData.side_station_type = String(formData.side_station_type)
    formData.side_station_result = String(formData.side_station_result)
}

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 apiSupervisionSideStationEdit(data)
        : await apiSupervisionSideStationAdd(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>
