<template>
  <div class="edit-popup">
    <popup
      ref="popupRef"
      :title="title"
      :async="true"
      width="550px"
      @confirm="handleSubmit"
      @close="handleClose"
    >
      <el-form>
        <el-form-item label="">
          <el-input
            style="width: 300px; margin-right: 16px"
            placeholder="请输入要搜索的地址"
            v-model="searchText"
            clearable
          />
          <el-button type="primary" @click="serach()">搜索</el-button>
          <el-button @click="resetMap()">重置</el-button>
        </el-form-item>
        <el-form-item label-width="80px" label="地图">
          <MapContainer ref="mapRef" @changeMaps="changeMaps"></MapContainer>
        </el-form-item>
        <el-form-item label-width="80px" label="已选地点">
          <el-input
            style="width: 350px; margin-right: 16px"
            placeholder="请点击上方地图选择地点"
            readonly
            :value="address[0]?.address"
          />
        </el-form-item>
        <!-- <el-form-item label-width="80px" label="起点">
          <el-input
            style="width: 350px; margin-right: 16px"
            placeholder="请点击上方地图选择地点"
            readonly
            :value="address[0]?.address"
          />
        </el-form-item>
        <el-form-item label-width="80px" label="中转点">
          <el-input
            style="width: 350px; margin-right: 16px"
            placeholder="请点击上方地图选择地点"
            readonly
            :value="address[1]?.address"
          />
        </el-form-item>
        <el-form-item label-width="80px" label="终点">
          <el-input
            style="width: 350px; margin-right: 16px"
            placeholder="请点击上方地图选择地点"
            readonly
            :value="address[2]?.address"
          />
        </el-form-item> -->
      </el-form>
    </popup>
  </div>
</template>
  
  <script lang="ts" setup name="taskTemplateEdit">
import type { FormInstance } from "element-plus";
import Popup from "@/components/popup/index.vue";
import {
  apiTaskTemplateAdd,
  apiTaskTemplateEdit,
  apiTaskTemplateDetail,
} from "@/api/task_template";
import { timeFormat } from "@/utils/util";
import type { PropType } from "vue";
import { dictDataLists } from "@/api/setting/dict";
import MapContainer from "@/components/map/MapContainer.vue";
defineProps({
  dictData: {
    type: Object as PropType<Record<string, any[]>>,
    default: () => ({}),
  },
});
const emit = defineEmits(["success", "close"]);
const formRef = shallowRef<FormInstance>();
const popupRef = shallowRef<InstanceType<typeof Popup>>();
const mode = ref("add");
const datalist = ref([]);
const title = ref("");

// 获取详情
const setFormData = async (data: Record<any, any>) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key];
    }
  }
};

const getDetail = async (row: Record<string, any>) => {
  const data = await apiTaskTemplateDetail({
    id: row.id,
  });
  setFormData(data);
};

// 地图控件
const mapRef = ref(null);

// 重置
const resetMap = () => {
  mapRef.value.resetMap();
};

// 搜索
const searchText = ref("");
const serach = () => {
  mapRef.value.searchMap(searchText.value);
};

const address = ref([]);
const changeMaps = (e: any) => {
  address.value = JSON.parse(JSON.stringify(e));
  //   console.log("当前选择", e);
};

// 提交按钮
const handleSubmit = async () => {
  // if (address.value.length < 3) return ElMessage.error("请先选择三个地点");
  if (address.value.length < 1) return ElMessage.error("请先选择地点");
  popupRef.value?.close();
  emit("success", address.value);
};

//打开弹窗
const open = async (type = "地点") => {
  title.value = "选择" + type;
  popupRef.value?.open();
  await nextTick();
  resetMap();
};

// 关闭回调
const handleClose = () => {
  emit("close");
};

defineExpose({
  open,
  setFormData,
  getDetail,
});
</script>
  