<template>
    <a-modal :open="modal.open" :title="modal.title" :width="800" :confirm-loading="modal.confirmLoading"
        :after-close="onAfterClose" :cancel-text="cancelText" :ok-text="okText" @ok="handleOk" @cancel="handleCancel">
        <a-spin :spinning="spining">
            <a-form ref="formRef" :model="formData" :rules="formRules">
                <a-card class="mb-8-2">
                    <a-row :gutter="12">
                        <a-col :span="24">
                            <a-form-item :label="'字典名称'" name="name">
                                <a-input :placeholder="'请输入字典名称'" v-model:value="formData.name"
                                    style="width: 100%;"></a-input>
                            </a-form-item>
                        </a-col>
                         <a-col :span="24">
                            <a-form-item :label="'字典类型'" name="code">
                                <a-input :placeholder="'请输入字典类型'" v-model:value="formData.code"
                                    style="width: 100%;"></a-input>
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item :label="'备注'" name="introduction">
                                <a-input :placeholder="'请输入备注'" v-model:value="formData.introduction"
                                    style="width: 100%;"></a-input>
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item :label="'状态'" name="status">
                                <a-radio-group v-model:value="formData.status" :options="[
                                    { label: '启用', value: 'enabled' },
                                    { label: '停用', value: 'disabled' },
                                ]"></a-radio-group>
                            </a-form-item>
                        </a-col>
                    </a-row>
                </a-card>
            </a-form>
        </a-spin>
    </a-modal>
</template>

<script setup>
import { ref, h, onBeforeMount } from 'vue'
import { config } from '@/config'
import apis from '@/apis'
import { useForm, useModal, useSpining } from '@/hooks'
import { message } from 'ant-design-vue'
import { useI18n } from 'vue-i18n'
import storage from '@/utils/storage'
const emit = defineEmits(['ok'])
const { t } = useI18n() // 解构出t方法
const { modal, showModal, hideModal, showLoading, hideLoading } = useModal()
const { formRecord, formData, formRef, formRules, resetForm } = useForm()
const { spining, showSpining, hideSpining } = useSpining()
const cancelText = ref(t('button.cancel'))
const okText = ref(t('button.confirm'))
formRules.value = {
    name: { required: true, message: '请输入字典名称' },
    code: { required: true, message: '请输入字典类型' },
}

/**
 * 新建
 */
function handleCreate() {
    showModal({
        type: 'create',
        title: '新建字典',
    })
    formData.value.status = 'enabled'
}

/**
 * 编辑
 */
async function handleEdit(record = {}) {
    showModal({
        type: 'edit',
        title: '编辑字典',
    })
    try {
        showSpining()
        const { data, success } = await apis.dict.getItem(record.id).catch()
        if (!success) {
            hideModal()
            return
        }
       formData.value = { ...data }
        hideSpining()
    } catch (error) {
        message.error({ content: error.message })
        hideSpining()
    }

}
/**
 * 确定
 */
function handleOk() {
    formRef.value.validateFields().then(async (values) => {
        try {

            showLoading()
            const params = {
                ...formData.value,
            }
            let result = null
            switch (modal.value.type) {
                case 'create':
                    params.companyId = storage.local.getItem('companyId')
                    result = await apis.dict.createItem(params).catch((error) => {
                        console.log(error.message)
                        throw new Error(error)
                    })
                    break
                case 'edit':
                    result = await apis.dict.updateItem(formData.value.id, params).catch(() => {
                        console.log(error.message)
                        throw new Error(error)
                    })
                    break
            }
            hideLoading()
            if (config('http.code.success') === result?.success) {
                hideModal()
                emit('ok')
            }
        } catch (error) {
            message.error({ content: error.message })
            hideLoading()
        }
    })
        .catch((e) => {
            hideLoading()
        })
}

// 在 setup 中添加这些方法

/**
 * 取消
 */
function handleCancel() {

    hideModal()
}

/**
 * 关闭后
 */
function onAfterClose() {
    resetForm()
    hideLoading()
}

defineExpose({
    handleCreate,
    handleEdit,
})

</script>

<style lang="less" scoped></style>
