const navigationBarHeight = (getApp().statusBarHeight + 50) + 'px'
let config = require("../../../config/config.js");
let Http = require("../../../utils/HttpBasics");
const util = require("../../../utils/util");
let app = getApp();
const imgurl = require("../../../utils/imgurl");
Page({
  data: {
    showBox: false,//显示授权手机框
    navigationBarHeight,
    noOrdersUrl: imgurl.noOrders.url,
    loadingUrl: imgurl.loading.url,
    wmhome: imgurl.wmhome.url,
    wmdiscount: imgurl.wmdiscount.url,
    wmgive: imgurl.wmgive.url,
    boatFlag: "all",
    tabs: [{
      key: "all",
      name: "全部"
    },
    {
      key: 0,
      name: "待付款"
    },
    {
      key: 1,
      name: "已完成"
    },
    {
      key: 3,
      name: "售后"
    }
    ],
    list: [],
    current: "",
    loading: true, //"上拉加载"的变量，默认false，隐藏
    // current_scroll: "1",
    allow_load: true,
    page: 1,
    content: "",
    canIUse: tt.canIUse("button.open-type.getPhoneNumber"),//判断小程序是否能用获取手机插件
    ifshowtab:null,
  },

  setPhone(paramData) {//子组件调用这个方法说明手机号已经授权成功
    this.setData({
      showBox: paramData.detail,
    })
    this.getList('all', 1)
    tt.showToast({
      title: '授权成功', // 内容
      icon: 'none', // 图标
      success: (res) => {
      },
      fail: (res) => {

      },
    });
  },

  setBox(paramData) {
    this.setData({
      showBox: paramData.detail,
    })
    tt.showToast({
      title: '取消授权', // 内容
      icon: 'none', // 图标
      success: (res) => {

      },
      fail: (res) => {

      },
    });
  },

  goback: function () {
    tt.reLaunch({
      url: '/index/index',
    })
  },
  onLoad(e) {
    // this.setData({
    //   current_scroll: e.id
    // });
  },
  onShow() {
    
    Http.get({
      url: config.api.checkUserStatus,
      data: {
        token: app.globalData.token
      }
    }).then(res => {
      Http.get({
        url: config.api.checkPhoneStatus,

      }).then(res => {
      }).catch(err => {
        if (app.globalData.ifCongPh==1) {
          app.globalData.skip = 'navigateBack'
          tt.navigateTo({
            url: `/pages/getPhone/getPhone?skipUrl=1`,
          })
        }else{
          this.setData({
            showBox:true
          })
        }

      })
    }).catch(err => {

      if (err.code == 11004) {//未授权抖音
        app.globalData.type = 'mo'
        tt.navigateTo({
          url: '/pages/getuserinfo/getuserinfo',

        });
      }
    })


    this.setData({
      ifshowtab : app.globalData.ifshowtab,
      allow_load: true
    })
    this.getList(this.data.boatFlag, 1);
  },
  /**  
   * 砍价中的状态
   * 跳转到砍价详情页面
   */
  gotoDiscount: function (e) {
    tt.navigateTo({
      url: `/pages/bargain/bargainDatail/bargainDatail?orderId=${e.currentTarget.dataset.id}`
    })
  },
  cancel(e){//取消订单
    tt.showModal({
      title: '提示',
      content: '您是否要取消该订单',
      confirmText: "确定",
      success: (res) => {
          if(res.confirm){
            Http.post({
              url:config.api.orderCancel,
              data:{
                orderId: e.currentTarget.dataset.id
              }
            }).then(res=>{
              this.getList(this.data.boatFlag, 1);
              tt.showToast({
                title: "取消订单成功！",
                icon: 'none',
                duration: 2000,
                mask: false
              });
            }).catch(err=>{
              tt.showToast({
                title: err.message?err.message:err.data,
                icon: 'none',
                duration: 2000,
                mask: false
              });
            })

          } else if (res.cancel) {
            console.log("取消");
          }
      },
      fail(err) {
        console.log(`showModal 调用失败`, err);
      },
    });
  },
  gotopay: function (e) {
    let level = e.currentTarget.dataset.level
    if (level == 0) {
      tt.navigateTo({
        url: `/pages/order/detail/index?orderId=${e.currentTarget.dataset.id}&flag='pay'`
      })
    } else {
      tt.navigateTo({
        url: `/pages/order/detail/index?orderId=${e.currentTarget.dataset.id}&dingdan=order&flag='pay'`
      })
    }


  },
  //再次购买
  gotopayAgain: function (e) {
    tt.navigateTo({
      url: `/pages/coupon/detail/index?couponChannelId=${e.currentTarget.dataset.couponchannelid
        }&couponId=${e.currentTarget.dataset.couponid}`
    });
  },
  // gotoSpellPay(e) {
  //   console.log(e, 333)
  //   let data = e.currentTarget.dataset.data
  //   tt.navigateTo({
  //     url: `/pages/spellGroup/paySpellGroup/index?orderId=${data.orderId}&couponId=${data.couponId}&orderGroupId=${data.orderGroupId}&couponChannelId=${data.couponChannelId}`,
  //   })
  // },
  getList(key, pageNum) {
    let that = this;
    if (key == undefined) {
      key = 'all'
    }
    /**
     * key==0
     * 不发送该字段
     */
    if (key == 'all') {
      var variable = {
        pageNum: pageNum,
        pageSize: 10,
        paymentType: 0
      };
    } else {
      var variable = {
        pageNum: pageNum,
        pageSize: 10,
        orderStatus: key,
        paymentType: 0
      };
    }

    if (that.data.allow_load) {
      that.setData({
        loading: true,
        content: '小主,我在玩命加载中...'
      })

      Http.get({
        url: config.api.orderList,
        data: variable
      }).then(res => {
        if (pageNum >= res.data.pages) {
          that.setData({
            allow_load: false
          });
          setTimeout(function () {
            that.setData({
              loading: false,
            })
          }, 1400);
        }
        setTimeout(function () {
          that.setData({
            loading: false,
          })
        }, 1400);
        if (pageNum == 1) {
          that.setData({
            list: []
          })
        }
        var tmpArr = that.data.list;
        tmpArr.push.apply(tmpArr, res.data.list);
        // 将砍价的状态过滤出来
        console.log(tmpArr)
        tmpArr.map(item => {

          item.orders.map(itemChild => {
            itemChild.createDate = util.formatTime(itemChild.createDate, "yyyy-MM-dd hh:mm:ss");
          })
        })

        that.setData({
          list: tmpArr
        })
        for (let i = 0; i < that.data.list.length; i++) {
          var createDate1 = util.formatTime(that.data.list[i].createDate, "yyyy-MM-dd hh:mm:ss");
          /**
           * 修改list的endtime
           * 渲染到页面
           */
          var createDate = 'list[' + i + '].createDate'
          that.setData({
            [createDate]: createDate1
          });
        }
      })
        .catch(err => {
          tt.showModal({
            title: '提示',
            content: err.message ? err.message : err.data,
            showCancel: false
          })
        })
    }
    else {
      that.setData({
        loading: true,
        content: "——— 再拉裤子就掉了啦 ———"
      })
      setTimeout(function () {
        that.setData({
          loading: false,
        })
      }, 1400)
    }
  },
  handleChange({ 
    detail
  }) {
    this.setData({
      current: detail.key
    });
  },

  setBoatFlag(e) {
    if (app.globalData.ifCongPh==1) {
    let index = e.currentTarget.dataset.index
    console.log(index);
    this.setData({
      boatFlag: index,
      pageNum: 1,
      allow_load: true,
    })
    this.getList(index, 1)
    } else {
    this.setData({
      showBox: true
    })
    }

  },



  // handleChangeScroll({
  //   detail
  // }) {
  //   this.setData({
  //     list: [],
  //     allow_load: true,
  //     current_scroll: detail.key,
  //     page: 1
  //   })
  //   this.getList(detail.key, 1);
  // },
  onReachBottom: function () {
    var that = this;
    that.data.page++;
    that.setData({
      page: that.data.page
    });
    that.getList(that.data.boatFlag, that.data.page);

  },
});