<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="title">
                                <a-input :placeholder="'请输入会客厅名称'" v-model:value="formData.title"></a-input>
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item :label="'会客厅人数'" name="maxNum">
                                <a-input-number :placeholder="'请输入排序'" v-model:value="formData.maxNum"
                                    style="width: 100%;">
                                    <template #addonAfter>
                                        人间
                                    </template>
                                </a-input-number>
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item :label="'会客厅描述'" name="content">
                                 <x-editor v-model="formData.content"></x-editor>
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item :label="'会客厅图片'" name="imgs">
                                <gx-upload v-model="formData.imgs" accept-types=".jpg,.png,.webp" :fileNumber="10" />
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item :label="'状态'" name="status">
                                <a-radio-group v-model:value="formData.status" :options="mettingStatus.getAll()"
                                    disabled></a-radio-group>
                            </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, 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 { mettingStatus } from '@/enums/index.js'
import { spliceUrl } from '@/utils/util'
import { status } from 'nprogress'
const areaFormRef = ref()
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 treeData = ref([])
formRules.value = {
    title: { required: true, message: '请输入会客厅名称' },
    maxNum: { required: true, message: '请输入人数' },
    content: { required: true, message: '请输入会客厅描述' },
    status: { required: true, message: '请选择状态', trigger: 'change' },
    imgs: { required: true, message: '请选择状态', trigger: 'change' }
}


/**
 * 新建
 */
function handleCreate() {
    showModal({
        type: 'create',
        title: '新增',
    })
    formData.value.status = 1
}

/**
 * 编辑
 */
async function handleEdit(record = {}) {
    showModal({
        type: 'edit',
        title: '编辑',
    })
    try {
        showSpining()
        const { data, success } = await apis.mettingRoom.getMenu(record.id).catch()
        if (!success) {
            hideModal()
            return
        }
        hideSpining()
        formData.value = { ...data }
        formData.value.imgs=data.imgs.length>0?data.imgs.map(item=>config('http.apiBasic')+item):[]
    } catch (error) {
        message.error({ content: error.message })
        hideSpining()
    }
}
/**
 * 确定
 */
function handleOk() {
    formRef.value.validateFields().then(async (values) => {
        try {
            showLoading()
            const params = {
                ...values,
                status:1,
                imgs:formData.value.imgs.length>0?formData.value.imgs.map(item=>spliceUrl(item)):[],
                num:formData.value.num||0,
                roomType:formData.value.roomType||1,
            }
            let result = null
            switch (modal.value.type) {
                case 'create':
                    result = await apis.mettingRoom.createMenu(params).catch((error) => {
                        throw new Error(error)
                    })
                    break
                case 'edit':
                    result = await apis.mettingRoom.updateMenu(formData.value.id, params).catch(() => {
                        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()
        })
}


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

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

defineExpose({
    handleCreate,
    handleEdit,
})


</script>

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