<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="90px" :rules="formRules">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="报告编号" prop="check_code">
                            <el-input v-model="formData.check_code" clearable placeholder="请输入报告编号" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="检验结果" prop="check_result">
                            <el-radio-group v-model="formData.check_result">
                                <el-radio label="1">
                                    符合标准
                                </el-radio>
                                <el-radio label="2">
                                    不符合标准
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="获得日期" prop="check_time">
                            <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="操作人" prop="check_user">
                            <el-input v-model="formData.check_user" clearable placeholder="请输入操作人" />
                        </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" value="check_annex"></uploadAnnex>
                        </el-form-item>
                    </el-col>
                </el-row>

            </el-form>
            <div style="margin-bottom: 30px;">问题</div>
            <div style="margin-bottom: 30px;">
                <el-table :data="formData.check_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="problem_name">

                        <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="supervisionDiaryEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apisupervision_material_parallel_testing_detail_check } from '@/api/supervision_material_parallel_testing'


import { timeFormat } from '@/utils/util'
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 popupTitle = computed(() => {
    return '检验结果'
})

const handleAdd1 = () => {
    formData.check_problem.push({
        "problem_cate": '',
        "problem_description": "",
        "problem_name": ""
    })
}
const handleDelete1 = async (row: any) => {
    const index = formData.check_problem.indexOf(row);
    formData.check_problem.splice(index, 1);
}


// 表单数据
const formData = reactive({
    "id": 1,
    "check_code": 1,
    "check_result": 1,
    "check_time": "",
    "check_user": "",
    "check_remark": "",
    "check_annex": [
    ],
    "check_problem": [
        {
            "problem_cate": '',
            "problem_description": "",
            "problem_name": ""
        }
    ]
})


// 表单验证
const formRules = reactive<any>({
    project_id: [{
        required: true,
        message: '请输入项目id',
        trigger: ['blur']
    }],
    date: [{
        required: true,
        message: '请输入日期',
        trigger: ['blur']
    }],
    week: [{
        required: true,
        message: '请输入星期',
        trigger: ['blur']
    }],
    air_temperature: [{
        required: true,
        message: '请输入气温',
        trigger: ['blur']
    }],
    climate: [{
        required: true,
        message: '请输入气候',
        trigger: ['blur']
    }],
    user: [{
        required: true,
        message: '请输入监理人员',
        trigger: ['blur']
    }],
    director: [{
        required: true,
        message: '请输入监理总监',
        trigger: ['blur']
    }],
    engineering_dynamics: [{
        required: true,
        message: '请输入工程动态',
        trigger: ['blur']
    }],
    supervision_work: [{
        required: true,
        message: '请输入监理工作',
        trigger: ['blur']
    }],
    construction_situation: [{
        required: true,
        message: '请输入安全文明施工情况',
        trigger: ['blur']
    }],
    other_matters: [{
        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 handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apisupervision_material_parallel_testing_detail_check(data)
        : await apisupervision_material_parallel_testing_detail_check(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>
