<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="'标题'" 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="order">
                            <a-input :defaultValue="0" type="number" v-model:value="formData.order"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item :label="'是否推荐到首页'" name="is_recommend">
                            <a-radio-group v-model:value="formData.is_recommend" :options="[
                                { label: '是', value: true },
                                { label:'否', value: false },
                            ]"></a-radio-group>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item :label="'图片类型'" name="type">
                            <a-radio-group v-model:value="formData.type" :options="[
                                { label: '轮播图片', value: '1'},
                                { label: '营销图片', value: '2'},
                            ]"></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() {
    console.log('添加handleCreate')
    showModal({
        type: 'create',
        title: '添加图片',
    })
}

/**
 * 编辑
 */
async function handleEdit(record = {}) {
    showModal({
        type: 'edit',
        title: '编辑图片',
    })

    const { data, success } = await apis.banner.getBanner(record.id).catch()
    if (!success) {
        message.error(t('component.message.error.save'))
        hideModal()
        return
    }

    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>
