// packageF/timeAppointment/timeAppointmentMylist/timeAppointmentMylist.js
var behavior = require('../common.js');
var app = getApp();
Page({
  behaviors: [behavior],
  /**
   * 页面的初始数据
   */
  data: {
    tabsIndex:0,

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

    listStatus:1,
    store_info:{},

    codeShow:false,
    codeList:{},
    codeUrl:'',
    is_boss:0,
    status_arr:['未完成','已完成','申请取消','取消成功','','','','','','取消失败']
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.status){
      this.setData({listStatus:options.status});
    }
    if(this.data.listStatus==1){
      wx.setNavigationBarTitle({
        title: '我的预约',
      });
      this.getStore();
    }
    this.getList();
  },
  initCustomField(){
    if(this.data.listStatus==2){
      wx.setNavigationBarTitle({
        title: this.data.reserve_obj_text+'记录',
      });
    }
  },

  setTabsIndex(evt){
    let index = evt.currentTarget.dataset.index;
    this.setData({
      tabsIndex:index
    });
    this.getList();
  },

  setCodeShow() {
    this.setData({
      codeShow: !this.data.codeShow,
    });
  },

 
  gotoRefund(evt){
    let item = evt.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/packageD/member/myOrder/refund/refund?order_id='+item.order_id,
    });
  },

  gotoAftersales(evt){
    let item = evt.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/packageD/member/myOrder/Aftersales/Aftersales?refund_id='+item.refund_id,
    });
  },

  gotoGoods(evt){
    let item = evt.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/packageA/detail_v2/detail_v2?id='+item.goods_id,
    });
  },

  getCodeUrl(evt){
    let id = evt.currentTarget.dataset.id;
    if(this.data.codeList[id]){
      this.setData({
        codeShow: true,
        codeUrl: this.data.codeList[id],
      });
      return;
    }
    let urlStr = app.getNetAddresss("plugin.reserve-simple.frontend.reserve-order.qr-code-url");
    app._getNetWork({
      url: urlStr,
      data:{id},
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        let index = 'codeList['+id+']';
        this.setData({
          codeShow: true,
          codeUrl: res.data.qr_code_url,
          [index]:res.data.qr_code_url
        });
        
      }
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  initData() {
    this.setData({
      page: 1,
      total_page: 0,
      isLoadMore: true,
      listData:[],
      networkLoading: false,
    });
  },
  getList(){
    this.initData();
    let urlStr = app.getNetAddresss("plugin.reserve-simple.frontend.reserve-order.get-my-list");
    if(this.data.listStatus == 2){
      urlStr = app.getNetAddresss("plugin.reserve-simple.frontend.reserve-order.get-all-list");
    }
    let json={};

    if(this.data.tabsIndex!=0){
      json.reserve_type = this.data.tabsIndex;
    }
   
    app._getNetWork({
      url: urlStr,
      data:json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        console.log(res);
        this.data.total_page = res.data.last_page || res.data.list.last_page;
        if (!this.data.total_page) {
          this.data.total_page = 0;
        }
        let listData = [];
        let changeData={};
        if(this.data.listStatus == 1){
          listData = res.data.data;
        }else if(this.data.listStatus == 2){
          listData = res.data.list.data;
          changeData.is_boss = res.data.is_boss;
        }
        changeData.listData = listData;
        changeData.networkLoading = true;
        this.setData(changeData);
        
      }
    });
  },

  //加载更多数据
  _getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      // that.loading = true;
      return;
    } else {
      this.data.page += 1;
      let json={ page: this.data.page };
      let urlStr = app.getNetAddresss("plugin.reserve-simple.frontend.reserve-order.get-my-list");
      if(this.data.listStatus == 2){
        urlStr = app.getNetAddresss("plugin.reserve-simple.frontend.reserve-order.get-all-list");
      }
      if(this.data.tabsIndex!=0){
        json.reserve_type = this.data.tabsIndex;
      }
      app._postNetWork({
        url: urlStr,
        data: json, 
        success: (resdata) => {
          let res = resdata.data;
          this.data.isLoadMore = true;
          if (res.result === 1) {
            let listData=[];
            if(this.data.listStatus == 1){
              listData = this.data.listData.concat(res.data.data);
            }else if(this.data.listStatus == 2){
              listData = this.data.listData.concat(res.data.list.data);
            }
            
            this.setData({
              listData
            });
          } else {
            this.data.page = this.data.page - 1;
            this.data.isLoadMore = false;
          }
        },
      });
    }
  },

  getStore(){
    let urlStr = app.getNetAddresss("plugin.reserve-simple.frontend.index.get-shop-info");
    app._postNetWork({
      url: urlStr,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        this.setData({
          store_info:res.data
        });
      },
    });
  },
  goToAdress() {
    let latitude = this.data.store_info.lat;
    let longitude = this.data.store_info.lng;
    let store_name = this.data.store_info.name;
    let point = app.BMapTransqqMap(parseFloat(longitude), parseFloat(latitude));
    wx.openLocation({
      latitude: point.lat,
      longitude: point.lng,
      scale: 18,
      name: store_name
    });
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
});