<template>
    <a-modal :open="modal.open" :title="modal.title" :width="640" :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="title">
                                <a-input :placeholder="'请输入名称'" v-model:value="formData.title"></a-input>
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item :label="'顺序'" name="sequence">
                                <a-input-number :placeholder="'请输入顺序'" v-model:value="formData.sequence"
                                    style="width: 100%;"></a-input-number>
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item :label="'状态'" name="status">
                                <a-radio-group v-model:value="formData.status"
                                    :options="homeStatus.getAll()" disabled></a-radio-group>
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item :label="'图片'" name="img">
                                <gx-upload v-model="formData.img" :fileNumber="1" />
                            </a-form-item>
                        </a-col>
                    </a-row>
                </a-card>
            </a-form>
        </a-spin>
    </a-modal>
</template>

<script setup>
import { cloneDeep } from 'lodash-es'
import { ref } 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 dayjs from 'dayjs'
import { homeStatus } from '@/enums/index.js'
import { spliceUrl } from '@/utils/util'
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'))
const rolesValue = ref([])
const roles = ref([])
const img = ref('')

formRules.value = {
    title: { required: true, message: '请输入名称' },
    status: [{ required: true, message: '请选择状态', trigger: 'change' }],
    sequence: [{ required: true, message: '请选择顺序', trigger: 'change' }],
    img: [{ required: true, message: '请上传图片', trigger: 'change' }],
}

/**
 * 新建
 */
function handleCreate() {
    showModal({
        type: 'create',
        // 80对应about
        title: '添加',
    })
    formData.value.status = 1
}

/**
 * 编辑
 */
async function handleEdit(record = {}) {
    showModal({
        type: 'edit',
        title: t('pages.system.user.edit'),
    })
    showSpining()
    const { data, success } = await apis.imgmgt.getMenu(record.id).catch()
    if (!success) {
        hideModal()
        return
    }
    hideSpining()
    formData.value = { ...data }
    formData.value.img = [config('http.apiBasic') + data.img]
}

/**
 * 确定
 */
function handleOk() {
    if (!formData.value.img) return message.error('请上传图片');
    formRef.value.validateFields().then(async (values) => {
        console.log(values)
        try {
            showLoading()
            const params = {
                ...values,
                img: spliceUrl(formData.value?.img[0]),
                scene: 2,
                status:1
            }
            let result = null
            switch (modal.value.type) {
                case 'create':
                    result = await apis.imgmgt.createMenu(params).catch(() => {
                        throw new Error()
                    })
                    break
                case 'edit':
                    // result = await apis.imgmgt.updateMenu(formData.value.id, params).catch(() => {
                    result = await apis.imgmgt.updateMenu(formData.value.id, params).catch(() => {
                        throw new Error()
                    })
                    break
            }
            hideLoading()
            if (config('http.code.success') === result?.success) {
                hideModal()
                emit('ok')
            }
        } catch (error) {
            console.log(error)
            hideLoading()
        }
    })
        .catch((e) => {
            console.log(e)
            hideLoading()
        })
}

/**
 * 取消
 */
function handleCancel() {
    formData.value.img = ['']
    hideModal()
}

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

defineExpose({
    handleCreate,
    handleEdit,
})
</script>

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