<template>
	<view class="content">
		<!-- tabs -->
		<u-sticky bgColor="#FFF" zIndex="9" >
			<u-tabs
				:current="tabsCurrent"
				:list="tabs"
				:scrollable="false"
				lineColor="#446EFE"
				:activeStyle="{
					color: '#111',
					fontWeight: 'bold',
					fontSize: '32rpx'
				}"
				:inactiveStyle="{
					color: '#606266',
					fontSize: '30rpx'
				}"
				@click="onTabs"
			></u-tabs>
		</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">{{item.order.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">
							<text v-for="(itemType, indexType) in item.order.item_type" :key="indexType" v-if="itemType.number > 0">{{itemType.title}}x{{itemType.number}}</text>
						</view>
					</view>
					<view class="orders-content-block">
						<view class="item-flex" v-for="(citem, cindex) in item.order.items" :key="cindex">
							<view class="item-flex-title">{{citem.institution.title}}</view>
							<view class="item-flex-value">￥{{citem.price}}</view>
						</view>
					</view>
					<view class="orders-content-item" v-if="item.order.total > 0">
						<label>咨询服务费</label>
						<view class="nowrap">￥{{item.order.total}}</view>
					</view>
					<view class="orders-content-item">
						<label>下单时间</label>
						<view class="nowrap">{{item.created_at}}</view>
					</view>
				</view>
				<view class="orders-flex">
					<view class="user">
						<image class="user-cover" :src="item.to.avatar" mode="aspectFill"></image>
						<view class="user-name">{{item.to.nickname}}</view>
					</view>
					<view class="btns">
						<block v-if="type == 'in' && tabsCurrent == 0">
							<view class="btns-item btns-red" @click="onRefused(item.business_order_transfer_id)">拒绝</view>
							<view class="btns-item btns-border" @click="onAgree(item.business_order_transfer_id)">接收</view>
						</block>
						<view class="btns-item" @click="$Router.push({name: 'OrderInfo', params: {orderId: item.order.business_order_id}})">查看</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 订单是空的 -->
		<view class="order-null" v-else>
			<u-empty
				mode="order"
				icon="http://cdn.uviewui.com/uview/empty/order.png"
				text="暂无相关订单"
			>
			</u-empty>
		</view>
	</view>
</template>

<script>
	import { lists, transferAgree, transferRefuse } from '@/apis/interfaces/transfers.js'
	export default {
		data() {
			return {
				tabsCurrent	: 0,
				type		: 'in',
				tabs		: [
					{val: 1, name: '转让中'},
					{val: 3, name: '已转让'},
					{val: 2, name: '转让失败'},
				],
				orders		: []
			};
		},
		created() {
			this.type = this.$Route.query.type;
			this.getList()
		},
		methods: {
			// 切换列表
			onTabs(e){
				this.tabsCurrent = e.index
				this.orders = []
				this.getList()
			},
			// 变更当前列表状态
			removeListVal(id){
				let ListArr 	= this.orders
				let ListIndex 	= ListArr.findIndex(val => val.business_order_transfer_id == id)
				if(ListIndex >= 0){
					this.orders.splice(ListIndex, 1)
				}
				this.$store.commit('setOrderId', null)
			},
			// 接收订单
			onAgree(id){
				uni.showModal({
					title		: '接收提醒',
					content		: '确认接收该订单嘛？',
					confirmText	: '接收',
					success : ModalRes => {
						uni.showLoading({
							title: '加载中...',
							mask : true
						})
						if(ModalRes.confirm){
							transferAgree(id).then(res => {
								uni.showToast({
									title: res
								})
								// 移出操作后的订单
								this.removeItem(id)
							}).catch(err => {
								uni.showToast({
									title: err.message,
									icon : 'none'
								})
							})
						}
					}
				})
			},
			// 拒绝订单
			onRefused(id){
				uni.showModal({
					title		: '接收提醒',
					content		: '确认接收该订单嘛？',
					confirmText	: '拒绝',
					success : ModalRes => {
						if(ModalRes.confirm){
							transferRefuse(id).then(res => {
								uni.showToast({
									title: res
								})
								// 移出操作后的订单
								this.removeItem(id)
							}).catch(err => {
								console.log(err)
							})
						}
					}
				})
			},
			// 更新转让单
			removeItem(id){
				let index = this.orders.findIndex(val => val.business_order_id == id)
				if(index >= 0){
					this.orders.splice(index, 1)
				}
			},
			// 获取列表
			getList(){
				lists({
					status: this.tabs[this.tabsCurrent].val,
					type  : this.type
				}).then(res => {
					let { data } = res;
					this.orders = data
				}).catch(err => {
					uni.showToast({
						title: err.message,
						icon : 'none'
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	// 订单为空
	.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;
				}
			}
			&-type{
				text{
					margin-right: 30rpx;
					position: relative;
					display: inline-block;
					&::after{
						position: absolute;
						content: "/";
						width: 30rpx;
						text-align: center;
						font-size: 30rpx;
						top: 0;
						right: -30rpx;
					}
					&:last-child{
						margin-right: 0;
						&::after{
							display: none;
						}
					}
				}
			}
			&-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;
			}
			.no{
				font-size: 30rpx;
				color: #111;
				line-height: 60rpx;
				width: calc(100% - 230rpx);
			}
			.state{
				color: $main-color;
				font-weight: bold;
				font-size: 30rpx;
				line-height: 60rpx;
				width: 200rpx;
				text-align: right;
			}
			.user{
				padding-left: 90rpx;
				position: relative;
				min-height: 70rpx;
				box-sizing: border-box;
				width: calc(100% - 400rpx);
				.user-cover{
					position: absolute;
					left: 0;
					top: 0;
					width: 70rpx;
					height: 70rpx;
					border-radius: 50%;
					background: #ddd;
				}
				.user-name{
					line-height: 70rpx;
					font-size: 30rpx;
					font-weight: bold;
				}
			}
			.btns{
				width: 400rpx;
				text-align: right;
				.btns-item{
					display: inline-block;
					height: 70rpx;
					line-height: 70rpx;
					background: $main-color;
					color: white;
					border-radius: 35rpx;
					padding: 0 30rpx;
					font-size: 28rpx;
					&.btns-border{
						line-height: 66rpx;
						box-sizing: border-box;
						border:solid 1rpx $main-color;
						background: white;
						color: $main-color;
						margin-right: 20rpx;
					}
					&.btns-red{
						line-height: 66rpx;
						box-sizing: border-box;
						border:solid 1rpx red;
						background: white;
						color: red;
						margin-right: 20rpx;
					}
				}
			}
		}
	}
</style>
