<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="120px" :rules="formRules">
                <el-form-item label="项目名称" prop="project_name">
                    <el-input v-model="formData.project_name" clearable placeholder="点击选择项目" readonly
                        @click="showDialog = true" />
                </el-form-item>
                <el-form-item label="材料/设备名称" prop="name">
                    <el-input v-model="formData.name" clearable placeholder="请输入材料/设备名称" />
                </el-form-item>
                <el-form-item label="材料/设备品牌" prop="brand">
                    <el-input v-model="formData.brand" clearable placeholder="请输入材料/设备品牌" />
                </el-form-item>
                <el-form-item label="型号" prop="model">
                    <el-input v-model="formData.model" clearable placeholder="请输入型号" />
                </el-form-item>
                <el-form-item label="计数单位" prop="unit">
                    <el-input v-model="formData.unit" clearable placeholder="请输入计数单位" v-type="'unit'" />
                </el-form-item>
                <el-form-item label="计划数量" prop="number">
                    <el-input v-model="formData.number" clearable placeholder="请输入计划数量" type="number" />
                </el-form-item>
                <el-form-item label="合同约定品牌" prop="contract_brand">
                    <el-radio-group v-model="formData.contract_brand" placeholder="合同约定品牌">
                        <el-radio :label="0">
                            否
                        </el-radio>
                        <el-radio :label="1">
                            是
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="类型" prop="type">
                    <el-radio-group v-model="formData.type" placeholder="类型">
                        <el-radio :label="0">
                            材料
                        </el-radio>
                        <el-radio :label="1">
                            设备
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="备注">
                    <el-input v-model="formData.remark" clearable placeholder="请输入备注" type="textarea" />
                </el-form-item>
                <el-dialog v-model="showDialog" title="选择项目" width="70%">
                    <dialogTable @customEvent="customEvent" :config="supervision_project" />
                </el-dialog>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="supervisionMaterialEquipmentInfoEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiSupervisionMaterialEquipmentInfoAdd, apiSupervisionMaterialEquipmentInfoEdit, apiSupervisionMaterialEquipmentInfoDetail } from '@/api/supervision_material_equipment_info'
import type { PropType } from 'vue'
import { supervision_project } 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 popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑工程监理--材料设备信息' : '新增工程监理--材料设备信息'
})

// 表单数据
const formData = reactive({
    id: '',
    project_id: '',
    project_name: "",
    name: '',
    brand: '',
    model: '',
    unit: '',
    number: '',
    contract_brand: '',
    type: '',
    remark: '',
})

const customEvent = (e) => {
    formData.project_id = e.id
    formData.project_name = e.project_name
    showDialog.value = false
}


// 表单验证
const formRules = reactive<any>({
    project_id: [{
        required: true,
        message: '请输入项目id',
        trigger: ['blur']
    }],
    name: [{
        required: true,
        message: '请输入材料/设备名称',
        trigger: ['blur']
    }],
    brand: [{
        required: true,
        message: '请输入材料/设备品牌',
        trigger: ['blur']
    }],
    model: [{
        required: true,
        message: '请输入型号',
        trigger: ['blur']
    }],
    unit: [{
        required: true,
        message: '请输入计数单位',
        trigger: ['blur']
    }],
    number: [{
        required: true,
        message: '请输入计划数量',
        trigger: ['blur']
    }],
    contract_brand: [{
        required: true,
        message: '请输入合同约定品牌 0-否 1-是',
        trigger: ['blur']
    }],
    type: [{
        required: true,
        message: '请输入类型 0-材料 1-设备',
        trigger: ['blur']
    }],
    remark: [{
        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 data = await apiSupervisionMaterialEquipmentInfoDetail({
        id: row.id
    })
    setFormData(data)
}


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