const util = require("../../../utils/util.js");
const app = getApp();
Page({
  data: {
    showMore: false,
    showGoodsRecom: false,
    orderid: "",
    jump: true,
    alone: 0, //0单独报名,1拼团
    info: {},
    imgUrl: util.config.imgPath,
    presellStatus: '',
    presellOpen: '',
    btype: 0,
    iconView: false,
    link: '',
    tradeChannelWeixin: '',
    payStatus: '报名成功',
    invitefbtn: true
  },
  onLoad(options) {
    console.log('success', options)
    if (util.isEmpty(options.orderid)) {
      util.showTips('缺少订单号。');
      return false;
    }
    if (!util.isObjEmpty(options)) {
      this.setData({
        orderid: options.orderid,
        alone: options.alone || 0,
        presellOpen: options.presellOpen ? options.presellOpen : '',
        presellStatus: options.presellStatus ? options.presellStatus : '',
        btype: options.btype,
        tradeChannelWeixin: options.tradeChannelWeixin
      });
    }
    if (this.data.tradeChannelWeixin == 'CMBC') {
      setTimeout(() => {
        this.getBlankInfo()
      }, 300);
    } else {
      this.getInfo();
    }
  },
  // 页面返回时，跳转至活动详情页
  onUnload() {
    // if(this.data.jump){
    //   let url = this.data.info.atype == '1' ? `/pages/product/goods/detail?aid=${this.data.info.aid}` : `/pages/product/activity/index?aid=${this.data.info.aid}`
    //   wx.reLaunch({
    //     url: url
    //   })
    // }
  },
  jumpToIns() {
    wx.navigateTo({
      url: '/pages/product/alternateInstract/index',
    })
  },
  jumpToHome(e) {
    let url = e.currentTarget.dataset.url;
    this.setData({
      jump: false
    })
    wx.reLaunch({
      url: url,
    })
  },
  showMoreInfo() {
    this.setData({
      showMore: true
    })
  },
  // 判定支付银行   paySource 1 ——微信支付  0 ——支付宝
  getBlankInfo() {
    let that = this;
    util.ajax({
      func: "v2/pay/result",
      data: {
        "orderid": that.data.orderid,
        paySource: 1
      }
    }, function (res) {
      if (res.code == 0) {
        that.getInfo()
      } else {
        that.setData({
          payStatus: res.reason
        })
      }
    })
  },
  getInfo: function () {
    var that = this,
      presellStatus = '';
    // 定义跳转订单详情页链接
    let link = '/pages/order/detail/index?orderid=' + this.data.orderid;
    // link = link+'&otype=' + (that.data.btype==0 || that.data.btype == 2)?2:1;
    if (that.data.btype == 0 || that.data.btype == 2 || that.data.btype == 3) {
      link = link + '&otype=2'
    } else {
      link = link + '&otype=1'
    }
    if (that.data.presellOpen != '' && that.data.presellStatus == '') {
      presellStatus = that.data.presellOpen != 0 ? 1 : 0;
      link = link + '&presellStatus=' + presellStatus;

    } else if (that.data.presellStatus != '' && that.data.presellOpen == '') {
      presellStatus = (that.data.presellStatus == 1 || that.data.presellStatus == 2) ? 2 : 0;
      link = link + '&presellStatus=' + presellStatus;
    }
    that.setData({
      link
    })
    util.ajax({
      func: "v2/order/detail",
      data: {
        "orderid": that.data.orderid,
        "otype": that.data.presellOpen != '' ? (that.data.presellOpen == 0 ? 0 : 1) : 2,
        presellStatus: presellStatus,
      }
    }, function (res) {
      if (res.code == 0) {
        // 尾款时间
        res.data.startTime = util.formatDate(util.formatUnixtimestamp(res.data.saleTime), 'yyyy-MM-dd hh:mm:ss', false);
        res.data.endTime = util.formatDate(util.formatUnixtimestamp(res.data.presellPaymentDeadline), 'yyyy-MM-dd hh:mm:ss', false);
        that.setData({
          info: res.data
        });
        that.getGoodsRecommend();
        if (res.data.groupinfo && res.data.groupinfo.discount) { //拼团购买，生成团号
          that.group();
        }
        let m = 0,
          length = res.data.groupinfo.discount ? res.data.groupinfo.discount.users.length : 0;
        if (length > 0) {
          res.data.groupinfo.discount.users.forEach(el => {
            m = el.status == 4 ? m++ : m;
            if (el.status == 4) {
              m++;
            }
          });
          that.setData({
            iconView: m == 0 ? false : true
          })
        }
      } else {
        util.showTips(res.reason);
      }
    });
  },
  getGoodsRecommend() {
    let that = this;
    util.getGoodsRecommend(this.data.info.aid).then(res => {
      that.setData({
        showGoodsRecom: res
      })
    })
  },
  group() { //生成拼团
    var that = this;
    util.ajax({
      func: "user/groupbook2",
      data: {
        "orderid": that.data.orderid,
        "aid": that.data.info.aid
      },
      load: false
    }, function (res) {
      if (res.code == 0) {
        that.data.info.groupinfo = res.data;
        if (that.data.info.groupinfo && that.data.info.groupinfo.discount) {
          var num = that.data.info.groupinfo.discount.next - that.data.info.groupinfo.discount.total;
          for (var i = 0; i < num; i++) {
            that.data.info.groupinfo.discount.users.push({
              isHead: true
            });
          }
          if (that.data.info.groupinfo.discount.deadline) {
            that.data.info.groupinfo.discount.deadline = that.data.info.groupinfo.discount.deadline - new Date().getTime();
          }
          if ((that.data.info.groupinfo.gstatus == 0 || that.data.info.groupinfo.gstatus == 2) && (that.data.info.groupinfo.discount.status == 0 || that.data.info.groupinfo.discount.status == 4) && that.data.info.groupinfo.discount.deadline > 0) {
            that.updateTime();
          }
        }

        that.setData({
          info: that.data.info
        });
      }
    })
  },
  updateTime() {
    let self = this,
      data = this.data.info,
      lag = data.groupinfo.discount.deadline / 1000;
    data.updateTime = {};
    data.updateTime.second = Math.floor(lag % 60);
    data.updateTime.minite = Math.floor((lag / 60) % 60);
    data.updateTime.hour = Math.floor(lag / 3600);
    data.updateTime.day = Math.floor((lag / 3600) / 24);

    data.updateTime.hour = isTimeLeng(data.updateTime.hour);
    data.updateTime.minite = isTimeLeng(data.updateTime.minite);
    data.updateTime.second = isTimeLeng(data.updateTime.second);
    data.groupinfo.discount.deadline = data.groupinfo.discount.deadline - 1000;
    self.setData({
      info: data
    });
    clearTimeout(updateTimer);
    let updateTimer = setTimeout(function () {
      self.updateTime();
    }, 1000);
    // 3570647
    function isTimeLeng(val) {
      var s = val.toString();
      return s.length == 1 ? "0" + val : val;
    }
  },
  invite(e) {
    this.setData({
      make: true,
      invitefbtn: e.currentTarget.dataset.invitefbtn == '0' ? true : false
    });
  },
  onShareAppMessage(res) {
    let info = this.data.info,
      data = [],
      title = "";
    data.push('aid=' + info.aid)
    data.push('invite=' + app.globalData.userInfo.code)
    data.push('sharesid=' + info.sid)
    data.push('sharepid=' + info.pid)
    data.push('sharer=' + app.globalData.userInfo.nickname)
    if (!util.isEmpty(info.groupinfo.groupid) && info.groupinfo.groupid != 'null' && info.groupinfo.groupid != 'undefined' && info.groupinfo.groupbook == 1 && info.subOrders[0].status != 15) {
      data.push('groupid=' + info.groupinfo.groupid)
    }
    if (!util.isEmpty(info.sid) && !util.isEmpty(info.pid) && util.isEmpty(info.groupinfo.groupid)) {
      title = app.globalData.userInfo.nickname + '已报名并邀请你一起参与' + info.title
    } else {
      title = (info.groupinfo ? '约吗？快来和我家孩子一起拼团挑战' : '') + info.title;
    }
    return {
      title: title,
      path: info.groupinfo ? ('/pages/product/activity/index?' + data.join('&')) : '/pages/home/index',
      imageUrl: info.groupinfo ? '' : '../images/share.png'
    }
  }
})