// packageE/Rankings/Rankings.js
const app = getApp();
const dataNow = Date.now();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    dataNows: dataNow,
    mock: [],
    panelIndex: 0,
    zhaosCut: "1",
    shareShow: false,
    showEarnings: false,
    page: 1,
    status: false,
    commission_name: '', // 分销订单自定义字样
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      if (scene) {
        var info_arr = [];
        info_arr = scene.split(",");
        for (let i = 0; i < info_arr.length; i++) {
          let chil_arr = [];
          chil_arr = info_arr[i].split("=");
          if (chil_arr[0] == "ind") {
            console.log(chil_arr[1]);
            this.setData({
              panelIndex: chil_arr[1],
            });
          } else if (chil_arr[0] == "type") {
            console.log(chil_arr[1]);
          } else if (chil_arr[0] == "mid") {
            app._setMid(chil_arr[1]);
          }
        }
      }
    }
    this.getdata(this.data.panelIndex);
  },
  Earnings() {
    this.setData({
      showEarnings: true,
    });
  },
  goWd() {
    wx.navigateTo({
      url: "/packageC/microshop/microShop_home/microShop_home",
    });
  },
  tapCut(e) {
    console.log(e);
    let id = e.currentTarget.dataset.ind;
    this.setData({
      zhaosCut: id,
    });
    console.log(this.data.zhaosCut);
    if (id == "1") {
      // 招商
      this.setData({
        title: "staffLevel",
      });
      this.inData("staffLevel");
    } else if (id == "2") {
      // 招商中心
      this.setData({
        title: "merchantLevel",
      });
      this.inData("merchantLevel");
    }
  },
  apply(e) {
    if (this.data.title) {
      console.log(this.data.title);
      let title = this.data.title;
      switch (title) {
      case "commission":
        wx.navigateTo({
          url: "/packageA/member/extension/ExtensionApply/ExtensionApply",
        });
        break;
      case "areaDividend":
        wx.navigateTo({
          url:
            "/packageB/member/income/applyRegionalAgency/applyRegionalAgency",
        });
        break;
      case "teamDividend":
        wx.navigateTo({
          url: "/packageA/member/extension/ExtensionApply/ExtensionApply",
        });
        break;
      case "micro":
        wx.navigateTo({
          url: "/packageC/microshop/microShop_apply/microShop_apply",
        });
        break;
      case "staffLevel":
        wx.navigateTo({
          url:
            "/packageB/member/enterprise/enterprise_apply/enterprise_apply",
        });
        break;
      case "merchantLevel":
        wx.navigateTo({
          url:
            "/packageB/member/enterprise/enterprose_center_apply/enterprose_center_apply",
        });
        break;
      case "room":
        wx.navigateTo({
          url: "/packageD/directSeeding/anchorApply/anchorApply",
        });
        break;
      }
    }
  },
  goDetail(e) {
    if (this.data.title) {
      console.log(this.data.title);
      let title = this.data.title;
      switch (title) {
      case "commission":
        wx.navigateTo({
          url: "/packageA/member/distribution/distribution",
        });
        break;
      case "areaDividend":
        wx.navigateTo({
          url:
            "/packageB/member/income/regionalAgencyCenter/regionalAgencyCenter",
        });
        break;
      case "teamDividend":
        wx.navigateTo({
          url: "/packageB/member/income/teamAgentCenter/teamAgentCenter",
        });
        break;
      case "staffLevel" || "merchantLevel":
        wx.navigateTo({
          url:
            "/packageB/member/enterprise/enterprise_index/enterprise_index",
        });
        break;
      case "room":
        wx.navigateTo({
          url: "/packageD/directSeeding/liveDividend/liveDividend",
        });
        break;
      }
    }
  },
  goTx() {
    wx.navigateTo({
      url: "/packageA/member/withdrawal/withdrawal",
    });
  },
  chooseTabData(e) {
    console.log(e);
    let ind = e.detail.index;
    let title = e.detail.title;
    this.setData({
      panelIndex: ind,
      page: 1,
      isCut: "",
    });
    let tapin;
    if (title == this.data.commission_title) {
      // 分销商
      tapin = "commission";
    } else if (title == this.data.team_dividend_title) {
      // 经销商
      tapin = "teamDividend";
    } else if (title == "招商") {
      // 招商员，招商中心
      if (this.data.zhaosCut == 1) {
        tapin = "staffLevel";
      } else if (this.data.zhaosCut == 2) {
        tapin = "merchantLevel";
      }
    } else if (title == this.data.micro_title) {
      // 微店
      tapin = "micro";
    } else if (title == this.data.area_dividend_title) {
      // 区域代理
      tapin = "areaDividend";
    } else if (title == this.data.room_title) {
      // 直播
      tapin = "room";
    }
    this.setData({
      title: tapin,
    });
    console.log(tapin);
    this.inData(tapin);
  },
  showTap(e) {
    console.log(e);
    console.log(this.data.title);
    this.setData({
      shareShow: true,
    });
    wx.showLoading({
      title: "海报加载中",
    });
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.ranking.api.poster.generateGoodsPoster"
    );
    app._getNetWork({
      url: urlStr,
      data: {
        getSwitch: "getSwitch",
        plugin_type: that.data.title,
        id: that.data.panelIndex,
      },
      success: function (resdata) {
        var res = resdata.data;
        console.log(res);
        if (res.result == 1) {
          that.setData({
            image_url: res.data.image_url,
          });
          wx.hideLoading({
            complete: (res) => {},
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
          wx.hideLoading({
            complete: (res) => {},
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  couponclose() {
    this.setData({
      shareShow: false,
      showEarnings: false,
    });
  },
  goHome() {
    wx.reLaunch({
      url: "/packageG/index/index",
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

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

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

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

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(this.data.current_page, this.data.last_page);
    if (this.data.current_page >= this.data.last_page) {
      // wx.showToast({
      //   title: '没有更多了',
      //   icon: 'none',
      //   duration: 1000
      // })
      return;
    } else {
      let pages = this.data.page + 1;
      this.setData({
        page: pages,
      });
      this.inDataMore(this.data.title);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  onShareTimeline(){},
  getdata(key) {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.ranking.api.index.index");
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        console.log(res);
        if (res.result == 1) {
          that.setData({
            commission: res.data.getSet.commission,
            merchantLevel: res.data.getSet.merchantLevel,
            micro: res.data.getSet.micro,
            staffLevel: res.data.getSet.staffLevel,
            team_dividend: res.data.getSet.team_dividend,
            area_dividend: res.data.getSet.area_dividend,
            room: res.data.getSet.room,
            getSet: res.data.getSet,
            commission_title: res.data.getSet.commission_title,
            merchantLevel_title: res.data.getSet.merchantLevel_title,
            staffLevel_title: res.data.getSet.staffLevel_title,
            micro_title: res.data.getSet.micro_title,
            team_dividend_title: res.data.getSet.team_dividend_title,
            area_dividend_title: res.data.getSet.area_dividend_title,
            room_title: res.data.getSet.room_title,
            thumb: res.data.getSet.thumb,
            txTitle: res.data.getSet.title,
            commission_name: res.data.getSet.commission_order,
          });
          let mock = [];
          if (res.data.getSet.commission_title) {
            mock.push(res.data.getSet.commission_title);
          }
          if (res.data.getSet.micro_title) {
            mock.push(res.data.getSet.micro_title);
          }
          if (res.data.getSet.team_dividend_title) {
            mock.push(res.data.getSet.team_dividend_title);
          }
          if (res.data.getSet.area_dividend_title) {
            mock.push(res.data.getSet.area_dividend_title);
          }
          if (res.data.getSet.room_title) {
            mock.push(res.data.getSet.room_title);
          }
          if (
            res.data.getSet.merchantLevel_title ||
            res.data.getSet.staffLevel_title
          ) {
            mock.push(
              res.data.getSet.merchantLevel_title ||
                res.data.getSet.staffLevel_title
                ? "招商"
                : ""
            );
          }
          that.setData({
            mock: mock,
          });
          console.log(mock, key, res.data.getSet);
          let tapin;
          if (mock[key] == res.data.getSet.commission_title) {
            // 分销商
            tapin = "commission";
          } else if (mock[key] == res.data.getSet.team_dividend_title) {
            // 经销商
            tapin = "teamDividend";
          } else if (mock[key] == "招商") {
            // 招商员，招商中心
            if (that.data.zhaosCut == 1) {
              tapin = "staffLevel";
            } else if (that.data.zhaosCut == 2) {
              tapin = "merchantLevel";
            }
          } else if (mock[key] == res.data.getSet.micro_title) {
            // 微店
            tapin = "micro";
          } else if (mock[key] == res.data.getSet.area_dividend_title) {
            // 区域代理
            tapin = "areaDividend";
          } else if (mock[key] == res.data.getSet.room_title) {
            // 直播
            tapin = "room";
          }
          console.log(tapin);
          that.setData({
            title: tapin,
          });
          that.inData(tapin);
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  inData(key) {
    console.log(key);
    wx.showLoading({
      title: "加载中",
    });
    let json = {
      getSwitch: "getSwitch",
      plugin_type: key,
    };
    let that = this;
    let urlStr = app.getNetAddresss("plugin.ranking.api.index.index");
    app._getNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        console.log(res);
        if (res.result == 1) {
          that.setData({
            list: res.data.getSwitch,
          });
          if (!app._isTextEmpty(res.data.getSwitch)) {
            if (res.data.getSwitch.status == "1") {
              that.setData({
                status: true,
                errText: res.data.getSwitch.text,
              });
            } else {
              that.setData({
                current_page: res.data.getSwitch.rankingCommission.current_page,
                last_page: res.data.getSwitch.rankingCommission.last_page,
                status: false,
              });
            }
          }
          wx.hideLoading({
            complete: (res) => {},
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
          wx.hideLoading({
            complete: (res) => {},
          });
        }
      },
      fail: function (res) {
        console.log(res);
        wx.hideLoading({
          complete: (res) => {},
        });
      },
    });
  },
  inDataMore(key) {
    let json = {
      getSwitch: "getSwitch",
      plugin_type: key,
      page: this.data.page,
    };
    wx.showLoading({
      title: "加载中",
    });
    let that = this;
    let urlStr = app.getNetAddresss("plugin.ranking.api.index.index");
    app._getNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            "list.rankingCommission.data": that.data.list.rankingCommission.data.concat(
              res.data.getSwitch.rankingCommission.data
            ),
          });
          if (!app._isTextEmpty(res.data.getSwitch)) {
            that.setData({
              current_page: res.data.getSwitch.rankingCommission.current_page,
              last_page: res.data.getSwitch.rankingCommission.last_page,
            });
          }
          wx.hideLoading({
            complete: (res) => {},
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
          wx.hideLoading({
            complete: (res) => {},
          });
        }
      },
      fail: function (res) {
        console.log(res);
        wx.hideLoading({
          complete: (res) => {},
        });
      },
    });
  },
  saveImg() {
    // var times = _this.data.timeend - _this.data.timestart;
    wx.getSetting({
      success(res) {
        if (!res.authSetting["scope.writePhotosAlbum"]) {
          wx.authorize({
            scope: "scope.writePhotosAlbum",
            success() {
              console.log("授权成功");
            },
          });
        }
      },
    });
    var imgSrc = this.data.image_url;
    wx.downloadFile({
      url: imgSrc,
      success: function (res) {
        console.log(res);
        //图片保存到本地
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function (data) {
            wx.showToast({
              title: "保存成功",
              icon: "none",
              duration: 2000,
            });
          },
          fail: function (err) {
            console.log(err);
            if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
              console.log("当初用户拒绝，再次发起授权");
              wx.openSetting({
                success(settingdata) {
                  if (settingdata.authSetting["scope.writePhotosAlbum"]) {
                    wx.showToast({
                      icon: "none",
                      title: "获取权限成功,再次长按保存图片",
                      duration: 1500,
                    });
                  } else {
                    wx.showToast({
                      icon: "none",
                      title: "获取权限失败,保存图片需要用户授权",
                      duration: 1500,
                    });
                  }
                },
              });
            }
          },
          complete(res) {
            console.log(res);
          },
        });
      },
    });
  },
});
