const navigationBarHeight = (getApp().statusBarHeight + 50) + 'px'
var config = require("../../config/config.js");
const Http = require("../../utils/HttpBasics");
const imgurl = require("../../utils/imgurl");
const { spellStatus,spellStatus02 } = require("../../utils/spell");
let app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    mouldType: 0,
    mouldConfig: {},
    navigationBarHeight,
    noCoupon: imgurl.noCoupon.url,
    bannerUrl: imgurl.banner.url,
    loadingUrl: imgurl.loading.url,
    nodata: imgurl.nodata.url,
    wmhome: imgurl.wmhome.url,
    orangeImg: imgurl.orange.url,
    blueImg: imgurl.blue.url,
    grayImg: imgurl.gray.url,
    spellList: true,
    myspellList: false,
    list: [],
    lists: [],
    myLists:[],
    page: 1, // 设置加载的第几次，默认是第一次
    pageSize: 10, //返回数据的个数
    searchLoadingComplete: false, //“没有数据”的变量，默认false，隐藏
    allow_load: true, // 是否允许继续加载标识  默认 true 允许，false 加载完成
    myorder: false,
    actUrl: imgurl.act.url,
    loading:true,
    content:'数据正在加载中',
    flag: 'spellList',
    reloadIf:false,
    goHomeUrl:"",
  },
  goback: function () {
    let this_ = this
    wx.switchTab({
      url: this_.data.goHomeUrl,
    })
  },
  onLoad: function (options) {
    this.setData({
      mouldType: app.globalData.mouldType,
      mouldConfig: app.globalData.mouldConfig,
      goHomeUrl: app.globalData.goHomeUrl,
    })
    console.log(this.data.mouldConfig,"mouldConfig")
    let that = this;
    that.getBannerlist();
    var todayDate = new Date().getTime();
    that.setData({
      optionsData: options,
      todayDate: todayDate
    })
    if(options&&options.from=='myhtml'){
      that.getmyList();
    }else{
      // 去支付
      if (options && options.from == 'myspellList'){
        that.getList(1, "myspellList");
        that.setData({
          flag: "myspellList",
          spellList: false,
          myspellList: true,
          lists: null
        })
        wx.setNavigationBarTitle({
          title: '我的拼团'
        })
      }else{
        that.getList(1, "spellList");
        that.setData({
          flag: "spellList",
          spellList: true,
          myspellList: false,
          lists: null
        })
        wx.setNavigationBarTitle({
          title: '拼团专场'
        })
      }
    }
  },
  onShow(){
    this.setData({
      mouldType: app.globalData.mouldType
    })
    let that=this;
    if (this.data.reloadIf){
      wx.setNavigationBarTitle({
        title: '拼团专场'
      })
      that.getList(1, "spellList");
      that.setData({
        flag: "spellList",
        spellList: true,
        myspellList: false,
        lists: null
      })
    }
  },
  //切换拼团列表
  getSpellList(){
    this.setData({
      flag: "spellList",
      page:1,
      allow_load:true
    })
    wx.setNavigationBarTitle({
      title: '拼团专场'
    })
    this.getList(1, "spellList")
  },
  //去使用
  gotoUse(e){
    console.log(e)
    wx.navigateTo({
      url: '/pages/couponorder/index/index',
    })
  },
  //重新拼团
  gotoAgain(e){
    let data = e.currentTarget.dataset.data
    wx.navigateTo({
      url: `/pages/spellGroup/mySpellGroup/index?couponId=${data.couponId}&couponChannelId=${data.couponChannelId}`,
    })
  },
  //跳转到我的拼团详情
  gotoStatusDetail(e){
    console.log(e,333)
    let data = e.currentTarget.dataset.data
    wx.navigateTo({
      url: `/pages/spellDetail/index?orderId=${data.orderId}&couponId=${data.couponId}&orderGroupId=${data.orderGroupId}&couponChannelId=${data.couponChannelId}`,
    })
  },
  //切换我的拼团
  getmyList(){
    this.setData({
      flag: "myspellList",
      page: 1,
      allow_load: true,
      lists:null
    })
    wx.setNavigationBarTitle({
      title: '我的拼团'
    })
    this.getList(1, "myspellList")
  },
  /**
   * 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
      });
    });
  },
  changeStatus(status){
    return spellStatus02.filter(item=>status==item.value)[0].name
  },
  changeStatus02(status) {
    console.log(status)
    return spellStatus.filter(item => status == item.value)[0].name
  },
  gotoPay(e){
    this.setData({
      reloadIf:true
    })
    let data = e.currentTarget.dataset.data
    wx.navigateTo({
      url: `/pages/spellGroup/paySpellGroup/index?orderId=${data.composeOrderId}&couponId=${data.couponId}&orderGroupId=${data.orderGroupId}&couponChannelId=${data.couponChannelId}&composeOrderType=${data.composeOrderType}`,
    })
  },
  gotoEdit(e){
    let status = e.currentTarget.dataset.status
    if (status==10){

    }
  },
  getList(pageNum, flag) {
    var that = this;
    console.log(pageNum)
    if (that.data.allow_load) {
      /**
       * myspellList: 我的砍价
       * spellList: 砍价商品
        */
      that.setData({
        loading: true,
        content: '小主,我在玩命加载中...'
      })
      var param = {};
      if (flag == 'myspellList') {
        console.log(22222222)
        that.setData({
          flag: "myspellList"
        })
        var param = {
          pageNum: pageNum,
          pageSize: 10
        };
        var url = config.api.getMySepllList;
      } else if (flag == 'spellList') {
        console.log(111111)
        that.setData({
          flag: "spellList"
        })
        var param = {
          pageNum: pageNum,
          pageSize: 10,
          targetAd: 7
        };
        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 == 'spellList') {
          var tmpArr = that.data.lists;
          tmpArr.push.apply(tmpArr, res.data.list);
          that.setData({
            lists: tmpArr
          })
        } else if (flag == 'myspellList') {
          console.log(res.data.list)
          var tmpArr = that.data.lists;
          res.data.list.map((item,index)=>{
            item.statusText = that.changeStatus(item.orderStatus);
            item.statusText02 = that.changeStatus02(item.orderStatus)
          })
          tmpArr.push.apply(tmpArr, res.data.list);
          console.log(tmpArr)
          let lists = [];
          tmpArr.map(file => {
              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) {
    Http.post({
      url: config.api.orderSave,
      data: {
        couponId: "" + couponId,
        couponChannelId: "" + couponChannelId,
        press: true
      }
    })
      .then(res => {
        console.log(res)
        wx.navigateTo({
          url: `/pages/bargain/bargainDatail/bargainDatail?orderId=${res.data.orderNumber}`
        })
      })
      .catch(err => {
        wx.showToast({
          title: err.message,
          icon: "none"
        })
      })
  },
  inviteFriend: function (e) {

    /**
     * 添加标识
     */
    wx.navigateTo({
      url: `/pages/bargain/bargainDatail/bargainDatail?orderId=${e.currentTarget.dataset.id}`
    })
  },
  // barginAgain: function (e) {
  //   let that = this;
  //   console.log(e)
  //   let couponId = e.currentTarget.dataset.couponid;
  //   let couponChannelId = e.currentTarget.dataset.couponchannelid;
  //   // let orderId = e.currentTarget.dataset.id;
  //   that.orderSave(couponId, couponChannelId)
  // },
  // myspellList: function () {
  //   let that = this;
  //   that.setData({
  //     spellList: false,
  //     myspellList: true,
  //     flag: "myspellList",
  //     allow_load: true,
  //     loading: false,
  //     content: "",
  //     page: 1
  //   })
  //   that.getList(1, 'myspellList');
  //   wx.setNavigationBarTitle({
  //     title: '我的砍价'
  //   })
  // },
  spellList: function () {
    let that = this;
    that.setData({
      spellList: true,
      myspellList: false,
      flag: "spellList",
      allow_load: true,
      loading: false,
      page: 1,
      content: ""
    })

    that.getList(1, 'spellList');
    wx.setNavigationBarTitle({
      title: '砍价专场'
    })
  },
  /**
   * 
   * @param goto拼团券详情
   */
  invite: function (e) {

    let couponChannelId = e.currentTarget.dataset.id;
    let couponId = e.currentTarget.dataset.couponid;
    if (couponChannelId && couponId) {
      wx.navigateTo({
        url: `/pages/spellGroup/mySpellGroup/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 == 'spellList') {
      that.getBannerlist();
      var todayDate = new Date().getTime();
      that.setData({
        todayDate: todayDate
      })
      that.getList(1, "spellList");
      that.setData({
        flag: "spellList",
        spellList: true,
        myspellList: false
      })
    } else if (that.data.flag == 'myspellList') {
      that.getList(1, "myspellList");
      that.setData({
        flag: "myspellList",
        spellList: false,
        myspellList: true
      })
    }
  },
})