<!--终端的新增编辑弹出框-->
<template>
    <el-dialog
        draggable
        class="cn-operate-dialog"
        v-model="machineVisible"
        :title="title"
        style="width: 415px;"
        top="30vh"
    >
        <el-scrollbar>
            <el-form :inline="false" :model="form" label-width="120px" :rules="rules" ref="formRef">
                <el-form-item label="终端名" prop="name">
                    <el-input v-model="form.name" placeholder="请输入终端名" clearable />
                </el-form-item>
                <el-form-item label="型号">
                    <el-input v-model="form.type" placeholder="请输入型号" clearable />
                </el-form-item>
                <el-form-item label="终端损失" prop="machineLoss">
                    <el-input v-model="form.machineLoss" placeholder="请输入终端损失" clearable>
                        <template #append>万元</template>
                    </el-input>
                </el-form-item>
                <el-form-item label="原料损失" prop="materialLoss">
                    <el-input v-model="form.materialLoss" placeholder="请输入原料损失" clearable>
                        <template #append>万元</template>
                    </el-input>
                </el-form-item>
                <el-form-item label="功率" prop="machinePower">
                    <el-input v-model="form.machinePower" placeholder="请输入功率" clearable>
                        <template #append>kW</template>
                    </el-input>
                </el-form-item>
            </el-form>
        </el-scrollbar>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="machineVisible = false">取消</el-button>
                <el-button type="primary" @click="submit">确认</el-button>
            </span>
        </template>
    </el-dialog>
</template>
<script lang="ts" setup>
import { ref, reactive, inject } from 'vue'
import { ElMessage } from 'element-plus'
import TableStore from '@/utils/tableStore'
import { addSgMachine, updateSgMachine } from '@/api/advance-boot/sgGroven/sgMachine'

const tableStore = inject('tableStore') as TableStore
const machineVisible = ref(false)
const title = ref('')
const formRef = ref()
const regex = /^[+]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?$/

// 注意不要和表单ref的命名冲突
const form = reactive({
    id: '',
    productLineId: '',
    name: '',
    type: '',
    machineLoss: 0,
    materialLoss: 0,
    machinePower: 0
})

//form表单校验规则
const rules = {
    name: [{ required: true, message: '终端名不能为空', trigger: 'blur' }],
    machineLoss: [
        { required: true, message: '终端损失不能为空', trigger: 'blur' },
        { pattern: regex, message: '请输入有效的数值', trigger: 'blur' }
    ],
    materialLoss: [
        { required: true, message: '原料损失不能为空', trigger: 'blur' },
        { pattern: regex, message: '请输入有效的数值', trigger: 'blur' }
    ],
    machinePower: [
        { required: true, message: '功率不能为空', trigger: 'blur' },
        { pattern: regex, message: '请输入有效的数值', trigger: 'blur' }
    ]
}

const open = (text: string, data?: any) => {
    title.value = text
    if (data.name) {
        // 表单赋值
        for (let key in form) {
            form[key] = data[key]
        }
    } else {
        resetForm()
        // 在此处恢复默认表单
        for (let key in form) {
            form[key] = ''
        }
    }
    form.productLineId = data.productLineId
    machineVisible.value = true
}

//重置表单内容
const resetForm = () => {
    if (formRef.value) {
        formRef.value.resetFields()
    }
}

/**
 * 提交用户表单数据
 */
const submit = () => {
    formRef.value.validate(async (valid: any) => {
        if (valid) {
            if (form.id) {
                await updateSgMachine(form)
            } else {
                await addSgMachine(form)
            }
            ElMessage.success('保存成功')
            tableStore.index()
            machineVisible.value = false
        }
    })
}

/************针对tab切换*************/

defineExpose({ open })
</script>

<style scoped>
.el-upload-list__item {
    transition: none !important;
}

.el-select {
    min-width: 180px;
}
</style>
