var app = getApp();
var that;
Page({
  data: {
    src: '',
    list: [1, 2, 3],
    gxd: 0,
    minuteIndex: 0,//条件选择
    minuteArray: [],
    page: 1,
    isVal: false,
    keyword: '',
  },
  onLoad(e) {
    that = this;
    dd.setNavigationBar({ title: "福利兑换" });
    this.getPrizeList();
  },
  onShow(){
    this.getLsit();
  },
  onReset() {
    this.setData({
      keyword: '',
      isVal: false,
      page: 1,
    })
    this.getPrizeList();
  },
  //上啦加载
  onScrollToLower() {
    if (!that.data.isData) {
      this.setData({ page: ++that.data.page })
      this.getPrizeList(true);
    }
  },
  bindKeyInput(e) {
    this.setData({
      page: 1,
      isVal: e.detail.value ? true : false,
      keyword: e.detail.value ? e.detail.value : '',
    })
    this.getPrizeList();
  },
  //获取统计数据
  getLsit() {
    var http1 = app.$get("api/shop/achievement/detail", { employee_id: app.globalData.userData.id, page: 1, page_size: 5000, export: 0 });
    var http2 = app.$get("api/shop/goods/category/list", {});
    Promise.all([http1, http2]).then(resArr => {
      if (resArr[0].data.code == 1) {
        that.setData({
          gxd: resArr[0].data.data.all
        });
      }
      if (resArr[1].data.code == 1) {
        let arr = resArr[1].data.data.list;
        arr.unshift({ id: -1, name: '全部分类' })
        that.setData({
          minuteArray: arr
        });
      }
    })
  },
  getPrizeList(is) {
    let data = {
      page: this.data.page,
      page_size: 20,
      status: 1,
      export: 0,
      keyword: this.data.keyword,
    }
    if (this.data.minuteArray[this.data.minuteIndex] && this.data.minuteArray[this.data.minuteIndex].id != -1) {
      data.category_id = this.data.minuteArray[this.data.minuteIndex].id;
    }
    app.$get("api/shop/goods", data).then((res) => {
      var data = res.data.data.list;
      if (that.data.page == 1) { this.setData({ list: [] }) }
      var list = that.data.list;
      if (is) {
        if (data.length < 20) {
          this.setData({ list: list.concat(data), isData: true })
        } else {
          this.setData({ list: list.concat(data) })
        }
      } else {
        this.setData({
          list: data,
          isData: false
        })
      }
    })
  },
  activeItem(e) {
    this.setData({ page: 1, minuteIndex: e.detail.value, })
    this.getPrizeList();
  },
  openDetail(e) {
    var item = e.target.dataset.item;
    item.gxd=this.data.gxd
    dd.navigateTo({
      url: '../commodityDetail/commodityDetail?item=' +JSON.stringify(item) 
    })
  },
  openUrl(e) {
    var index = e.target.dataset.index;
    if (index == 1) {
      dd.navigateTo({
        url: '../gxdDetail/gxdDetail'
      })
    } else {
      dd.navigateTo({
        url: '../exchangeRecord/exchangeRecord'
      })
    }
  },
});
