<template>
    <a-modal :open="modal.open" :title="modal.title" :confirm-loading="modal.confirmLoading" :after-close="onAfterClose"
        @ok="handleOk" @cancel="handleCancel">
        <a-card>
            <a-form ref="formRef" scroll-to-first-error :model="formData" :rules="formRules">
                <a-form-item label="用户名" name="userName">
                    <a-input v-model:value="formData.userName"></a-input>
                </a-form-item>
                <a-form-item label="手机号" name="phone">
                    <a-input v-model:value="formData.phone"></a-input>
                </a-form-item>
                <a-form-item label="算力券名称" name="couponName">
                    <a-input v-model:value="formData.couponName"></a-input>
                </a-form-item>
                <a-form-item label="券面点数" name="faceValue">
                    <a-input v-model:value="formData.faceValue"></a-input>
                </a-form-item>
                <a-form-item label="可用点数" name="availablePoints">
                    <a-input v-model:value="formData.availablePoints"></a-input>
                </a-form-item>
                <a-form-item label="生效时间" name="effectiveTime">
                    <a-date-picker v-model:value="formData.effectiveTime" style="width: 100%;"/>
                </a-form-item>
                <a-form-item label="失效时间" name="expiryTime">
                    <a-date-picker v-model:value="formData.expiryTime" style="width: 100%;"/>
                </a-form-item>
                <a-form-item label="状态" name="status">
                    <a-radio-group v-model:value="formData.status" name="radioGroup">
                        <a-radio value="1">启用</a-radio>
                        <a-radio value="2">停用</a-radio>
                    </a-radio-group>
                </a-form-item>
            </a-form>
        </a-card>
    </a-modal>
</template>

<script setup>
import { cloneDeep } from 'lodash-es'
import apis from '@/apis'
import { useForm, useModal } from '@/hooks'

const emit = defineEmits(['ok'])

const { modal, showModal, hideModal, showLoading, hideLoading } = useModal()
const { formRef, formRules, formRecord, formData, resetForm } = useForm()

formRules.value = {
    userName: { required: true, message: '请输入用户名' },
    phone: { required: true, message: '请输入手机号' },
    couponName: { required: true, message: '请输入算力券名称' },
    faceValue: { required: true, message: '请输入券面点数' },
    availablePoints: { required: true, message: '请输入可用点数' },
    effectiveTime: { required: true, message: '请选择开始时间',trigger:'change' },
    expiryTime: { required: true, message: '请选择结束时间',trigger:'change' },
    status: { required: true, message: '请选择状态',trigger:'change' },
}

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

/**
 * 编辑
 */
function handleEdit(record = {}) {
    showModal({
        type: 'edit',
        title: '编辑',
    })
    formRecord.value = record
    formData.value = cloneDeep(record)
}

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

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

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

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

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