// packageH/turmaroundTime/turmaroundMy/turmaroundMy.js
const app = getApp();
const ismyUid = wx.getStorageSync('yz_uid');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showIn: false, // 历史成绩是否显示
    reward: false,
    arr: [100, 50, 20, 1, 0.5, 0.1],
    page: 1,
    title: '',
    amount: '',
    uid: '',
    showShare: false,
    swiperInd: 0,
    current_page:1,
    last_page:1,
    myUid:ismyUid
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        id: options.id
      });
    }
    if (options.ranking) {
      this.setData({
        ranking: options.ranking
      });
    }
    if (options.uid) {
      this.setData({
        uid: options.uid
      });
    }

    this.getData();
    this.getinfo();
    console.log(this.data.id);
  },
  selectMoney(e) {
    console.log(e);
    let money = e.currentTarget.dataset.str;
    this.setData({
      amount: money
    });
  },
  tapLikeIo(e){
    // 点赞
    // if(this.data.memberInfo.is_give_like == 2){
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.kart.Frontend.Store.Modules.Like.Controllers.Store.index"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        by_uid: this.data.uid?this.data.uid:wx.getStorageSync('yz_uid'),
      },
      success: (resdata) => {
        var res = resdata.data;
        wx.hideLoading({
          success: (res) => {},
        });
        if (res.result == 1) {
          wx.showToast({
            title: res.msg,
            duration:1000,
            icon:'none'
          });
          that.getData();
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });
        }
      },
    });
    // }
  },
  tapinp(e) {
    console.log(e);
    let mun = e.detail.value;
    this.setData({
      amount: mun
    });
  },
  goStore(e) {
    if(!this.data.uid){
      let id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/packageC/o2o/HomeSeller/HomeSeller?store_id=' + id,
      });
    }
  },
  showHistory(e) {
    this.setData({
      showIn: !this.data.showIn
    });

  },
  close() {
    this.setData({
      reward: false
    });
  },
  showReward() {
    this.setData({
      reward: true
    });
  },
  rightNext(e) {
    let len = e.currentTarget.dataset.ind.length;
    if (this.data.swiperInd == len - 1) {
      this.setData({
        swiperInd: 0
      });
    } else {
      this.setData({
        swiperInd: this.data.swiperInd + 1
      });
    }
  },
  leftNext(e) {
    let len = e.currentTarget.dataset.ind.length;
    console.log(this.data.swiperInd, len);
    if (this.data.swiperInd == 0) {
      this.setData({
        swiperInd: len - 1
      });
    } else {
      this.setData({
        swiperInd: this.data.swiperInd - 1
      });
    }

  },
  closePoster(e) {
    this.setData({
      showShare: false
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.current_page >= this.data.last_page) {
      return;
    } else {
      let pages = this.data.page + 1;
      this.setData({
        page: pages
      });
      this.getMoreData();
    }
  },
  shareBoxPos(){
    wx.showToast({
      title: '点击图片长按转发给朋友',
      icon:'none',
      duration:1000
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: "/packageH/turmaroundTime/turmaroundMy/turmaroundMy",
      title: this.data.title
    };
  },
  saveImage() {
    wx.showToast({
      title: '点击图片长按转发给朋友',
      icon:'none',
      duration:1000
    });
    // let ind = this.data.swiperInd;
    // wx.downloadFile({
    //   url: this.data.poster[ind],
    //   success: function (res) {
    //     wx.saveImageToPhotosAlbum({
    //       filePath: res.tempFilePath,
    //       success: function (data) {
    //         wx.showToast({
    //           title: '保存成功',
    //           icon: 'none',
    //           duration: 2000
    //         });
    //       },
    //       fail: function (err) {
    //         if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
    //           wx.showToast({
    //             title: '获取权限失败,保存图片需要用户授权',
    //             icon: 'none',
    //             duration: 1000
    //           })
    //         }
    //       }
    //     })
    //   },
    //   fail: function (err) {
    //     console.log(err)
    //   }
    // })
  },
  preview(e) {
    let arr = this.data.poster;
    wx.previewImage({
      urls: arr,
      current: arr[this.data.swiperInd],
    });
  },
  getPoster() {
    wx.showLoading({
      title: '海报加载中',
    });
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.kart.Frontend.Store.Modules.Poster.Controllers.records.index"
    );
    let is_uid = this.data.uid ? this.data.uid : wx.getStorageSync('yz_uid');
    app._postNetWork({
      url: urlStr,
      data: {
        store_id: this.data.id,
        ranking: this.data.ranking,
        store_uid: is_uid
      },
      success: (resdata) => {
        var res = resdata.data;
        wx.hideLoading({
          success: (res) => {},
        });
        if (res.result == 1) {
          that.setData({
            poster: res.data,
            showShare: true
          });

        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });
        }
      },
    });
  },
  Accepts() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.kart.Frontend.Store.Modules.Accept.Controllers.Accepts.index"
    );
    let json = {
      amount: this.data.amount,
      pay_type: 55,
      accepts_uid: this.data.uid,
      store_id: this.data.id,
      client_type: 2
    };
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          let pay_data = res.data.config;
          var payParams = {
            'timeStamp': pay_data.timestamp,
            'nonceStr': pay_data.nonceStr,
            'package': pay_data.package,
            'signType': pay_data.signType,
            'paySign': pay_data.paySign
          };
          console.log('payParams::::', res.data);
          that.WXPay(payParams);
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });
        }
      },
    });
  },
  getMoreData() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.kart.Frontend.Account.Modules.Store.Controllers.Store.historyList"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        page: this.data.page
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            history: that.data.history.concat(res.data.data),
            current_page: res.data.current_page,
            last_page: res.data.last_page
          });
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });
        }
      },
    });
  },
  getinfo() {
    let that = this;
    let urlStr;
    let json;
    if(this.data.uid){
      urlStr = app.getNetAddresss(
        "plugin.kart.Frontend.Store.Modules.Ranking.Controllers.GradeHistory.index"
      );
      json ={
        store_id:this.data.id,
        store_uid:this.data.uid,
        page : this.data.page
      };
    }else{
      urlStr = app.getNetAddresss(
        "plugin.kart.Frontend.Account.Modules.Store.Controllers.Store.historyList"
      );
      json ={
        page : this.data.page
      };
    }
    
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          if(that.data.uid){
            that.setData({
              history:res.data
            });
          }else{
            that.setData({
              history: res.data.data,
              current_page: res.data.current_page,
              last_page: res.data.last_page
            });
          }
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });
        }
      },
    });
  },
  getData() {
    wx.showLoading({
      title: "加载中",
    });
    let that = this;
    let urlStr;
    let json;
    if (this.data.uid) {
      console.log('获取指定');
      urlStr = app.getNetAddresss("plugin.kart.Frontend.Store.Modules.KartUser.Controllers.KartUser.index");
      json = {
        store_id: this.data.id,
        store_uid: this.data.uid
      };
    } else {
      urlStr = app.getNetAddresss("plugin.kart.Frontend.Account.Modules.Store.Controllers.Store.index");
      json = {
        store_id: this.data.id,
      };
    }
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data;
        wx.hideLoading({
          success: (res) => {},
        });
        if (res.result == 1) {
          that.setData({
            memberInfo: res.data,
            title: res.data.title
          });
          if (res.data.title) {
            wx.setNavigationBarTitle({
              title: res.data.title,
            });
          }
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  WXPay: function (payParams, url) {
    let that = this;
    wx.requestPayment({
      'timeStamp': payParams.timeStamp,
      'nonceStr': payParams.nonceStr,
      'package': payParams.package,
      'signType': payParams.signType ? payParams.signType : 'MD5',
      'paySign': payParams.paySign,
      'success': function (res) {
        console.log(res);
        wx.showToast({
          title: '打赏成功',
          icon: 'none',
          duration: 1000
        });
        that.setData({
          reward: false
        });
      },
      'fail': function (res) {
        wx.showToast({
          title: '支付失败请稍后重试',
          icon: 'none',
          duration: 2000,
          success: function () {},
          fail: function () {}
        });
      }
    });
  },
});