<template>
	<view class="content">
		<view class="filter-box">
			<u-search placeholder="请输入关键字" v-model="filterText" @search="setFilterText" @custom="setFilterText"></u-search>
		</view>
		<u-tabs-swiper ref="uTabs" :active-color="mainColor" :list="tabList" :current="current" @change="tabsChange"
		 :is-scroll="false"></u-tabs-swiper>
		<swiper :current="swiperCurrent" @transition="transition" @animationfinish="animationfinish" class="swiper-box">
			<swiper-item class="swiper-item" v-for="(item, index1) in tabList" :key="index1">
				<scroll-view scroll-y class="scroll-box" @scrolltolower="handleLoadMore" :refresher-enabled="true"
				 :refresher-triggered="triggered" :refresher-threshold="100" refresher-background="white" @refresherrefresh="onRefresh"
				 @refresherrestore="onRestore">
					<u-card :show-head="false" :foot-border-top="false" margin="10px 15px" borderRadius="40" :foot-style="{padding: '0 10px 5px 0'}" v-for="(site, index2) in item.tableList"
					 :key="index2" class="card-box">
						<view slot="body" class="card-content">
							<view class="student-info">
								<view style="width: 100%;">
									<text class="info-name" style="margin-right: 10px;">{{ site.studentName }}</text>
									<text class="info-name">{{ site.venueName }}</text>
								</view>
								<view class="info-phone">{{ site.className }}</view>
								<template v-if="site.giftList.length">
									<view class="info-phone" v-for="(iten, index3) in site.giftList" :key="index3">{{ iten.name }}</view>
								</template>
								<view class="info-phone">{{ getStatus(site.approvalStatus) }}</view>
							</view>
						</view>
						<view slot="foot" style="text-align: right;" v-if="site.giftType == 0 && site.approvalStatus == 3" >
							<u-button type="warning" :ripple="true" shape="circle" :custom-style="{...handleCustomStyle, marginRight: '5px'}" size="mini" @click="handleGiveClick(site.id,site.venueId)">赠送</u-button>
							<u-button type="warning" :ripple="true" shape="circle" :custom-style="handleCustomStyle" size="mini" @click="handleNoGiveClick(site.id)">不送</u-button>
						</view>
					</u-card>
					<u-divider v-if="item.isOver" bg-color="transparent" :style="{paddingTop : item.tableList.length == 0 ? '10px' : ''}">没有更多了</u-divider>
				</scroll-view>
			</swiper-item>
		</swiper>
		<!-- 礼物数据  -->
		<u-popup v-model="giftShow" mode="center" border-radius="30" width="600rpx" height="500px" >
			<view class="common-title">礼物</view>
			<view class="menber-box" style="overflow-y: auto;height:390px;">
				<u-form :model="form" ref="giftForm" label-width="80">
					<view style="display:flex;justify-content: space-between;" v-for="(item,index) in formInfo" :key="index">
						<u-form-item label="商品" :prop="item.signOne" required right-icon="arrow-right" style="width:44%;"  @click.native="handleShowProductClick(index)">
							<text>{{ item.signOne }}</text>
						</u-form-item>
						<u-form-item label="规格" :prop="item.two" required right-icon="arrow-right" style="width:44%;" @click.native="handleShowSpecClick(index)">
							<text>{{ item.signTwo }}</text>
						</u-form-item>
						<u-icon v-if="formInfo.length != 1" name="minus-circle" size="34" @click="handleGiftDelClick(index)"></u-icon>
					</view>
					<view style="width:100;display:flex;justify-content: flex-end;">
						<u-button type="warning"  :ripple="true" size="mini" :custom-style="{ backgroundColor: mainColor }"  @click="handleGiftAddClick">添加</u-button>
					</view>
				</u-form>
			</view>
			<view class="button-box">
				<u-button type="warning" shape="circle" :ripple="true" :custom-style="customStyle" @click="handleSubmitClick">确定</u-button>
			</view>
		</u-popup>
		<!-- 商品 -->
		<u-picker mode="selector" v-model="productShow" :range="productList" @confirm="handleSetProductClick" title="礼物" range-key="value"></u-picker>
		<!-- 规格 -->
		<u-picker mode="selector" v-model="specShow" :range="specList" @confirm="handleSetSpecClick" title="规格" range-key="value"></u-picker>
		<u-top-tips ref="uTips" z-index="12000"></u-top-tips>
	</view>
</template>

