<template>
	<view class="content">
		<view class="order-title">
			<view class="body-title">
				<view class="goback-box" @click="toBack">
					<image class="goback" src="../../static/icon/fanhui.png" mode=""></image>
				</view>
				<view class="header">接单中心</view>
			</view>
			<view class="roder-content">
				<view class="left" :class="{ current: currentIndex === index }" :key="index"
					v-for="(item, index) in maiList" @click="navGo(index)">
					{{ item.title }}
					<view class="dian" v-if="currentIndex != index && item.content != 0">{{ item.content }}</view>
				</view>
			</view>
		</view>
		<scroll-view class="list-scroll-content" scroll-y @scrolltolower="loadData">
			<!-- 订单列表 -->
			<view class="order-item" v-for="(item, index) in maiList[currentIndex].orderList">
				<view class="order-top flex">
					<view class="order-top-left">
						<image class="order-top-left-image" src="../../static/img/order5.png" mode=""></image>
						<view class="order-top-left-font">订单信息</view>
					</view>
					<view class="order-top-right">待接单</view>
				</view>
				<view class="order-info flex">
					<view class="order-info-left">预约时间：</view>
					<view class="order-info-right">{{ item.time }}</view>
				</view>
				<view class="order-info flex">
					<view class="order-info-left">姓名：</view>
					<view class="order-info-right">{{ item.name }}</view>
				</view>
				<view class="order-info flex">
					<view class="order-info-left">性别：</view>
					<view class="order-info-right">{{ item.sex == 1 ? '男' : '女' }}</view>
				</view>
				<view class="order-info flex" v-if="currentIndex == 0">
					<view class="order-info-left">代办医院：</view>
					<view class="order-info-right">{{ item.hospital }}</view>
				</view>
				<view class="order-info flex" v-if="currentIndex == 0">
					<view class="order-info-left">代办类型：</view>
					<view class="order-info-right">{{ item.catename }}</view>
				</view>
				<view class="order-info flex" v-if="currentIndex == 1">
					<view class="order-info-left">医院：</view>
					<view class="order-info-right">{{ item.hospital }}</view>
				</view>
				<view class="order-info flex" v-if="currentIndex == 1">
					<view class="order-info-left">陪诊类型：</view>
					<view class="order-info-right">{{ item.catename }}</view>
				</view>
				<view class="order-info flex" v-if="currentIndex == 1">
					<view class="order-info-left">陪诊服务：</view>
					<view class="order-info-right">{{ item.servicename }}</view>
				</view>
				<view class="order-info flex" v-if="currentIndex == 1 && item.issm == 2">
					<view class="order-info-left">接送地址：</view>
					<view class="order-info-right">{{ item.transfer_address }}</view>
				</view>
				<view class="order-info flex">
					<view class="order-info-left">备注：</view>
					<view class="order-info-right">{{ item.remarks || '无' }}</view>
				</view>
				<view class="order-info flex">
					<view class="order-info-left">佣金：</view>
					<view class="order-info-price">{{ item.commission }}</view>
				</view>
				<view class="order-btn" @click="takeOrder(item)">
					<image class="order-btn-icon" src="../../static/img/order5.png" mode=""></image>
					<view class="order-btn-font">立即接单</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	import empty from '@/components/empty';
	import {
		order_receiving_hall,
		order_receiving
	} from '@/api/jdr.js';
	import {
		getTime
	} from '@/utils/rocessor.js';
	export default {
		components: {
			uniLoadMore,
			empty
		},
		data() {
			return {
				currentIndex: 0, //当前选中的第一级0为我要买单，1我要卖单
				maiList: [{
						state: 2,
						title: '代办订单',
						content: '0',
						loadingType: 'more',
						orderList: [],
						page: 1, //当前页数
						limit: 10 //每次信息条数
					},
					{
						state: 1,
						title: '陪诊订单',
						content: '0',
						loadingType: 'more',
						orderList: [],
						page: 1, //当前页数
						limit: 10 //每次信息条数
					}
				]
			};
		},

		onLoad(options) {
			this.shul();
			this.loadData();
		},
		methods: {
			navGo(index) {
				this.currentIndex = index;
				this.loadData('shua');
			},
			toBack() {
				uni.switchTab({
					url: '/pages/user/my'
				});
			},
			shul() {
				order_receiving_hall({
					type: 2
				}).then(({
					data
				}) => {
					this.maiList[0].content = data.count;
				});
				order_receiving_hall({
					type: 1
				}).then(({
					data
				}) => {
					this.maiList[1].content = data.count;
				});
			},
			loadData(source) {
				//这里是将订单挂载到tab列表下
				let index = this.currentIndex;
				let navItem = this.maiList[index];
				let state = navItem.state;
				if (source === 'shua') {
					navItem.loaded = false;
					navItem.page = 1;
					navItem.limit = 10;
					navItem.loadingType = 'more';
					navItem.orderList = [];
				}
				if (source === 'tabChange' && navItem.loaded === true) {
					//tab切换只有第一次需要加载数据
					return;
				}
				if (navItem.loadingType === 'loading') {
					//防止重复加载
					return;
				}
				if (navItem.loadingType === 'noMore') {
					//防止重复加载
					return;
				}
				// 修改当前对象状态为加载中
				navItem.loadingType = 'loading';

				order_receiving_hall({
						type: state,
						page: navItem.page,
						limit: navItem.limit
					})
					.then(({
						data
					}) => {
						console.log(data);
						data.data.forEach(e => {
							if (e.remarks == undefined || e.remarks == 'undefined') {
								e.remarks = '无';
							}
							let key = Object.keys(e.cate);
							let name = '';
							key.forEach(e => {
								if (name == '') {
									name = name + e;
								} else {
									name = name + '/' + e;
								}
							});
							e.catename = name;
							let key1 = Object.keys(e.service);
							let name1 = '';
							e.issm = 1;
							key1.forEach(es => {
								console.log(e);
								if (es == '上门接送') {
									e.issm = 2;
								}
								if (name1 == '') {
									name1 = name1 + es;
								} else {
									name1 = name1 + '/' + es;
								}
							});
							e.servicename = name1;
							e.time = getTime(e.time);
						});
						navItem.orderList = navItem.orderList.concat(data.data);
						// console.log(navItem.orderList);
						navItem.page++;
						if (navItem.limit == data.data.length) {
							//判断是否还有数据， 有改为 more， 没有改为noMore
							navItem.loadingType = 'more';
							return;
						} else {
							//判断是否还有数据， 有改为 more， 没有改为noMore
							navItem.loadingType = 'noMore';
						}
						uni.hideLoading();
						this.$set(navItem, 'loaded', true);
					})
					.catch(e => {
						console.log(e);
					});
			},
			takeOrder(opt) {
				const obj = this;
				uni.showModal({
					title: '提示',
					content: '是否接取该订单，订单佣金:' + opt.commission + '前往医院:' + opt.hospital,
					success: function(res) {
						if (res.confirm) {
							order_receiving({}, opt.id).then(e => {
								uni.showToast({
									title: '接单成功',
									duration: 2000
								});
								wx.getSetting({
									withSubscriptions: true,
									success(res) {
										if (res[
											'2FOjTcnlEPwVnHoXzppZiuB3dXWSfpn8qJlD6p3s0GA'] ==
											'accept') {
											console.log('订阅成功');
										}
									}
								});
								if (wx.requestSubscribeMessage) {
									wx.requestSubscribeMessage({
										tmplIds: [
											'2FOjTcnlEPwVnHoXzppZiuB3dXWSfpn8qJlD6p3s0GA'],
										success(res) {},
										fail(err) {
											obj.$api.msg('订阅消息失败');
											console.log(err);
										},
										complete() {}
									});
								}
								obj.loadData('shua');
								obj.shul();
							});
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			}
		}
	};
</script>

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

	.order-title {
		width: 750rpx;
		height: 270rpx;
		background: url(../../static/img/jrzxbg.png) repeat-x 0;
		display: flex;
		flex-direction: column;
		text-align: center;

		.body-title {
			height: 80rpx;
			text-align: center;
			font-size: 35rpx;
			position: relative;

			.header {
				position: absolute;
				left: 0;
				top: 30rpx;
				width: 100%;
				font-size: 36rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #fffeff;
				height: 80rpx;
				font-size: 36rpx;
				font-weight: 700;
				z-index: 9;
				display: flex;
				justify-content: center;
				align-items: center;
			}

			.goback-box {
				position: absolute;
				left: 18rpx;
				top: 30rpx;
				height: 80rpx;
				display: flex;
				align-items: center;
			}

			.goback {
				z-index: 100;
				width: 34rpx;
				height: 34rpx;
			}
		}

		text {
			margin: 30px 0;
			font-size: 17px;
			font-family: PingFang SC;
			font-weight: bold;
			color: #ffffff;
		}

		.roder-content {
			margin-top: 80rpx;
			display: flex;
			justify-content: space-around;

			.left {
				position: relative;
				width: 168px;
				height: 40px;
				line-height: 40px;
				border: 1px solid #ffffff;
				border-radius: 5px;
				text-align: center;
				font-size: 15px;
				font-family: PingFang SC;
				font-weight: bold;
				color: #ffffff;
				background: #24a17d;
			}

			.dian {
				position: absolute;
				z-index: 2;
				top: 10rpx;
				right: 20%;
				width: 40rpx;
				height: 40rpx;
				border-radius: 50%;
				background-color: red;
				color: #ffffff;
				font-size: 24rpx;
				display: flex;
				align-items: center;
				justify-content: center;
			}

			.current {
				color: #24a17d;
				background: #ffffff;
			}
		}
	}

	.list-scroll-content {
		height: calc(100% - 150px);
	}

	.order-item {
		width: 702rpx;
		background: #ffffff;
		box-shadow: 0px 0px 20rpx 0px rgba(50, 50, 52, 0.06);
		border-radius: 20rpx;
		margin: 20rpx auto 0;
		padding: 24rpx;

		.order-top-left {
			display: flex;
			align-items: center;

			.order-top-left-image {
				width: 32rpx;
				height: 37rpx;
			}

			.order-top-left-font {
				margin-left: 10rpx;
				font-size: 30rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #333333;
			}
		}

		.order-top-right {
			font-size: 26rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #ff6f0f;
		}

		.order-info {
			margin-top: 24rpx;
			align-items: flex-start;

			.order-info-left {
				width: 200rpx;
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #666666;
			}

			.order-info-right {
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #333333;
			}

			.order-info-price {
				font-size: 38rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #ff6f0f;
			}
		}

		.order-btn {
			margin: 34rpx auto;
			align-items: center;
			display: flex;
			justify-content: center;
			width: 566rpx;
			height: 74rpx;
			background: #24a17d;
			border-radius: 15rpx;

			.order-btn-icon {
				width: 35rpx;
				height: 36rpx;
			}

			.order-btn-font {
				margin-left: 7rpx;
				font-size: 30rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #ffffff;
			}
		}
	}
</style>