<template>
    <a-modal :open="modal.open" :title="modal.title" :confirm-loading="modal.confirmLoading" :after-close="onAfterClose"
        @ok="handleOk" @cancel="handleCancel">
        <a-form ref="formRef" scroll-to-first-error :model="formData" :rules="formRules"
            :label-col="{ style: { width: '80px' } }">
            <a-form-item label="封禁原因" name="blackReason">
                <a-textarea v-model:value="formData.blackReason"></a-textarea>
            </a-form-item>
            <a-form-item label="封禁天数" name="blackDays">
                <a-input :defaultValue="0" type="number" v-model:value="formData.blackDays"></a-input>
            </a-form-item>
            <a-form-item label="备注" name="remark">
                <a-textarea v-model:value="formData.remark"></a-textarea>
            </a-form-item>
        </a-form>
    </a-modal>
</template>

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

const emit = defineEmits(['ok'])

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

formRules.value = {
    blackReason: { required: true, message: '请输入理由' },
    blackDays: { required: true, message: '请输入封禁天数' }
}

/**
 * 新建
 */
function handleCreate() {
    showModal({
        type: 'create',
        title: '新建',
    })
}

/**
 * 编辑
 */
function handleEdit(record = {}) {
    showModal({
        type: 'edit',
        title: '用户封禁',
    })
    formRecord.value = record
}

/**
 * 确定
 */
function handleOk() {
    formRef.value
        .validateFields()
        .then(async (values) => {
            try {
                showLoading()
                const params = {
                    blackDays: Number(values.blackDays),
                    blackReason: values.blackReason,
                    remark: values.remark
                }
                let result = null
                switch (modal.value.type) {
                    case 'create':
                        result = await apis.common.create(params).catch(() => {
                            throw new Error()
                        })
                        break
                    case 'edit':
                        result = await apis.userControl.updateCustomers(formRecord.value, 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()
}

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

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