var e = getApp(),
  a = e.globalData.setting,
  os = a,
  t = e.request,
  d = e.globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    qr_code_object: {
      val: "12121",
      content: "请将二维码展示给核销员，服务更快捷！"
    },
    iurl: a.imghost,
    defimgurl: "/miniapp/images/yyservice/yyxmdefault.jpg",
    service_List: [], //服务项目列表
    is_service_read: 0,
    curpage: 1, //当前分页数
    pageSize: 10, //页大小
    total: 0,
    ismore: 0, //是否加载完毕
    userinfo: null,
    is_sub: 0, //是否重复提交预约
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
		console.log('options==>', options);
    var userinfo = getApp().globalData.userInfo;
    if (userinfo == null) {
      //界面必须使用warn 做ID,并调用
      // return false;
    };
    this.setData({
      userinfo: userinfo,
			options,
    });
    var th = this;
		// if(options.UserId) {
			th.query_service();
		// };
    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var th = this;
    var my_confirm = th.selectComponent("#my_confirm"); //组件的id
    my_confirm.open_cancel(0);
  },
	
  onReachBottom: function() {
    var th = this;
    if (this.data.total <= th.data.pageSize) return;
    if (this.data.ismore) return;

    th.query_service();
  },
  //查询服务项目列表
  query_service: function() {
    wx.showLoading({
      title: '加载中',
    })
    var th = this;
    var url = "/api/weshop/marketing/reservation/sm/page?State=1";
    getApp().request.promiseGet(url, {
      data: {
        storeId: a.stoid,
        userId: th.data.options.UserId,
        page: th.data.curpage,
        pageSize: th.data.pageSize
      }
    }).then(res => {
      wx.hideLoading();
      if (res.data.code == 0) {
        th.data.curpage++;
        var arr1 = th.data.service_List;
        var arr2 = res.data.data.pageData;
        var arr3 = [...arr1, ...arr2];
        var ismore = 0;
        if (arr3.length == res.data.data.total) ismore = 1
        th.setData({
            service_List: arr3,
            total: res.data.data.total,
            ismore: ismore,
            is_service_read: 1,
          }),
          wx.stopPullDownRefresh(); //停止下拉刷新
      } else {
        th.setData({
          is_service_read: 1
        })
      }
    })
  },
  /*-- 打开服务项目 --*/
  open_fw: function(e) {
    wx.showLoading({
      title: '加载中',
    })
    var th = this;
    var url = e.currentTarget.dataset.url;
    var service_id = e.currentTarget.dataset.service_id; //服务id
    var project_id = e.currentTarget.dataset.project_id;
    var ind = e.currentTarget.dataset.ind;
    var item = this.data.service_List[ind];
    var ewm = "^" + th.data.userinfo.erpvipid + "|" + item.ProjectID + "|" + item.BuyType + "|TY01|";
    var tt = th.format_time(1);
    ewm = ewm + tt;
    th.data.qr_code_object.val = ewm;
    th.data.qr_code_object.is_fw = 1;
    var qc_com = th.selectComponent("#qc_com"); //组件的id
    getApp().request.promiseGet("/api/weshop/marketing/reservation/reservation/check", {
      data: {
        serviceId: service_id,
        storeId: a.stoid,
        userId: th.data.options.UserId,
      }
    }).then(res => {
      wx.hideLoading();
      th.setData({
        is_sub: 0
      })
      if (res.data.data != null) {
        var my_confirm = th.selectComponent("#my_confirm"); //组件的id
        my_confirm.open_cancel(1);
        my_confirm.open(
          "已有预约记录!",
          "预约详情",
          "继续使用",
          function() {
            my_confirm.open_cancel(0);
            var deta_url = "/packageA/pages/my_service/tment_details?userid="+ th.data.options.UserId + "&number=" + res.data.data.Number;
            wx.navigateTo({
              url: deta_url
            })
          },
          function() {
            my_confirm.open_cancel(0);
            qc_com.open(th.data.qr_code_object)
          }

        )

      } else {
        qc_com.open(th.data.qr_code_object)
      }
    })

  },
  //图片失败，默认图片
  bind_bnerr1: function(e) {
    var _errImg = e.target.dataset.errorimg;
    var _Img = e.target.dataset.img;
    if (_Img != undefined) {
      var _errObj = {};
      _errObj[_errImg] = "/miniapp/images/no_cate_def.png";
      this.setData(_errObj) //注意这里的赋值方式,只是将数据列表中的此项图片路径值替换掉 ;
    }
  },
  format_time: function(isFull) {
    var d = new Date();
    var m = d.getMonth() + 1;
    if (m < 10) m = "0" + m;
    var dd = d.getDate();
    if (dd < 10) dd = "0" + dd;
    var fm = [d.getFullYear(), m, dd].join('-');
    if (isFull == 1)
      fm = fm + ' ' + [d.getHours(), d.getMinutes(), d.getSeconds()].join(':')
    return fm;
  },
  //跳转到首页
  goto: function(e) {
    var url = e.currentTarget.dataset.url;
    getApp().goto(url);
  },
  //校正是否可预约
  correcting: function(e) {
    var th = this;
    wx.showLoading({
      title: '加载中',
    })
    if (th.data.is_sub == 0) {
      th.setData({
        is_sub: 1
      })
      var url = e.currentTarget.dataset.url;
      var service_id = e.currentTarget.dataset.service_id; //服务id
      var project_id = e.currentTarget.dataset.project_id;
      getApp().request.promiseGet("/api/weshop/marketing/reservation/reservation/check", {
        data: {
          serviceId: service_id,
          storeId: a.stoid,
          userId: th.data.options.UserId,
        }
      }).then(res => {
        wx.hideLoading();
        th.setData({
          is_sub: 0
        })
        if (res.data.code == 0) {
          if (res.data.data != null) {

            var number = res.data.data.Number;
            var my_confirm = th.selectComponent("#my_confirm"); //组件的id
            my_confirm.open_cancel(1);
            my_confirm.open(
              "已有预约记录!",
              "预约详情",
               "继续预约",
              function() {
                my_confirm.open_cancel(0);
                var deta_url = "/packageA/pages/my_service/tment_details?number=" + number + "&userid="+ th.data.options.UserId;
                wx.navigateTo({
                  url: deta_url
                })
              },
               function() {
                my_confirm.open_cancel(0);
                getApp().request.promiseGet("/api/weshop/marketing/reservation/reservation/sm/check", {
                  data: {
                    projectId: project_id,
                    serviceId: service_id,
                    storeId: a.stoid,
                    userId:  th.data.options.UserId,
                  }
                }).then(res => {
                  if (res.data.code == 0) {
                    wx.navigateTo({
                      url: url
                    })
                  } else {
                    getApp().my_warnning(res.data.msg, 0, th);
                  }
                })
              }
            )
          } else {
            wx.navigateTo({
              url: url,
            })
          }

        } else {
          getApp().my_warnning(res.data.msg, 0, th);
        }
      })
    }
  },
  //关闭导航
  close: function () {
    var th = this;
    var nav_b = th.selectComponent("#nav_b"); //组件的id
    nav_b.close_box();
  }

})