// pages/cashier_qr/cashier_qr.js
var intervalid;
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    qrcode: "",
    vedioSrc: "",
    adShow: false,
    hotel_id: '',
    storeBalance:false
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    if(options.name == 'storeBalance'){
      // 门店独立余额
      this.setData({
        store_id:options.store_id,
        name:options.name,
        storeBalance:true
      });
      wx.setNavigationBarTitle({
        title: '我的二维码',
      });
      this.getBalanceCode();
      return;
    }
    if (app._isTextEmpty(options.hotel_id)) {
      this.getQrcode();
    } else {
      this.setData({
        hotel_id: options.hotel_id
      });
      this.getHotelData();
    }
  },

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

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

  },

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

  },

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

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

  },

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

  },

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

  },
  // 门店独立余额
  getBalanceCode(){
    var that = this;
    let urlStr = app.getNetAddresss('plugin.store-cashier.frontend.store.balance.agent.getQrCode');
    app._getNetWork({
      url: urlStr,
      data:{
        store_id:this.data.store_id
      },
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            qrcode: res.data.qr_code,
            adShow: false
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  //获取收货地址
  getQrcode() {
    var that = this;
    let urlStr = app.getNetAddresss('plugin.store-cashier.frontend.cashier.QrCode');
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            qrcode: res.data.small_url,
            adShow: res.data.audio_open == 1 ? true : false
          });
          if (res.data.audio_open == 1) {
            that.openTime(res.data.store_id);
          }
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  openTime(id) {
    intervalid = setInterval(() => {
      var that = this;
      let urlStr = app.getNetAddresss('plugin.store-cashier.frontend.cashier.voice-broadcast.index');
      urlStr += '&store_id=' + id;
      wx.request({
        url: urlStr,
        data: {},
        header: {
          'Content-Type': 'application/json',
          'Accept': 'application/json',
        },
        success: function(res) {
          if (res.data.result == 1) {
            that.setData({
              vedioSrc: res.data.data.url,
            });
            console.log(res.data.data.url);
            if (that.data.vedioSrc) {
              that.audioCtx = wx.createAudioContext('yp');
              that.audioCtx.play();
            }
          }
        },
        fail: function(res) {
        },
        complete: function(res) {
        }
      });
    }, 5000);
  },
  closeTimeInterval() {
    clearInterval(intervalid);
  },
  getHotelData() {
    var that = this;
    let urlStr = app.getNetAddresss('plugin.hotel.frontend.cashier.qr-code.index');
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            qrcode: res.data.url
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  }
});
