var app = getApp();
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');

Page({
  data: {
    campMinus: {},
    page: 1,
    size: 4,
    referrer: 0,
    storeId: '',
    campMinusId: '',
    merchSn: '',
    openCampAttr: false,
    storeName: '',
    isShare: '',
    categoryFilter: false,
    filterCategory: [],
    goodsList: [],
    categoryId: 0,
    currentSortType: 'desc',
    currentSortOrder: 'a.create_time',
    goodsCount: 0,
  },
  onLoad: function(options) {
    let that = this;
    // options.scene = '6&8&满4件减1&1';
    if (options.scene) {
      var scene = decodeURIComponent(options.scene);
      console.log("scene is ", scene);
      var scanArray = scene.split('&');
      var id = scanArray[0];
      var storeId = scanArray[1];
      var campName = scanArray[2];
      var isShare = scanArray[3];
      // console.log(campName);
      // console.log(storeId);
      this.setData({
        campMinusId: id,
        storeId: storeId,
        campName: campName,
        isShare: isShare
      });
      if(campName){
        wx.setNavigationBarTitle({
          title: campName
        })
      }
    } else {
      if (options.campName) {
        wx.setNavigationBarTitle({
          title: options.campName
        })
      }
      if (options.campMinusId) {
        that.setData({
          campMinusId: options.campMinusId
        });
      }
      if (options.storeId) {
        that.setData({
          storeId: options.storeId
        });
      }
      if (options.isShare) {
        that.setData({
          isShare: options.isShare
        });
      }
    }
  },
  onReady: function() {
    let that = this;
    that.getCampminusDetail();
    that.getGoodsList();
  },
  getCampminusDetail() {
    var that = this;
    util.request(api.CampminusDetail, {
      campMinusId: that.data.campMinusId,
      storeId: that.data.storeId
    })
      .then(function (res) {
        if (res.errno === 0) {
          if (res.data) {
            that.setData({
              campMinus: res.data,
              storeName: res.data.storeName
            });
            util.countdown(that, that.data.campMinus, 'campMinus', null);

            // console.log(that.data.campMinus.dateformat.day)
            // console.log(that.data.campMinus.dateformat.hr)
            // console.log(that.data.campMinus.dateformat.min)
            // console.log(that.data.campMinus.dateformat)
            // console.log(that.data.campMinus.dateformat.sec)
          }
        }
      });
  },
  getGoodsList() {
    wx.showLoading({
      title: '加载中...',
    });
    var that = this;
    util.request(api.StoreCampMinusGoodsList, { campMinusId: that.data.campMinusId, page: that.data.page, size: that.data.size, order: that.data.currentSortOrder, sort: that.data.currentSortType, categoryId: that.data.categoryId, storeId: that.data.storeId })
      .then(function (res) {
        if (res.errno === 0) {
          console.log(res.data.goodsList);
          let goodsList = that.data.goodsList.concat(res.data.goodsList);
          that.setData({
            goodsList: goodsList,
            goodsCount: res.data.goodsCount,
            filterCategory: res.data.filterCategory
          });
          wx.hideLoading();
          if (that.data.categoryId > 0) {
            let filterCategory = that.data.filterCategory;
            filterCategory.forEach(function (val, index, arr) {
              if (val.id == that.data.categoryId) {
                val.checked = true;
                filterCategory[index] = val;
                that.setData({ filterCategory: filterCategory });
              } else {
                val.checked = false;
              }
            }, that);
          }
        }
      });
  },
  openSortFilter: function (event) {
    let currentId = event.currentTarget.id;
    switch (currentId) {
      case 'categoryFilter':
        this.setData({
          'categoryFilter': !this.data.categoryFilter,
          'currentSortType': 'category',
          'currentSortOrder': 'asc'
        });
        break;
      case 'priceSort':
        let tmpSortOrder = 'asc';
        if (this.data.currentSortOrder == 'asc') {
          tmpSortOrder = 'desc';
        }
        this.setData({
          'currentSortType': 'price',
          'currentSortOrder': tmpSortOrder,
          'categoryFilter': false,
          goodsList: [],
          page: 1
        });
        this.getGoodsList();
        break;
      default:
        //综合排序
        this.setData({
          'currentSortType': 'default',
          'currentSortOrder': 'desc',
          'categoryFilter': false,
          goodsList: [],
          page: 1
        });
        this.getGoodsList();
    }
  },
  selectCategory: function (event) {
    let that = this;
    let currentIndex = event.target.dataset.categoryIndex;
    this.setData({
      categoryFilter: false,
      categoryId: this.data.filterCategory[currentIndex].id,
      goodsList: []
    });
    that.getGoodsList();
  },
  /**
   * 去逛逛
   */
  toIndexPage: function() {
    wx.switchTab({
      url: "/pages/index/index"
    });
  },
  selectCampMinus(e) {
    let goodsBizType = e.currentTarget.dataset.goodsType;
    let that = this;
    // wx.navigateTo({
    //   url: '/pages/shopping/coupon/coupon',
    // })
    that.setData({
      openCampAttr: !that.data.openCampAttr
    })
    if (that.data.openCampAttr) {
      // that.getTicketDiscountList(goodsBizType);
    }
  },
  hideSwitchCampPop: function() {
    this.setData({
      openCampAttr: false
    })
  },
  onReachBottom() {
    var that = this;
    if (!that.data.goodsList) {
      wx.showLoading({
        title: '加载中...',
      })
    }
    that.setData({
      page: that.data.page + 1,
    });
    that.getGoodsList();
  }
})