<template>
	<view class="content">
		<!-- tabs -->
		<u-sticky bgColor="#FFF" zIndex="9" customNavHeight="0" >
			<view class="screen-flex">
				<view class="screen-picker">
					<picker :range="statusArr" range-key="title" :value="statusIndex" @change="onPickerChange($event, 'statusIndex')">
						<view class="screen-text nowrap">
							{{ statusArr[statusIndex].title }}
							<uni-icons class="screen-icon" type="bottom" size="32rpx" color="#999"></uni-icons>
						</view>
					</picker>
				</view>
				<view class="screen-picker">
					<picker :range="empowerArr" range-key="title" :value="empowerIndex" @change="onPickerChange($event, 'empowerIndex')">
						<view class="screen-text nowrap">
							{{ empowerArr[empowerIndex].title }}
							<uni-icons class="screen-icon" type="bottom" size="32rpx" color="#999"></uni-icons>
						</view>
					</picker>
				</view>
			</view>
		</u-sticky>
		<!-- 订单管理列表 -->
		<view class="orders" v-if="orders.length > 0">
			<view class="orders-item" v-for="(item, index) in orders" :key="index">
				<view class="orders-flex">
					<view class="no nowrap" @click="copyNo(item.order_no)">
						<text class="orders-tag" v-if="!item.is_my">客户</text>
						<text class="orders-tag order-tag-my" v-else>个人</text>
						{{item.order_no}}
					</view>
					<view class="state">{{item.status_text}}</view>
				</view>
				<view class="orders-content">
					<view class="orders-content-item">
						<label>课程名称</label>
						<view class="nowrap orders-content-type">{{item.empower.title}}</view>
					</view>
					<view class="orders-content-item orders-content-bottom" @click="item.is_show_type = !item.is_show_type">
						<label>课程数量</label>
						<view class="nowrap orders-content-type">
							×{{item.items.count}}
						</view>
						<uni-icons class="orders-content-icon" :type="item.is_show_type ? 'top': 'bottom'" size="14" color="gray"></uni-icons>
					</view>
					<view class="orders-content-block" v-show="item.is_show_type">
						<view class="item-flex" v-for="(citem, cindex) in item.items.lists" :key="cindex">
							<view class="item-flex-title">{{citem.semester.subtitle}}({{citem.name}})</view>
							<view class="item-flex-value">￥{{citem.price}}</view>
						</view>
					</view>
					<view class="orders-content-item">
						<label>支付金额</label>
						<view class="nowrap">￥{{item.price}}</view>
					</view>
					<view class="orders-content-item">
						<label>下单时间</label>
						<view class="nowrap">{{item.created_at}}</view>
					</view>
					<view class="orders-content-item" v-if="item.paid_at != ''">
						<label>支付时间</label>
						<view class="nowrap">{{item.paid_at}}</view>
					</view>
				</view>
				<view class="orders-flex">
					<view class="btns">
						<view class="btns-item btns-border" v-if="item.status == 0" @click="onCancel(item.order_id)">取消</view>
						<view class="btns-item btns-border" v-if="item.status == 0" @click="onPay(item)">支付</view>
						<view class="btns-item" @click="onShowUsers(item.items)">报名信息</view>
					</view>
				</view>
			</view>
			<!-- 分页 -->
			<u-loadmore v-if="pagesShow" :status="status" />
		</view>
		<!-- 订单是空的 -->
		<view class="order-null" v-else>
			<u-empty
				mode="order"
				icon="http://cdn.uviewui.com/uview/empty/order.png"
				text="暂无相关订单"
			>
			</u-empty>
		</view>
		<!-- 弹出报名人信息 -->
		<u-popup :show="usersShow" closeable round="30rpx" @close="usersShow = false">
			<view class="users-content">
				<view class="users-title">报名信息</view>
				<view class="users-lists">
					<view class="users-lists-item" v-for="(item, index) in users" :key="index">
						<view class="users-item nowrap"><label>报名姓名</label>{{item.name}}</view>
						<view class="users-item nowrap"><label>报名手机</label>{{item.mobile}}</view>
						<view class="users-item nowrap"><label>课程名称</label>{{item.semester.subtitle}}</view>
						<view class="users-item nowrap"><label>签到状态</label><text class="bold">{{item.status_text}}</text></view>
						<view class="users-item"><label>课程地址</label>{{item.semester.address}}</view>
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import { orderInit, orderList, orderCancel } from '@/apis/interfaces/empower.js'
	export default {
		data() {
			return {
				statusArr	: [{ title: '全部订单', id: '' }],
				empowerArr	: [{ title: '全部课程', id: '' }],
				statusIndex	: 0,
				empowerIndex: 0,
				// 报名信息
				users		: [],
				usersTotal	: 0,
				usersShow	: false,
				// 订单列表
				orders		: [],
				// 分页
				page		: {
					current	: 1,
					has_more: false,
				},
				pagesShow	: false,
				status   	: ''
			};
		},
		created() {
			orderInit().then(res => {
				let { status, empower } = res;
				this.statusArr   = status
				this.empowerArr  = [...this.empowerArr, ...empower]
				// 获取列表
				this.getList()
			}).catch(err => {
				uni.showToast({
					title: err.message,
					icon : 'none'
				})
			})
		},
		methods: {
			// 报名信息
			onShowUsers(obj){
				let { count, lists } = obj
				this.users 		= lists
				this.usersTotal = count
				this.usersShow	= true
			},
			// 筛选类型
			onPickerChange(e, key){
				let { value } = e.detail
				this[key] = value
				this.page 			= { current: 1, has_more: false }
				this.orders 		= []
				this.getList()
			},
			// 获取列表
			getList(){
				uni.showLoading({
					title: '加载中...',
					mask : true
				})
				orderList({
					status	: this.statusArr[this.statusIndex].id,
					empower	: this.empowerArr[this.empowerIndex].id,
					page  	: this.page.current ,
				}).then(res => {
					let { data, page } = res;
					data.map(val => {
						val.is_show_type = false
					})
					let atList   	= page.current == 1 ? [] : this.orders
					this.orders 	= atList.concat(data)
					this.page  		= page
					this.pagesShow 	= false
					uni.hideLoading()
				}).catch(err => {
					uni.showToast({
						title: err.message,
						icon : 'none'
					})
				})
			},
			// 复制订单号码
			copyNo(no){
				uni.setClipboardData({
					data	: no,
					success	: res => {
						uni.showToast({
							title: '订单号已复制',
							icon : 'none'
						})
					}
				})
			},
			// 取消订单
			onCancel(id){
				uni.showModal({
					title	: '提示',
					content	: '取消订单后无法找回，确认取消吗？',
					success : modalRes => {
						if(modalRes.confirm){
							uni.showLoading({
								title: '加载中...',
								mask : true
							})
							orderCancel(id).then(res => {
								uni.showToast({
									title: '订单已取消',
									icon : 'none'
								})
								this.removeListVal(id)
							}).catch(err => {
								uni.showToast({
									title: err.message,
									icon : 'none'
								})
							})
						}
					}
				})
			},
			// 变更当前列表状态
			removeListVal(id){
				let ListArr 	= this.orders
				let ListIndex 	= ListArr.findIndex(val => val.business_order_id == id)
				if(ListIndex >= 0){
					this.orders.splice(ListIndex, 1)
				}
			},
			// 订单支付
			onPay(obj){
				let { order_id, order_type } = obj
				this.$Router.push({
					name: 'Pay',
					params: {
						orderId		: order_id,
						orderType	: order_type
					}
				})
			}
		},
		onReachBottom() {
			this.pagesShow = true;
			if(this.page.has_more){
				this.status    = 'loading';
				this.page.current++
				this.getList()
				return
			}
			this.status    = 'nomore';
		}
	}
