<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="device_name" label-width="120px">
                    <el-input v-model="formData.device_name" clearable placeholder="请输入监控设备名称" />
                </el-form-item>
                <el-form-item label="设备类型" prop="device_type">
                    <el-select class="flex-1" v-model="formData.device_type" clearable placeholder="请选择设备类型">
                        <el-option v-for="(item, index) in dictData.device_type" :key="index" :label="item.name"
                            :value="parseInt(item.value)" />
                    </el-select>
                </el-form-item>
                <el-form-item label="设备序列号" prop="device_sn">
                    <el-input v-model="formData.device_sn" clearable placeholder="请输入设备序列号" />
                </el-form-item>
                <el-form-item label="编号" prop="number">
                    <el-input v-model="formData.number" clearable placeholder="请输入编号" />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="formData.remark" clearable placeholder="请输入备注" />
                </el-form-item>
                <el-dialog v-model="showDialog" title="选择项目" width="70%">
                    <dialogTable @customEvent="customEvent" :config="supervision_project" />
                </el-dialog>
                <formTable :formData="formData.devices" :config="tableConfig"></formTable>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="supervisionMonitoringEquipmentEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiSupervisionMonitoringEquipmentAdd, apiSupervisionMonitoringEquipmentEdit, apiSupervisionMonitoringEquipmentDetail } from '@/api/supervision_monitoring_equipment'
import { apiSupervisionMonitoringEquipmentDetailLists, apiSupervisionMonitoringEquipmentDetailDelete } from '@/api/supervision_monitoring_equipment_detail'
import { supervision_project } 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 popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑工程监理--监控设备信息' : '新增工程监理--监控设备信息'
})

// 表单数据
const formData = reactive({
    id: '',
    project_id: '',
    project_name: "",
    device_name: '',
    device_type: '',
    device_sn: '',
    number: '',
    remark: '',
    devices: []
})

const tableConfig = reactive(
    {
        title: "设备列表",
        tableConfig: [
            {
                label: "设备名称",
                value: 'name'

            },
            {
                label: "是否显示",
                value: 'is_show',
                select: [
                    {
                        name: '是',
                        value: 0
                    },
                    {
                        name: '否',
                        value: 1
                    }
                ]
            },
        ],
        deleteApi: apiSupervisionMonitoringEquipmentDetailDelete,
    }
)



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: '请选择项目',
        trigger: ['blur']
    }],
    device_name: [{
        required: true,
        message: '请输入监控设备名称',
        trigger: ['blur']
    }],
    device_type: [{
        required: true,
        message: '请输入设备类型',
        trigger: ['blur']
    }],
    device_sn: [{
        required: true,
        message: '请输入设备序列号',
        trigger: ['blur']
    }],
    number: [{
        required: true,
        message: '请输入编号',
        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]
        }
    }
    let res = await apiSupervisionMonitoringEquipmentDetailLists({ device_id: data.id })
    formData.devices = res.lists

}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiSupervisionMonitoringEquipmentDetail({
        id: row.id
    })
    setFormData(data)
}


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