<template>
<view id="coupon">
    <view class="navbar mx-1px-top">
        <block v-for="(item, index) in tabList" :key="index" >
            <view :id="index" class="navbar-item activity" :hidden="activeIndex != index" :style="'color: ' + config.mainColor" @tap="tabClick">
                <view class="navbar-title">{{item.title}}</view>
            </view>

            <view :id="index" :hidden="activeIndex == index" class="navbar-item" @tap="tabClick">
                <view class="navbar-title">{{item.title}}</view>
            </view>
        </block>
        <view class="navbar-slider" :style="'width: ' + width + 'px; transform: translateX(' + sliderOffset + 'px); -webkit-transform: translateX(' + sliderOffset + 'px); background: ' + config.mainColor"></view>
    </view>
    <view class="tab-panel">
        <view class="tab-content" :hidden="activeIndex != 0" v-if="dataList && dataList[0] && dataList[0][0] && tabList && tabList[0]">
            <view class="no-list" v-if="!dataList[0][0].length && tabList[0].init">
                <i class="iconfont icon-youhuiquan"></i>
                <view>报告主人，这里暂无<span v-if="is_coupon == 1">优惠券~</span><span v-else>促销活动</span></view>
            </view>
            <view v-for="(items, idx) in dataList[0]" :key="idx" >
                <view class="coupon" :data-id="item.id" :data-limit="item.usage_limit" @tap="jumpDetailOn" v-for="(item, index) in dataList[0][idx]" :key="index">
                    <view class="coupon-left" :class="is_coupon != 1 ? 'discount' : ''" :style="'background: ' + config.mainColor">
                        <view class="text-wrap">
                            <view class="text-box">
                                <view class="text">
                                    <text class="money" v-if="item.action_type.type == 'cash'">￥</text>
                                    <span>{{ item.action_type.value }}</span>

                                    <text class="money" v-if="item.action_type.type == 'discount'">折</text>
                                </view>
                                <text class="label text">{{ item.label }}</text>
                            </view>

                        </view>
                        <view class="dot-wrap">
                            <view class="dot-item">

                            </view>
                            <view class="dot-item">

                            </view>
                            <view class="dot-item">

                            </view>
                            <view class="dot-item">

                            </view>
                            <view class="dot-item">

                            </view>
                            <view class="dot-item">

                            </view>
                            <view class="dot-item">

                            </view>
                            <view class="dot-item">

                            </view>
                        </view>
                    </view>
                    <view class="coupon-right">
                        <view class="top">
                                <span class="type">
                                    <span v-if="item.channel == 'ec'">商城</span>
                                    <span v-else>门店</span>
                                </span>
                            <text class="info">{{item.title}}</text>
                        </view>
                        <view class="bottom">
                            <view class="bottom-use">
                                <view class="tiem-box">
                                    <text>{{item.use_start_time}}至{{item.use_end_time}}</text>
                                </view>
                            </view>
                        </view>
                        <view class="share mx-1px-top">
                            <view class="text">
                                <i class="iconfont icon-fenxianganniu"></i>
                                <view>分享优惠券</view>
                            </view>
                            <view class="iconfont icon-jiantou">

                            </view>
                        </view>
                    </view>
                </view>
            </view>
        </view>
        <view class="tab-content" :hidden="activeIndex != 1" v-if="dataList && dataList[1] && dataList[1][0] && tabList && tabList[1]">
            <view class="no-list" v-if="!dataList[1][0].length && tabList[1].init">
                <i class="iconfont icon-youhuiquan"></i>
                <view>报告主人，这里暂无<span v-if="is_coupon == 1">优惠券~</span><span v-else>促销活动</span></view>
            </view>
            <view v-for="(items, idx) in dataList[1]" :key="idx" >
                <view class="coupon" :data-id="item.id" :data-limit="item.usage_limit" @tap="jumpDetail" v-for="(item, index) in dataList[1][idx]" :key="index">
                    <view class="coupon-left" :class="is_coupon != 1 ? 'discount' : ''" :style="'background: ' + config.mainColor">
                        <view class="text-wrap">
                            <view class="text-box">
                                <view class="text">
                                    <text class="money" v-if="item.action_type.type == 'cash'">￥</text>
                                    <span>{{ item.action_type.value }}</span>

                                    <text class="money" v-if="item.action_type.type == 'discount'">折</text>
                                </view>
                                <text class="label text">{{ item.label }}</text>
                            </view>

                        </view>
                        <view class="dot-wrap">
                            <view class="dot-item">

                            </view>
                            <view class="dot-item">

                            </view>
                            <view class="dot-item">

                            </view>
                            <view class="dot-item">

                            </view>
                            <view class="dot-item">

                            </view>
                            <view class="dot-item">

                            </view>
                            <view class="dot-item">

                            </view>
                            <view class="dot-item">

                            </view>
                        </view>
                    </view>
                    <view class="coupon-right">
                        <view class="top">
                                <span class="type">
                                    <span v-if="item.channel == 'ec'">商城</span>
                                    <span v-else>门店</span>
                                </span>
                            <text class="info">{{item.title}}</text>
                        </view>
                        <view class="bottom">
                            <view class="bottom-use">
                                <view class="tiem-box">
                                    <text>{{item.use_start_time}}至{{item.use_end_time}}</text>
                                </view>
                            </view>
                        </view>
                        <view class="share mx-1px-top">
                            <view class="text">
                                <i class="iconfont icon-fenxianganniu"></i>
                                <view>分享优惠券</view>
                            </view>
                            <view class="iconfont icon-jiantou">

                            </view>
                        </view>
                    </view>
                </view>
            </view>
        </view>
    </view>
