<template>
    <el-dialog draggable width="500px" v-model.trim="dialogVisible" :title="title">
        <el-form :model="form" ref="formRef" class="form-one" label-width="auto" :rules="rules">
            <el-form-item label="名称" prop="name">
                <el-input maxlength="32" show-word-limit    v-model.trim="form.name" placeholder="请输入名称" />
            </el-form-item>
            <el-form-item label="编码" prop="code">
                <el-input maxlength="32" show-word-limit    v-model.trim="form.code" placeholder="请输入编码" />
            </el-form-item>
            <el-form-item label="类型" v-if="title == '新增' || title == '编辑'" >
                <el-select v-model.trim="form.type" placeholder="请选择类型" clearable>
                    <el-option v-for="item in options" :key="item.value" :label="item.name" :value="item.value" />
                </el-select>
            </el-form-item>
            <el-form-item label="备注">
                <el-input maxlength="300" show-word-limit    v-model.trim="form.remark" placeholder="请输入备注" />
            </el-form-item>

            <el-form-item label="排序" prop="sort">
                <el-input-number v-model.number="form.sort" style="width: 100%;" :min="0" />
            </el-form-item>
        </el-form>

        <template #footer>
            <span class="dialog-footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="submit" :loading="loading">确定</el-button>
            </span>
        </template>
    </el-dialog>
</template>
<script lang="ts" setup>
import { ref, inject } from 'vue'
import { reactive } from 'vue'
import TableStore from '@/utils/tableStore'
import { addDictTree, updateStatistical } from '@/api/system-boot/dictTree'
import { ElMessage } from 'element-plus'

const emits = defineEmits<{
    (e: 'over'): void
}>()
defineOptions({
    name: 'govern/setting/statisticalType/add'
})
const tableStore = inject('tableStore') as TableStore
const loading = ref(false)
const form = reactive({
    name: '',
    code: '',
    remark: '',
    type: 2,
    sort: 100,
    pid: '',
    id: ''
})
const formRef = ref()
const rules = {
    name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
    code: [{ required: true, message: '请输入编码', trigger: 'blur' }],
    sort: [{ required: true, message: '请输入排序', trigger: 'blur' }]
}
const options = ref([
    { name: '通用指标', value: 2 },
    { name: 'APF模块', value: 3 },
    { name: '星型接线', value: 4 },
    { name: '角型接线', value: 5 },
    { name: 'V型接线', value: 6 },
    { name: '驾驶舱指标', value: 7 }
])

const dialogVisible = ref(false)
const title = ref('新增菜单')
const open = (text: string, data?: anyObj) => {
    formRef.value?.resetFields()
    title.value = text
    for (let key in form) {
        form[key] = data ? data[key] : ''

        if (key == 'sort') {
            form[key] = data?.sort ?? 100
        }
    }
    dialogVisible.value = true
}
const submit = async () => {
    // 非空校验
    formRef.value.validate(async valid => {
        if (valid) {
            loading.value = true
            if (form.id) {
                await updateStatistical(form).then(res => {
                    ElMessage.success('编辑成功')
                }).finally(() => {
                    loading.value = false
                })
            } else {
                if (!form.pid) {
                    form.pid = tableStore.table.params.pid
                }
                await addDictTree(form).then(res => {
                    ElMessage.success('新增成功')
                }).finally(() => {
                    loading.value = false
                })
            }
            emits('over')
            dialogVisible.value = false
        }
    })
}

defineExpose({ open })
</script>
