<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="duration">
                                <a-input-number :placeholder="'请输入浏览时长'" v-model:value="formData.duration"  style="width: 100%;"></a-input-number>
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item  :label="'次数/每周期'" name="maxCount">
                                <a-input-number :placeholder="'请输入次数/每周期'" v-model:value="formData.maxCount"
                                    style="width: 100%;"></a-input-number>
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item :label="'积分数'" name="points" >
                                <a-input-number :placeholder="'请输入积分数'" v-model:value="formData.points"
                                    style="width: 100%;"></a-input-number>
                            </a-form-item>
                        </a-col>
                     
                        <a-col :span="24">
                            <a-form-item :label="'天数/每周期'" name="maxDays">
                                <a-input-number :placeholder="'请输入天数/每周期'" v-model:value="formData.maxDays"
                                    style="width: 100%;"></a-input-number>
                            </a-form-item>
                        </a-col>
                                             <!-- <a-col :span="24">
                            <a-form-item :label="'规则说明'">
                                <x-editor v-model="editorContent" :uploadHandler="uploadHandler"
                                    :height="300"></x-editor>
                            </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 { spliceUrl,decryptString,encryptString  } from "@/utils/util.js"
import { signInEnum } from "@/enums/useEnum"
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 listData = ref([])
const numChecked = ref(false)
const editorContent = ref('')
const probabilityChecked = ref(false)
formRules.value = {
    duration: { required: true, message: '请输入浏览时长' },
    points: { required: true, message: '请输入积分' },
    maxCount: { required: true, message: '请输入次数' },
    maxDays: { required: true, message: '请输入天数/每周期', trigger: 'change' },
    status: [{ required: true, message: '请输入状态', trigger: 'change' }],
    content: { 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.earnPoint.getItem(record.id).catch()
        if (!success) {
            hideModal()
            return
        }
        hideSpining()
        formData.value = {
            ...data,
            cycleType: data.maxDays < 0 ? '自然月' : '固定天数',
        }
        // editorContent.value = decryptString(data.content) || ''
    } catch (error) {
        message.error({ content: error.message })
        hideSpining()
    }

}
/**
 * 确定
 */
function handleOk() {
    formRef.value.validateFields().then(async (values) => {
        try {
           
            showLoading()
            const params = {
                ...formData.value,
                // content: encryptString(editorContent.value),
              
            }
            let result = null
            switch (modal.value.type) {
                case 'create':
                    result = await apis.earnPoint.createProject(params).catch((error) => {
                        console.log(error.message)
                        throw new Error(error)
                    })
                    break
                case 'edit':

                    result = await apis.earnPoint.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>
