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

const app = getApp();
var isPaying = false;
var paytype=3;//3是单买 4是套餐 5是礼品卡
var buytype = 110;//110是6个月 112是12个月，111是永久
var detail="";
var selectedCouponID=0;

Page({
  data: {
    CouponList: [],
  },
  onLoad: function (options) {
    var id = options.id;
    paytype = options.paytype;
    buytype = 110;
    var result, price = 0;
    var programList = server.getProgramList();
    var arrProduct=wx.getStorageSync("ProductPrice");
    for (var j = 0; j < arrProduct.length; j++) {
      if (id==arrProduct[j].ProductNum){
        for (var k = 0; k < arrProduct[j].Period.length; k++) {
          if (arrProduct[j].Period[k].Select) {
            price = arrProduct[j].Period[k].Price;
            detail = arrProduct[j].Period[k].Detail;
          }
        }
        result = arrProduct[j];
      }
    }

    this.setData({
      Containnerheight: common.getSystemHeight(),
      Product: result,
      Price: price,
      IsDiscount: false,
      CouponName:"未选择",

    });
  },
  selectPeriod: function (e) {
    var id = e.currentTarget.dataset.id;
    var list = this.data.Product.Period;
    var price = 0;
    for (var i = 0; i < list.length; i++) {
      if (i == id) {
        list[i].Select = true;
        price = list[i].Price;
        if (this.data.Coupon){
          price=price-Number(this.data.Coupon.Price);
        }
        
        price = common.FormatMoney(price*100);

        detail = list[i].Detail;
        if (i==0)
          buytype=110;
        else if (i == 1)
          buytype = 112;
        else if (i == 2)
          buytype = 111;
      }
      else
        list[i].Select = false;
    }
    this.data.Product.Period = list;
    this.setData({
      Product: this.data.Product,
      Price: price,
    });
  },
  showDiscount: function () {
    this.GetUserCouponListByUserID();
  },
  closeDiscount: function () {
    this.setData({
      IsDiscount: false,
    });
  },
  GetUserCouponListByUserID: function (callback) {
    var that = this;
    that.setData({
      IsDiscount: true,
    });
    wx.showLoading({
      title: '请稍后',
    });
    server.getData('GetUserCouponListByUserID?ProductID='+app.globalData.ProgramID+'&UserID=' + app.globalData.userInfo.UserID, function (data) {
        if (data) {
          that.setData({
            CouponList:data,
          });
        }
        wx.hideLoading();
    });
  },
  selectCoupon:function(e){
    selectedCouponID=e.currentTarget.dataset.id;
    var list = this.data.CouponList;
    for(var i=0;i<list.length;i++){
      if (selectedCouponID==list[i].ID){
        var price = Number(this.data.Price) - Number(list[i].Price);
        if (this.data.Coupon)
          price+=Number(this.data.Coupon.Price);

        price = common.FormatMoney(price * 100);

        this.setData({
          IsDiscount: false,
          Coupon: list[i],
          Price: price,
        });
        wx.pageScrollTo({
          scrollTop: 2000,
        });
        break;
      }
    }
  },
  gotoFeedback: function () {
    server.gotoFeedback();
  },
  getPay: function (e) {
    if (!isPaying) {
      isPaying = true;
      var that = this;
      var money = that.data.Price;
      if (that.data.Coupon)
        detail += "_" + that.data.Coupon.ID+":"+Number(that.data.Coupon.Price)*100;
      server.payMoney(paytype, buytype, money, detail, function () {
        wx.redirectTo({
          url: './payfinish?paytype=' + paytype,
        });
      });
      setTimeout(function () {
        isPaying = false;
      }, 3000);
    }
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})