// packageH/orderImport/orderImportIndex/orderImportIndex.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabIndex:0,
    tabArray:[{name:'全部'},{name:"已结算"},{name:'未结算'},{name:'无效'}],

    //开始时间
    startTimeText:'',
    //结束时间
    endTimeText:'',


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

    details:{},

    sid:0,   //记录点击哪条记录  

    shop_type:0
  },

  setShopType(evt){
    let index = evt.currentTarget.dataset.index;
    if(index == this.data.shop_type) return ;
    this.setData({
      shop_type:index
    });
    this.getListData();
  },
  setTabIndex(evt){
    let index = evt.currentTarget.dataset.index;
    if(index == this.data.tabIndex) return ;
    this.setData({
      tabIndex:index
    });
    this.getListData();
  },

  handRecord(evt){
    let id = evt.currentTarget.dataset.id;
    if(id == this.data.sid){
      this.setData({
        sid:0
      });
      return;
    }else {
      this.setData({
        sid:id
      });
    }
  },

  bindDateChange(evt){
    let type = evt.currentTarget.dataset.type;
    let value = evt.detail.value;
    if(type == "start" && this.data.startTimeText == value) return;
    if(type == "end" && this.data.endTimeText == value) return;
    let name="startTimeText";
    if(type == "end"){
      name = "endTimeText";
    }
    this.setData({
      [name]:value
    });
    if(this.data.startTimeText!='' && this.data.endTimeText!=''){
      //请求
      this.getListData();
    }
  },


  initPage() {
    this.setData({
      listData: [],
      networkLoading: false
    });
    this.data.page = 1;
    this.data.total_page = 0;
    this.data.isLoadMore = true;

  },
  getListData() {
    this.initPage();
    let urlStr = app.getNetAddresss("plugin.owner-order-import.frontend.controllers.page.index");
    let json = {};
    
    if(this.data.startTimeText!='' && this.data.endTimeText!=''){
      json.start = parseInt(new Date(this.data.startTimeText.replace(/-/g, "/")).getTime()/1000);
      json.end = parseInt(new Date(this.data.endTimeText.replace(/-/g, "/")).getTime()/1000);
    }

    if(this.data.tabIndex==1) json.status = 2;
    if(this.data.tabIndex==2) json.status = 1;
    if(this.data.tabIndex==3) json.status = -1;
    json.shop_type = this.data.shop_type;
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        if(res.data.info.need_perfect==true){
          wx.showModal({
            content: '请完善店主信息',
            showCancel:false,
            success (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/packageH/orderImport/orderImportApply/orderImportApply?shopkeeper=1',
                });
              }
            }
          });
          return;
        }
        this.setData({
          isLoadMore: true,
          total_page: res.data.page_list.last_page,
          listData: res.data.page_list.data,
          details:res.data.info,
          networkLoading: true,
          
        });
        if (!this.data.total_page) {
          this.data.total_page = 0;
        }

      }
    });
  },
  //获取更多数据
  getMoreData() {
    let urlStr = app.getNetAddresss("plugin.owner-order-import.frontend.controllers.page.index");
    // 防止多次请求分页数据
    this.data.isLoadMore = false;
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page++;
      let json = {
        page: this.data.page
      };
      if(this.data.startTimeText!='' && this.data.endTimeText!=''){
        json.start = parseInt(new Date(this.data.startTimeText.replace(/-/g, "/")).getTime()/1000);
        json.end = parseInt(new Date(this.data.endTimeText.replace(/-/g, "/")).getTime()/1000);
      }
      if(this.data.tabIndex==1) json.status = 2;
      if(this.data.tabIndex==2) json.status = 1;
      if(this.data.tabIndex==3) json.status = -1;
      json.shop_type = this.data.shop_type;
      app._getNetWork({
        url: urlStr,
        data:json,
        success: (resdata) => {
          var res = resdata.data;

          if (res.result == 1) {
            this.data.isLoadMore = true;
            var myData = res.data.page_list.data;
            this.setData({
              listData: this.data.listData.concat(myData)
            });
          } else {
            this.data.isLoadMore = false;
            this.data.page--;
          }
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getListData();
  },


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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },

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

  }
});