<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-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="status">
                                <a-radio-group v-model:value="formData.status"
                                    :options="[{ label: '通过', value: 2 }, { label: '拒绝', value: 5 }]"></a-radio-group>
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item :label="'备注'" name="remark">
                                <a-input v-model:value="formData.remark"></a-input>
                            </a-form-item>
                        </a-col>
                    </a-row>
                </a-card>
            </a-form>
        </a-spin>
    </a-modal>
</template>

<script setup>
import { cloneDeep } from 'lodash-es'
import { ref } 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'
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'))
formRules.value = {
    status: { required: true, message: '请选择审核状态', trigger: 'change' },
    remark: [{ required: true, message: '请输入备注', trigger: 'remark' }],
}
/**
 * 编辑
 */
async function handleEdit(record = {}) {
    showModal({type: 'edit',title: '审核'})
    formData.value.id=record.id
}

/**
 * 确定
 */
function handleOk() {
    formRef.value.validateFields().then(async (values) => {
        try {
            showLoading()
            const params = {
                id: formData.value.id,
                status: formData.value.status,
                remark: formData.value.remark
            }
            let result = null
            switch (modal.value.type) {
                case 'create':
                    result = await apis.nbdMutualAid.createMenu(params).catch(() => {
                        throw new Error()
                    })
                    break
                case 'edit':
                    result = await apis.nbdMutualAid.updateMenu(formData.value.id, params).catch(() => {
                        throw new Error()
                    })
                    break
            }
            hideLoading()
            if (config('http.code.success') === result?.success) {
                hideModal()
                emit('ok')
            }
        } catch (error) {
            console.log(error)
            hideLoading()
        }
    })
        .catch((e) => {
            console.log(e)
            hideLoading()
        })
}


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

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

defineExpose({
    handleEdit,
})
</script>

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