const navigationBarHeight = (getApp().statusBarHeight + 44) + 'px'
var config = require("../../config/config.js");
const Http = require("../../utils/HttpBasics");
const imgurl = require("../../utils/imgurl");
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    navigationBarHeight,
    noCoupon: imgurl.noCoupon.url,
    orangeImg: imgurl.orange.url,
    blueImg: imgurl.blue.url,
    grayImg: imgurl.gray.url,
    bannerUrl: imgurl.banner.url,
    loadingUrl: imgurl.loading.url,
    bargaincancel: imgurl.bargaincancel.url,
    wmhome: imgurl.wmhome.url,
    bargainfail: imgurl.bargainfail.url,
    bargainsuccess: imgurl.bargainsuccess.url,
    bargaingoods: true,
    mybargain: false,
    list: [],
    lists: [],
    page: 1, // 设置加载的第几次，默认是第一次
    pageSize: 10, //返回数据的个数
    searchLoadingComplete: false, //“没有数据”的变量，默认false，隐藏
    allow_load: true, // 是否允许继续加载标识  默认 true 允许，false 加载完成
    myorder: false,
    actUrl: imgurl.act.url,
    loadingUrl: imgurl.loading.url,
    flag: 'bargaingoods',
    goHomeUrl: "",
  },
  goback: function () {
    let this_=this
    wx.switchTab({
      url: this_.data.goHomeUrl,
    })
  },
  onLoad: function (options) {
    this.setData({
      goHomeUrl: app.globalData.goHomeUrl
    })
    let that = this;
    // options.from =='myhtml'
    // 来自我的列表
    if (options && options.from =='myhtml'){
      that.mybargain();
    }else{
      that.getList(1, "bargaingoods");
      that.setData({
        flag: "bargaingoods",
        bargaingoods: true,
        mybargain: false
      })
    }
    that.getBannerlist();
    var todayDate=new Date().getTime();
    that.setData({
      todayDate:todayDate
    })
    
  },
  /**
   * banner
   */
  getBannerlist: function () {
    let that = this;
    Http.get({
      url: config.api.bannerlist,
      data: {
        pageNum: 1,
        pageSize: 7
      }
    }).then(res => {
      that.setData({
        list: res.data.list
      });
    });
  },
  getList(pageNum, flag) {
    var that = this;
    console.log(pageNum)
    if (that.data.allow_load) {
      /**
       * mybargain: 我的砍价
       * bargaingoods: 砍价商品
        */
      that.setData({
        loading: true,
        content: '小主,我在玩命加载中...'
      })
      var param = {};
      if (flag == 'mybargain') {
        that.setData({
          flag: "mybargain"
        })
        var param = {
          pageNum: pageNum,
          pageSize: 10
        };
        var url = config.api.pressOrderList;
      } else if (flag == 'bargaingoods') {
        that.setData({
          flag: "bargaingoods"
        })
        var param = {
          pageNum: pageNum,
          pageSize: 10,
          targetAd: 6
        };
        var url = config.api.couponChannelList;
      }
      // 请求接口
      Http.get({
          url: url,
          data: param
        }).then(res => {
          console.log(res)
          /**
           * 加载完成
           */
          if (pageNum >= res.data.pages) {
            if (res.data.pages == 0 || res.data.pages == 1) {
              that.setData({
                allow_load: true,
                loading: false,
                content: ""
              });
            } else {
              that.setData({
                allow_load: false,
                loading: true,
                content: "——— 再拉裤子就掉了啦 ———",
              });
            }
          }
          if (pageNum == 1) {
            that.setData({
              lists: [],
            })
          }
          if (flag == 'bargaingoods'){    
            var tmpArr = that.data.lists;
            tmpArr.push.apply(tmpArr, res.data.list);
            that.setData({
              lists: tmpArr
            })
          } else if (flag == 'mybargain'){
            console.log(res.data.list)
            var tmpArr = that.data.lists;
            tmpArr.push.apply(tmpArr, res.data.list);
            console.log(tmpArr)
            let lists = [];
            tmpArr.map(file => {
              if (file.orderStatus != 1) {
                lists.push(file);
              }
            })
            that.setData({
              lists: lists
            })
          }
          wx.stopPullDownRefresh();
        })
        .catch(err => {
          wx.stopPullDownRefresh();
          wx.showToast({
            title: err.message,
            icon: 'none',
            duration: 2000,
            mask: false
          });
        })
      setTimeout(function () {
        that.setData({
          loading: false,
        })
      }, 1400);
    } else {
      that.setData({
        loading: true,
        content: "——— 再拉裤子就掉了啦 ———"
      })
      setTimeout(function () {
        that.setData({
          loading: false,
        })
      }, 1400)
    }
  },
  /**
   * 如果是重新砍价，需要重新下单
   */
  orderSave: function (couponId,couponChannelId,formId) {
    Http.post({
        url: config.api.orderSave,
        data: {
          couponId: "" + couponId,
          formId:formId,
          couponChannelId:""+couponChannelId,
          press: true
        }
      })
      .then(res => {
        console.log(res)
        wx.navigateTo({
          url: `/pages/bargain/bargainDatail/bargainDatail?orderId=${res.data.mainOrderId}`
        })
      })
      .catch(err => {
        wx.showToast({
          title: err.message,
          icon: "none"
        })
      })
  },
  inviteFriend: function (e) {
    
    let data = e.currentTarget.dataset.data
    console.log(data)
    /**
     * 添加标识
     */
    wx.navigateTo({
      url: `/pages/bargain/bargainDatail/bargainDatail?orderId=${data.id}&composeOrderType=${data.composeOrderType}`
    })
  },
  barginAgain: function (e) {
    let that = this;
    console.log(e)
    let formId = e.detail.formId;
    let couponId = e.currentTarget.dataset.couponid;
    let couponChannelId = e.currentTarget.dataset.couponchannelid;
    // let orderId = e.currentTarget.dataset.id;
    that.orderSave(couponId,couponChannelId,formId)
  },
  mybargain: function () {
    let that = this;
    that.setData({
      bargaingoods: false,
      mybargain: true,
      flag: "mybargain",
      allow_load: true,
      loading: false,
      content: "",
      page:1
    })
    that.getList(1, 'mybargain');
    wx.setNavigationBarTitle({
      title: '我的砍价'
    })
  },
  bargaingoods: function () {
    let that = this;
    that.setData({
      bargaingoods: true,
      mybargain: false,
      flag: "bargaingoods",
      allow_load: true,
      loading: false,
      page:1,
      content: ""
    })

    that.getList(1, 'bargaingoods');
    wx.setNavigationBarTitle({
      title: '砍价专场'
    })
  },
  /**
   * 
   * @param {砍价} 发起砍价 
   */
  invite: function (e) {
    let couponChannelId = e.currentTarget.dataset.id;
    let couponId = e.currentTarget.dataset.couponid;
    if (couponChannelId && couponId) {
      wx.navigateTo({
        url: `/pages/coupon/detail/index?couponChannelId=${couponChannelId}&couponId=${couponId}`
      })
    }
  },
  //加载更多
  onReachBottom: function () {
    let that = this;
    that.data.page++;
    that.setData({
      page: that.data.page
    });
    that.getList(that.data.page, that.data.flag);
  },
  /**
* 刷新
*/
  onPullDownRefresh: function (e) {
    let that = this;
    if (that.data.flag == 'bargaingoods'){
      that.getBannerlist();
      var todayDate = new Date().getTime();
      that.setData({
        todayDate: todayDate
      })
      that.getList(1, "bargaingoods");
      that.setData({
        flag: "bargaingoods",
        bargaingoods: true,
        mybargain: false
      })
    } else if (that.data.flag == 'mybargain'){
      that.getList(1, "mybargain");
      that.setData({
        flag: "mybargain",
        bargaingoods: false,
        mybargain: true
      })
    }
  },
})