<template>
    <view id="coupon-index">

        <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">
				
               <view class="no-list" v-if="(!dataList[0].length || !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.discount.id" @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.discount.action_type.type == 'cash'">￥</text>
                                        <span>{{ item.discount.action_type.value }}</span>

                                        <text class="money" v-if="item.discount.action_type.type == 'discount'">折</text>
                                    </view>
                                    <text class="label text">{{ item.discount.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>
                        </view>
						
                        <view class="coupon-right">
                            <view class="top">
                                <span class="type">
                                    <span v-if="item.discount.channel == 'ec'">商城</span>
                                    <span v-else>门店</span>
                                </span>
                                <text class="info">{{item.discount.title}}</text>
                            </view>
							
                            <view class="bottom">
                                <view class="bottom-use">
                                    <view class="tiem-box">
                                        <text>{{item.discount.use_start_time}}至{{item.discount.use_end_time}}</text>
                                    </view>
                                    <view v-if="is_coupon == 1">

                                        <!--<view class="btn use" data-code="item.discount.code" data-index="idx"-->
                                              <!--data-findex="index" v-if="!item.discount.has_get" @tap.stop="getCoupon">-->
                                            <!--点击领取-->
                                        <!--</view>-->
									<!---->
                                        <view class="btn already"
                                              :style="'color: ' + config.mainColor + '; border-color: ' + config.mainColor"
                                              :data-id="item.id" @tap.stop="jumpList">
                                            去使用
                                        </view>
                                    </view>


                                </view>
                            </view>
                        </view>
						
						
						
                    </view>
                </view>
            </view>
            <view class="tab-content" :hidden="activeIndex != 1">
               <view class="no-list" v-if="(!dataList[1].length || !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.discount.id" :data-coupon="item.id" @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.discount.action_type.type == 'cash'">￥</text>
                                        <span>{{ item.discount.action_type.value }}</span>

                                        <text class="money" v-if="item.discount.action_type.type == 'discount'">折</text>
                                    </view>
                                    <text class="label text">{{ item.discount.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>
                        </view>
                        <view class="coupon-right">
                            <view class="top">
                                <span class="type">
                                    <span v-if="item.discount.channel == 'ec'">商城</span>
                                    <span v-else>门店</span>
                                </span>
                                <text class="info">{{item.discount.title}}</text>
                            </view>
                            <view class="bottom">
                                <view class="bottom-use">
                                    <view class="tiem-box">
                                        <text>{{item.discount.use_start_time}}至{{item.discount.use_end_time}}</text>
                                    </view>
                                    <view v-if="is_coupon == 1">
                                        <!--<view class="btn use" :data-code="item.discount.code"
                                                          :data-index="idx" :data-findex="index"
                                                          v-if="!item.discount.has_get" @tap.stop="getCoupon">
                                        点击领取
                                    </view>-->
                                        <view class="btn already"
                                              :style="'color: ' + config.mainColor + '; border-color: ' + config.mainColor"
                                              :data-id="item.discount.id" :data-coupon="item.id" @tap.stop="jumpDetail">
                                            去使用
                                        </view>
                                    </view>

                                </view>
                            </view>
                        </view>
                    </view>
                </view>
            </view>
        </view>
        <view class="see-over" @tap="toast">
            查看已失效优惠券
        </view>
    </view>

</template>
<script>
    import {pageLogin, getUrl, config} 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：促销折扣
                gbConfig: '',
            };
        },

        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({
                    icon: 'error',
                    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
            //    });
            //
            this.config = gbConfig;

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

            ;
            this.queryCouponList();
        },

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

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

            jumpOff() {
                wx.navigateTo({
                    url: '/pages/coupon/over/over'
                });
            },

            jumpDetailOn(e) {
                var id = e.currentTarget.dataset.id;
                wx.navigateTo({
                    url: '/pages/coupon/onDetail/onDetail?id=' + id + '&is_coupon=' + this.is_coupon
                });
            },

            jumpDetail(e) {
                var id = e.currentTarget.dataset.id;
                var coupon_id = e.currentTarget.dataset.coupon;
                wx.navigateTo({
                    url: '/pages/coupon/offDetail/offDetail?id=' + id + '&is_coupon=' + this.is_coupon + '&coupon_id=' + coupon_id
                });
            },

            jumpList(e) {
                var id = e.currentTarget.dataset.id;
                wx.navigateTo({
                    url: '/pages/coupon/goods/goods?id=' + id
                });
            },

            convert() {
                var code = this.code;

                if (code == '') {
                    wx.showModal({
                        title: '',
                        content: "请输入兑换码",
                        showCancel: false
                    });
                } else {
                    this.convertCoupon(code);
                }
            },

            input(e) {
                var vlaue = e.detail.value;
                this.setData({
                    code: vlaue
                });
            },

            viewDetail(e) {
                var type = e.currentTarget.dataset.info.discount.type;
                var id = e.currentTarget.dataset.info.id;

                if (type == 0) {
                    wx.navigateTo({
                        url: '/pages/coupon/onDetail/onDetail?id=' + id
                    });
                } else {
                    wx.navigateTo({
                        url: '/pages/coupon/offDetail/offDetail?id=' + id
                    });
                }
            },

            // 查询优惠券列表
            queryCouponList(type = 0, page = 1) {
                wx.showLoading({
                    title: "加载中",
                    mask: true
                });
                var token = this.$cookieStorage.get('user_token') || '';
                this.$http.get({
                    api: 'api/coupon',
                    header: {
                        Authorization: token
                    },
                    data: {
                        page,
                        channel: type ? 'shop' : 'ec',
                        is_active: 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.dataList[type][page - 1] = res.data;
                            this.tabList[type].init = true;
                            this.tabList[type].page = current_page;
                            this.tabList[type].more = current_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: res.message || '请求失败',
                        showCancel: false
                    });
                });
            },

            toast() {
                wx.navigateTo({
                    url: '/pages/coupon/over/over'
                });
            },

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