<template>
	<view class="app-container">
		<view class="tab-container">
			<view class="item" :class="tabCurrent === '' ? 'current':''" @tap="changeTab('')">全部({{count.all || 0}})</view>
			<view class="item" :class="tabCurrent === 0 ? 'current':''" @tap="changeTab(0)">未使用({{count.wsy || 0}})</view>
			<view class="item" :class="tabCurrent === 1 ? 'current':''" @tap="changeTab(1)">已使用({{count.ysy || 0}})</view>
		</view>
		
		<view class="list-container">
			<block v-for="(item, index) in dataList" :key='index'>
				<view class="item">
					<view class="left">
						<view class="row">
							<view class="label">优惠码：</view>
							<view class="value">{{item.id}}</view>
							<view class="copy" v-if="item.status == 0" @tap="copy(item.id)">复制</view>
						</view>
						<view class="row">
							<view class="label">生成时间：</view>
							<view class="value">{{item.bindTime}}</view>
						</view>
						<view class="row">
							<view class="label">有效时间：</view>
							<view class="value">{{item.startTime | dateToYYmmdd}} 至 {{item.endTime | dateToYYmmdd}}</view>
						</view>
						<view class="row" v-if="item.status == 1">
							<view class="label">使用时间：</view>
							<view class="value">{{item.useTime}}</view>
						</view>
					</view>
					<view class="right">
						<view class="status" :class="item.status ? 'on':''">{{item.status ? '已使用':'未使用'}}</view>
						<view class="price">{{item.amount | numToFixed}}</view>
					</view> 
				</view>
			</block>
		</view>
		<no-data v-if="!dataList.length" :showText="'空空如也'"></no-data>
		<loading-text v-if="dataList.length"  :loading="loading" :noMore="noMore" ></loading-text>
		
		<view class="bottom-container">
			<view class="button" @tap="isCreateDialog = true">生成优惠码</view>
		</view>
		
		<view class="create-dialog" v-show="isCreateDialog">
			<view class="dialog">
				<view class="title">生成优惠码</view>
				<view class="content">
					<view class="row">
						<text>优惠金额：</text><input type="digit" v-model="createForm.price" /><text>元</text>
					</view>
					<view class="row">
						<text>有效时间：</text>
						<radio-group @change="changeDay">
							<label v-for="(item, index) in days" :key="item.value">
								<radio style="transform:scale(0.7)" color="#FF3F42" :value="item.value" :checked="item.value == createForm.day" />
								<view>{{item.name}}</view>
							</label>
						</radio-group>
					</view>
				</view>
				<view class="btn">
					<view class="left" @tap="cancelCreateForm">取消</view>
					<view class="right" @tap="confirmCreateForm">生成优惠码</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {mapState} from 'vuex';
		
	export default {
		data() {
			return {
				configInfo: uni.getStorageSync('configInfo'),
				count: {}, // 优惠券数量统计
				tabCurrent: '', // 当前选择值
				dataList: [], // 优惠券列表
				pageNum: 1,
				pageSize: 8,
				noMore: false,
				loading: false,
				
				isCreateDialog: false,
				createForm: {
					price: '',
					day: 1,
				},
				days: [
					{value: 1, name: '1天'},
					{value: 3, name: '3天'},
					{value: 5, name: '5天'},
				]
			}
		},
		
		computed:{
			...mapState(['userInfo', 'isLogin', 'userId'])
		},
		
		onLoad() {
			this.getCount();
			this.getList();
		},
		
		// 下拉刷新
		onPullDownRefresh() {
			this.pageNum = 1;
			this.getList();
		},
		
		// 上拉加载
		onReachBottom() {
			this.getList(1);
		},
		
		methods: {
			getCount() {
				this.$axios({
					url: '/exchange/code/count',
					method: 'get',
					params: {
						userId: this.userId
					},
				}).then(res => {
					this.count = res.data;
				})
			},
			
			getList(loadMore) {
				if(this.noMore && loadMore)return;
				this.noMore = false
				if(!loadMore){
					this.pageNum = 1;
				}else{
					this.loading = true;
				}
				this.$axios({
					url: '/exchange/code/list',
					method: 'get',
					params: {
						pageNo: this.pageNum,
						pageSize: this.pageSize,
						status: this.tabCurrent
					},
					isLoading: !loadMore
				}).then(res => { 
					let _list = res.data.records;
					let pageTotal = res.data.pages;
					if(this.pageNum >= pageTotal){
						this.noMore = true;
					}
					if (_list.length) {
						this.pageNum += 1
					}
					if (loadMore) {
						this.dataList = this.dataList.concat(_list);
						this.loading = false;
					} else {
						this.dataList = _list;
					}
					
					uni.stopPullDownRefresh();
				})
			},
			
			changeTab(tab) {
				this.tabCurrent = tab;
				this.getList();
			},
			
			// 复制
			copy(val) {
				uni.setClipboardData({
				    data: val,
				    success: () => {
				        this.$successToast('复制成功');
				    }
				});
			},
			
			changeDay(e) {
				this.createForm.day = e.detail.value;
			},
			
			// 取消表单
			cancelCreateForm() {
				this.createForm.price = '';
				this.createForm.day = 1;
				this.isCreateDialog = false;
			},
			
			// 提交表单
			confirmCreateForm() {
				if(!this.createForm.price) {
					return this.$toast('请填写优惠金额');
				}
				this.$axios({
					url: '/exchange/code/bind',
					method: 'post',
					params: {
						amount: this.createForm.price,
						day: this.createForm.day,
					},
					isLoading: 1
				}).then(res => {
					this.$successToast('生成成功');
					this.cancelCreateForm();
					this.getCount();
					this.getList();
				})
			},
		}
	}
