<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-form ref="formRef" :model="formData" :rules="formRules">
            <a-card class="mb-8-2">
                <a-row :gutter="24">
                    <a-col :span="24">
                        <a-form-item :label="$t('pages.system.role.form.name')" name="name">
                            <a-input v-model:value="formData.name"></a-input>
                        </a-form-item>
                    </a-col>

                    <a-col :span="24">
                        <a-form-item :label="$t('pages.system.role.form.sequence')" name="sequence">
                            <a-input :defaultValue="0" type="number" v-model:value="formData.sequence"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item :label="$t('pages.system.role.form.status')" name="status">
                            <a-radio-group v-model:value="formData.status" :options="[
                                { label: $t('pages.system.role.form.status.enabled'), value: 'enabled' },
                                { label: $t('pages.system.role.form.status.disabled'), value: 'disabled' },
                            ]"></a-radio-group>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item :label="'描述'">
                            <a-textarea v-model:value="formData.description"></a-textarea>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item :label="'上传图片'" name="permissions">
                            <GxUpload :fileNumber="1" />
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-card>
        </a-form>
    </a-modal>
</template>

<script setup>
import { cloneDeep } from 'lodash-es'
import { message } from 'ant-design-vue'
import { ref, watch } from 'vue'
import { config } from '@/config'
import apis from '@/apis'
import { useForm, useModal } from '@/hooks'
import GxUpload from '@/components/GxUpload/index.vue'
const emit = defineEmits(['ok'])
import { useI18n } from 'vue-i18n'
const { modal, showModal, hideModal, showLoading, hideLoading } = useModal()
const { formRecord, formData, formRef, formRules, resetForm } = useForm()
const { t } = useI18n() // 解构出t方法
const cancelText = ref(t('button.cancel'))
const okText = ref(t('button.confirm'))
formData.value.enabled='enabled'
formRules.value = {
    name: { required: true, message: t('pages.system.role.form.name.placeholder') },
    code: { required: true, message: t('pages.system.role.form.code.placeholder') },
    status: { required: true, message: t('pages.system.role.form.status.placeholder') },
}

/**
 * 新建
 */
function handleCreate() {
    showModal({
        type: 'create',
        title: t('pages.system.role.add'),
    })
}

/**
 * 编辑
 */
async function handleEdit(record = {}) {
    showModal({
        type: 'edit',
        title: t('pages.system.role.edit'),
    })

    const { data, success } = await apis.role.getRole(record.id).catch()
    if (!success) {
        message.error(t('component.message.error.save'))
        hideModal()
        return
    }
    let menus = []
    if (data.menus) {
        for (let item of data.menus) {
            menus.push(item.menu_id)
        }
    }
    checkedKeys.value = menus
    formRecord.value = data
    formData.value = cloneDeep(data)
}

/**
 * 确定
 */
function handleOk() {
    formRef.value
        .validateFields()
        .then(async (values) => {
            try {
                showLoading()
                const params = {...values}
                let result = null
                switch (modal.value.type) {
                    case 'create':
                        result = await apis.banner.createBanner(params).catch(() => {
                            throw new Error()
                        })
                        break
                    case 'edit':
                        result = await apis.banner.updateBanner(formData.value.id, params).catch(() => {
                            throw new Error()
                        })
                        break
                }
                hideLoading()
                if (config('http.code.success') === result?.success) {
                    hideModal()
                    emit('ok')
                }
            } catch (error) {
                hideLoading()
            }
        })
        .catch(() => {
            hideLoading()
        })
}

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

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

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

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