// pages/member/rankingListSecond/rankingListSecond.js
var app = getApp();
const STORE_INFO_URL = 'plugin.store-cashier.frontend.boss.child-store.index';
const STORE_ORDER_URL = 'plugin.store-cashier.frontend.boss.child-store.getStoreOrderList';
const CASHIER_ORDER_URL = 'plugin.store-cashier.frontend.boss.child-store.getCashierOrderList';
const AUDIO_IS_OPEN = 'plugin.store-cashier.frontend.boss.voice-broadcast.verifyOpenAudio';
const GET_AUDIO_URL = 'plugin.store-cashier.frontend.boss.voice-broadcast.index';
var intervalid;
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    language: '',
    store_info: {},
    store_count: {},
    tabArr: [
      '门店订单',
      '收银台订单'
    ],
    currentIndex: 0,
    store_order_show: false,
    cashier_order_show: false,

    // 门店订单
    storeList: [],
    store_loading: false,
    store_allLoaded: false,
    store_goload: true,
    store_isLoadMore: true,
    store_page: 1,
    store_total_page: 0,

    // 收银台订单
    cashierList: [],
    cashier_loading: false,
    cashier_allLoaded: false,
    cashier_goload: true,
    cashier_isLoadMore: true,
    cashier_page: 1,
    cashier_total_page: 0,

    // 语音播报
    vedioSrc: '',
    audioAction: {
      method: 'play'
    },
    tabShowin:false,
    // 门店独立余额
    open_store_balance:false,
    open_store_show:false,
    balanceInfo:[],
    page:1,
  },

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

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});
    this.audioCtx = wx.createAudioContext('yp');
  },

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

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

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

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

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {
    if(this.data.open_store_show){
      if(this.data.page >= this.data.last_page){
        console.log('没有更多了。。');
        return;
      }else{
        wx.showLoading({
          title: '加载更多',
        });
        this.getMoreBalance();
      }
    }else{
      if (this.data.store_isLoadMore) {
        this.getMoreStoreList();
      } else {
        wx.showToast({
          title: '没有更多数据',
          icon: 'none'
        });
      }
    }
  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {},
  getStoreInfo() {
    let urlStr = app.getNetAddresss(STORE_INFO_URL);
    urlStr += '&store_id=' + this.options.id;
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resdata = res.data;
        if (resdata.result === 1) {
          if(resdata.data.open_store_balance){
            this.setData({
              open_store_balance:resdata.data.open_store_balance,
              //有开启独立门店余额插件  默认第一个是充值订单
              open_store_show:true, 
            });
            this.getBalance();
          }
          this.setData({
            store_info: resdata.data.store,
            store_count: resdata.data.statistics,
            tabShowin:true
          });
          this.getCashierList(this.data.store_info.cashier_id);
        } else {
          wx.showToast({
            title: resdata.msg,
            icon: 'none'
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  toggle(index) {
    let currentIndex = index.detail.name;
    let store_order_show,cashier_order_show,open_store_show = false;
    if(currentIndex == '3'){
      open_store_show = true;
      store_order_show = false;
      cashier_order_show = false;
      this.setData({
        page:1,
        balanceInfo:[]
      });
      this.getBalance();
    }else if(currentIndex == '2'){
      cashier_order_show=true;
      store_order_show = false;
      open_store_show = false;
    }else if(currentIndex == '1'){
      store_order_show=true;
      cashier_order_show = false;
      open_store_show = false;
    }
    this.setData({
      open_store_show,
      cashier_order_show,
      store_order_show
    });
  },
  getBalance() {
    let urlStr = app.getNetAddresss("plugin.store-cashier.frontend.store.balance.award.boss");
    urlStr += '&store_id=' + this.options.id + '&page=' + this.data.page;
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resdata = res.data;
        if (resdata.result === 1) {
          this.setData({
            balanceInfo: resdata.data.list.data,
            last_page: resdata.data.list.last_page
          });
        } else {
          wx.showToast({
            title: resdata.msg,
            icon: 'none'
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  getMoreBalance(){
    this.setData({
      page : this.data.page + 1
    });
    let urlStr = app.getNetAddresss("plugin.store-cashier.frontend.store.balance.award.boss");
    urlStr += '&store_id=' + this.options.id + '&page=' + this.data.page;
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        wx.hideLoading({
          success: (res) => {},
        });
        let resdata = res.data;
        if (resdata.result === 1) {
          let mydata = resdata.data.list.data;
          this.data.balanceInfo = this.data.balanceInfo.concat(mydata);
          this.setData({
            balanceInfo: this.data.balanceInfo,
            last_page: resdata.data.list.last_page
          });
        } else {
          wx.showToast({
            title: resdata.msg,
            icon: 'none'
          });
        }
      },
      fail: function(res) {
        wx.hideLoading({
          success: (res) => {},
        });
        console.log(res);
      }
    });
  },
  getStoreList() {
    let urlStr = app.getNetAddresss(STORE_ORDER_URL);
    urlStr += '&store_id=' + this.options.id + '&page=' + this.data.store_page;
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resdata = res.data;
        if (resdata.result === 1) {
          this.setData({
            storeList: resdata.data.data,
            store_total_page: resdata.data.last_page
          });
        } else {
          wx.showToast({
            title: resdata.msg,
            icon: 'none'
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  getMoreStoreList() {
    if (this.data.store_page == this.data.store_total_page) {
      return;
    }
    if (this.data.store_page >= this.data.store_total_page) {
      this.setData({
        store_loading: true,
        store_allLoaded: true
      });
      return;
    } else {
      let page = this.data.store_page + 1;
      this.setData({
        store_page: page
      });
      let urlStr = app.getNetAddresss(STORE_ORDER_URL);
      urlStr += '&store_id=' + this.options.id + '&page=' + this.data.store_page;
      app._getNetWork({
        url: urlStr,
        success: (res) => {
          let resdata = res.data;
          if (resdata.result === 1) {
            let list = this.data.storeList.concat(resdata.data.data);
            this.setData({
              storeList: list,
              store_loading: false,
              store_allLoaded: false
            });
          } else {
            let pages = this.data.store_page - 1;
            this.setData({
              store_page: pages,
              store_loading: true,
              store_allLoaded: true,
              store_isLoadMore: false
            });
          }
        },
        fail: function(res) {
          console.log(res);
        }
      });
    }
  },
  getCashierList(id) {
    let urlStr = app.getNetAddresss(CASHIER_ORDER_URL);
    urlStr += '&cashier_id=' + id + '&page=' + this.data.cashier_page;
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resdata = res.data;
        if (resdata.result === 1) {
          this.setData({
            cashierList: resdata.data.data,
            cashier_total_page: resdata.data.last_page
          });
        } else {
          wx.showToast({
            title: resdata.msg,
            icon: 'none'
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  // 语音播报
  isOpenAudio() {
    let urlStr = app.getNetAddresss(AUDIO_IS_OPEN);
    urlStr += '&store_id=' + this.options.id;
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resdata = res.data;
        if (resdata.result === 1) {
          if (resdata.data.audio_open == 1) {
            this.getAudio();
          }
        } else {
          wx.showToast({
            title: resdata.msg,
            icon: 'none'
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },

  getAudio() {
    intervalid = setInterval(() => {
      let urlStr = app.getNetAddresss(GET_AUDIO_URL);
      urlStr += '&store_id=' + this.options.id;
      app._getNetWork({
        url: urlStr,
        success: (res) => {
          let resdata = res.data;
          if (resdata.result === 1) {
            this.setData({
              vedioSrc: resdata.data.url
            });
            this.audioPlay();
          } else {
            wx.showToast({
              title: resdata.msg,
              icon: 'none'
            });
          }
        },
        fail: function(res) {
          console.log(res);
        }
      });
    }, 5000);
  },
  audioPlay() {
    var pages = getCurrentPages(); // 获取加载的页面
    var currentPage = pages[pages.length - 1]; // 获取当前页面的对象
    var url = currentPage.route;
    let json = {
      url
    };
    let urlStr = app.getNetAddresss('member.member.wxJsSdkConfig');
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (res) => {
        let resdata = res.data;
        if (resdata.result === 0) {
          wx.showToast({
            title: resdata.msg,
            icon: 'none'
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  // 清楚定时器
  closeTimeInterval() {
    console.log('dsdsdsdsds');
    clearInterval(intervalid);
  }
});