</script>

<style lang="scss" scoped>
	.content{ background: #f7f8f9; min-height: calc(100vh - 44px); }
	// 订单弹出层
	.users-content{
		.users-title{
			text-align: center;
			font-weight: bold;
			font-size: 44rpx;
			color: #333;
			height: 70rpx;
			line-height: 70rpx;
			padding: 30rpx;
		}
		.users-lists{ 
			padding: 0 30rpx 50rpx;
			max-height: 70vh;
			overflow-y: scroll;
			.users-lists-item{
				background: #f7f8f9;
				padding: 30rpx;
				border-radius: 20rpx;
				font-size: 30rpx;
				margin-bottom: 30rpx;
				.users-item{
					position: relative;
					line-height: 45rpx;
					min-height: 45rpx;
					padding-left: 160rpx;
					margin: 5rpx 0;
					text-align: right;
					label{ position: absolute; left: 0; top: 0; color: gray; }
					.bold{ font-weight: bold; color: $mian-color; }
				}
			}
		}
	}
	// 订单筛选
	.screen-flex{
		display: flex;
		flex-wrap: wrap;
		.screen-picker{ width: 50%; text-align: center; }
		.screen-text{ line-height: 90rpx; height: 90rpx; display: inline-block; font-size: 30rpx; }
		.screen-icon{ margin-left: 10rpx; }
	}
	// 订单为空
	.order-null{
		height: 80vh;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	// 订单列表
	.orders{
		padding: 30rpx 0 10rpx;
		.orders-item{
			margin: 0 30rpx 20rpx;
			background-color: white;
			border-radius: $radius;
		}
		.orders-content{
			padding: 20rpx 30rpx;
			&-item{
				line-height: 70rpx;
				display: flex;
				justify-content: space-between;
				font-size: 30rpx;
				color: #111111;
				label{
					color: #999999;
				}
				&.orders-content-bottom{
					padding-right: 40rpx;
					position: relative;
					.orders-content-icon{
						position: absolute;
						right: 0;
						top: 0;
					}
				}
			}
			&-block{
				background: rgba(68, 110, 254, .03);
				padding: 20rpx;
				font-size: 28rpx;
				border-radius: 10rpx;
				margin: 10rpx 0;
				.item-flex{
					display: flex;
					justify-content: space-between;
					line-height: 50rpx;
				}
			}
		}
		.orders-flex{
			border-bottom: solid 1rpx #F6F6F6;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 20rpx $padding;
			&:last-child{
				border-top: solid 1rpx #F6F6F6;
				border-bottom: none;
			}
			.orders-tag{
				display: inline-block;
				background: $mian-color;
				font-size: 26rpx;
				color: white;
				border-radius: 10rpx;
				padding: 0 10rpx;
				height: 40rpx;
				line-height: 40rpx;
				margin-right: 10rpx;
				&.order-tag-my{
					background: $mian-color;
				}
			}
			.no{
				font-size: 30rpx;
				color: #111;
				line-height: 60rpx;
				width: calc(100% - 150rpx);
			}
			.state{
				color: $mian-color;
				font-weight: bold;
				font-size: 30rpx;
				line-height: 60rpx;
				width: 150rpx;
				text-align: right;
			}
			
			.btns{
				text-align: right;
				width: 100%;
				.btns-item{
					display: inline-block;
					height: 70rpx;
					line-height: 70rpx;
					background: $mian-color;
					color: white;
					border-radius: 35rpx;
					padding: 0 30rpx;
					font-size: 30rpx;
					&.btns-border{
						line-height: 68rpx;
						box-sizing: border-box;
						border:solid 1rpx $mian-color;
						background: white;
						color: $mian-color;
						margin-right: 20rpx;
					}
				}
			}
		}
	}
</style>
