<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="70vw" @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="采购单位" 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="theme">
                            <el-input v-model="formData.theme" clearable placeholder="请输入进场主题" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="进场时间" prop="enter_time">
                            <el-date-picker class="flex-1 !flex" v-model="formData.enter_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="co_participant">
                            <el-input v-model="formData.co_participant" clearable placeholder="请输入共同参与人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="进场结果" prop="enter_result">
                            <el-radio-group v-model="formData.enter_result" placeholder="请选择骑缝盖章">
                                <el-radio :label="0">进场</el-radio>
                                <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="parallel_test">
                            <el-radio-group v-model="formData.parallel_test" placeholder="请选择骑缝盖章">
                                <el-radio :label="0">否</el-radio>
                                <el-radio :label="1">是</el-radio>
                            </el-radio-group>
                        </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>
                    <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="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-dialog v-model="showDialog3" title="选择材料信息" width="70%">
                    <dialogTable :config="supervision_material_equipment_info" :query="{ type: 0 }"
                        @customEvent="customEvent2">
                    </dialogTable>
                </el-dialog>
            </el-form>
            <formTable :formData="formData.entry_detail" :config="tableConfig"></formTable>
        </popup>
    </div>
</template>

<script lang="ts" setup name="supervisionMaterialEntryEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiSupervisionMaterialEntryAdd, apiSupervisionMaterialEntryEdit, apisupervision_material_entry_detail, apisupervision_material_entry_detail_delete } from '@/api/supervision_material_entry'
import { supervision_project, supervision_participating_units, supervision_material_equipment_info } 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 showDialog3 = ref(false)
const tableIndex = ref(0)



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

// 表单数据
const formData = reactive({
    id: '',
    project_id: 0,
    project_name: "",
    company_id: '',
    company_name: "",
    theme: '',
    code: '',
    enter_time: '',
    co_participant: '',
    enter_result: '',
    parallel_test: '',
    remark: '',
    create_user: "",
    create_time: "",
    entry_detail: []
})

const tableConfig = reactive(
    {
        title: "材料信息",
        tableConfig: [
            {
                label: "材料名称",
                value: 'name'

            },
            {
                label: "材料品牌",
                value: 'brand'

            },
            {
                label: "单位",
                value: 'unit'
            },
            {
                label: "是否合同约定品牌",
                value: 'contract_brand',
                select: [
                    {
                        name: "否",
                        value: 0
                    },
                    {
                        name: "是",
                        value: 1
                    }
                ]
            },
            {
                label: "型号",
                value: 'model',
            },
            {
                label: "进场数量",
                value: 'entry_number'
            },

        ],
        deleteApi: apisupervision_material_entry_detail_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.unit_name
    showDialog2.value = false
}
const customEvent2 = (e) => {
    let { name, brand, model, unit, contract_brand, entry_number } = e
    Object.assign(formData.entry_detail[tableIndex.value], { name, brand, model, unit, contract_brand, entry_number })
    showDialog3.value = false
}


// 表单验证
const formRules = reactive<any>({
    project_id: [{
        required: true,
        message: '请选择项目',
        trigger: ['blur']
    }],
    company_id: [{
        required: true,
        message: '请输入采购单位id',
        trigger: ['blur']
    }],
    theme: [{
        required: true,
        message: '请输入进场主题',
        trigger: ['blur']
    }],
    code: [{
        required: true,
        message: '请输入编号',
        trigger: ['blur']
    }],
    enter_time: [{
        required: true,
        message: '请输入进场时间',
        trigger: ['blur']
    }],
    co_participant: [{
        required: true,
        message: '请输入共同参与人',
        trigger: ['blur']
    }],
    enter_result: [{
        required: true,
        message: '请输入进场结果',
        trigger: ['blur']
    }],
    parallel_test: [{
        required: true,
        message: '请输入是否平行检验',
        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]
        }
    }
    let res = await apisupervision_material_entry_detail({ material_entry_id: formData.id })
    formData.entry_detail = res.lists
}



// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiSupervisionMaterialEntryEdit(data)
        : await apiSupervisionMaterialEntryAdd(data)
    popupRef.value?.close()
    emit('success')
}

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

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



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