<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="phone">
                                <a-input :placeholder="'请输入联系电话'" v-model:value="formData.phone"></a-input>
                            </a-form-item>
                        </a-col>

                        <a-col :span="24">
                            <a-form-item :label="'积分'" name="point">
                                <a-input-number :placeholder="'请输入积分'" v-model:value="formData.point"
                                    style="width: 100%;"></a-input-number>
                            </a-form-item>
                        </a-col>


                        <a-col :span="24">
                            <a-form-item :label="'添加原因'" name="reason">
                                <a-textarea v-model:value="formData.reason" placeholder="输入原因" auto-size />
                            </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, Modal } from 'ant-design-vue'
import { useI18n } from 'vue-i18n'
import dayjs from 'dayjs'
import GxUpload from '@/components/GxUpload/index.vue'
import { customersEnum, areaEnum } from "@/enums/useEnum"
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 fileList = ref([])
formRules.value = {
    phone: [{ required: true, message: '请输入联系电话' }],
    reason: [{ required: true, message: '请输入添加原因' }],
    point: [{ required: true, message: '请输入积分' }]
}


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

/**
 * 编辑
 */
async function handleEdit(record = {}) {
    showModal({
        type: 'edit',
        title: '编辑客户',
    })
    try {
        showSpining()
        const { data, success } = await apis.customer.getItem(record.id).catch()
        if (!success) {
            hideModal()
            return
        }
        hideSpining()
        formData.value = { ...data }
        formData.value.birthday = dayjs(data.birthday)
        if (data.avatar) {
            formData.value.fileList = [config('http.apiBasic') + data.avatar]
        }
    } catch (error) {
        message.error({ content: error.message })
        hideSpining()
    }

}
const uploadSuccess = (data) => {
    fileList.value.push(data)
}
/**
 * 确定
 */
function handleOk() {
    Modal.confirm({
        title: '即将添加业主，是否继续',
        content: t('button.confirm'),
        okText: t('button.confirm'),
        onOk: () => {
            formRef.value.validateFields().then(async (values) => {
                try {
                    showLoading()
                    const params = {
                        ...values
                    }
                    const result = await apis.customer.createHomer(params)
                    console.log(result)
                    hideLoading()
                    if (config('http.code.success') === result?.success) {
                        hideModal()
                        // emit('ok')
                    } else {
                        message.error({ content: result?.error.detail })
                        hideModal()
                    }
                } catch (error) {
                    // console.log(error)
                    // message.error({ content: error.detail })
                    hideLoading()
                }
            })
               
        }
    })

}


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

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

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

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