<template>
	<view class="content">
		<!-- tabs -->
		<u-sticky bgColor="#FFF" zIndex="9" >
			<u-tabs
				:current="tabsCurrent"
				:list="tabs"
				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" @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 orders-content-bottom" @click="item.is_show_type = !item.is_show_type">
						<label>业务类型</label>
						<view class="nowrap orders-content-type">
							<text v-for="(itemType, indexType) in item.item_type" :key="indexType" v-if="itemType.number > 0">{{itemType.title}}x{{itemType.number}}</text>
						</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" :key="cindex">
							<view class="item-flex-title">{{citem.institution.title}}({{citem.business_type.title}})</view>
							<view class="item-flex-value">￥{{citem.price}}</view>
						</view>
					</view>
					<view class="orders-content-item" v-if="item.total > 0">
						<label>咨询服务费</label>
						<view class="nowrap">￥{{item.total}}</view>
					</view>
					<view class="orders-content-item" v-if="item.diff_prices_pays > 0">
						<label>补差价金额</label>
						<view class="nowrap">￥{{item.diff_prices_pays}}</view>
					</view>
					<view class="orders-content-item" v-if="item.deliver_count.can && item.deliver_count.num < item.deliver_count.all" @click="$Router.push({name: 'MailedOrder', params: {orderId: item.business_order_id}})">
						<label>邮寄资料</label>
						<view class="nowrap orders-content-btn">({{item.deliver_count.num}}/{{item.deliver_count.all}})去邮寄<uni-icons type="right" size="32rpx" color="#446EFE"></uni-icons></view>
					</view>
					<view class="orders-content-item" v-if="item.deliver_count.num > 0" @click="$Router.push({name: 'WorkExpress', params: {orderId: item.business_order_id}})">
						<label>邮寄状态</label>
						<view class="nowrap orders-content-btn">查看<uni-icons type="right" size="32rpx" color="#446EFE"></uni-icons></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" @click="makePhone(item.user.username)">
						<image class="user-cover" :src="item.user.avatar" mode="aspectFill"></image>
						<view class="user-name">{{item.user.nickname}}</view>
					</view>
					<view class="btns">
						<view class="btns-item btns-border" v-if="item.status.value == 0 || item.status.value == 2 || item.status.value == 3" @click="onCancel(item.business_order_id)">取消</view>
						<view class="btns-item btns-border" v-if="item.can.transfer" @click="onTransfers(index, item)">转让</view>
						<view class="btns-item btns-border" v-if="item.status.value == 2" @click="onOrdersService(item.business_order_id)">匹配</view>
						<view class="btns-item btns-border" v-if="item.status.value == 4 && item.is_my" @click="onSign(item.business_order_id)">签约</view>
						<view class="btns-item btns-border" v-if="item.status.value == 3" @click="$Router.push({name: 'Pay', params: {orderId: item.business_order_id, type: 'acting', paytype: 'price'}})">缴费</view>
						<view class="btns-item btns-border" v-if="item.status.value == 5" @click="$Router.push({name: 'WorkPerfectChoose', params: {orderId: item.business_order_id, type: 'perfect'}})">完善</view>
						<view class="btns-item btns-border" v-if="item.status.value == 6" @click="$Router.push({name: 'WorkPerfectChoose', params: {orderId: item.business_order_id }})">查看资料</view>
						<!-- type: 'perfect' -->
						<view class="btns-item btns-border" v-if="item.status.value == 8" @click="$Router.push({name: 'OrderLog', params: {orderId: item.business_order_id}})">查看记录</view>
						<view class="btns-item btns-border" v-if="item.can.confirm && item.is_my" @click="$Router.push({name: 'ConfirmScheme', params: {orderId: item.business_order_id,}})">去确认</view>
						<view class="btns-item" @click="$Router.push({name: 'OrderInfo', params: {orderId: item.business_order_id}})">查看</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-action-sheet
			:actions="actionLevels"
			description="请选择订单转让对象"
			cancelText="取消"
			round="10"
			:show="actionShow"
			@close="actionShow = false"
			@select="onActionSheet"
		></u-action-sheet>
	</view>
</template>

<script>
	import { lists, orderSign, getOrderSignStatus, orderCancel } from '@/apis/interfaces/order.js'
	import { levels, transfer } from '@/apis/interfaces/transfers.js'
