let config = require("../../../config/config.js");
let util = require("../../../utils/util");
let Http = require("../../../utils/HttpBasics");
let app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showModalStatus: false,
    flag: 0,
    order: null,
    orderId: null,
    //存储计时器
    setInter: "",
    mystatus: ''
  },


  powerDrawer: function (e) {
    let that = this;
    // couponOrderStatus
    // 0 未使用
    // 1 已使用
    // 2 已过期
    // 3 已经退款
    console.log(e);
    if (that.data.mystatus == "" || that.data.mystatus == undefined) {
      wx.navigateTo({
        url: `/pages/orderquanma/index?quancode=${
          e.currentTarget.dataset.quancode
        }&title=${e.currentTarget.dataset.title}&subtitle=${
          e.currentTarget.dataset.subtitle
        }&remark=${e.currentTarget.dataset.remark}&couponorderstatus=${
          e.currentTarget.dataset.couponorderstatus
          }&sight=${that.data.sight}`
      });
      console.log(e.currentTarget.dataset.couponorderstatus);
    } else {
      wx.navigateTo({
        url: `/pages/orderquanma/index?quancode=${
          e.currentTarget.dataset.quancode
        }&title=${e.currentTarget.dataset.title}&subtitle=${
          e.currentTarget.dataset.subtitle
        }&remark=${e.currentTarget.dataset.remark}&couponorderstatus=${
          that.data.mystatus
        }&sight=${that.data.sight}`
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    this.setData({
      orderId: options.orderId
    });
    wx.showLoading({
      title: "加载中"
    });
    setTimeout(function () {
      wx.hideLoading();
    }, 1500);
    // that.data.setInter = setInterval(function() {

    Http.get({
      url: config.api.orderDetail,
      data: {
        orderId: options.orderId
      }
    }).then(res => {
      console.log(res);
      console.log("我是订单详情");
      that.setData({
        order: res.data
      });

      //createDate 创建时间
      var createDate = util.formatTime(res.data.createDate, "yyyy-MM-dd hh:mm:ss");
      console.log(createDate);
      that.setData({
        createDate: createDate
      });
    });

    // }, 100);
  },

  /**
   * 发起支付
   */
  orderFunc(e) {
    var that = this;
    wx.showLoading({
      title: "加载中..."
    });

    const orderId = "" + that.data.orderId;
    if (that.data.order.payment > 0) {
      // 支付金额不为0
      Http.post({
        url: config.api.payOrderCreate,
        data: {
          orderId: orderId
        }
      }).then(res => {
        console.log(res);
        /// Begin payment ----
        var payOrderId = "" + res.data.payOrderId;
        wx.hideLoading();
        wx.requestPayment({
          timeStamp: res.data.timeStamp,
          nonceStr: res.data.nonceStr,
          package: res.data.package,
          signType: (res.data.signType) ? res.data.signType : "MD5",
          paySign: res.data.paySign,
          success: res => {
            console.log(res);
            that.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功
            console.log(res);
            if (res.errMsg == "requestPayment:ok") {
              wx.showToast({
                title: "购买成功",
                duration: 2000,
                image: "./../../../assets/img/success.png",
                mask: false,
                success: function () {
                  wx.showLoading({
                    title: "加载中..."
                  });

                  setTimeout(function () {
                    wx.hideLoading();
                  }, 1600);
                  setTimeout(() => {
                    wx.navigateTo({
                      url: `/pages/order/detail/index?orderId=${
                        that.data.orderId
                      }`
                    });
                  }, 1600);
                }
              });
            }
          },
          fail: res => {
            that.payOrderUpdate(that.data.orderId, payOrderId, 2); 
            console.log(res);
            // wx.showToast({
            //   title: "支付失败",
            //   image: "./../../../assets/img/fail.png",
            //   duration: 2000,
            //   mask: false
            // });
            return;
          },
          // complete: res => {
          //   console.log(res);
          //   if (res.errMsg == "requestPayment:ok") {} else {
          //     wx.showToast({
          //       title: "支付失败",
          //       image: "./../../../assets/img/fail.png",
          //       duration: 2000,
          //       mask: false
          //     });
          //   }
          //   return;
          // }
        });
        /// End payment  --------
      });
    } else {
      // 免费券
      that.payOrderUpdate(orderId, "0", 1) // 支付成功
      wx.showToast({
        title: "支付成功",
        duration: 2000,
        image: "./../../../assets/img/success.png",
      });
    }
  },
  /**
   * 支付订单更新
   */
  payOrderUpdate: (orderId, payOrderId, status, reason) => {
    // 支付成功
    Http.post({
        url: config.api.payOrderUpdate,
        data: {
          payOrderId: payOrderId,
          orderId: orderId,
          status: status,
          reason: reason
        }
      })
      .then(res => {
        console.log("payOrderUpdate then", res);
        // wx.showToast({
        //   title: "购买成功",
        //   duration: 2500
        // });
      })
      .catch(err => {
        console.log("payOrderUpdate catch", err);
      });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onShow: function () {
    let that = this;
    wx.getScreenBrightness({
      success: function (res) {
        that.setData({
          sight: res.value
        })
      }
    });
  },

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

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
});