<template>
<view id="store-seckill">
    <view class="contents">
       <view class="no-list" v-if="!list.length && init">
           暂无数据
       </view>
		
        <view class="seckill-list">
			
           <!-- <view wx:for="{{list}}" wx:key="*this" wx:for-index="idx" wx:for-item="items">
                <view class="item" data-id="{{item.goods.id}}" wx:for="{{list[idx]}}" wx:key="*this" wx:for-index="index" wx:if="{{!item.is_end}}" bindtap="jump">
                    <view class="groups-img">
                        <view class="num">
                            {{item.number}}人团
                        </view>
                        <image mode="widthFix" src="{{item.img}}" alt="" />
                        <view class="time-box {{item.init_status == 1 ? 'active' : ''}}">
                            <group end="{{item.ends_at}}" starts="{{item.starts_at}}" server="{{item.server_time}}" mold="list" index="{{index}}" findex="{{idx}}" bind:starts="isStarts" bind:end="isEnd"></group>
                        </view>
                    </view>
                    <view class="groups-name">
                        {{item.goods.name}}
                    </view>
                    <div class="groups-bottom">
                        <view class="money">
                            <view class="new">
                                ￥{{item.groupon_price}}
                            </view>
                            <view class="old">
                                单买价 <span>¥{{item.goods.sell_price}}</span>
                            </view>
                        </view>
                        <view class="buttom">
                            去拼团
                        </view>
                    </div>
                </view>
            </view> -->
			
            <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"  @tap="jump">
                    <view class="left">
                        <image mode="widthFix" :src="item.goods.img" alt></image>
                    </view>
                    <view class="right">
                        <view class="name">
                            {{item.goods.name}}
                        </view>
                        <view class="price-box">
                            <view class="old-price">
                                单买价 ￥{{item.goods.sell_price}}
                            </view>
                            <view class="meal-price">
                                <view class="price">
                                    <span>{{item.number}}人团</span>
                                    ￥{{item.price}}
                                </view>
                                <view class="btn" :style="'background: ' + config.mainColor">
                                    <span v-if="item.init_status == 1">去拼团 
									  <!-- <i class="iconfont icon-Chevron"></i> -->
									</span>
                                    <span v-else>即将开抢 
									<!-- <i class="iconfont icon-Chevron"></i> -->
									</span>
                                </view>
                            </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 group from "@/components/group/group";

export default {
  data() {
    return {
      hasMore: true,
      init: false,
      list: [],
      page: '',
      showText: '正在加载下一页数据',
      show: false,
      config: '',
      author: config.PACKAGES.author
    };
  },
  
  components:{
	  group
  },
  // 加载更多
  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) {
    return {
      title: '惊喜不断，超低价拼团火热进行中',
      path: '/pages/store/groups/groups'
    };
  },

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

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

    // 请求秒杀列表
    querySeckillList(page) {
      this.$http.get({
        api: 'api/multiGroupon/list',
        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) {
      var findex = e.detail.findex;
      var index = e.detail.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.detail.findex;
      var index = e.detail.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];
        });
      });
    }
  },
  computed: {},
  watch: {}
};
</script>
<style rel="stylesheet/less" lang="less">
    @import "groups";
</style>