const util = require('../../../utils/util.js');
const app = getApp();
Page({
  data: {
    img: util.config.imgPath,
    queryData: {
      otype: 0,
      pageIndex: 1,
      pageSize: 20
    },
    stat: {},
    list: [],
    loadMore: true,
    modalInfo: {
      show: false,
      aid: '',
      orderid: ''
    },
    // ——inviteFbtn 是否显示邀请海报按钮
  },
  onLoad(options) {
    console.log(options)
    this.data.queryData.otype = options.otype || 0;
    this.setData({
      queryData: this.data.queryData
    });
  },
  onShow() {
    this.stat();
    this.getList(false);
  },
  setTabbar(e) {
    let index = e.currentTarget.dataset.index;
    this.data.queryData.otype = index;
    this.data.queryData.pageIndex = 1;
    this.setData({
      queryData: this.data.queryData,
      list: []
    });
    this.getList(true);
  },
  stat() {
    let that = this;
    util.ajax({
      func: 'v2/user/stat',
      load: false
    }, data => {
      if (data.code == 0) {
        that.setData({
          stat: data.data
        });
      }
    });
  },
  getList(load) {
    let that = this;
    util.ajax({
      func: "v2/order/list",
      data: that.data.queryData,
      load: load
    }, function (res) {
      if (res.code == 0) {
        let newData = res.data.list || [];
        if (newData.length > 0) {
          newData.forEach(el => {
            if (el.presellPaymentDeadline && el.presellPaymentDeadline > 0) {
              el.failureTime = util.formatDate(util.formatUnixtimestamp(el.presellPaymentDeadline), 'MM月dd日 hh:mm', false);
            }
            el.timeContrast = util.timeContrast(el.stopBuy);
          });
        }
        let loadMore = true,
          oldData = !load ? newData : [...that.data.list, ...newData];
        if (newData.length < that.data.queryData.pageSize && oldData.length > that.data.queryData.pageSize) loadMore = false;
        that.setData({
          list: oldData,
          loadMore
        });
      } else
        util.showTips(res.reason);
    });
  },
  cancelOrder(e) {
    let that = this,
      orderid = e.currentTarget.dataset.orderid;
    util.ajax({
      func: "order/cancel",
      data: {
        orderid
      }
    }, function (res) {
      if (res.code == 0) {
        that.data.queryData.pageIndex = 1;
        that.setData({
          queryData: that.data.queryData
        });
        that.getList(false);
      } else {
        util.showTips(res.reason);
      }
    })
  },
  cancelRefund(e) {
    let that = this,
      orderid = e.currentTarget.dataset.orderid;
    util.ajax({
      func: "order/cancel_refund",
      data: {
        orderid,
        password: ''
      }
    }, function (res) {
      if (res.code == 0) {
        that.data.queryData.pageIndex = 1;
        that.setData({
          queryData: that.data.queryData
        });
        that.getList(false);
      } else {
        util.showTips(res.reason);
      }
    })
  },
  comment(e) {
    let orderid = e.currentTarget.dataset.orderid;
    wx.navigateTo({
      url: '/pages/order/comment/comment?orderid=' + orderid + '&otype=' + this.data.queryData.otype,
    })
  },
  // 跳转至表单详情页（支付尾款）
  jumpBalancePayment(e) {
    console.log(e)
    let data = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/pages/product/balancePayment/balancePayment?aid=' + data.aid + '&otype=' + data.otype + '&orderid=' + data.orderid + '&presellstatus=' + data.presellstatus + '&campsex=' + data.campsex + '&military=' + data.military,
    })
  },
  // 立即支付（支付尾款） --待付款状态
  pay(e) {
    let that = this;
    console.log(e)
    let presellstatus = e.currentTarget.dataset.presellstatus;
    util.WXPay({
      orderid: e.target.dataset.orderid,
      btype: presellstatus == 2 ? 0 : presellstatus
    }, that.data.aid, that.data.title).then(function () {
      that.getList(false);
    }, function (res) {
      if (res.code != 0) util.showTips(res.reason);
    });
  },
  errorImg(e) {
    this.data.list[e.currentTarget.dataset.index].logo = "/images/noimg.png";
    this.setData({
      list: this.data.list
    });
  },
  onPullDownRefresh() {
    var self = this;
    wx.showNavigationBarLoading();
    setTimeout(function () {
      self.data.queryData.pageIndex = 1;
      self.setData({
        queryData: self.data.queryData
      });
      self.getList(false);
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    }, 1000);
  },
  onReachBottom() {
    var self = this;
    if (self.data.loadMore == false) return false;

    setTimeout(function () {
      self.data.queryData.pageIndex = self.data.queryData.pageIndex + 1;
      self.setData({
        queryData: self.data.queryData
      });
      self.getList(true);
    }, 1000);
  },
  // 邀请拼团
  inviteGroup(ev) {
    console.log(ev)
    let data = ev.currentTarget.dataset.info;
    let obj = {
      show: true,
      aid: data.aid,
      orderid: data.orderid,
      sid: data.sid,
      pid: data.pid,
      logo: data.logo,
      title: data.atitle,
      status: data.status,
      invite: app.globalData.userInfo.code,
      from: 'button',
      sharer: app.globalData.userInfo.nickname,
      groupbook: data.groupbook,
      invitefbtn: ev.currentTarget.dataset.invitefbtn == '0' ? true : false
    }
    if (!util.isEmpty(data.groupid) && data.groupid != 'null' && data.groupid != 'undefined' && data.status != 15) {
      obj.groupid = data.groupid
    }
    this.setData({
      modalInfo: obj
    })
  },
  onShareAppMessage() {
    console.log(this.data.modalInfo)
    let title = '个人订单信息',
      url = '/pages/index/order',
      logo = "../images/share.png";
    if (this.data.modalInfo.from === 'button') {
      let data = this.data.modalInfo,
        str = [];
      if (!util.isEmpty(data.sid) && !util.isEmpty(data.pid) && util.isEmpty(data.groupid)) {
        title = data.sharer + '已报名并邀请你一起参与' + data.title;
      } else {
        title = "约吗？快来和我家孩子一起拼团挑战" + data.title;
      }
      str.push('aid=' + data.aid)
      str.push('orderid=' + data.orderid)
      str.push('invite=' + data.invite)
      str.push('sharesid=' + data.sid)
      str.push('sharepid=' + data.pid)
      str.push('sharer=' + data.sharer)
      if (!util.isEmpty(data.groupid) && data.groupid != 'null' && data.groupid != 'undefined' && data.groupbook == 1) {
        str.push('groupid=' + data.groupid)
      }
      url = "/pages/product/activity/index?" + str.join('&');
      logo = data.logo.indexOf("noimg.png") == -1 ? data.logo + '@!logo' : logo;
    }
    console.log(url)
    return {
      title: title,
      path: url,
      imageUrl: logo
    }
  },
})