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

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    current: 0, //当前轮播索引
    saleboxShow: false, //出售弹窗
    money: '', //出售价格
    page: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '直播授权码',
    });
  },
  init() {
    this.setData({
      page: 1,
      last_page: 1,
      current: 0,
      saleboxShow: false,
      info: []
    });
  },
  changeActive(e) {
    this.setData({
      info: [],
      active: e.detail.index
    });
    this.init();
    this.getdata();
  },
  // init() {
  //   this.setData({
  //     page: 1,
  //     current_page: 1,
  //     last_page: 1,
  //     info: []
  //   });
  // },
  swiperChange(e) {
    this.setData({
      current: e.detail.current
    });
  },
  tapSele() {
    this.setData({
      saleboxShow: true,
      money: ''
    });
  },
  moneyInp(e) {
    console.log(e);
    this.setData({
      money: e.detail
    });
  },
  goDetail() {
    wx.navigateTo({
      url: '/packageH/liveAuthorization/liveAuthorization_detail/liveAuthorization_detail',
    });
  },
  saleClose() {
    this.setData({
      saleboxShow: false
    });
  },
  sureBtnTap() {
    // let that = this;
    let money = this.data.money;
    console.log(this.data.info,this.data.current);
    if (this.data.info[this.data.current].code_num == 0) {
      wx.showToast({
        title: '授权码数量不足',
        icon: 'none',
        duration: 1000
      });
      return;
    }
    let id = this.data.info[this.data.current].id;
    let kid = this.data.info[this.data.current].level_id;
    if (app._isTextEmpty(money)) {
      wx.showToast({
        title: '请输入出售价格',
        icon: 'none',
        duration: 1000
      });
    } else {
      this.sendLogs(id,kid); //出售记录

    }
  },
  tapCode(e) {
    let id = this.data.info[this.data.current].level_id;
    wx.navigateTo({
      url: '/packageH/liveAuthorization/liveAuthorization_collection/liveAuthorization_collection?id=' + id,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.active == '0') {
      if (this.data.current_page >= this.data.last_page) {
        return;
      } else {
        let pages = this.data.page + 1;
        this.setData({
          page: pages
        });
        this.getMore();
      }
    }
  },

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

  },
  getdata() {
    let that = this;
    let urlStr;
    if (that.data.active == 1) {
      // 可出售
      urlStr = app.getNetAddresss("plugin.room.frontend.code.salabilityCode");
    } else if (that.data.active == 0) {
      // 已购买
      urlStr = app.getNetAddresss("plugin.room.frontend.code.purchasedCode");
    }
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          if (that.data.active == "1") {
            that.setData({
              info: res.data
            });
          } else if (that.data.active == "0") {
            that.setData({
              info: res.data.data,
              current_page: res.data.current_page,
              last_page: res.data.last_page
            });
          }
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getMore() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.room.frontend.code.purchasedCode");
    app._getNetWork({
      url: urlStr,
      data: {
        page: that.data.page
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            info: that.data.info.concat(res.data.data),
            current_page: res.data.current_page,
            last_page: res.data.last_page
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  sendLogs(e,ke) {
    let that = this;
    let kid = e;
    let id = ke;
    let uelStr = app.getNetAddresss("plugin.room.frontend.code.generatePoster");
    app._getNetWork({
      url: uelStr,
      data: {
        id: kid,
        money: that.data.money
      },
      success: function (resdata) {
        console.log(resdata);
        let res = resdata.data;
        if (res.result === 1) {
          wx.showToast({
            title: '出售成功',
            icon: 'none',
            duration: 1500,
            success: function (res) {
              console.log(res);
              setTimeout(() => {
                that.setData({
                  saleboxShow: false
                });
                wx.navigateTo({
                  url: '/packageH/liveAuthorization/liveAuthorization_collection/liveAuthorization_collection?id=' + id,
                });
              }, 1500);
            }
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon:'none',
            duration:1000
          });
        }

      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  tapActivate(item) {
    let that = this;
    if (item.type == 1) {
      let uelStr = app.getNetAddresss("plugin.room.frontend.code.activationCode");
      app._getNetWork({
        url: uelStr,
        data: {
          id: item.id,
        },
        success: function (resdata) {
          that.getdata();
        },
        fail: function (res) {
          console.log(res);
        },
      });
    }
  }
});