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

  /**
   * 页面的初始数据
   */
  data: {
    url: a.url, //接口网址
    iurl: a.imghost, //图片网址
    activeCategoryId: 0, //类别选中下标
    defimgurl: "/miniapp/images/yyservice/yyxmdefault.jpg",
    categories: [{
      name: "全部",
      id: 0
    }, {
      name: "未服务",
      id: 1
    }, {
      name: "已服务",
      id: 2
    }, {
      name: "已过期",
      id: 3
    }],
    tm_order_list: [], //我的预约订单列表
    curpage: 1, //当前分页数
    pageSize: 10, //页大小
    total: 0,
    ismore: 0, //是否加载完毕
    is_read: 0, //是否加载接口
    states: 4, //4为取消预约
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var state=options.state;
    if (state != null && state != undefined && state!=""){
      this.setData({ activeCategoryId: state });
    }
    
  },
  changeTab: function(e) {
    var th = this;
    var id = e.currentTarget.dataset.id;
    var activeCategoryId = th.data.activeCategoryId;
    if (id != activeCategoryId) {
      th.setData({
        activeCategoryId: id,
        tm_order_list: [],
        curpage: 1,
        ismore: 0,
        is_read: 0
      })
      th.query_orderList();
    } else {
      return false;
    }
  },
  //显示核销码
  code_show: function(e) {
    var th = this;
    //--获取成功的时候--
    var no = e.currentTarget.dataset.order_sn;
    var qc_com = th.selectComponent("#qc_com"); //组件的id
    var obj = {
      val: no,
      content: "请将二维码展示给核销员，使用更快捷"
    };
    qc_com.open(obj)
  },
  query_orderList: function() {
    wx.showLoading({
      title: '加载中',
    })
    var th = this;
    var url = "/api/weshop/marketing/reservation/reservation/page";

    var state_index = th.data.activeCategoryId;
    if (state_index == 0) {
      state_index = "";
    } else {
      --state_index;
    }
    th.setData({
      is_read: 0
    })
    getApp().request.promiseGet(url, {
      data: {
        storeId: a.stoid,
        userId: d.user_id,
        state: state_index,
        page: th.data.curpage,
        pageSize: th.data.pageSize
      }
    }).then(res => {
      wx.hideLoading();
      if (res.data.code == 0) {
        th.data.curpage++;
        var arr1 = th.data.tm_order_list;
        var arr2 = res.data.data.pageData;
        var arr3 = [...arr1, ...arr2];
        for(var i = 0;i<arr3.length;i++){
          arr3[i].ArrangeTime = arr3[i].ArrangeTime.substring(0, 16);
        }
        var ismore = 0;
        if (arr3.length == res.data.data.total) ismore = 1
        th.setData({
          tm_order_list: arr3,
          total: res.data.data.total,
          ismore: ismore,
          is_read: 1
        }), wx.stopPullDownRefresh(); //停止下拉刷新 {
      } else {
        wx.hideLoading();
        th.setData({
          tm_order_list: [],
          is_read: 1
        })
      }
    })

  },
  //取消预约
  cancel_tment: function(e) {
    var th = this;
    var th = this;
    var my_confirm = th.selectComponent("#my_confirm"); //组件的id
    my_confirm.open(
      "确定取消？",
      "取消",
      "确定",
      function() {
        my_confirm.open_cancel(0);
      },
      function() {
        my_confirm.open_cancel(0);
        var index = e.currentTarget.dataset.index;
        var states = th.data.states;
        var order_list = th.data.tm_order_list;
        var number = order_list[index].Number;
        var json = {
          "number": number,
          "storeId": a.stoid,
          "states": 3
        };
        var data = JSON.stringify(json);
        var url = th.data.url + "/api/weshop/marketing/reservation/reservation/update"; //预约接口地址

        getApp().request.promisePut(url, {
          data: json,is_json:1,is_normal:1
        }).then(res => {
            if (res.data.code == 0) {
              getApp().my_warnning("取消成功", 1, th);
              var id = th.data.activeCategoryId;
              if (id == 1) {
                order_list.splice(index, 1);
                th.setData({
                  tm_order_list: order_list
                })
              } else {
                var start = 'tm_order_list[' + index + '].State';
                th.setData({
                  [start]: 3
                })
              }
              wx.stopPullDownRefresh(); //停止下拉刷新
            } else {
              getApp().my_warnning(res.data.msg, 0, th);
            }

        })
      })

  },
  //图片失败，默认图片
  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) //注意这里的赋值方式,只是将数据列表中的此项图片路径值替换掉 ;
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var th = this;
    th.setData({
      curpage: 1,
      total: 0,
      ismore: 0,
      is_read: 0,
      tm_order_list: []
    })
    th.query_orderList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var th = this;
    if (th.data.total <= th.data.pageSize) return;
    if (th.data.ismore) return;

    wx.showLoading({
      title: '加载中...',
    })
    th.query_orderList();
  },
  //关闭导航
  close: function() {
    var th = this;
    var nav_b = th.selectComponent("#nav_b"); //组件的id
    nav_b.close_box();
  }

})