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:''
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    let that = this;
    that.setData({
      orderId: options.id,
      tabIndex: options.tabIndex,
      isRefundStatus: options.isRefundStatus,
      refundStatus: options.refundStatus,
      approvalRemark: options.approvalRemark
    });
  },
  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,
          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;
    wx.redirectTo({
      url: '/pages/pay/pay?orderId=' + that.data.orderId
      + '&actualPrice=' + that.data.orderInfo.actual_price
    })
  },
  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
        })
      }
    });
  }
})