<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-col :span="24">
                        <a-form-item label="所属区域" name="areaId">
                            <a-select v-model:value="formData.areaId" allowClear>
                                <a-select-option :value="1">南通</a-select-option>
                                <a-select-option :value="2">盐城</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item :label="'导入文件'" name="fileList">
                            <gx-upload v-model="formData.fileList" accept=".xlsx,.xls"
                                :customUploadHandler="uploadExcelFile" @uploadSuccess="uploadSuccess" />
                        </a-form-item>
                    </a-col>
                </a-card>
            </a-form>
        </a-spin>
    </a-modal>
</template>

<script setup>
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 dayjs from 'dayjs'
import GxUpload from '@/components/GxUpload/index.vue'
const emit = defineEmits(['ok'])
const { t } = useI18n() // 解构出t方法
const { modal, showModal, hideModal, showLoading, hideLoading } = useModal()
const { 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([])

onBeforeMount(() => {
    formData.value.areaId = ''
})

/**
 * 新建
 */
function handleCreate() {
    showModal({
        type: 'create',
        title: '导入文件',
    })
    // initData()
    // formData.value.status = 'enabled'
}

/**
 * 编辑
 */
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() {
    hideModal()
    formData.value.areaId = null
    fileList.value = []
}


// 上传 Excel 的专用方法
const uploadExcelFile = async (file) => {
    const areaId = formData.value.areaId
    if (!areaId) {
        message.error('请先选择所属区域')
        throw new Error('缺少 areaId')
    }

    const formDataUpload = new FormData()
    formDataUpload.append('file', file)

    // 明确获取响应
    const res = await apis.customer.pushFile(areaId, formDataUpload)

    if (!res.success) {
        // message.error('Excel 上传失败')
        throw new Error(res.message)
    }

    message.success('Excel 上传成功')
    emit('ok')
    hideModal()
    formData.value.areaId = null
    fileList.value = []
    return res.data
}


/**
 * 取消
 */
function handleCancel() {
    formData.value.areaId = null
    hideModal()
}

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

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

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