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

  /**
   * 页面的初始数据
   */
  data: {
    activity_id: '',
    backgroundRGB: {
      red: 0,
      green: 0,
      black: 0
    },
    userInfo: {
      buy_count: 0,
      commission_sum: 0
    },
    shareOptions: {},
    rankList: [],
    isMore: false,
    isShow: false,
    posterImg: '',
  },

  onLoad: function (options) {
    if (options.id) {
      this.setData({
        activity_id: options.id * 1,
      });
    }
  },

  onShow () {
    this.getData();
  },

  onReady: function () {

  },


  getData () {
    let url = app.getNetAddresss("plugin.deposit-ladder.frontend.index.rankList");
    app._getNetWork({
      url,
      data: {
        id: this.data.activity_id
      },
      success: (resdata) => {
        // console.log(resdata)
        if (resdata.data.result == 1) {
          let res = resdata.data;
          let bgColor = this.hexStringToRGB(res.data.theme_color);
          this.setData({
            userInfo: res.data.member,
            countList: res.data.rankList,
            commission: res.data.commission,
            shareOptions: res.data.share,
            backgroundRGB: bgColor
          });
          if (res.data.rankList.length >= 10) {
            this.setData({
              rankList: this.data.countList.slice(0, 10),
              isMore: true
            });
          } else {
            this.setData({
              rankList: this.data.countList,
              isMore: false
            });
          }
        } else {
          app.tips(resdata.data.msg);
        }
      }
    });
  },

  // 查看更多
  loadMore() {
    let rankLength = this.data.rankList.length;
    let countList =  this.data.countList;
    let countLength = countList.length;
    if (countLength > rankLength + 10) {
      let pushArr = countList.slice(rankLength, rankLength + 10);
      this.setData({
        rankList: this.data.rankList.concat(pushArr),
        isMore: true
      });
    } else {
      let pushArr = countList.slice(rankLength, countLength);
      this.setData({
        rankList: this.data.rankList.concat(pushArr),
        isMore: false
      });
    }
  },

  //获取海报图片地址
  getPoster () {
    var urlStr = app.getNetAddresss("plugin.deposit-ladder.frontend.index.poster");
    wx.showLoading({
      title: '加载中',
    });
    app._getNetWork({
      url: urlStr,
      data: {
        id: this.data.activity_id,
      },
      success: (resdata) => {
        wx.hideLoading();
        let res = resdata.data;
        if (res.result != 1) {
          wx.showToast({
            title: res.msg,
          });
          return;
        }
        this.setData({
          posterImg: res.data.poster,
        });
      },
      fail () {
        wx.hideLoading();
      }
    });
  },

  // 把十六进制颜色转换为rgb
  hexStringToRGB(hexString) {
    let reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
    if (!hexString) hexString = '#fec400';
    let color = hexString.toLowerCase();
    if (reg.test(color)) {
      // 如果只有三位的值，需变成六位
      if (color.length === 4) {
        var colorNew = "#";
        for (let i = 1; i < 4; i += 1) {
          colorNew += color.slice(i, i + 1).concat(color.slice(i, i + 1));
        }
        color = colorNew;
      }
      var colorChange = [];
      for (let j = 1; j < 7; j += 2) {
        colorChange.push(parseInt("0x" + color.slice(j, j + 2)));
      }
      return colorChange;
    } 
    return [];
  },

  // 长按图片保存
  // saveImage(e) {
  //   wx.showActionSheet({
  //     itemList: ['保存到相册'],
  //     success: (res) => {
  //       let url = this.data.posterImg;
  //       wx.getSetting({
  //         success: (res) => {
  //           if (!res.authSetting['scope.writePhotosAlbum']) {   // 未授权
  //             wx.authorize({
  //               scope: 'scope.writePhotosAlbum',
  //               success: () => {
  //                 this.saveImgSuccess(url);
  //               },
  //               fail: (res) => {
  //                 wx.showModal({
  //                   title: '保存失败',
  //                   content: '请开启访问手机相册的权限',
  //                   success(res) {
  //                     wx.openSetting()
  //                   }
  //                 })
  //               }
  //             })
  //           } else {  // 已授权
  //             this.saveImgSuccess(url);
  //           }
  //         },
  //         fail: (res) => {
  //           console.log(res);
  //         }
  //       })
  //     },
  //     fail(res) {
  //       console.log(res.errMsg)
  //     }
  //   })
  // },
  // 同意授权保存到相册
  // saveImgSuccess(url) {
  //   wx.getImageInfo({
  //     src: url,  // 通过getImageInfo将src转换成改图片的本地路径，给saveImageToPhotosAlbum使用
  //     success: (res) => {
  //       let path = res.path;
  //       wx.saveImageToPhotosAlbum({
  //         filePath: path,   // filePath路径不能是网络图片路径
  //         success: (res) => {
  //           wx.showToast({
  //             title: '已保存到相册',
  //           })
  //         },
  //         fail: (res) => {
  //           console.log(res);
  //         }
  //       })
  //     },
  //     fail: (res) => {
  //       console.log(res);
  //     }
  //   })
  // },

  // 显示 分享海报弹框
  showSharePopup () {
    this.setData({
      isShow: true
    });
    if (this.data.posterImg) {
      return;
    }
    this.getPoster();
  },

  closeSharePopup () {
    this.setData({
      isShow: false,
    });
  },


  onShareAppMessage (options) {
    let shareObj = {};
    if (this.data.shareOptions.share_title && this.data.shareOptions.share_img) {
      shareObj = {
        path: `/packageH/deposit_group/rankList/rankList?id=${this.data.activity_id}`,
        title: this.data.shareOptions.share_title,
        imgUrl: this.data.shareOptions.share_img,
      };
    } else {
      shareObj = {
        path: `/packageH/deposit_group/rankList/rankList?id=${this.data.activity_id}`,
        title: "芸众商城",
        imgUrl: this.data.actData.goods_option.thumb,
      };
    }
    return shareObj;
  },
  // 分享朋友圈
  onShareTimeline() {
    let shareObj = {};
    if (this.data.shareOptions.share_title && this.data.shareOptions.share_img) {
      shareObj = {
        query: `id=${this.data.activity_id}`,
        title: this.data.shareOptions.share_title,
        imageUrl: this.data.shareOptions.share_img,
      };
    } else {
      shareObj = {
        path: `id=${this.data.activity_id}`,
        title: "芸众商城",
      };
    }
    return shareObj;
  },
});
