<template>
<view id="callList">

    <view class="no-list" v-if="!list.length && init">
        暂无数据
    </view>

    <view class="call-item" v-for="(item, index) in list" :key="index" :data-goods="item.goods.id" :data-id="item.id" @tap="jumpCall"  v-if="item.activity_status != 'End'">
        <view class="time mx-1px-bottom" v-if="item.activity_status == 'NoBegin'">
            <seckill :end="item.ends_at" typename="集call" :starts="item.starts_at" :index="index" :server="item.server_time" @starts="isStarts" @end="isEnd" mold="list"></seckill>
        </view>
        <view class="item-warp">
            <view class="call-img">
                <image :src="item.img"></image>
            </view>
            <view class="call-info">
                <view class="call-goods">
                    <view class="goods-name">
                        {{item.label}}
                    </view>
                    <view class="goods-money-box">
                        <view class="money">
                            <view class="new">
                                {{item.goods.sell_price}}
                            </view>
                            <view class="old">
                                {{item.goods.market_price}}
                            </view>
                        </view>
                        <view class="nun">
                            限量{{item.limit}}件
                        </view>
                    </view>
                </view>
                <view class="action-box">
                    <view class="free">
                        集满{{item.per_count}}个Call即可免费领
                    </view>
                    <view class="action">
                        去打Call
                    </view>
                </view>
            </view>
        </view>
    </view>
</view>
</template>
<script>
import {pageLogin, getUrl,config,is} from '@/common/js/utils.js';
import Animation from '@/common/js/animation.js';
import seckill from "@/components/seckill/seckill";

export default {
  data() {
    return {
      init: false,
      list: []
    };
  },

  // 分享
  onShareAppMessage(res) {
    var title = config.BRAND.name == 'MIER' ? '米尔优选，惊喜不断，打 call 免费领火热进行中' : '惊喜不断，打 call 免费领火热进行中';
    return {
      title: title,
      path: '/pages/store/callList/callList'
    };
  },

  onLoad(e) {
    wx.showLoading({
      title: "加载中",
      mask: true
    });
    this.queryCallList();
  },

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

    jumpCall(e) {
      var token = this.$cookieStorage.get('user_token');

      if (token) {
        var id = e.currentTarget.dataset.id;
        wx.navigateTo({
          url: '/pages/store/call/call?id=' + id
        });
      } else {
        wx.showModal({
          content: '请先登录',
          success: res => {
            if (res.confirm || !res.cancel && !res.confirm) {
              pageLogin(getUrl());
            }
          }
        });
      }
    },

    // 请求打call列表
    queryCallList(page) {
      this.$http.get({
        api: 'api/free_event/list'
      }).then(res => {
        if (res.statusCode == 200) {
          res = res.data;

          if (res.status) {
            this.setData({
              list: res.data
            });
          } else {
            wx.showModal({
              title: '',
              content: '请求失败',
              showCancel: false
            });
          }
        } else {
          wx.showModal({
            content: '请求失败',
            showCancel: false
          });
        }

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

    // 集Call结束
    isEnd(e) {
      var index = e.detail.index;

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

        if (item.activity_status != 'End') {
          // this.setData({
          //   [`list[${index}].activity_status`]: 'End'
          // });
		  this.list[index].activity_status='End';
		  
        }
      }
    },

    // 集call开始
    isStarts(e) {
      var index = e.detail.index;

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

        if (item.activity_status != 'End') {
          // this.setData({
          //   [`list[${index}].activity_status`]: 'OnGoing'
          // });
		  
		   this.list[index].activity_status='OnGoing';
		
        }
      }
    },

    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 "callList";
</style>