<script>
	import {
		mapGetters
	} from 'vuex'
	const NET = require('@/utils/request')
	const API = require('@/config/api')
	export default {
		data() {
			return {
				listId: 0,
				venueId: 0,
				// filterText: '',
				triggered: false,
				formInfo: [
					{
						signOneId: '',
						signOne: '',
						signTwoId: '',
						signTwo: ''
					}
				],
				tabList: [
				{
					name: '未赠送',
					isOver: false,
					pageIndex: 1,
					filterText: '',
					tableList: [],
				}, {
					name: '已赠送',
					isOver: false,
					pageIndex: 1,
					filterText: '',
					tableList: [],
				}, {
					name: '不赠送',
					isOver: false,
					pageIndex: 1,
					filterText: '',
					tableList: [],
				}],
				current: 0,
				swiperCurrent: 0,
				giftIndex: 0,
				giftShow: false,
				productShow: false,
				productList: [],
				specShow: false,
				specList: [],
			}
		},
		onShow() {
			this.getData()
		},
		computed: {
			...mapGetters([
				'mainColor',
				'customStyle',
				'handleCustomStyle',
				'handleDefaultCustomStyle',
			]),
			getStatus() {
				return function(index) {
					switch (index) {
						case 0:
							return '待审批'
						case 1:
							return '审批通过'
						case 2:
							return '驳回'
						case 3:
							return '提交'
					}
				}
			}
		},
		methods: {
			getData() {
				this.tabList = Object.assign([], this.$options.data().tabList)
				this.getTableList(0)
				this.getTableList(1)
				this.getTableList(2)
			},
			handleGiveClick(id,venueId) {
				this.giftShow = true
				this.listId = id
				this.venueId = venueId
			},
			handleShowProductClick(index) {
				this.giftIndex = index
				NET.request(API.findVenueGiftOneList, {
					id: this.venueId
				}, 'POST').then(res => {
					this.productList = res.data
					this.productShow = true
				})
			},
			handleSetProductClick(index) {
				this.formInfo[this.giftIndex].signOneId = this.productList[index].id
				this.formInfo[this.giftIndex].signOne = this.productList[index].value
			},
			handleShowSpecClick(index) {
				this.giftIndex = index
				if(!this.formInfo[this.giftIndex].signOneId) {
					this.$refs.uTips.show({
						title: '请先选择商品',
						type: 'warning',
					})
					return
				}
				const id = this.formInfo[this.giftIndex].signOneId
				const venueId = this.venueId
				NET.request(API.findVenueGiftTwoList, {
					id, venueId
				}, 'POST').then(res => {
					this.specList = res.data
					this.specShow = true
				})
			},
			handleSetSpecClick(index) {
				this.formInfo[this.giftIndex].signTwoId = this.specList[index].id
				this.formInfo[this.giftIndex].signTwo = this.specList[index].value
			},
			handleGiftAddClick() {
				this.formInfo.push({
					signOneId: '',
					signOne: '',
					signTwoId: '',
					signTwo: ''
				})
			},
			handleGiftDelClick(index) {
				this.formInfo.splice(index,1)
			},
			handleSubmitClick() {
				let flag = true
				this.formInfo.forEach( item => {
					if(!item.signOneId) {
						this.$refs.uTips.show({
							title: '请选择要赠送的礼物',
							type: 'warning',
						})
						flag = false
						return 
					}
					if(item.signOneId && !item.signTwoId) {
						this.$refs.uTips.show({
							title: `请选择商品${item.signOne}的规格`,
							type: 'warning',
						})
						flag = false
						return 
					}
				})
				if(flag) {
					const id = this.listId
					const venueId = this.venueId
					const giftIds = this.formInfo.filter(item => item.signTwoId != '').map( item => {
						return item.signTwoId
					})

					NET.request(API.giveGiftStudent, {
						id, venueId, giftIds
					}, 'POST').then(res => {
						if(res.status == 10000) {
							this.$refs.uTips.show({
								title: res.message,
								type: 'success',
							})
						} else {
							this.$refs.uTips.show({
								title: res.message,
								type: 'warning',
							})
						}
						this.getData()
						this.giftShow = false
					}).catch(error => {
						this.$refs.uTips.show({
							title: error.message,
							type: 'warning',
						})
					})
				}
			}, 
			// 不送
			handleNoGiveClick(id) {
				NET.request(API.giveGiftRefuse, {
					id
				}, 'POST').then(res => {
					if(res.status == 10000) {
						this.$refs.uTips.show({
							title: res.message,
							type: 'success',
						})
					} else {
						this.$refs.uTips.show({
							title: res.message,
							type: 'warning',
						})
					}
					this.getData()
				})
			},
			//  获取列表数据
			getTableList(index) {
				NET.request(API.giftStudentPage, {
					giftType: index,
					name: this.tabList[index].filterText,
					page: this.tabList[index].pageIndex,
					size: 10,
				}, 'POST').then(res => {
					this.triggered = false
					this.tabList[index].tableList = this.tabList[index].tableList.concat(res.data.row)
					this.tabList[index].isOver = res.data.row.length != 10
				}).catch(error => {
					this.triggered = false
					this.$refs.uTips.show({
						title: error.msg,
						type: 'warning',
					})
				})
			},
			//  设置过滤字段
			setFilterText(value) {
				this.tabList[this.current].filterText = value
				this.onRefresh()
			},
			//  tab页面切换
			tabsChange(index) {
				this.swiperCurrent = index;
			},
			//  swiper-item左右移动，通知tabs的滑块跟随移动
			transition(e) {
				let dx = e.detail.dx;
				this.$refs.uTabs.setDx(dx);
			},
			//  swiper滑动结束，分别设置tabs和swiper的状态
			animationfinish(e) {
				let current = e.detail.current;
				this.$refs.uTabs.setFinishCurrent(current);
				this.swiperCurrent = current;
				this.current = current;
			},
			//  下拉刷新
			onRefresh() {
				if (!this.triggered) {
					this.triggered = true
					this.tabList[this.current].isOver = false
					this.tabList[this.current].pageIndex = 1
					this.tabList[this.current].tableList = []
					this.getTableList(this.current)
				}
			},
			//  重置下拉刷新状态
			onRestore() {
				this.triggered = 'restore'
				this.triggered = false
			},
			//  懒加载
			handleLoadMore() {
				if (!this.tabList[this.current].isOver) {
					this.tabList[this.current].pageIndex++
					this.getTableList(this.current)
				}
			}
		},
	}
