const app = getApp()
const navigationBarHeight = (getApp().statusBarHeight + 50) + "px"
const Http = require("../../utils/HttpBasics");
let config = require("../../config/config.js");
const util = require("../../utils/util")
Page({
  data: {
    navigationBarHeight,
    ifPay:true,
    orderId:"",
    detailObj:{},
    createDate:"",
    mystatus: '',
  },
  updetail(){
    Http.get({
      url:config.api.orderDetail,
      data:{
        orderId: this.data.orderId
      }
    }).then(res=>{
      console.log(res);
      let createDate = util.formatTime(res.data.createDate, "yyyy-MM-dd hh:mm:ss");
      this.setData({
        detailObj:res.data,
        createDate: createDate
      })
    }).catch(err => {
      tt.showToast({
        title: err.message ? err.message : err.data, // 内容
        icon: "none"
      });
    })
  },
  /**
  * 支付订单更新
  */
 payOrderUpdate(orderId, payOrderId, status, reason, type, _this, composeOrderType) {
  let that = this;
  Http.post({
    url: config.api.payOrderUpdate,
    data: {
      payOrderId: payOrderId,
      composeOrderId: orderId,
      status: status,
      reason: reason
    }
  }).then(res => {
    tt.hideLoading();
    if(!type&&type!="free"){
      tt.showToast({
        title: "购买成功",
        duration: 2000,
        mask: false,
        success: function () {
          tt.showLoading({
            title: "加载中..."
          });
          setTimeout(function () {
            tt.hideLoading();
          }, 1600);
          setTimeout(() => {
            let url = ""
            if (_this.data.orderFlag){
              tt.redirectTo({
                url: `/pages/detailOrder/detailOrder?orderId=${orderId}&dingdan=order`
              });
            }else{
              tt.redirectTo({
                url: `/pages/detailOrder/detailOrder?orderId=${orderId}`
              });
            }
            
          }, 1600);
        }
      });
    } else if (type == 'free'){
      tt.redirectTo({
        url: `/pages/detailOrder/detailOrder?orderId=${orderId}`
      });
    }
  }).catch(err => {
    console.log(err);
    if (!type) {
      setTimeout(function () {
        _this.payOrderUpdate(orderId, payOrderId, status, reason, type, _this, composeOrderType);
      }, 2000)
    }
  })
},
powerDrawer: function (e) {
  let that = this;
  console.log(e)
  // couponOrderStatus
  // 0 未使用
  // 1 已使用
  // 2 已过期
  // 3 已经退款
  if (that.data.mystatus == "" || that.data.mystatus == undefined) {
    tt.navigateTo({
      url: `/pages/orderquanma/orderquanma?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}&validstatus=${e.currentTarget.dataset.validstatus}
        &contentType=${that.data.contentType}`
    });
  } else {
    tt.navigateTo({
      url: `/pages/orderquanma/orderquanma?quancode=${
        e.currentTarget.dataset.quancode
      }&title=${e.currentTarget.dataset.title}&subtitle=${
        e.currentTarget.dataset.subtitle
      }&remark=${e.currentTarget.dataset.remark}&couponorderstatus=${
        that.data.mystatus}&validstatus=${e.currentTarget.dataset.validstatus}
        &contentType=${that.data.contentType}`
    });
  }
},
  gotopay() {
    let that = this;
    Http.get({//判断是否授权抖音
      url: config.api.checkPhoneStatus,

    }).then(res => {
          Http.post({
            url: config.api.payOrderCreate,
            data: {
              orderId: that.data.detailObj.id,
              composeOrderType: that.data.detailObj.composeOrderType
            }
          }).then(payres => {
           
            tt.hideLoading();
            tt.pay({
              service: 5,
              orderInfo: {
                order_id: payres.data.orderId,
                order_token: payres.data.token,
              },
              success(suRes) {
                if(suRes.code==0){
                  tt.showLoading({
                    title: '订单正在处理中...',
                  })
                  that.payOrderUpdate(that.data.detailObj.id,payres.data.outOrderNo,1,"","",that,that.data.detailObj.composeOrderType)
                } else if (res.code === 1) {
                  tt.showToast({
                      title: '支付超时', // 内容
                      icon: "none"
                  });

              } else if (res.code === 2) {
                  tt.showToast({
                      title: '支付失败', // 内容
                      icon: "none"
                  });
              } else if (res.code === 3) {
                  tt.showToast({
                      title: '支付关闭', // 内容
                      icon: "none"
                  });
              } else if (res.code === 4) {
                  tt.showToast({
                      title: '支付取消', // 内容
                      icon: "none"
                  });
              } else if (res.code === 9) {
                  tt.showToast({
                      title: '订单状态开发者自行获取', // 内容
                      icon: "none"
                  });
              }
                
              },
              fail(res) {
                console.log(123);
                // handle fail
              },
            })
          }).catch(err => {
            tt.showToast({
              title: err.message ? err.message : err.data, // 内容
              icon: "none"
            });
          })
       
    })
    // .catch(err => {
    //   if (err.code == 11005) {
    //     this.setData({
    //       showPhoem: true
    //     })
    //   }
    // })
  },
  onLoad: function (options) {
    console.log(options.orderId);
    this.setData({
      orderId:options.orderId
    })
    this.updetail()
  }
})