// pages/member/rankingListSecond/rankingListSecond.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    language: "",
    show1: false,
    show2: false,
    show3: false,
    show4: false,
    nickname: "",
    avatar: "",
    summarizing: {
      goods_queue_count: 0,
      store_queue_count: 0,
      cashier_queue_count: 0,
    },
    getDataApi: "",
    rewardList: [],
    activeName: "shop",
    chooseName: "queue",
    status: "",
    chooseId: "",
    chooseRatio: "",
    msg: "",
    typeData: {},
    WXtitle: "",
    items: {}, // 显示完整信息
    // more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    // 兑换类型 1=>积分 2=>爱心值
    type: "",
    convertType: "",
    convertTypeId: 0,
    convertTypeUnit: 0,
    convertTypeExchange_count: 0,
    exchangeLogs: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.checkrouter();
    this.getTitle();
    this.getData(this.data.activeName, "plugin.share-chain.frontend.queue.getList");
    this.getUser();
    this.getHomeInfo();
  },
  checkrouter() {
    try {
      // 推广中心是否开启该功能，没开启跳转到指定路径
      let basic_info = wx.getStorageSync("yz_basic_info");
      let stop_info = basic_info.popularize_page.mini.vue_route;
      for (let i = 0; i < stop_info.length; i++) {
        if (stop_info[i] == "RewardQueue") {
          console.log(basic_info.popularize_page.mini.mini_url + "跳转的路径");

          wx.showToast({
            title: "未开启推广权限",
            duration: 1000,
            icon: "none",
            success: function () {
              setTimeout(() => {
                wx.redirectTo({
                  url: basic_info.popularize_page.mini.mini_url,
                });
              }, 1000);
            },
          });
          return;
        }
      }
    } catch (e) {
      console.log(e);
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync("langIndex");
    this.setData({ language: language.en });
  },

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

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

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

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      wx.showToast({
        title: "没有更多数据",
        icon: "none",
      });
    }
  },

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  toDetail() {
    wx.navigateTo({
      url: "/packageB/member/enterprise/reward_detail/reward_detail",
    });
  },
  toProcess(item) {
    let items = item.currentTarget.dataset.item || item.target.dataset.item;
    wx.navigateTo({
      url: "/packageB/member/enterprise/RewardProcess/RewardProcess" + "?source_code=" + this.data.activeName + "&queue_id=" + items.id,
    });
  },
  showComplete(obj) {
    let item = obj.currentTarget.dataset.item || obj.target.dataset.item;
    item.show = true;
    let arr = this.data.rewardList;
    for (var i = 0; i < arr.length; i++) {
      if (arr[i].id == item.id) {
        arr[i].show = true;
      }
    }
    this.setData({
      rewardList: arr,
    });
  },
  // 点击蒙层时触发
  leaveOut() {
    this.setData({
      show3: false,
    });
  },
  getTitle() {
    // 兑换操作
    let urlStr = app.getNetAddresss("plugin.share-chain.frontend.plugin-name.index");
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result === 1) {
          res.data = res.data.data;
          wx.setNavigationBarTitle({
            title: res.data.plugin_name ? res.data.plugin_name : "",
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  chooseType(convertType) {
    // 根据选择类型显示弹框
    let level = convertType.target.dataset.point || convertType.currentTarget.dataset.point;
    let typeData = this.data.typeData;
    if (level.code === 1) {
      this.setData({
        convertType: typeData.modules.point.name,
        convertTypeId: typeData.modules.point.code,
        convertTypeUnit: typeData.modules.point.unit,
        convertTypeExchange_count: typeData.modules.point.exchange_count,
      });
    } else if (level.code === 2) {
      this.setData({
        convertType: typeData.modules.love.name,
        convertTypeId: typeData.modules.love.code,
        convertTypeUnit: typeData.modules.love.unit,
        convertTypeExchange_count: typeData.modules.love.exchange_count,
      });
    }
  },
  exchange(item) {
    let items = item.currentTarget.dataset.item || item.target.dataset.item;
    this.setData({
      chooseId: items.id,
      convertType: "",
      convertTypeId: 0,
      convertTypeUnit: 0,
      convertTypeExchange_count: 0,
    });
    let urlStr = app.getNetAddresss("plugin.share-chain.frontend.exchange.index");
    urlStr += "&source_code=" + this.data.activeName + "&queue_id=" + items.id;
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result === 1) {
          res.data = res.data.data;
          this.setData({
            show3: true,
            typeData: res.data,
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  SureExchange() {
    // 兑换操作
    let urlStr = app.getNetAddresss("plugin.share-chain.frontend.exchange.handle");
    urlStr += "&source_code=" + this.data.activeName + "&queue_id=" + this.data.chooseId + "&exchange_code=" + this.data.convertTypeId;
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result === 1) {
          res.data = res.data.data;
          this.setData({
            show3: false,
          });
          this.getData(this.data.activeName, "plugin.share-chain.frontend.queue.getList");
          wx.showToast({
            title: res.data.msg,
            icon: "none",
          });
        } else {
          this.setData({
            show3: false,
          });
          wx.showToast({
            title: res.data.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  endReward() {
    this.setData({
      show4: false,
    });
  },
  exchangeLog(item) {
    let obj = item.currentTarget.dataset.item || item.target.dataset.item;
    let urlStr = app.getNetAddresss("plugin.share-chain.frontend.exchange.log");
    urlStr += "&source_code=" + this.data.activeName + "&queue_id=" + obj.id;
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result === 1) {
          res.data = res.data.data;
          this.setData({
            show4: true,
            exchangeLogs: res.data,
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  luckyDraw(item) {
    // 抽奖
    let itemse = item.currentTarget.dataset.item || item.target.dataset.item;
    this.setData({
      show1: true,
      chooseId: itemse.id,
      chooseRatio: itemse.ratio,
    });
  },
  sureTake(item) {
    // 确认抽奖
    // let itemsk = item.currentTarget.dataset.item || item.target.dataset.item
    let urlStr = app.getNetAddresss("plugin.share-chain.frontend.lucky.handle");
    urlStr += "&source_code=" + this.data.activeName + "&queue_id=" + this.data.chooseId;
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result === 1) {
          res.data = res.data.data;
          this.setData({
            show1: false,
            show2: true,
            msg: res.data.msg,
          });
          this.getData(this.data.activeName, "plugin.share-chain.frontend.queue.getList");
        } else {
          this.setData({
            show1: false,
          });
          wx.showToast({
            title: res.data.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  // 取消抽奖
  sureTakere() {
    this.setData({
      show1: false,
    });
  },
  getUser() {
    let urlStr = app.getNetAddresss("plugin.share-chain.frontend.member.getInfo");
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result === 1) {
          res.data = res.data.data;
          this.setData({
            nickname: res.data.nickname,
            avatar: res.data.avatar,
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getHomeInfo() {
    let urlStr = app.getNetAddresss("plugin.share-chain.frontend.center.statistics");
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result === 1) {
          res.data = res.data.data;
          this.setData({
            summarizing: res.data,
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  handleClick1(tab) {
    if (tab.detail.index == 0) {
      this.getData(this.data.activeName, "plugin.share-chain.frontend.queue.getList");
      this.setData({
        chooseName: "queue",
      });
    } else if (tab.detail.index == 1) {
      this.setData({
        chooseName: "record",
      });
      this.getData("", "plugin.share-chain.frontend.log.getList");
    }
  },
  handleClick(tab) {
    if (tab.detail.index == 0) {
      this.setData({
        activeName: "shop",
      });
    } else if (tab.detail.index == 1) {
      this.setData({
        activeName: "store",
      });
    } else if (tab.detail.index == 2) {
      this.setData({
        activeName: "cashier",
      });
    }
    this.getData(this.data.activeName, "plugin.share-chain.frontend.queue.getList");
  },
  // 获取数据
  getData(status, api) {
    this.setData({
      getDataApi: api,
      status: status,
    });
    let urlStr = app.getNetAddresss(api);
    urlStr += "&source_code=" + status;
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result === 1) {
          res.data = res.data.data;
          this.setData({
            isLoadMore: true,
            total_page: res.data.last_page,
            rewardList: res.data.data,
          });
          if (!this.data.total_page) {
            this.setData({
              total_page: 0,
            });
          }
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 1500,
            success: function () {
              setTimeout(function () {
                //要延时执行的代码
                wx.navigateTo({
                  url: "/packageG/pages/member/extension/extension",
                });
              }, 1500); //延迟时间
            },
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  // 获取更多数据
  getMoreData() {
    this.setData({
      isLoadMore: false,
    });
    let urlStr = app.getNetAddresss(this.data.getDataApi);
    let pages = this.data.page + 1;
    this.setData({
      page: pages,
    });
    if (this.data.status) {
      urlStr += "&source_code=" + status + "&page=" + this.data.page;
    } else {
      urlStr += "&page=" + this.data.page;
    }
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      app._getNetWork({
        url: urlStr,
        success: (res) => {
          if (res.data.result === 1) {
            res.data = res.data.data;
            let myData = this.data.rewardList.concat(res.data.data);
            this.setData({
              isLoadMore: true,
              rewardList: myData,
            });
          } else {
            let pageOne = this.data.page - 1;
            this.setData({
              page: pageOne,
              isLoadMore: false,
            });
          }
        },
        fail: function (res) {
          console.log(res);
        },
      });
    }
  },
});