</script>

<style>
	page {
		width: 100%;
		height: 100%;
		background-color: #f7f7f7;
	}
</style>
<style lang="scss" scoped>
	@import "@/static/css/themes.scss";

	.content {
		width: 100%;
		float: left;

		.filter-box {
			height: 48px;
			padding: 10px 15px;
			background-color: #FFFFFF;
		}

		.swiper-box {
			height: calc(100vh - 82px);

			.swiper-item {
				height: calc(100vh - 82px);

				.scroll-box {
					width: 100%;
					height: calc(100vh - 82px);

					.card-box {
						.card-content {
							display: flex;
							align-items: center;

							.student-info {
								flex: 1;

								.info-name {
									// width: 64px;
									// float: left;
									line-height: 28px;
									font-size: 14px;
									font-weight: bold;
								}

								.info-type {
									padding: 0 10px;
									margin-top: 3px;
									border-radius: 20px;
									float: left;
									line-height: 20px;
									font-size: 10px;
									color: #FFFFFF;
									background-color: #999999;
								}

								.info-type-active {
									background-color: $mainColor;
								}

								.info-phone {
									// float: left;
									line-height: 28px;
									font-size: 12px;
									color: #999999;
								}
							}
						}
					}
				}
			}
		}
	}
	.menber-box {
		width: 100%;
		// float: left;
		padding: 10px 15px;
		margin-bottom: 10px;
		
		.menber-col {
			width: 100%;
			padding: 15px;
			margin-bottom: 10px;
			display: inline-block;
			background-color: #FFFFFF;
			border-radius: 15px;
			box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.2);
			position: relative;
			overflow: hidden;
			box-sizing: border-box;
			
			.menber-label {
				width: 100%;
				margin-bottom: 5px;
				float: left;
				font-size: 14px;
				// line-height: 20px;
			}
	
			.menber-num {
				width: 100%;
				float: left;
				font-size: 26px;
				line-height: 28px;
				color: $mainColor;
			}
	
			.menber-icon {
				font-size: 100px;
				color: $mainColor;
				position: absolute;
				right: -5px;
				bottom: -30px;
				opacity: 0.5;
			}
		}
	}
	.common-title {
		width:100%;
		text-align: center;
		font-size: 20px;
		margin: 10px 0;
	}
	.fix-add-icon {
		position: fixed;
		bottom: 15px;
		right: 15px;
	}
	.button-box {
		// width: 100%;
		padding: 10px 15px;
		box-sizing: border-box;
	}
</style>

