import common from '../../utils/util';
import main from '../../utils/main';

var app = getApp();
var userid = 0;

Page({
  data: {
    PayPanel:0,
  },
  onPullDownRefresh: function () {
    this.gotoIndex();
    wx.stopPullDownRefresh();
  },
  onLoad: function (options) {
    var that = this;
    that.setData({
      Containnerheight: main.getWindowHeight(),
    });

    userid = options.UserID;

    var PayType = options.PayType;
    if (PayType == "6") {//自定义开具
      if (options.PayUserID != app.globalData.userInfo.UserID) {
        that.setData({
          PayPanel: 2,
        });
      }
      else {
        var EndDate = common.formatTime(common.addDate("y",1,new Date()));
        if (options.DayNumber)
          EndDate = common.formatTime(new Date());

        if (options.EndDate)
          EndDate=options.EndDate;
        this.init(PayType, EndDate, options.Price, options.DayNumber);
      }
    }
    else {
      var EndDate = options.EndDate;
      this.init(PayType, EndDate);
    }

  },
  init: function (PayType, EndDate, Price, DayNumber) {
    var that = this;
    var url = 'GetMiaoguoPayInfo3?EndDate=' + EndDate + '&PayType=' + PayType + '&UserID=' + app.globalData.userInfo.UserID;
    if (Price && DayNumber) {
      url += "&Price=" + Price + "&DayNumber=" + DayNumber;
    }

    main.getData(url, function (data) {
      if (data) {
        that.setData({
          Info: data,
          PayType:PayType
        });
      }
    });
  },
  payMoney: function () {
    var that = this;
    var money = this.data.Info.PayMoney;
    var detail = {};
    detail.UserID = app.globalData.userInfo.UserID;
    detail.Introducer = app.globalData.userInfo.Introducer;
    for (var i = 0; i < that.data.Info.List1.length; i++) {
      if (that.data.Info.List1[i].Name == "有效期") {
        detail.EndTime = common.formatDateENG(that.data.Info.List1[i].Content, "/");
      }
    }
    if (that.data.PayType)
      detail.PayType=that.data.PayType;

    detail = JSON.stringify(detail);

    //console.log(detail);
    //console.log(money);
    var remark = null;
    if (that.data.Options && that.data.Options.Remark)
      remark = that.data.Options.Remark;
    main.payMoney(7, remark, money, detail, function () {
      that.setData({
        PayPanel: 1,
      });
    });
  },
  gotoIndex: function () {
    wx.reLaunch({
      url: '../index/index',
    })
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})