var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');

Page({
  data: {
    orderId: 0,
    orderInfo: {},
    orderGoods: [],
    handleOption: {},
    wuliu: {},
    currentStoreId: '',
    tabIndex: 0,
    refundInfo: {},
    createTime: '',
    refundTime:'',
    isRefundStatus: '',//true :申请中； false ：已退款
    refundStatus:'',
    approvalRemark:'',
    orderSn: ''
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    let that = this;
    that.setData({
      orderId: options.id,
      tabIndex: options.tabIndex,
      isRefundStatus: options.isRefundStatus,
      refundStatus: options.refundStatus,
      approvalRemark: options.approvalRemark
    });
  },
  copyBtn: function (e) { 
    var self = this;
    wx.setClipboardData({
      data: this.data.orderSn,
      success: function (res) {
        wx.showToast({
          title: '复制成功',
        });
      }
    });
  },
  getOrderDetail() {
    let that = this;
    util.request(api.OrderDetail, {
      orderId: that.data.orderId
    }).then(function (res) {
      if (res.errno === 0) {
        // console.log(res.data);
        that.setData({
          orderInfo: res.data.orderInfo,
          orderSn: res.data.orderInfo.order_sn,
          orderGoods: res.data.orderGoods,
          handleOption: res.data.handleOption,
          wuliu: res.data.wuliu
        });
        if (that.data.orderInfo.order_status == 401 || that.data.orderInfo.order_status == 402) {
          that.setData({
            refundInfo: res.data.refundInfo,
            createTime: util.tsFormatTime(res.data.refundInfo.createTime, 'Y-M-D h:m:s'),
            refundTime: util.tsFormatTime(res.data.refundInfo.refundTime, 'Y-M-D h:m:s')
          });
        }
        if (that.data.orderInfo.order_status == 0) {
          wx.setNavigationBarTitle({ title: "待支付" });
        }
        //待付款倒计时
        if (that.data.orderInfo.pay_status == 0||that.data.orderInfo.pay_status == 1){
          util.countdown(that, res.data.orderInfo, 'orderInfo', null)
        }
      }
    });
  },
  payOrder() {
    let that = this;
    // console.log(that.data.orderInfo.orderBizType)
    if (that.data.orderInfo.orderBizType == '10') {
      //校验获取用户id上次活体人脸核身时间是否在可调用范围内
      util.request(api.CheckFaceTimeByUserId, {
        userId: wx.getStorageSync('userId'),
        storeId: wx.getStorageSync('storeId')
      }, 'POST').then(function (checkRes) {
        if (checkRes.errno === 0) {
          //根据用户上次活体人脸核身时间判断出，该用户可以调起人脸接口
          if (checkRes.data === true) {
            //根据订单号获取是否认证成功，认证成功则调起支付，否则提示
            that.checkFace();
          } else {
            //调起支付
            wx.redirectTo({
              url: '/pages/pay/pay?orderIds=' + that.data.orderId
                + '&actualPrice=' + that.data.orderInfo.actual_price + '&isMergePay=0&orderBizType10=true'
            })
          }
        }
      });
    } else {
      wx.redirectTo({
        url: '/pages/pay/pay?orderIds=' + that.data.orderId
          + '&actualPrice=' + that.data.orderInfo.actual_price + '&isMergePay=0'
      })
    }
  },
  checkFace(){
    var that = this;
    util.request(api.GetIsFaceCheckByOrderId, {
      orderIds: that.data.orderId
    }).then(function (res) {
      if (res.errno === 0) {
        if (res.data.isFaceCheck === '1') {
          wx.redirectTo({
            url: '/pages/pay/pay?orderIds=' + that.data.orderId
              + '&actualPrice=' + that.data.orderInfo.actual_price + '&isMergePay=0&orderBizType10=true'
          })
        } else {
          wx.showToast({
            title: '现场速递商品购买需先认证身份信息再进行支付',
            icon: 'none',
            duration: 1500
          })
        }
      }
    });
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
    this.getOrderDetail();
    this.setData({
      currentStoreId: wx.getStorageSync('storeId')
    });
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  postComment() {
    let that = this;
    wx.navigateTo({
      url: '/pages/commentPost/commentPost?typeId=0'+ '&orderId='+that.data.orderId,
    })
  },
  lookComment() {
    let that = this;
    wx.navigateTo({
      url: '/pages/comment/comment?typeId=0' + '&orderId=' + that.data.orderId,
    })
  },
  cancelOrder() {
    let that = this;
    util.request(api.OrderCancel, {
      orderId: that.data.orderId
    }).then(function (res) {
      if (res.errno === 0) {
        wx.showToast({
          title: '取消成功',
          duration: 1500,
          success: function () {
            setTimeout(function () {
              wx.navigateBack({
                delta: 1
              });
            },1500);
          }
        })
      }else {
        wx.showToast({
          title: res.errmsg,
          duration: 1500
        })
      }
    });
  },
  confirmOrder() {
    let that = this;
    util.request(api.OrderConfirm, {
      orderId: that.data.orderId
    }).then(function (res) {
      if (res.errno === 0) {
        wx.showToast({
          title: '订单完成',
          duration: 2000,
          complete: function () {
            wx.redirectTo({
              url: '/pages/ucenter/order/order',
            });
          }
        })
      }
    });
  },
  refund(){
    let that = this;
    util.request(api.refund, {
      orderId: that.data.orderId
    }).then(function (res) {
      if (res.errno === 0) {
        wx.showToast({
          title: '成功退款',
          duration: 2000,
          complete: function () {
            wx.redirectTo({
              url: '/pages/ucenter/order/order',
            });
          }
        })
      }else{
        wx.showModal({
          title: '',
          confirmColor: '#b4282d',
          content: res.errmsg,
          showCancel:false
        })
      }
    });
  }
})