<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="门店编号" prop="storeCode">
              <el-input v-model="queryParams.storeCode" placeholder="请输入门店编号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="门店名称" prop="name">
              <el-input v-model="queryParams.name" placeholder="请输入门店名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="详细地址" prop="address">
              <el-input v-model="queryParams.address" placeholder="请输入详细地址" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['physical:store:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['physical:store:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['physical:store:remove']"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['physical:store:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="storeList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="编号" align="center" prop="id" v-if="true" />
        <el-table-column label="门店编号" align="center" prop="storeCode" />
        <el-table-column label="门店名称" align="center" prop="name" />
        <el-table-column label="门店类型" align="center" prop="storeTypeName" />
        <el-table-column label="所属赛区" align="center" prop="zoneId" />
        <el-table-column label="详细地址" align="center" prop="address" />
        <el-table-column label="门店图标" align="center" prop="iconUrl" width="120">
          <template #default="scope">
            <img v-if="scope.row.iconUrl" :src="scope.row.iconUrl" alt="" style="width: 40px; height: 40px; object-fit: cover; border-radius: 4px" />
            <span v-else>无</span>
          </template>
        </el-table-column>

        <el-table-column label="门店背景图" align="center" prop="backgroundUrl" width="120">
          <template #default="scope">
            <img
              v-if="scope.row.backgroundUrl"
              :src="scope.row.backgroundUrl"
              alt=""
              style="width: 40px; height: 40px; object-fit: cover; border-radius: 4px"
            />
            <span v-else>无</span>
          </template>
        </el-table-column>
        <el-table-column label="营业开始时间" align="center" prop="businessStartTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.businessStartTime, '{h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>

        <el-table-column label="营业结束时间" align="center" prop="businessEndTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.businessEndTime, '{h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'info'">
              {{ scope.row.status === 1 ? '开业中' : '闭店中' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createdAt" width="180"></el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['physical:store:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['physical:store:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改门店信息对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="600px" append-to-body>
      <el-form ref="storeFormRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="门店编号" prop="storeCode">
          <el-input v-model="form.storeCode" placeholder="请输入门店编号" />
        </el-form-item>
        <el-form-item label="门店名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入门店名称，最多20字" />
        </el-form-item>
        <el-form-item label="门店类型" prop="storeTypeId">
          <el-select v-model="form.storeTypeId" placeholder="请选择门店类型" style="width: 100%">
            <el-option v-for="item in tagOptions" :key="item.id" :label="item.serviceName" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="所属赛区" prop="zoneId">
          <el-input v-model="form.zoneId" placeholder="请输入所属赛区" />
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" type="textarea" placeholder="请输入内容" />
          <!-- 添加地图选择按钮 -->
          <el-button type="primary" @click="openMapSelector">选择地图位置</el-button>
        </el-form-item>
        <el-form-item label="门店图标" prop="iconUrl">
          <div class="upload-container">
            <el-upload
              class="upload-icon"
              action="#"
              :on-change="handleIconChange"
              :on-remove="handleIconRemove"
              :file-list="fileList"
              :auto-upload="false"
              :limit="1"
              accept="image/*"
            >
              <template #trigger>
                <el-button type="primary">点击选择图标</el-button>
              </template>

              <!-- 预览图区域 -->
              <template #default>
                <div class="preview-area" @click="handlePreviewClick">
                  <img
                    v-if="iconPreviewUrl || form.iconUrl"
                    :src="iconPreviewUrl || form.iconUrl"
                    alt="预览图"
                    style="max-width: 100px; max-height: 100px; margin-top: 10px; cursor: pointer"
                  />
                  <div v-else style="margin-top: 10px; color: #999">无</div>
                </div>
              </template>

              <template #tip>
                <div class="el-upload__tip">
                  <span v-if="fileList.length > 0">当前已选文件：{{ fileList[0].name }}</span>
                </div>
              </template>
            </el-upload>
          </div>
        </el-form-item>

        <el-form-item label="门店背景" prop="backgroundUrl">
          <div class="upload-container">
            <el-upload
              class="upload-icon"
              action="#"
              :on-change="handleIconChange2"
              :on-remove="handleIconRemove2"
              :file-list="fileList2"
              :auto-upload="false"
              :limit="1"
              accept="image/*"
            >
              <template #trigger>
                <el-button type="primary">点击选择背景</el-button>
              </template>

              <!-- 预览图区域 -->
              <template #default>
                <div class="preview-area" @click="handlePreviewClick2">
                  <img
                    v-if="iconPreviewUrl2 || form.backgroundUrl"
                    :src="iconPreviewUrl2 || form.backgroundUrl"
                    alt="预览图"
                    style="max-width: 100px; max-height: 100px; margin-top: 10px; cursor: pointer"
                  />
                  <div v-else style="margin-top: 10px; color: #999">无</div>
                </div>
              </template>

              <template #tip>
                <div class="el-upload__tip">
                  <span v-if="fileList2.length > 0">当前已选文件：{{ fileList2[0].name }}</span>
                </div>
              </template>
            </el-upload>
          </div>
        </el-form-item>

        <el-form-item label="门店标签" prop="customTags">
          <div class="tag-container" style="display: flex; align-items: center; gap: 8px">
            <!-- 已选标签 -->
            <el-tag
              v-for="(tag, index) in customTags"
              :key="index"
              closable
              @close="handleTagClose(index)"
              style="margin-right: 8px; margin-bottom: 8px"
            >
              {{ tag }}
            </el-tag>

            <!-- 添加新标签按钮 -->
            <el-button type="primary" size="small" plain @click="openAddTagDialog" style="margin-top: 4px"> + 添加标签 </el-button>
          </div>
        </el-form-item>

        <el-form-item label="营业时间" prop="businessStartTime">
          <el-time-picker
            v-model="form.businessStartTime"
            format="HH:mm:ss"
            value-format="HH:mm:ss"
            placeholder="请选择营业开始时间"
            :picker-options="{
              start: '00:00',
              end: '23:59',
              step: '00:30'
            }"
          />
        </el-form-item>

        <el-form-item label="闭店时间" prop="businessEndTime">
          <el-time-picker
            v-model="form.businessEndTime"
            format="HH:mm:ss"
            value-format="HH:mm:ss"
            placeholder="请选择营业结束时间"
            :picker-options="{
              start: '00:00',
              end: '23:59',
              step: '00:30'
            }"
          />
        </el-form-item>

        <el-form-item label="联系方式" prop="backgroundUrl">
          <el-input v-model="form.phone" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">开业中</el-radio>
            <el-radio :label="2">闭店中</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 地图选择对话框 -->
    <el-dialog title="选择地理位置" v-model="mapDialogVisible" width="800px" append-to-body>
      <div style="height: 500px">
        <!-- 搜索栏 -->
        <div style="margin-bottom: 10px; display: flex">
          <el-input v-model="searchKeyword" placeholder="请输入地址关键字搜索" style="flex: 1" @keyup.enter="searchAddress">
            <template #append>
              <el-button @click="searchAddress">搜索</el-button>
            </template>
          </el-input>
          <el-button @click="clearSearch" style="margin-left: 10px">清空</el-button>
        </div>

        <!-- 地图容器 -->
        <div id="amap-container" style="width: 100%; height: 400px"></div>

        <!-- 选中地址显示 -->
        <div style="margin-top: 10px">
          <el-input v-model="selectedAddress" placeholder="点击地图选择位置或搜索地址" readonly>
            <template #prepend>选中地址:</template>
          </el-input>
        </div>
      </div>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancelMapSelection">取 消</el-button>
          <el-button type="primary" @click="confirmMapSelection" :disabled="!selectedAddress">确 定</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 图片预览弹窗 -->
    <el-dialog v-model="dialogVisible" title="图片预览" width="50%">
      <img :src="previewSrc || iconPreviewUrl || form.iconUrl" alt="预览图片" style="max-width: 100%; max-height: 80vh" />
    </el-dialog>

    <el-dialog v-model="dialogVisible2" title="图片预览" width="50%">
      <img :src="previewSrc2 || iconPreviewUrl2 || form.backgroundUrl" alt="预览图片" style="max-width: 100%; max-height: 80vh" />
    </el-dialog>

    <!-- 添加标签弹窗 -->
    <el-dialog title="添加门店标签" v-model="addTagDialogVisible" width="400px">
      <el-input v-model="newTagName" placeholder="请输入标签名称" clearable @keyup.enter="confirmAddTag" style="width: 100%; margin-bottom: 16px" />
      <template #footer>
        <el-button @click="addTagDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmAddTag">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Store" lang="ts">
import { listStore, getStore, delStore, addStore, updateStore } from '@/api/system/physical/store';
import { StoreVO, StoreQuery, StoreForm } from '@/api/system/physical/store/types';
import { uploadTournament } from '@/api/system/business/tournaments';
import { selectAllPhysicalTagsSelList } from '@/api/system/physical/tag';

// 引入高德地图加载器
import AMapLoader from '@amap/amap-jsapi-loader';
import { TagVO } from '@/api/system/physical/tag/types';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const storeList = ref<StoreVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const storeFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: StoreForm = {
  id: undefined,
  storeCode: undefined,
  name: undefined,
  storeTypeId: undefined,
  zoneId: undefined,
  address: undefined,
  iconUrl: undefined,
  backgroundUrl: undefined,
  businessStartTime: undefined,
  businessEndTime: undefined,
  status: 1,
  phone: '',
  customTags: [], // 初始化为空数组
  createdAt: undefined,
  updatedAt: undefined,
  deletedAt: undefined
};
const data = reactive<PageData<StoreForm, StoreQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    storeCode: undefined,
    name: undefined,
    storeTypeId: undefined,
    zoneId: undefined,
    address: undefined,
    iconUrl: undefined,
    backgroundUrl: undefined,
    businessStartTime: undefined,
    businessEndTime: undefined,
    status: undefined,
    createdAt: undefined,
    updatedAt: undefined,
    deletedAt: undefined,
    params: {}
  },
  rules: {
    id: [{ required: true, message: '主键ID不能为空', trigger: 'blur' }],
    storeCode: [{ required: true, message: '门店编号，如M0001不能为空', trigger: 'blur' }],
    name: [{ required: true, message: '门店名称，最多20字不能为空', trigger: 'blur' }],
    storeTypeId: [{ required: true, message: '门店类型ID，关联service_type表不能为空', trigger: 'blur' }],
    zoneId: [{ required: true, message: '所属赛区分组ID，关联zone表不能为空', trigger: 'blur' }],
    address: [{ required: true, message: '详细地址不能为空', trigger: 'blur' }],
    businessStartTime: [{ required: true, message: '营业开始时间不能为空', trigger: 'blur' }],
    businessEndTime: [{ required: true, message: '营业结束时间不能为空', trigger: 'blur' }],
    status: [{ required: true, message: '状态：1=开业中，2=闭店中不能为空', trigger: 'change' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询门店信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await listStore(queryParams.value);
  storeList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  storeFormRef.value?.resetFields();

  // 显式重置 customTags
  customTags.value = [];

  // 清空上传相关状态
  fileList.value = [];
  fileList2.value = [];
  iconPreviewUrl.value = '';
  iconPreviewUrl2.value = '';
  form.value.iconUrl = ''; // 清除表单中的URL
  form.value.backgroundUrl = ''; // 清除表单中的URL
};
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: StoreVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加门店信息';
  // 清空上传相关状态
  fileList.value = [];
  fileList2.value = [];
  iconPreviewUrl.value = '';
  iconPreviewUrl2.value = '';
  form.value.iconUrl = ''; // 清除表单中的URL
  form.value.backgroundUrl = ''; // 清除表单中的URL
  // 确保 customTags 被清空
  customTags.value = [];
};

/** 修改按钮操作 */
const handleUpdate = async (row?: StoreVO) => {
  reset();
  const _id = row?.id || ids.value[0];
  const res = await getStore(_id);
  Object.assign(form.value, res.data);

  // 保留已有的图片URL，但清除上传组件的状态
  if (form.value.iconUrl) {
    iconPreviewUrl.value = form.value.iconUrl; // 设置预览图
  }
  if (form.value.backgroundUrl) {
    iconPreviewUrl2.value = form.value.backgroundUrl; // 设置预览图
  }
  // 从后端获取的标签数据
  if (res.data.customTags && Array.isArray(res.data.customTags)) {
    customTags.value = [...res.data.customTags];
  } else {
    customTags.value = [];
  }
  dialog.visible = true;
  dialog.title = '修改门店信息';
};
/** 提交按钮 */
const submitForm = () => {
  storeFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateStore(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addStore(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: StoreVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除门店信息编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
  await delStore(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'system/store/export',
    {
      ...queryParams.value
    },
    `store_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  getList();
  loadTagOptions(); // 加载门店类型选项
});
// 响应式变量
const iconPreviewUrl = ref('');
const iconPreviewUrl2 = ref('');
const fileList = ref([]);
const fileList2 = ref([]);
const dialogVisible = ref(false);
const dialogVisible2 = ref(false);
const previewSrc = ref('');
const previewSrc2 = ref('');

/** 处理图标上传 */
const handleIconChange = async (file) => {
  const index = fileList.value.findIndex((f) => f.uid === file.uid);
  fileList.value = [];

  if (file.raw) {
    iconPreviewUrl.value = URL.createObjectURL(file.raw);
  }

  if (index === -1) {
    // 如果文件不在列表中，则添加进去
    fileList.value.push(file);
  }

  try {
    const rawFile = file.raw;
    const res = await uploadTournament(rawFile);
    if (res.code === 200) {
      // 更新文件状态为成功
      fileList.value[index] = {
        ...file,
        status: 'success',
        response: res.data.url
      };
      form.value.iconUrl = fileList.value[index].response;
      iconPreviewUrl.value = fileList.value[index].response;
      ElMessage.success('上传成功');
    } else {
      throw new Error(res.msg);
    }
  } catch (error) {
    // 更新文件状态为失败
    fileList.value[index] = {
      ...file,
      status: 'fail',
      error: '上传失败'
    };
    ElMessage.error('上传失败，请重试');
  }
};

/** 处理图标删除 */
const handleIconRemove = (file, updatedFileList) => {
  fileList.value = updatedFileList;
  // 清除预览图和临时链接
  iconPreviewUrl.value = '';
  form.value.iconUrl = ''; // 清除表单中的URL
};

/** 点击预览图触发放大 */
const handlePreviewClick = () => {
  const currentSrc = iconPreviewUrl.value || form.value.iconUrl;
  if (currentSrc) {
    dialogVisible.value = true;
  }
};

/** 处理背景图上传 */
const handleIconChange2 = async (file) => {
  const index = fileList2.value.findIndex((f) => f.uid === file.uid);
  fileList2.value = [];

  if (file.raw) {
    iconPreviewUrl2.value = URL.createObjectURL(file.raw);
  }

  if (index === -1) {
    // 如果文件不在列表中，则添加进去
    fileList2.value.push(file);
  }

  try {
    const rawFile = file.raw;
    const res = await uploadTournament(rawFile);
    if (res.code === 200) {
      // 更新文件状态为成功
      fileList2.value[index] = {
        ...file,
        status: 'success',
        response: res.data.url
      };
      form.value.backgroundUrl = fileList2.value[index].response;
      iconPreviewUrl2.value = fileList2.value[index].response;
      ElMessage.success('上传成功');
    } else {
      throw new Error(res.msg);
    }
  } catch (error) {
    // 更新文件状态为失败
    fileList2.value[index] = {
      ...file,
      status: 'fail',
      error: '上传失败'
    };
    ElMessage.error('上传失败，请重试');
  }
};

/** 处理背景图删除 */
const handleIconRemove2 = (file, updatedFileList) => {
  fileList2.value = updatedFileList;
  // 清除预览图和临时链接
  iconPreviewUrl2.value = '';
  form.value.backgroundUrl = ''; // 清除表单中的URL
};

/** 点击预览图触发放大 */
const handlePreviewClick2 = () => {
  const currentSrc = iconPreviewUrl2.value || form.value.backgroundUrl;
  if (currentSrc) {
    dialogVisible2.value = true;
  }
};
// 响应式变量
const tagOptions = ref<TagVO[]>([]);
// 加载门店类型选项
const loadTagOptions = async () => {
  try {
    const res = await selectAllPhysicalTagsSelList();
    if (res.code === 200) {
      tagOptions.value = res.data;
    } else {
      ElMessage.error('加载门店类型失败：' + res.msg);
    }
  } catch (error) {
    console.error('请求出错:', error);
    ElMessage.error('请求失败，请检查网络');
  }
};
// 自定义标签相关状态
const customTags = ref<string[]>([]);
const addTagDialogVisible = ref(false);
const newTagName = ref('');
// 添加标签
const confirmAddTag = () => {
  if (!newTagName.value.trim()) {
    ElMessage.warning('标签名称不能为空');
    return;
  }
  // 检查是否已达到最大数量限制
  if (customTags.value.length >= 3) {
    ElMessage.warning('最多只能添加3个标签');
    return;
  }
  // 检查是否已存在
  if (customTags.value.includes(newTagName.value)) {
    ElMessage.warning('该标签已存在');
    return;
  }

  customTags.value.push(newTagName.value);
  newTagName.value = '';
  addTagDialogVisible.value = false;
};

// 删除标签
const handleTagClose = (index: number) => {
  customTags.value.splice(index, 1);
};

// 表单提交时，将标签数组赋值给 form.customTags
watch(
  customTags,
  () => {
    // 确保赋值的是一个数组
    if (Array.isArray(customTags.value)) {
      form.value.customTags = customTags.value;
    }
  },
  { deep: true }
);
const openAddTagDialog = () => {
  newTagName.value = '';
  addTagDialogVisible.value = true;
};
// 添加响应式变量
const mapDialogVisible = ref(false);
const selectedAddress = ref('');
const selectedLocation = ref({ lng: 0, lat: 0 });
const searchKeyword = ref('');
let amapInstance: any = null;
let marker: any = null;

/** 打开地图选择器 */
const openMapSelector = () => {
  mapDialogVisible.value = true;
  nextTick(() => {
    initAMap(); // 等待 el-dialog 渲染完成再初始化
  });
};

/** 初始化高德地图 */
const initAMap = () => {
  AMapLoader.load({
    key: 'ed2e29e4cbcc0f96905acb2106e82b42', // 请替换为您的高德地图key
    version: '2.0',
    plugins: ['AMap.Geocoder', 'AMap.PlaceSearch', 'AMap.AutoComplete', 'AMap.Marker', 'AMap.ToolBar', 'AMap.Scale', 'AMap.Geolocation']
  })
    .then((AMap) => {
      amapInstance = AMap;

      // 确保容器存在后再创建地图
      const container = document.getElementById('amap-container');
      if (!container) return;

      const map = new AMap.Map(container, {
        resizeEnable: true,
        zoom: 12,
        center: [116.397428, 39.90923] // 默认中心点
      });

      // 添加定位功能
      map.addControl(
        new AMap.Geolocation({
          zoomToCurrent: true,
          position: 'rtb'
        })
      );

      // 添加点击事件
      map.on('click', function (e) {
        const lnglat = e.lnglat;
        setSelectedLocation(lnglat, AMap);
      });

      // 添加地图控件
      map.addControl(new AMap.ToolBar());
      map.addControl(new AMap.Scale());

      // 初始化标记
      marker = new AMap.Marker({
        map: map,
        draggable: true,
        cursor: 'move'
      });

      // 标记拖拽结束事件
      marker.on('dragend', function (e) {
        setSelectedLocation(e.lnglat, AMap);
      });

      // 将map实例保存到DOM元素上
      (container as any).__map__ = map;
    })
    .catch((e) => {
      console.error(e);
      proxy?.$modal.msgError('地图加载失败: ' + e.message);
    });
};

/** 设置选中位置 */
const setSelectedLocation = (lnglat: any, AMap: any) => {
  selectedLocation.value = {
    lng: lnglat.getLng(),
    lat: lnglat.getLat()
  };

  marker.setPosition(lnglat);

  // 逆地理编码获取地址
  const geocoder = new AMap.Geocoder();
  geocoder.getAddress(lnglat, function (status: string, result: any) {
    debugger;
    if (status === 'complete' && result.regeocode) {
      selectedAddress.value = result.regeocode.formattedAddress;
    }
  });
};

/** 搜索地址 */
const searchAddress = () => {
  if (!searchKeyword.value || !amapInstance) return;

  amapInstance.plugin('AMap.PlaceSearch', () => {
    const placeSearch = new amapInstance.PlaceSearch({
      pageSize: 5,
      pageIndex: 1,
      city: '全国', // 搜索范围
      map: document.getElementById('amap-container')?.__map__ // 使用正确的map引用
    });

    placeSearch.search(searchKeyword.value, (status: string, result: any) => {
      if (status === 'complete' && result.info === 'OK') {
        if (result.poiList && result.poiList.pois.length > 0) {
          const firstPoi = result.poiList.pois[0];
          const lnglat = [firstPoi.location.lng, firstPoi.location.lat];

          // 更新地图中心点
          const mapElement = document.getElementById('amap-container');
          if (mapElement) {
            const map = mapElement.__map__;
            if (map) {
              map.setCenter(lnglat);
              setSelectedLocation(new amapInstance.LngLat(firstPoi.location.lng, firstPoi.location.lat), amapInstance);
            }
          }
        }
      } else {
        proxy?.$modal.msgWarning('未找到相关地址');
      }
    });
  });
};

/** 确认选择地图位置 */
const confirmMapSelection = () => {
  if (selectedAddress.value) {
    form.value.address = selectedAddress.value;
    // 如果需要保存经纬度，可以在form中添加相应字段
    // form.value.longitude = selectedLocation.value.lng;
    // form.value.latitude = selectedLocation.value.lat;
  }
  mapDialogVisible.value = false;
};

/** 取消地图选择 */
const cancelMapSelection = () => {
  mapDialogVisible.value = false;
};

/** 清除搜索关键词 */
const clearSearch = () => {
  searchKeyword.value = '';
};
</script>
