<template>
<view class="seckill-box" :style="{marginBottom: meta.margin_bottom + 'px',background: meta.background_color}" v-if="indexData && indexData.length">
    <view class="title" data-src="/pages/store/seckill/seckill" @tap="_jumpImg">
        <span>限时秒杀</span>
        <span>
            更多
         <text class="iconfont icon-gengduo"></text>
        </span>
    </view>
    <view v-for="(item, index) in indexData" :key="index" >
        <view class="seckill-info" :data-id="item.associate.goods.id" @tap="_jumpToDetail">
            <view class="left">
                <image :src="item.image"></image>
            </view>
            <view class="right">
                <view class="goods-name">
                    {{item.associate.goods.name}}
                </view>
                <view class="price-box">
                    <text>￥{{item.associate.seckill_price}}</text>
                    <text class="old">￥{{item.associate.goods.sell_price}}</text>
                </view>
                <view class="seckill-time">
                    <view>
                        <seckill :end="item.associate.ends_at" :starts="item.associate.starts_at" :server="server" mold="index" @starts="isStarts" @end="isEnd" :data-index="index" :data-idx="bigIndex"></seckill>
                    </view>
                    <view :style="'background: ' + config.mainColor" class="buy">
                        去抢购
                    </view>
                </view>
            </view>
        </view>
    </view>
</view>
</template>
<script>
import seckill from "../seckill/seckill";

export default {
  data() {
    return {};
  },
  components: {
    seckill
  },
    options: {
        addGlobalClass: true
    },
  props: {
      indexData: {
          type: Array,
          value: ''
      },
      config: {
          type: null,
          value: ''
      },
      server: {
          type: String,
          value: ''
      },
      bigIndex: {
          type: Number,
          value: ''
      },
      meta: {
          type: Object,
          value: ""
      }
  },

    //组件的方法
    methods: {
      //
      _jumpImg(e) {
        var src = e.currentTarget.dataset.src;
        if (!src || src == 'uto_miniprogram') return;
        wx.navigateTo({
          url: src
        });
      },

      _jumpToDetail(e) {
        var id = e.currentTarget.dataset.id;
        wx.navigateTo({
          url: `/pages/store/detail/detail?id=${id}`
        });
      },

      // 秒杀结束
      isEnd(e) {
        var index = e.index;
        var idx = e.idx;
        var myEventDetail = {
          index: index,
          idx: idx
        };

  //         this.setData({
  //              [`indexData[${index}].associate.seckill`]: null
  //          })
   
		this.indexData[index]={associate:{seckill:null}};
		   
        this.$emit('endevent', myEventDetail);
      },

      // 秒杀开始
      isStarts(e) {
        var index = e.index;
        var idx = e.idx;
        var myEventDetail = {
          index: index,
          idx: idx
        };
	
	 
	
	   if(this.indexData[index]==undefined||
	    this.indexData[index].associate==undefined || 
	    this.indexData[index].associate.init_status==undefined ||
		this.indexData[index].associate.init_status != 1){
		   // this.setData({
		   //     [`indexData[${index}].associate.seckill.init_status`]:1   
		   // })
			this.indexData[index]={associate:{init_status:1}};
			
	    }
		   
        this.$emit('startevent', myEventDetail);
      },
        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 "index-seckill";
</style>