// pages/addressPage/index.js
// const { variableDeclaration } = require("jscodeshift");
const app = getApp();
var QQMapWX = require("../../utils/qqmap-wx-jssdk.min.js");
var qqmapsdk;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    onSiteRemark: "",
    jobDetail: {},
    jobClassify: 1,
    addressList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    var that = this;
    qqmapsdk = new QQMapWX({
      key: "5PTBZ-YI7C6-MZGS3-ES7QN-4T5O2-EJFVR",
    });
    console.log(wx.getStorageSync("addressList"));
    console.log('options', options);

    wx.getStorage({
      key: "addressList",
      success (res) {
        that.setData({
          jobDetail: res.data,
        });
        console.log(res.data);
        var data = res.data;
        new Promise(function (resolve, reject) {
          console.log(data);
          let jobClassify = ''
          if (options.jobClassify) {
            jobClassify = options.jobClassify
            that.setData({
              jobClassify
            })
          }
          if (jobClassify == 2) {
            var d1 = "";
            var d2 = "";
            var d3 = "";
            if (data.record.storeLat != "") {
              // 出现ip未授权问题, 不再使用经纬度反查地址信息
              // qqmapsdk.reverseGeocoder({
              //   location: `${data.record.storeLat},${data.record.storeLng}`,
              //   success: function (res) {
              // console.log(res);
              // d1 = res.result.formatted_addresses.recommend;
              that.data.addressList.push({
                color: "#027AFF",
                icon: "icon-qiye",
                name: "企业地址",
                addresss: data.record.storeAddr,
                lat: data.record.storeLat,
                lng: data.record.storeLng,
                addressName: data.record.storeAddr,
              })
              that.setData({
                addressList: that.data.addressList,
              });
              //   },
              // });
            }
            console.log('data.record.interviewLat', data.record.interviewLat);
            if (data.record.interviewLat != "") {
              // qqmapsdk.reverseGeocoder({
              //   location: `${data.record.interviewLat},${data.record.interviewLng}`,
              //   success: function (res) {
              //     console.log(res);
              //     d2 = res.result.formatted_addresses.recommend;
              that.data.addressList.push({
                color: "#1890ff",
                icon: "icon-mianshi",
                name: "面试地址",
                addresss: data.record.interviewAddr,
                lat: data.record.interviewLat,
                lng: data.record.interviewLng,
                addressName: data.record.interviewAddr,
              })
              that.setData({
                addressList: that.data.addressList,
              });
              //   },
              //   fail: function (err) {
              //     console.log(err);

              //   }
              // });
            }
            console.log('that.data.addressList', that.data.addressList);
          } else {
            // var d1 = "";
            // var d2 = "";
            // var d3 = "";
            // if (data.store.lat != "") {
            //   qqmapsdk.reverseGeocoder({
            //     location: `${data.store.lat},${data.store.lng}`,
            //     success: function (res) {
            //       console.log(res);
            //       d1 = res.result.formatted_addresses.recommend;
            //       that.data.addressList["storeAddress"] = {
            //         color: "#027AFF",
            //         icon: "icon-qiye",
            //         name: "企业地址",
            //         addresss: data.store.detailPosition,
            //         lat: data.store.lat,
            //         lng: data.store.lng,
            //         addressName: d1,
            //       };
            //       that.setData({
            //         addressList: that.data.addressList,
            //       });
            //     },
            //   });
            // }
            // if (data.storeJob.meettingAddr != "") {
            //   qqmapsdk.reverseGeocoder({
            //     location: `${data.storeJob.meettingLat},${data.storeJob.meettingLng}`,
            //     success: function (res) {
            //       console.log(res);
            //       d2 = res.result.formatted_addresses.recommend;
            //       that.data.addressList["meetingAddress"] = {
            //         color: "#4DB54B",
            //         icon: "icon-jieren",
            //         name: "接人地址",
            //         addresss: data.storeJob.meettingAddr,
            //         lat: data.storeJob.meettingLat,
            //         lng: data.storeJob.meettingLng,
            //         addressName: d2,
            //       };
            //       that.setData({
            //         addressList: that.data.addressList,
            //       });
            //     },
            //   });
            // }
            // if (data.storeJob.interviewAddress != "") {
            //   qqmapsdk.reverseGeocoder({
            //     location: `${data.storeJob.interviewLat},${data.storeJob.interviewLng}`,
            //     success: function (res) {
            //       console.log(res);
            //       d3 = res.result.formatted_addresses.recommend;
            //       that.data.addressList["interviewAddress"] = {
            //         color: "#1890ff",
            //         icon: "icon-mianshi",
            //         name: "面试地址",
            //         addresss: data.storeJob.interviewAddr,
            //         lat: data.storeJob.interviewLat,
            //         lng: data.storeJob.interviewLng,
            //         addressName: d3,
            //       };
            //       that.setData({
            //         addressList: that.data.addressList,
            //       });
            //     },
            //   });

            //   console.log(that.data.addressList);
            // }
          }

          resolve();
        })
          .then(() => {
            that.setData({
              addressList: that.data.addressList,
            });
            console.log(that.data.addressList);
          })
          .catch((err) => {
            console.log(err);
          });
      },
    });

    // resolve();
    // }).then(() => {
    // setTimeout(function () {
    //   that.setData({
    //     addressList: that.data.addressList,
    //   });
    //   console.log(that.data.addressList);
    // }, 800)

    // });
  },
  toMap (e) {
    var that = this;
    wx.setClipboardData({
      data: that.data.onSiteRemark,
      success (res) {
        wx.getClipboardData({
          success (res) {
            wx.showToast({
              title: "内容已复制",
              icon: "none",
              duration: 1500,
            });
            console.log(res.data); // data
            // wx.hideToast()
            // wx.showLoading({
            //   title: "加载中",
            // });
          },
          // ,
          // complete:()=>{
          //   wx.hideToast()
          // }
        });
      },
    });
    console.log(e.currentTarget.dataset.info.lat);
    if (app.isEmptyCheck(e.currentTarget.dataset.info.lat)) {
      return;
    }

    console.log(e);
    var address = e.currentTarget.dataset.info.addresss;
    let lat = e.currentTarget.dataset.info.lat;
    var formatted_addresses = "";
    //  wx.getStorageSync("addressList").store.aliasName;
    console.log(address);
    qqmapsdk.reverseGeocoder({
      // location: `${e.currentTarget.dataset.info.lat ? e.currentTarget.dataset.info.lat : "39.909115"},${e.currentTarget.dataset.info.lng ? e.currentTarget.dataset.info.lng : "116.397407"}`,
      location: `${e.currentTarget.dataset.info.lat},${e.currentTarget.dataset.info.lng}`,
      success: function (res) {
        console.log(res);
        formatted_addresses = res.result.formatted_addresses.recommend;
        wx.getLocation({
          type: "gcj02", //返回可以用于wx.openLocation的经纬度
          success (res) {
            console.log(formatted_addresses);
            console.log(lat);
            console.log(address);
            wx.openLocation({
              latitude: e.currentTarget.dataset.info.lat ? e.currentTarget.dataset.info.lat - 0 : 39.909115,
              longitude: e.currentTarget.dataset.info.lng ? e.currentTarget.dataset.info.lng - 0 : 116.397407,
              name: formatted_addresses,
              address: address ? address : "",
              scale: 15,
            });
          },
          complete (res) {
            wx.hideLoading();
            console.log(1123213);
          },
        });
      },
      fail (res) {
        console.log(res);
      },
    });
  },
  setCopy () {
    var that = this;
    wx.setClipboardData({
      data: that.data.onSiteRemark,
      success (res) {
        wx.getClipboardData({
          success (res) {
            console.log(res.data); // data
            wx.showToast({
              title: "内容已复制",
              icon: "none",
              duration: 1500,
            });
          },
        });
      },
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () { },

  chooseMap2 (e) {
    var that = this;
    setTimeout(() => {
      console.log(e.currentTarget.dataset.lat);
      if (e.currentTarget.dataset.lat == "") {
        return;
      }
      let name = e.currentTarget.dataset.postitle;
      let content = e.currentTarget.dataset.poscontent;
      let lat = e.currentTarget.dataset.lat - 0;
      let lng = e.currentTarget.dataset.lng - 0;

      wx.openLocation({
        latitude: lat,
        longitude: lng,
        name: name,
        address: content,
        scale: 15,
      });
    }, 200);
    // wx.setClipboardData({
    //   data: that.data.onSiteRemark,
    //   success (res) {
    //     wx.getClipboardData({
    //       success (res) {
    //         wx.showToast({
    //           title: '内容已复制',
    //           icon: 'none',
    //           duration: 1500
    //         })

    //         console.log(res.data) // data
    //         // wx.hideToast()
    //         // wx.showLoading({
    //         //   title: "加载中",
    //         // });

    //       }
    //       // ,
    //       // complete:()=>{
    //       //   wx.hideToast()
    //       // }
    //     })
    //   }
    // })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {
    console.log(wx.getStorageSync("onSiteRemark"));

    var that = this;
    that.setData({
      onSiteRemark: wx.getStorageSync("onSiteRemark"),
    });

    // new Promise((resolve, reject) => {
    //   let data = wx.getStorageSync("addressList");
    //   console.log("start Promise");
    //   if (data.store.lat != "") {
    //     qqmapsdk.reverseGeocoder({
    //       location: `${data.store.lat},${data.store.lng}`,
    //       success: function (res) {
    //         console.log(res);
    //         that.data.addressList.storeAddress["addressName"] = res.result.formatted_addresses.recommend;
    //       }
    //     })
    //   }

    //   if (data.storeJob.meettingLat != "") {
    //     qqmapsdk.reverseGeocoder({
    //       location: `${data.storeJob.meettingLat},${data.storeJob.meettingLng}`,
    //       success: function (res) {
    //         console.log(res);
    //         that.data.addressList.meetingAddress["addressName"] = res.result.formatted_addresses.recommend;
    //       }
    //     })
    //   }
    //   if (data.storeJob.interviewLat != "") {
    //     qqmapsdk.reverseGeocoder({
    //       location: `${data.storeJob.interviewLat},${data.storeJob.interviewLng}`,
    //       success: function (res) {
    //         console.log(res);
    //         that.data.addressList.interviewAddress["addressName"] = res.result.formatted_addresses.recommend;
    //       }
    //     })
    //   }
    //   resolve("resolved");
    // }).then(() => {
    //   that.setData({
    //     addressList: that.data.addressList,
    //   });
    //   console.log(that.data.addressList)
    // })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () { },

  /**
   * 用户点击右上角分享
   */
onShareAppMessage() {
  return app.sharePageImage()
},
});
