<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="handleCancel" @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="customerName">
                                <a-input v-model:value="formData.customerName"></a-input>
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item :label="'求助标题'" name="title">
                                <a-input v-model:value="formData.title"></a-input>
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item :label="'创建时间'" name="createdAt">
                                <a-date-picker v-model:value="formData.createdAt" style="width: 100%;"></a-date-picker>
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item :label="'状态'" name="status">
                                <a-radio-group v-model:value="formData.status"
                                    :options="nbdMutualStatus.getAll()" disabled></a-radio-group>
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item :label="'内容'" name="content">
                                <a-textarea v-model:value="formData.content"></a-textarea>
                            </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-col :span="24">
                            <a-form-item :label="'图片'" name="images">
                                <a-image :width="200" v-for="item in formData.images" :src="config('http.apiBasic')+item" />
                            </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'
import { nbdMutualStatus } from '@/enums/index.js'
import dayjs from 'dayjs'
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 = {
    // customerName: { required: true, message: '', trigger: 'change' },
    // status: { required: true, message: '请选择审核状态', trigger: 'change' },
    // remark: [{ required: true, message: '请输入备注', trigger: 'remark' }],
}
/**
 * 编辑
 */

async function handleEdit(record = {}) {
    showModal({
        type: 'edit',
        title: '编辑',
    })
       try {
        showSpining()
        const { data, success } = await apis.nbdMutuaAid.getMenu(record.id).catch()
        if (!success) {
            hideModal()
            return
        }
        hideSpining()
        formData.value = { ...data }
        formData.value.createdAt=dayjs(data.createdAt)
    } catch (error) {
        message.error({ content: error.message })
        hideSpining()
    }
}

/**
 * 确定
 */
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>
