// packageA/mycomponent/goodsComponent/plugin/appointment/appointment.js
var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    goodsId: {
      type: null
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    projects: [],
    appointmentLang: ''
  },
  lifetimes: {
    attached() {
      this.getProject();
      try {
        let info = wx.getStorageSync("yz_basic_info");
        if (info) {
          this.setData({
            appointmentLang: info.lang.appointment,
          });
        }
      } catch (err) {
        console.log(err);
      }
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 预约项目请求
    getProject() {
      var that = this;
      let urlStr = app.getNetAddresss("plugin.appointment.frontend.project.get-list-by-goods-id");
      app._postNetWork({
        url: urlStr,
        data: {
          goods_id: that.data.goodsId
        },
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              projects: res.data,
            });
          } else {
            wx.showToast({
              title: res.msg,
              icon: "none",
            });
            console.log(res.msg);
          }
        },
      });
    },
    // 预约项目
    goProjectDetails(e) {
      let that = this;
      let id = e.currentTarget.dataset.key;
      let point = wx.getStorageSync('address-point');
      if (!point) {
        wx.getLocation({
          type: "gcj02",
          success(res) {
            const latitude = res.latitude;
            const longitude = res.longitude;
            point = that._setaddressData({
              lat: latitude,
              lng: longitude,
            }, id);
          },
        });
      } else {
        wx.navigateTo({
          url: "/packageE/appointment/ProjectDetails/ProjectDetails?project_id=" + id + '&point=' + JSON.stringify(point),
        });
      }

    },
    // 预约项目   获取经纬度
    _setaddressData(e, id) {
      let that = this;
      app.getReverseGeocoder(e.lat, e.lng).then(function (res) {
        that.setData({
          'point.lat': e.lat,
          'point.lng': e.lng
        });
        let mapdata = res;
        let recommend = mapdata.formatted_addresses.recommend;
        that.setData({
          address: mapdata.address,
          title: recommend,
          city: !app._isTextEmpty(mapdata.address_component.city) ? mapdata.address_component.city : mapdata.address_component
            .province
        });
        wx.navigateTo({
          url: "/packageE/appointment/ProjectDetails/ProjectDetails?project_id=" + id + '&point=' + JSON.stringify(that.data.point),
        });
      }).catch(function (err) {
        console.log(err);
      });
    },
    // 预约项目查看更多
    gotoProjects() {
      this.setData({
        showProjects: true,
      });
    },
    // 预约项目弹窗关闭
    ProjectsClose(e) {
      this.setData({
        showProjects: false,
      });
    },
  }
});