// packageE/appointment/client/appointment/appointment.js

var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    active: 0,

    page: 1, //分页数，当前页数
    isLoadMore: true, //判断是否要加载更多的标志
    total_page: 0, //总页数

    point: {
      lat: "",
      lng: "",
    },

    projectList: [],
    qrCodeFlag: false,
    posterImageUrl: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    //scope.userLocation
    let userLocationFlag = await this.checkAlbum("scope.userLocation");
    console.log(userLocationFlag);
    wx.getLocation({
      type: "wgs84",
      success: (res) => {
        this.setData({
          "point.lat": res.latitude,
          "point.lng": res.longitude,
        });
        this.getListData();
      },
    });
  },

  changeTags(e) {
    console.log(e.detail.index, this.data.active);
    this.data.active = e.detail.index !=2 ? e.detail.index : 9;
    this.getListData();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  toAppointment(evt) {
    let has_one_order_project =
      evt.currentTarget.dataset.item.has_one_order_project;
    let has_one_store = evt.currentTarget.dataset.item.has_one_store;
    let store_id = has_one_store.id;
    let has_one_worker = evt.currentTarget.dataset.item.has_one_worker;
    let url_str =
      "?id=" +
      has_one_order_project.id +
      "&project_id=" +
      has_one_order_project.project_id +
      "&has_one_store=" +
      JSON.stringify(has_one_store);
    url_str +=
      "&store_id=" +
      store_id +
      "&has_one_worker=" +
      JSON.stringify(has_one_worker);

    wx.navigateTo({
      url:
        "/packageE/appointment/client/make_appointment/make_appointment" +
        url_str,
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

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

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},
  cancelIt(e){
    let id = e.currentTarget.dataset.item.id;
    wx.showModal({
      title:"您确定要取消此预约吗?",
      success:(res)=>{
        if(res.confirm){
          this.cancelDate(id);
        }
      }
    });
  },
  cancelDate(kid){
    let urlStr = app.getNetAddresss("plugin.appointment.frontend.appointment-service.cancel-service");
    app._getNetWork({
      url: urlStr,
      data: {
        id: kid
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          app.tips(res.msg);
          this.getListData();
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
        }
      },
    });
  },
  createQrc(evt) {
    wx.showLoading({
      title: "生成中",
      mask: true,
    });
    let item = evt.currentTarget.dataset.item;
    let urlStr = app.getNetAddresss(
      "plugin.appointment.frontend.appointment-order.qr-code-url"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        id: item.id,
      },
      success: (resdata) => {
        wx.hideLoading();
        let res = resdata.data;
        if (res.result !== 1) return this.tips(res.msg);

        this.setData({
          qrCodeFlag: true,
          posterImageUrl: res.data.miQrCodeUrl,
        });
      },
      fial: (err) => {
        wx.hideLoading();
      },
    });
  },

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},

  setQRCodeFlag() {
    this.setData({
      qrCodeFlag: !this.data.qrCodeFlag,
    });
  },
  gotoEvaluate(evt) {
    let service_id = evt.currentTarget.dataset.serverid;
    wx.navigateTo({
      url:
        "/packageE/appointment/client/evaluate/evaluate?service_id=" +
        service_id,
    });
  },
  initData() {
    this.data.page = 1;
    this.data.isLoadMore = true;
    this.data.total_page = 0;
  },
  getListData() {
    this.initData();
    let point = app.qqMapTransBMap(
      parseFloat(this.data.point.lng),
      parseFloat(this.data.point.lat)
    );
    if (!point.lng) {
      (point.lng = ""), (point.lat = "");
    }
    let urlStr = app.getNetAddresss(
      "plugin.appointment.frontend.appointment-service.get-list"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        lng: point.lng.toFixed(4),
        lat: point.lat.toFixed(4),
        page: 1,
        status: this.data.active,
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result !== 1) return this.tips(res.msg);

        this.data.isLoadMore = true;
        this.data.total_page = res.data.last_page;
        if (!this.data.total_page) {
          this.data.total_page = 0;
        }
        this.setData({
          projectList: res.data.data,
        });
      },
    });
  },

  checkAlbum(scopeName) {
    return new Promise(async (resolve, reject) => { // eslint-disable-line
      let writePhotosAlbum = await this.getAuthSetting(scopeName);
      if (!writePhotosAlbum) {
        wx.showModal({
          title: "请开启授权",
          content: "是否跳转设置页开启授权",
          success(res) {
            if (res.confirm) {
              wx.openSetting({
                success(settingdata) {
                  if (settingdata.authSetting[scopeName]) {
                    resolve(true);
                  } else {
                    resolve(false);
                  }
                },
              });
            } else if (res.cancel) {
              resolve(false);
            }
          },
          fail: () => {
            resolve(false);
          },
        });
      } else resolve(true);
    });
  },
  getAuthSetting(authName) {
    return new Promise((resolve, reject) => {
      wx.getSetting({
        success(res) {
          if (!res.authSetting[authName]) {
            wx.authorize({
              scope: authName,
              success() {
                resolve(true);
              },
              fail() {
                resolve(false);
              },
            });
          } else resolve(true);
        },
        fail() {
          resolve(false);
        },
      });
    });
  },

  tips(msg) {
    wx.showToast({
      title: msg,
      icon: "none",
      duration: 2000,
    });
    return false;
  },
});