</view>
</template>
<script>
import {pageLogin, getUrl,config,is} from '@/common/js/utils.js';

export default {
  data() {
    return {
      activeIndex: 0,
      sliderOffset: 0,
      width: 0,
      tabList: [{
        title: '线上',
        init: false,
        page: 0,
        more: true
      }, {
        title: '线下',
        init: false,
        page: 0,
        more: true
      }],
      dataList: {
        0: [],
        1: []
      },
      is_coupon: 1,
      // 用于判断是否为优惠券 1：优惠券 0：促销折扣
      config: ''
    };
  },

  onReachBottom(e) {
    var status = this.activeIndex;
    var page = this.tabList[status].page + 1;
    var tabList = `tabList[${status}]`;

    if (this.tabList[status].more) {
      this.queryCouponList(status, page);
    } else {
      wx.showToast({
        image: '../../../static/error.png',
        title: '再拉也没有啦'
      });
    }
  },

  onShow(e) {
    wx.getSystemInfo({
      success: res => {
        this.setData({
          width: res.windowWidth / this.tabList.length,
          sliderOffset: res.windowWidth / this.tabList.length * this.activeIndex
        });
      }
    });
  },

  onLoad(e) {
    // 第三方平台配置颜色
    var gbConfig = this.$cookieStorage.get('globalConfig') || '';
    this.setData({
      config: gbConfig
    });

    if (e.type) {
      this.setData({
        activeIndex: e.type
      });
    }

    ;
    this.queryCouponList();
  },

  methods: {
    tabClick(e) {
      var status = e.currentTarget.id;
      this.setData({
        sliderOffset: e.currentTarget.offsetLeft,
        activeIndex: status
      });

      if (!this.tabList[status].init) {
        this.queryCouponList(status);
      }
    },

    // 跳转线上详情
    jumpDetailOn(e) {
      var id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/pages/distribution/onDetail/onDetail?id=' + id + '&is_coupon=' + this.is_coupon
      });
    },

    // 跳转线下详情
    jumpDetail(e) {
      var id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/pages/distribution/offDetail/offDetail?id=' + id + '&is_coupon=' + this.is_coupon
      });
    },

    // 请求优惠券列表
    queryCouponList(type = 0, page = 1) {
      wx.showLoading({
        title: "加载中",
        mask: true
      });
      var token = this.$cookieStorage.get('user_token') || '';
      this.$http.get({
        api: 'api/discount/list',
        header: {
          Authorization: token
        },
        data: {
          page,
          type,
          is_coupon: this.is_coupon,
          is_agent: 1
        }
      }).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;
            var tabList = `tabList[${type}]`;
            // this.setData({
            //   [`dataList.${type}[${page - 1}]`]: res.data,
            //   [`${tabList}.init`]: true,
            //   [`${tabList}.page`]: current_page,
            //   [`${tabList}.more`]: current_page < total_pages
            // });
			
			this.$set(this.dataList[type], page -1, res.data);
			this.tabList[type].init=true;
			this.tabList[type].page= current_page;
			this.tabList[type].morecurrent_page < total_pages;
			
			
          } else {
            wx.showModal({
              content: res.message || '请求失败',
              showCancel: false
            });
          }
        } else {
          wx.showModal({
            content: res.message || '请求失败',
            showCancel: false
          });
        }

        wx.hideLoading();
      }).catch(rej => {
        wx.hideLoading();
        wx.showModal({
          content: rej.message || '请求失败',
          showCancel: false
        });
      });
    },

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