<template>
    <a-drawer v-model:visible="visible" :title="isEdit ? '编辑维度' : '新增维度'" @before-ok="save">
        <a-form ref="formRef" :model="form" auto-label-width>
            <a-form-item label="维度名称" field="dimensionName" required>
                <a-input v-model="form.dimensionName" />
            </a-form-item>
            <a-form-item label="岗位" field="deptName" required>
                <a-input v-model="form.deptName" />
            </a-form-item>
            <a-form-item label="描述" field="description">
                <a-textarea v-model="form.description" :rows="3" />
            </a-form-item>
            <a-form-item label="状态" field="status">
                <a-switch v-model="form.status" :checked-value="0" :unchecked-value="1" />
            </a-form-item>
        </a-form>
    </a-drawer>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { addDimension, updateDimension } from '@/apis/performance-setting'
import type { PerformanceDimension } from '@/apis/performance-setting/type'

const emit = defineEmits(['success'])
const visible = ref(false)
const isEdit = ref(false)
const formRef = ref()
const form = ref<Partial<PerformanceDimension>>({
    dimensionName: '',
    deptName: '',
    description: '',
    status: 0,
})

const open = (record?: PerformanceDimension) => {
    if (record) {
        Object.assign(form.value, record)
        isEdit.value = true
    } else {
        Object.assign(form.value, { dimensionName: '', deptName: '', description: '', status: 0 })
        isEdit.value = false
    }
    visible.value = true
}

const save = async () => {
    if (isEdit.value && form.value.dimensionId) {
        await updateDimension(form.value.dimensionId, form.value)
    } else {
        await addDimension(form.value)
    }
    visible.value = false
    emit('success')
}

defineExpose({ open })
</script>
