<template>
	<view class="container" v-if="fetch">
		<use-tabbar :tabbar="false"></use-tabbar>
		<view class="w-full navbar-area bg-main">
			<view class="state-area dflex-a">
				<view v-for="(item, index) in fetch.nav" :key="index" :class="{ active: status === index }" class="nav-item dflex-c pos-r fs padding-lr-lg h-full" @click="tabClick(index)">
					{{ item.name }}
					<text v-if="item.num > 0">({{ item.num }})</text>
				</view>
			</view>
		</view>

		<scroll-view class="list-scroll-content h-full" scroll-y >
			<!-- 空白页 -->
			<u-empty v-if="fetch.list && fetch.list.length == 0" marginTop="200" mode="coupon" text="暂无权益包内容"></u-empty>
			
			<view v-else class="padding-lr" v-for="(item, index) in fetch.list" :key="index">
				<view class="coupon_box border-radius margin-top-sm bg-main" @click="showModal(item.title, item.tip)">
					<view class="dflex-b">
						<view class="left pos-a h-full dflex-c dflex-flow-c">
							<view>
								<text class="price fs-lg">{{ item.config_name }}</text>
							</view>
							<view class="fs-sm">{{item.type_name}}</view>
						</view>
						<view class="right padding left_t flex1">
							<view class="dflex-b padding-bottom-xs">
								<view class="fwb fs">{{ item.name }}</view>
								<text class="arrow" v-if="item.tip">›</text>
							</view>
							<view class="dflex-b ft-dark fs-xs padding-bottom border-line">
								<view class="">{{ item.desc }}</view>
							</view>
							<view class="dflex-b padding-top-xs">
								<view class="fs-xs ft-dark">{{ item.edate }}</view>
								<view v-if="item.yue > 0">
									<view class="btn-receive bg-base ft-white border-radius-sm fs-xs padding-lr-sm padding-tb-xxs"
										@click.stop="receive(index, item.type_name)">
										领取
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
		<u-modal @close="closeModal" @confirm="closeModal" :show="show.state" :title="show.title" :closeOnClickOverlay="true">
			<view class="slot-content">
				<u-parse :content="show.content"></u-parse>
			</view>
		</u-modal>
		
		<!--
		<view style="height: 76px;"></view>
		<view class="pos-f btn-container padding-tb bg-drak">
			<view class="dflex-b margin-lr border-radius-big safe-area-inset-bottom-plus">
				<view class="tac padding-tb-sm flex1 bg-warn" @click="duihuan">赠送</view>
				<view class="tac padding-tb-sm flex1 bg-base">售卖</view>
			</view>
		</view>
		-->
		
		<u-popup mode="center" @close="close('receive')" :show="show.receive" round="14" :closeable="true">
			<view class="alert-area border-radius bg-main pos-r">
				<view class="dflex-b ft-dark margin-bottom-sm margin-lr-xs">
					<view>领取数量：最多{{show.num}}</view>
				</view>
		
				<view class="dflex border-radius-sm field" style="padding: 8px 7px;">
					<u--input placeholder="请输入数量，填写数字即可" v-model="num"
						class="fwb fs-sm" type="number" border="none" :focus="true" clearable
						style="max-height: 76px; min-height: 19px; height: auto; font: initial;"></u--input>
				</view>
		
				<!-- 提交按钮 -->
				<view class="bg-base margin-top-xl tac w-full border-radius-lg padding-tb-sm" @click="useReceive">
					提交
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				id:0,
				fetch: {},
				status: 0,
				show : {
					state : false,
					title : '',
					content : '',
					receive : false,
				},
				num: '',
				index : -1,
			};
		},
		onLoad(options) {
			this.id = options.id;
			this.loadData();
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.loadData();
		},
		methods: {
			loadData() {
				this.DeverApi.get(this, 'perk.getList', {id:this.id,status:this.status});
			},
			// 顶部tab点击
			tabClick(index) {
				this.status = index;
				this.fetch.list = {}
				this.loadData();
			},
			showModal(title, content) {
				if (content) {
					this.show.title = title;
					this.show.content = content;
					this.show.state = true;
				}
			},
			closeModal() {
				this.show.state = false;
			},
			receive(index, yue) {
				this.index = index;
				this.show.num = yue;
				this.show.receive = true;
			},
			close() {
				this.show.receive = false;
			},
			useReceive() {
				this.num = parseFloat(this.num);
				if (this.num && this.num > 0 && this.index >= 0) {
					var item = this.fetch.list[this.index]
					if (!item) {
						this.Dever.alert('请选择领取的项目');
						return;
					}
					if (this.num > item.yue) {
						this.Dever.alert('领取数量不能超过当前剩余数量');
						return;
					}
					this.close();
					this.DeverApi.post('perk.use', {num:this.num,user_perk_id:this.id,id:item.id}, r => {
						this.num = '';
						this.Dever.success('领取成功', () => {
							this.loadData();
						});
					}, s => {
						this.num = '';
						this.Dever.alert(s.msg);
					});
				} else {
					this.Dever.alert('请输入领取数量');
				}
			},
		}
	};
</script>

<style lang="scss">
	page,
	.container {
		min-height: 100%;
		background: $page-color-base;
	}

	/* 优惠券状态区 */
	.navbar-area {
		white-space: nowrap;

		.state-area {
			height: 7vh;
			box-shadow: 0 1px 5px rgba(0, 0, 0, 0.06);
			z-index: 10;
		}

		.nav-item {
			flex: 1;

			&.active {
				&:after {
					content: '';
					position: absolute;
					left: 50%;
					transform: translate(-50%);
					bottom: 0;
					width: 44px;
					height: 0;
					border-bottom: 2px solid $base-color;
				}
			}
		}
	}

	.coupon_box {
		position: relative;

		&:last-child {
			margin-bottom: 20rpx;
		}

		.left {
			background-color: #6a92ff;
			color: #fff;
			width: 30%;

			.price {
				color: #fff !important;
			}
		}

		.right {
			margin-left: 30%;
		}

		.border-line {
			border-bottom: 1px dotted #ededed;
		}
	}

	/* 按钮样式 */
	.btn-receive {
		background: $base-color;
		color: #fff;
		font-weight: bold;
	}

	.btn-receive-disabled {
		background: #3dafcb;
		color: #fff;
		font-weight: bold;
	}
	
	.btn-container {
	    left: 9px;
	    right: 9px;
	    bottom: 0;
	}
</style>
