<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-tabs v-model:activeKey="activeKey">
                        <a-tab-pane key="1" tab="基本信息">
                            <a-row :gutter="12">
                                <a-col :span="24">
                                    <a-form-item :label="'产品分类'" name="typeId">
                                        <a-select ref="select" v-model:value="formData.typeId" style="width: 100%">
                                            <a-select-option v-for="item in typeList" :value="item.value">{{
                                                item.label }}</a-select-option>
                                        </a-select>
                                    </a-form-item>
                                </a-col>
                                <a-col :span="24">
                                    <a-form-item :label="'产品排序'" name="sequence">
                                        <a-input-number :placeholder="'请输入排序'" v-model:value="formData.sequence"
                                            style="width: 100%;"></a-input-number>
                                    </a-form-item>
                                </a-col>
                                <a-col :span="24">
                                    <a-form-item :label="'门店名称'" name="storeName">
                                        <a-input :placeholder="'请输入门店名称'" v-model:value="formData.storeName"></a-input>
                                    </a-form-item>
                                </a-col>
                                <a-col :span="24">
                                    <a-form-item :label="'营业时间'" name="openAt">
                                        <a-input :placeholder="'请输入营业时间'" v-model:value="formData.openAt"></a-input>
                                    </a-form-item>
                                </a-col>
                                <a-col :span="24">
                                    <a-form-item :label="'联系电话'" name="openAt">
                                        <a-input :placeholder="'请输入联系电话'" v-model:value="formData.concatPhone"></a-input>
                                    </a-form-item>
                                </a-col>
                                <a-col :span="24">
                                    <a-form-item :label="'标签'" name="labels">
                                        <a-select v-model:value="formData.labels" mode="tags" style="width: 100%"
                                            placeholder="输入标签然后回车"></a-select>
                                    </a-form-item>
                                </a-col>
                                <a-col :span="24">
                                    <a-form-item :label="'状态'" name="status">
                                        <a-radio-group v-model:value="formData.status" :options="storeStatus.getAll()"
                                            disabled></a-radio-group>
                                    </a-form-item>
                                </a-col>
                                <a-col :span="24">
                                    <a-form-item :label="'店铺描述'" name="content">
                                        <a-textarea :placeholder="'请输入店铺描述'" v-model:value="formData.content"
                                            :auto-size="{ minRows: 2, maxRows: 5 }"></a-textarea>
                                    </a-form-item>
                                </a-col>
                                <a-col :span="12">
                                    <a-form-item :label="'店铺封面'" name="storeCover">
                                        <gx-upload v-model="formData.storeCover" accept-types=".jpg,.png,.webp"
                                            :fileNumber="1" />
                                    </a-form-item>
                                </a-col>
                                <a-col :span="12">
                                    <a-form-item :label="'详情图片'" name="imgs">
                                        <gx-upload v-model="formData.img" accept-types=".jpg,.png,.webp"
                                            :fileNumber="10" />
                                    </a-form-item>
                                </a-col>
                            </a-row>
                        </a-tab-pane>
                        <a-tab-pane key="2" tab="位置信息">
                            <a-row :gutter="12">
                                <a-col :span="12">
                                    <a-form-item :label="'经度'" name="longitude">
                                        <a-input :placeholder="'请输入经度'" v-model:value="formData.longitude"></a-input>
                                    </a-form-item>
                                </a-col>
                                <a-col :span="12">
                                    <a-form-item :label="'纬度'" name="latitude">
                                        <a-input :placeholder="'请输入纬度'" v-model:value="formData.latitude"></a-input>
                                    </a-form-item>
                                </a-col>
                                <a-col :span="24">
                                    <a-form-item :label="'详细地址'" name="storeAddress">
                                        <a-input :placeholder="'请输入详细地址'"
                                            v-model:value="formData.storeAddress"></a-input>
                                    </a-form-item>
                                </a-col>
                                <a-col :span="24">
                                    <gx-map @handleGetLng="handleGetLng" />
                                </a-col>
                            </a-row>
                        </a-tab-pane>
                    </a-tabs>


                </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 { storeStatus } from '@/enums/index.js'
import { spliceUrl } from '@/utils/util'
import { init } from 'echarts'
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 typeList = ref([])
const searchValue = ref('')
const activeKey = ref('1')
formRules.value = {
    typeId: [{ required: true, message: '请选择产品类别名称', trigger: 'change' }],
    sequence: { required: true, message: '请输入排序' },
    storeName: { required: true, message: '请输入门店名称' },
    longitude: { required: true, message: '请输入经度' },
    latitude: { required: true, message: '请输入纬度' },
    storeAddress: { required: true, message: '请输入门店地址' },
    status: { required: true, message: '请选择状态', trigger: 'change' }
}
const initData = async () => {
    try {
        showSpining()
        const { data, success } = await apis.serverType.getDataList({ pageSize: 99, current: 1 })
        if (success) {
            typeList.value = data.map(item => ({ value: item.id, label: item.label }))
            hideSpining()
        }
    } catch (error) {
        hideSpining()
    }
}
/**
 * 新建
 */
function handleCreate() {
    showModal({
        type: 'create',
        title: '新增',
    })
    initData()
    formData.value.status = 1
}

/**
 * 编辑
 */
async function handleEdit(record = {}) {
    showModal({
        type: 'edit',
        title: '编辑',
    })
    const { data, success } = await apis.serverList.getMenu(record.id).catch()
    if (!success) {
        hideModal()
        return
    }
    formData.value = { ...data }
    formData.value.storeCover = data.storeCover ? [config('http.apiBasic') + data.storeCover] : []
    formData.value.img =  data.imgs?.map(url => config('http.apiBasic') + url) || []
    initData()
}
/**
 * 确定
 */
function handleOk() {
    formRef.value.validateFields().then(async (values) => {
        try {
            showLoading()
            console.log(formData.value)
            const params = {
                ...values,
                storeCover: formData.value.storeCover ? spliceUrl(formData.value.storeCover[0]) : '',
                imgs: formData.value.img?.map(url => spliceUrl(url)) || [],
                latitude: parseFloat(formData.value.latitude),
                longitude: parseFloat(formData.value.longitude),
                status: 1
            }
            console.log("====",params)
            let result = null
            switch (modal.value.type) {
                case 'create':
                    result = await apis.serverList.createMenu(params).catch((error) => {
                        throw new Error(error)
                    })
                    break
                case 'edit':
                    result = await apis.serverList.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) => {
            console.log(e)
            hideLoading()
        })
}
const handleGetLng = (obj = {}) => {
    formData.value.longitude = obj.lng
    formData.value.latitude = obj.lat
    formData.value.storeAddress = obj.address
}

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

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

defineExpose({
    handleCreate,
    handleEdit,
})


</script>

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