// pages/purchased-content/index.js
const { request, serverName } = require('../../../utils/services');
const { defaultImg, noExhibitionImg } = require('../../../utils/images');

const { Toast } = require('../../../utils/util.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabs: [{
      name: '全部', tag: ""
    }, {
      name: '待付款', tag: "0"
    }, {
      name: '待使用', tag: "-1"
    }, {
      name: '待评论', tag: "-1"
    }],
    myOrders: [],
    // testImg: '../../../imgs/testImg/thumbSmallImg2.jpg',
    avatar: '../../../imgs/testImg/1.jpg',
    testImg: '../../../imgs/testImg/fdkz.png',

    activeIndex: 0,
    sliderOffset: 0,
    currentPage: 0,
    loading: false,
    loadMore: false,
    paymentStatus: ""
  },

  tabClick: function (e) {
      this.setData({
        myOrders: [],
        paymentStatus: e.currentTarget.dataset.status || "",
        activeIndex: e.currentTarget.id,
      });
      if (e.currentTarget.dataset.status != '-1') {
        this.getOrders(1);
      }      
      console.log(this.data.paymentStatus)
    
  },

  _clickItem: function (e) {
    let { paystatus, exhibitionid, ordersn } = e.currentTarget.dataset;
    console.log(exhibitionid)
    if (paystatus == '已付款' || paystatus=='已取消') {
      wx.navigateTo({
        url: `../../yuezhan/pay_page/index?id=${exhibitionid}`,
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    } else if (paystatus == '未付款') {
      this.doWeChatPay(ordersn);
    }
    else {

    }
  },

  doWeChatPay: function (orderSn) {
    let loginSessionKey = wx.getStorageSync("token");

    request['getPayParams']({
      orderSn,
      loginSessionKey,
    }, "get", res => {
      const { data: wechatPayModel } = res.data;
      const {
         timeStamp,
        nonceStr,
        signType,
        paySign,
        package: packageStr
        } = wechatPayModel;
      console.log(timeStamp,
        nonceStr,
        signType,
        paySign)

      // prettier-ignore
      wx.requestPayment({
        timeStamp,
        nonceStr,
        signType,
        paySign,
        'package': packageStr,
        success: res => {
          this.getOrders(1);
          console.log(res);
        },
        fail: err => {
          console.log(err);
          Toast.showToast2('warn', '支付取消');
        }
      });
    })


  },

  onLoad: function (options) {
    if (options.status && options.id) {
      console.log(options.status)
      let { status, id } = options;
      this.setData({
        paymentStatus: status,
        activeIndex: id,
        defaultImg
      })
    }
    this.setData({
      serverName,
      noExhibitionImg
    })
    let loginSessionKey = wx.getStorageSync("token") || "";
    
    if (options.status!='-1'){
      this.getOrders(1);

    }
  },

  loadMore: function () {
    if (!this.data.lastPage) {
      console.log(this.data.currentPage + 1)
      this.getOrders(this.data.currentPage + 1);
    } else {
      return;
    }
  },

  onReachBottom: function () {
    if (!this.data.loading) {
      this.loadMore();
      console.log('reach Bottom');
    }

  },

  getOrders: function (page) {
    let loginSessionKey = wx.getStorageSync("token") || "";
    let paymentStatus = this.data.paymentStatus
    request["getOrders"]({
      loginSessionKey,
      paymentStatus,
      pageNumber: page
    }, "", res => {
      if (res.data.code > -1) {
        let tempContent = this.data.myOrders
          ? this.data.myOrders
          : [];
        let { last: lastPage, totalPages, content: myOrders } = res.data.data;

        for (var i = 0; i < myOrders.length; i++) {
          if (myOrders[i].paymentStatus == 'paid') {
            myOrders[i].paymentStatus = '已付款'
          } else if (myOrders[i].paymentStatus == 'unpaid') {
            myOrders[i].paymentStatus = '未付款'
          }
          else {
            myOrders[i].paymentStatus = '已取消'
          }
        }
        // let { openTime } = myOrders;
        console.log(res)
        this.setData({
          currentPage: res.data.data.number + 1,
          lastPage,
          loading: false,
          myOrders: tempContent.concat(myOrders),
        });
        console.log(myOrders)
        wx.stopPullDownRefresh();
      }

    }, err => {

    },
      complete => {

      })
  },
});
