<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="checked">
                <a-radio-group v-model:value="formData.checked" name="radioGroup">
                    <a-radio :value="true">审核通过</a-radio>
                    <a-radio :value="false">审核拒绝</a-radio>
                </a-radio-group>
            </a-form-item>
            <a-form-item label="原因" name="reason">
                <a-textarea v-model:value="formData.reason" placeholder="请输入原因" :auto-size="{ minRows: 2, maxRows: 5 }" />
            </a-form-item>
        </a-form>
    </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 = {
    title: { required: true, message: '请输入标题' },
}

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

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

/**
 * 确定
 */
function handleOk() {
    formRef.value
        .validateFields()
        .then(async (values) => {
            try {
                showLoading()
                const params = {
                    ...formRecord.value,
                }
                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.invoice.sendResult(formRecord.value.id, 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>