import modal from 'uview-ui/libs/config/props/modal';
	export default {
		data() {
			return {
				tabsCurrent	: 0,
				tabType		: 0,
				tabs		: [
					{val: 0, name: '待预约'},
					{val: 2, name: '匹配服务包'},
					{val: 3, name: '待缴服务费'},
					{val: 4, name: '待签约'},
					{val: 5, name: '待完善资料'},
					{val: 6, name: '初审中'},
					{val: 7, name: '复审中'},
					{val: 8, name: '办理中'},
					{val: 9, name: '待结案'},
					{val: 10, name: '办理完成'}
				],
				orders		: [],
				actionShow	: false,
				actionLevels: [],
				getSignState: false,
				getNextTab	: false,
				// 分页
				page		: {
					current	: 1,
					has_more: false,
				},
				pagesShow	: false,
				status   	: ''
				
			};
		},
		created() {
			let { type } = this.$Route.query
			this.tabType = type;
			this.tabsCurrent = this.tabs.findIndex(val => val.val == type);
			this.getList()
		},
		onShow() {
			// 是否处理当前列表状态
			let isOrderId = this.$store.getters.getOrderId
			if(isOrderId != null){
				if(this.getSignState){
					this.onGetSign(isOrderId)
					return
				}
				// 匹配服务包变更了状态切换tab栏目获取新的列表数据
				if(this.getNextTab){
					this.tabsCurrent = this.tabsCurrent + 1
					this.tabType = this.tabs[this.tabsCurrent].val
					this.getList()
					this.getNextTab = false;
					return
				}
				this.getList()
				// this.removeListVal(isOrderId)
			}
		},
		methods: {
			// 切换列表
			onTabs(e){
				this.tabsCurrent = e.index
				this.tabType = e.val
				this.page = { current: 1, has_more: false }
				this.orders = []
				this.getList()
			},
			// 变更当前列表状态
			removeListVal(id){
				let ListArr 	= this.orders
				let ListIndex 	= ListArr.findIndex(val => val.business_order_id == id)
				if(ListIndex >= 0){
					this.orders.splice(ListIndex, 1)
				}
				this.$store.commit('setOrderId', null)
			},
			// 获取列表
			getList(){
				uni.showLoading({
					title: '加载中...',
					mask : true
				})
				if(this.$store.getters.getOrderId != null){
					this.page = { current: 1, has_more: false }
					this.$store.commit('setOrderId', null)
				}
				lists({
					status: this.tabType,
					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'
					})
				})
			},
			// 获取转让对象
			onTransfers(index, item){
				levels(item.business_order_id).then(res => {
					let ActionSheet = []
					for(let key in res){
						ActionSheet.push({
							key,
							name : res[key],
							val  : item,
							index
						})
					}
					this.actionLevels = ActionSheet
					this.actionShow = true
				}).catch(err => {
					uni.showToast({
						title: err.message,
						icon : 'none'
					})
				})
			},
			// 转让订单
			onActionSheet(e){
				uni.showLoading({
					title: '提交中',
					mask : true
				})
				transfer(e.val.business_order_id, e.key).then(res => {
					uni.hideLoading()
					uni.showModal({
						title		: '提示',
						content		: res,
						showCancel	: false,
						success: () => {
							e.val.can.transfer = false
							this.$set(this.orders, e.index, e.val)
						}
					})
				}).catch(err => {
					uni.showToast({
						title: err.message,
						icon : 'none'
					})
				})
			},
			// 匹配服务包
			onOrdersService(id){
				this.$Router.push({
					name: 'OrdersService',
					params: {
						orderId: id,
					},
				});
				this.getNextTab = true;
			},
			// 签约
			onSign(id){
				uni.showLoading({
					title: '获取签约信息...',
					mask : true
				})
				orderSign(id, {
					app_scheme: "doufire://",
					redirect_url: getApp().globalData.signUrl
				}).then(res => {
					this.$store.commit('setOrderId', id)
					this.getSignState = true
					this.$Router.push({
						name: 'ESign',
						params: {
							url: res.sign_url
						}
					})
					// plus.runtime.openURL(res.sign_url)
					uni.hideLoading()
				}).catch(err => {
					uni.showToast({
						title: err.message,
						icon : 'none'
					})
				})
			},
			// 查询订单签约状态
			onGetSign(id){
				uni.showLoading({
					title: '加载中...',
					mask : true
				})
				this.getSignState = false
				this.$store.commit('setOrderId', null)
				getOrderSignStatus(id).then(res => {
					if(res.status){
						this.removeListVal(id)
					}
					uni.hideLoading()
				}).catch(err => {
					uni.showToast({
						title: err.message,
						icon : 'none'
					})
				})
			},
			// 复制订单号码
			copyNo(no){
				uni.setClipboardData({
					data	: no,
					success	: res => {
						uni.showToast({
							title: '订单号已复制',
							icon : 'none'
						})
					}
				})
			},
			// 拨打电话
			makePhone(phone){
				uni.makePhoneCall({
					phoneNumber: phone,
					fail: err => {
						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'
								})
							})
						}
					}
				})
			}
		},
		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>
	// 订单为空
	.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-btn{
					color: $main-color;
				}
				&.orders-content-bottom{
					padding-right: 40rpx;
					position: relative;
					.orders-content-icon{
						position: absolute;
						right: 0;
						top: 0;
					}
				}
			}
			&-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;
			}
			.orders-tag{
				display: inline-block;
				background: $main-color;
				font-size: 26rpx;
				color: white;
				border-radius: 10rpx;
				padding: 0 10rpx;
				height: 40rpx;
				line-height: 40rpx;
				margin-right: 10rpx;
				&.order-tag-my{
					background: $text-price;
				}
			}
			.no{
				font-size: 30rpx;
				color: #111;
				line-height: 60rpx;
				width: calc(100% - 150rpx);
			}
			.state{
				color: $main-color;
				font-weight: bold;
				font-size: 30rpx;
				line-height: 60rpx;
				width: 150rpx;
				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: 30rpx;
					&.btns-border{
						line-height: 68rpx;
						box-sizing: border-box;
						border:solid 1rpx $main-color;
						background: white;
						color: $main-color;
						margin-right: 20rpx;
					}
				}
			}
		}
	}
</style>
