<template>
    <view>
        <view class="line">
            <view :class="'topLine ' + (swith_area == index ? 'active' : ' ')" :data-index="index" @tap="swith_tab" v-for="(item, index) in switch_space" :key="index">
                {{ item }}
            </view>
        </view>

        <view class="code">
            <view style="text-align: center"><input type="text" :value="code" placeholder="请输入兑换码" @input="handleInput" class="input" /></view>
            <button class="_button" :data-index="swith_area" @tap="ex">立即兑换</button>
        </view>
    </view>
</template>

<script>
// pages/user/discount/discount_ex/discount_ex.js
import { http } from '../../../../utils/util';
import { host } from '../../../../Gdata';
export default {
    data() {
        return {
            switch_space: ['优惠券', '文惠券', '观剧卡'],
            swith_area: 0,
            code: ''
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {},
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
        swith_tab: function (e) {
            let swith_area = e.currentTarget.dataset.index;
            this.setData({
                swith_area
            });
        },

        handleInput(event) {
            const value = event.detail.value;
            this.setData({
                code: value
            });
        },

        ex(e) {
            let token = getApp().globalData.token;
            let uid = getApp().globalData.uid;
            let url = ['/api/v1/coupon-exchange', '/api/v1/wenhui-exchange', '/api/v1/card-exchange'];

            if (e.currentTarget.dataset.index == 2) {
                uni.request({
                    url: host + url[e.currentTarget.dataset.index],
                    data: {
                        card_sn: this.code
                    },
                    method: 'post',
                    header: {
                        token: token,
                        uid: uid
                    },
                    success: (res) => {
                        if (200 == res.data.status) {
                            this.setData({
                                code: null
                            });
                            uni.showToast({
                                title: '兑换成功',
                                icon: 'success',
                                duration: 2000
                            });
                        } else {
                            uni.showToast({
                                title: '兑换码错误',
                                icon: 'fail',
                                duration: 2000
                            });
                        }
                    }
                });
            } else {
                uni.request({
                    url: host + url[e.currentTarget.dataset.index],
                    data: {
                        coupon_sn: this.code
                    },
                    method: 'post',
                    header: {
                        token: token,
                        uid: uid
                    },
                    success: (res) => {
                        if (200 == res.data.status) {
                            this.setData({
                                code: null
                            });
                            uni.showToast({
                                title: '兑换成功',
                                icon: 'success',
                                duration: 2000
                            });
                        } else {
                            uni.showToast({
                                title: '兑换码错误',
                                icon: 'fail',
                                duration: 2000
                            });
                        }
                    }
                });
            }
        }
    }
};
</script>
<style>
.line {
    display: flex;
}
.line .topLine {
    width: 33%;
    text-align: center;
    padding: 20rpx;
    color: #7b7b7b;
}

.line .topLine.active {
    position: relative;
    color: #181818;
    font-weight: bold;
    background: #fff;
}

.line .topLine.active:after {
		content: ' ';
		width: 1rem;
		height: 0.12rem;
		/* #ifdef MP-ALIPAY */
		width: 0.4rem;
		height: 0.04rem;
		/* #endif */
		background: #fff;
		position: absolute;
		/* bottom: -0.1rem; */
		bottom: -0.2rem;
		left: 50%;
		transform: translateX(-50%);
		background-color: #ff446b;
		border-radius: 0.12rem;
}

.code {
    height: 552rpx;
    background: #ffffff;
    padding: 200rpx 0 40rpx 0;
}
.code .input {
    width: 60%;
    margin: 0 20% 100px 20%;
    border-bottom: 2rpx solid #ff455b;
}

._button {
    width: 312rpx;
    background: linear-gradient(90deg, #ff4284 0%, #ff1d42 100%);
    border-radius: 44rpx;
    color: #fff;
    font-size: 28rpx;
    margin-bottom: 80rpx;
	margin: auto;
}
</style>
