<template>
<view id="store-seckill">
    <view class="contents">
		
      <view class="no-list" v-if="!list.length && init">
          暂无数据
      </view>
	  
        <view class="seckill-list">
            <view v-for="(items, idx) in list" :key="idx">
                <view class="item" :data-id="item.goods.id" v-for="(item, index) in list[idx]" :key="index"  v-if="!item.is_end" @tap="jump">
                    <view class="img">
                        <image mode="widthFix" :src="item.goods.img" alt></image>
                        <view class="time-box" :style="'background: ' + config.mainColor">
							
                            <seckill :findex="idx"  :end="item.ends_at" :starts="item.starts_at" :server="item.server_time" mold="list" :index="index"  @starts="isStarts" @end="isEnd"></seckill>
                        </view>
                    </view>
                    <view class="info">
                        <view class="name">
                            <span v-if="item.init_status == 2">[未开始]</span> {{item.goods.name}}
                        </view>
                        <view class="money-box">
                            <view class="num" v-if="item.init_status == 1">
                                已有{{item.sell_num}}人购买
                            </view>
                            <view class="money">
                                <span class="old">￥{{item.goods.sell_price}}</span>
                                <span class="new">￥{{item.seckill_price}}</span>
                            </view>
                        </view>
                    </view>

                </view>
            </view>
            <view class="loadingbox" :hidden="!show">
                {{showText}}
            </view>
        </view>
    </view>
</view>
</template>
<script>
import {pageLogin, getUrl,config,is} from '@/common/js/utils.js';
import seckill from "@/components/seckill/seckill";

export default {
  data() {
    return {
      hasMore: true,
      init: false,
      list: [],
      page: '',
      showText: '正在加载下一页数据',
      show: false,
      config: '',
	  findex:''
    };
  },
  
  components:{
	  seckill
  },

  // 加载更多
  onReachBottom() {
    if (this.hasMore) {
      var page = this.page + 1;
      this.setData({
        show: true
      });
      this.querySeckillList(page);
    } else {
      wx.showToast({
        image: '../../../static/error.png',
        title: '再拉也没有啦'
      });
    }
  },

  // 分享
  onShareAppMessage(res) {
    var title = '惊喜不断，超低价秒杀火热进行中';
    return {
      title: title,
      path: '/pages/store/seckill/seckill'
    };
  },

  onLoad(e) {
    // 第三方平台配置颜色
    var gbConfig = this.$cookieStorage.get('globalConfig') || '';
    this.setData({
      config: gbConfig
    });
    wx.showLoading({
      title: "加载中",
      mask: true
    });
    this.querySeckillList(1);
  },

  components: {
    seckill
  },
  methods: {
    jump(e) {
      wx.navigateTo({
        url: '/pages/store/detail/detail?id=' + e.currentTarget.dataset.id
      });
    },

    // 请求秒杀列表
    querySeckillList(page) {
      this.$http.get({
        api: 'api/seckill/all',
        data: {
          page: page
        }
      }).then(res => {
        if (res.statusCode == 200) {
          res = res.data;

          if (res.status) {
            var pages = res.meta.pagination;
            var current_page = pages.current_page;
            var total_pages = pages.total_pages;
            this.setData({
              // [`list[${page - 1}]`]: res.data,
              init: true,
              page: current_page,
              hasMore: current_page < total_pages
            });
			this.$set(this.list, page -1, res.data);
			
			
          } else {
            wx.showModal({
              title: '',
              content: '请求失败',
              showCancel: false
            });
          }
        } else {
          wx.showModal({
            title: '',
            content: '请求失败',
            showCancel: false
          });
        }

        wx.hideLoading();
        this.setData({
          show: false
        });
      }, err => {
        wx.hideLoading();
        this.setData({
          show: false
        });
      });
    },

    // 秒杀开始
    isStarts(e) {
	  console.log(e);
      var findex = e.findex;
      var index = e.index;

      if (index != undefined || findex != undefined) {
        var item = this.list[findex][index];

        if (item.init_status != 1) {
          // this.setData({
          //   [`list[${findex}][${index}].init_status`]: 1
          // });
		  this.list[findex][index].init_status=1;
        }
      }
    },

    // 秒杀结束
    isEnd(e) {
      var findex = e.findex;
      var index = e.index;

      if (index != undefined || findex != undefined) {
        var item = this.list[findex][index];

        if (item.is_end == false) {
          // this.setData({
          //   [`list[${findex}][${index}].is_end`]: true
          // });
		  this.list[findex][index].is_end=true;
        }
      }
    },

    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  },
};
</script>
<style rel="stylesheet/less" lang="less">
    @import "seckill";
</style>