import {  myFetch } from "../utils/fetch";

const amapFile = require("@/libs/amap-wx.130.js");
// 高德地图微信组件
const myAmapFun = new amapFile.AMapWX({
//  key: "5d8515c6bf9f852b092fb8c297036a9c",
  key: "6c10de64f2a22f25eebc3b03b7e2fc69",
});
const mapKey = "8ad6b1c0f18f26a721871ca50e751ef9"; // 高德地图web api
const API = {
  geo: "https://restapi.amap.com/v3/geocode/geo", //地理编码 API 服务
};

/*高德地图 */

export const geocoder = (data) => {
  data = { ...data, key: mapKey };
  return myFetch(API.geo, "get", data, "form");
};

export const apiSearch = (data = { keyword: "", location: "" }) => {
  return new Promise((resolve, reject) => {
    myAmapFun.getPoiAround({
      location: data.location,
      querykeywords: data.keyword,
      querytypes:data.querytypes,
      success: (res) => {
        console.log("阿里地图", res);
        const poisData = res.poisData;
        const rawMarkers=res.markers;
        let addressList = [];
        let markers = [];
        for (var i = 0; i < poisData.length; i++) {
          addressList.push({
            title: poisData[i].name,
            address: `${poisData[i].cityname}${poisData[i].adname}${poisData[i].address}`,
            distance: (poisData[i].distance / 1000).toFixed(1),
            latitude: Number(poisData[i].location.split(",")[1]),
            longitude: Number(poisData[i].location.split(",")[0]),
            adname: poisData[i].adname,
            tel: poisData[i].tel,
          });
          rawMarkers[i].title=rawMarkers[i].name;
          rawMarkers[i].name= "";
           rawMarkers[i].iconPath= "/static/images/common/point.png";
        }
        addressList.forEach(
          (item) => (item.title = item.title.replace(/奉化市/g, `奉化区`))
        );
        /*  if (data.keyword === "核酸") {
          addressList = addressList.filter(
            (item) => item.adname.district === "奉化区"
          );
        } */
        
          markers =
          rawMarkers.length > data.numberLimit
              ? rawMarkers.slice(0, data.numberLimit)
              : rawMarkers;
          addressList =
            addressList.length > data.numberLimit
              ? addressList.slice(0, data.numberLimit)
              : addressList;
       
        resolve({ addressList ,markers});
      },
      fail: (error) => {
        console.log(error);
        reject(error);
      },
    });
  });
};