</script>

<style lang="scss">
	.app-container {
		background: #F4F2F2;
		padding: 108rpx 20rpx 120rpx;
		box-sizing: border-box;
	}
	.tab-container {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		display: flex;
		background: #FFFFFF;
		.item {
			flex: 1;
			height: 88rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			position: relative;
			&.current {
				color: #FF3F42;
				&::after {
					content: '';
					display: block;
					width: 80rpx;
					height: 6rpx;
					background: #FF3F42;
					position: absolute;
					bottom: 0;
					left: 50%;
					margin-left: -40rpx;
				}
			}
		}
	}
	.list-container {
		.item {
			background: #FFFFFF;
			border-radius: 20rpx;
			padding: 12rpx 20rpx;
			display: flex;
			justify-content: space-between;
			margin-bottom: 20rpx;
			.left {
				.row {
					display: flex;
					align-items: center;
					padding: 10rpx 0;
					.label {
						font-size: 28rpx;
						color: #666666;
						width: 140rpx;
					}
					.value {
						font-size: 28rpx;
						color: #666666;
					}
					.copy {
						width: 80rpx;
						height: 40rpx;
						text-align: center;
						line-height: 40rpx;
						border-radius: 8rpx;
						background: #577BFF;
						color: #FFFFFF;
						font-size: 24rpx;
						margin-left: 16rpx;
					}
				}
			}
			.right {
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				align-items: flex-end;
				padding: 10rpx 0;
				.status {
					font-size: 28rpx;
					color: #666666;
					&.on {
						color: #3F9EFF;
					}
				}
				.price {
					font-size: 36rpx;
					color: #FE781F;
				}
			}
		}
	}
	.bottom-container {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		padding: 0 20rpx;
		box-sizing: border-box;
		display: flex;
		height: 120rpx;
		.button {
			width: 100%;
			height: 88rpx;
			line-height: 88rpx;
			border-radius: 88rpx;
			text-align: center;
			font-size: 28rpx;
			color: #ffffff;
			background: linear-gradient(-90deg,#ff3f42 0%, #fe781f 100%);
		}
	}
	.create-dialog {
		position: fixed;
		top: 0;
		left: 0;
		z-index: 999;
		width: 100%;
		height: 100%;
		background: rgba(0, 0, 0, 0.3);
		display: flex;
		align-items: center;
		justify-content: center;
		.dialog {
			width: 600rpx;
			background: #FFFFFF;
			border-radius: 20rpx;
			overflow: hidden;
			.title {
				font-size: 36rpx;
				font-weight: 500;
				text-align: center;
				line-height: 100rpx;
				padding-bottom: 10rpx;
			}
			.content {
				padding: 20rpx 40rpx;
				.row {
					height: 60rpx;
					display: flex;
					align-items: center;
					margin-bottom: 20rpx;
					font-size: 32rpx;
					color: #333333;
					input {
						width: 260rpx;
						height: 60rpx;
						border: 1px solid #E5E5E5;
						margin-right: 16rpx;
						padding: 0 12rpx;
					}
					::v-deep radio-group {
						display: flex;
						align-items: center;
						label {
							display: flex;
							align-items: center;
							margin-right: 10rpx;
							font-size: 28rpx;
						}
					}
				}
			}
			.btn {
				border-top: 1px solid #eaeaea;
				display: flex;
				&> view {
					flex: 1;
					text-align: center;
					line-height: 100rpx;
					font-size: 32rpx;
					&.left {
						color: #666666;
					}
					&.right {
						color: #FFFFFF;
						background: #FF3F42;
					}
				}
			}
		}
		
	}
</style>
