<template>
    <el-dialog v-model="showVisiable" :title="title" width="30%" :before-close="handleClose">
        <el-form ref="formRef" :rules="rules" :model="formData" status-icon label-position="left" label-width="120px">
            <el-form-item label="BOM编码" prop="bomCode">
                <el-input v-model="formData.bomCode" />
            </el-form-item>
            <el-form-item label="型号" prop="model">
                <el-input v-model="formData.model" />
            </el-form-item>
            <el-form-item label="商品货品名称" prop="productName">
                <el-input v-model="formData.productName" />
            </el-form-item>
        </el-form>
        <template #footer class="dialog-footer">
            <el-button @click="handleClose">取消</el-button>
            <el-button type="primary" @click="handleAdd">确定</el-button>
        </template>
    </el-dialog>
</template>

<script setup lang='ts'>
import { ref } from 'vue'

let emits = defineEmits(['clickAdd', 'clickEdit'])
let showVisiable = ref(false)
let formRef = ref()
let title = ref('添加规格属性')
let rules = ref({
    bomCode: { required: true, message: '', trigger: 'blur' },
    model: { required: true, message: '', trigger: 'blur' },
    productName: { required: true, message: '', trigger: 'blur' },
})
let formData = ref({
    bomCode: '',
    model: '',
    productName: ''
})

let handleAdd = () => {
    formRef.value?.validate((valid: boolean) => {
        if (valid) {
            if (title.value === '添加规格属性') {
                showVisiable.value = false
                emits('clickAdd', formData.value)
            } else {
                showVisiable.value = false
                emits('clickEdit', formData.value)
            }
        }
    })
}

let handleClose = () => {
    formData.value = {
        bomCode: '',
        model: '',
        productName: ''
    }
    showVisiable.value = false
}
defineExpose({
    showVisiable,
    formData,
    title,
    handleClose
})
</script>
 
<style lang = "scss" scoped>
</style>