<template>
	<view class="order">
		<view class="order-tab">
			<u-tabs :list="list" :is-scroll="false" active-color="#222222" inactive-color="#666666" font-size="28" :active-item-style="activeStyle" bar-height="6" bar-width="60" v-model="current" @change="changeTabs"></u-tabs>
		</view>
		<view class="order-list">
			<!-- 订单状态：0待付款1待接单2已接单3服务中4已完成5已取消6已退款 -->
			<view class="item" v-for="(a,i) in orderList" :key="i" @click="toDetail(a)">
				<view class="a">订单号：{{a.orderno}}<text>{{a.status == 0?'待付款':a.status == 1?'待接单':a.status == 2?'已接单':a.status == 3?'服务中':a.status == 4?'已完成':a.status == 5?'已取消':'已退款'}}</text></view>
				<view class="b">
					<image :src="a.img"></image>
					<view class="info">
						<view class="name">{{a.nickname}}陪玩师</view>
						<view class="desc">约玩项目：{{a.playConfigName}}</view>
						<view class="price">￥{{a.totalMoney-a.travelExpensesTotal}}元/{{a.duration}}小时</view>
					</view>
				</view>
				<view class="c">
					<!-- 订单状态：0待付款1待接单2已接单3服务中4已完成5已取消6已退款 -->
					<view class="btn" v-if="a.status == 0" @click.stop="toCancle(a)">取消订单</view>
					<view class="btn btn-01" v-if="a.status == 0" @click.stop="toZhifu(a)">立即支付</view>
					<view class="btn btn-01" v-if="a.status == 1" @click.stop="toShengqing(a)">申请退款</view>
					<view class="btn btn-01" v-if="a.status == 3" @click.stop="toFuwu(a)">服务完成</view>
					<view class="btn" v-if="a.status == 4">服务完成</view>
					<view class="btn btn-01" v-if="a.status == 5" @click.stop="toDel(a)">删除订单</view>
					<view class="btn" v-if="a.status == 5">已取消</view>
					<view class="btn" v-if="a.status == 6">已退款</view>
				</view>
			</view>
			<view class="empty" v-if="total == 0 ">
				<image src="@/static/empty.png"></image>
				<view class="txt">暂无数据</view>
			</view>
		</view>
		<u-popup v-model="show" mode="bottom">
			<view class="payPopup">
				<view class="title">选择支付方式</view>
				<view class="content">
					<template v-for="(a,i) in payInfo">
						<view class="item" v-if="a=='wx-1' || a=='ali-1'" :key="i" :class="payIndex == i?'item-on':''" @click="tabItem(i,a)">
							<view class="a" v-if="a=='wx-1'"><image src="@/static/wxpay.png"></image>微信支付</view>
							<view class="a" v-if="a=='ali-1'"><image src="@/static/zfb.png"></image>支付宝支付</view>
							<view class="b">
								<image class="img1" src="@/static/icon-check.png"></image>
								<image class="img2" src="@/static/icon-check-on.png"></image>
							</view>
						</view>
					</template>
				</view>
				<view class="fd">
					<view class="btn" @click="toPay()">立即支付</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				payIndex: 0,
				current: 0,
				list:[{'name': '全部'},{'name': '待付款'},{'name': '待接单'},{'name': '已接单'},{'name': '服务中'},{'name': '已完成'}],
				activeStyle:{'fontSize': '30rpx'},
				orderList: [],
				page: 1,
				total: 1,
				payInfo: {},
				obj: {},
				payType: ""
			};
		},
		onLoad(o) {
			this.current = o.type;
			this.getList()
		},
		// onReachBottom() {
		// 	const that = this;
		// 	uni.showLoading({
		// 		title: "加载中"
		// 	})
		// 	that.page ++;
		// 	setTimeout(function() {
		// 		that.getList(1);
		// 	}, 1000);
		// },
		methods: {
			tabItem(index,a){
				this.payIndex = index;
				this.payType = a
			},
			toZhifu(a) {
				this.obj = a;
				this.getConfigData();
			},
			//字典查询
			async getConfigData() {
				const that = this;
				let res1 = await that.$api.getConfigData({dictType: 'CONFIG_PAY_STATUS'});
				if(res1.data.content){
					this.payInfo = res1.data.content.split(",");
					this.show = true;
				}
			},
			//支付
			async toPay() {
				const that = this;
				const {code, data, msg} = await that.$api.paymentOrder({
					payType: that.payType =='wx-1'?1:2,
					outTradeNo: that.obj.orderno
				})
				that.show = false;
				if(code == 200){
					uni.hideLoading()
					uni.requestPayment({
					   provider: that.payType == 'ali-1'?'alipay':'wxpay',
					   orderInfo: that.payType == 'ali-1'?data.orderInfo:JSON.parse(data.orderInfo), //微信、支付宝订单数据 【注意微信的订单信息，键值应该全部是小写，不能采用驼峰命名】
						success: function (res) {
							uni.showToast({
								title: "支付成功"
							})
							setTimeout(()=>{
								that.getList();
							},2000)
						},
						fail: function (err) {
							console.log('fail:' + JSON.stringify(err));
							uni.showToast({
								icon: "error",
								title: "支付失败"
							})
						}
					});
				}else{
					uni.hideLoading();
					uni.showToast({
						icon: "none",
						position: "bottom",
						title: msg
					})
				}
			},
			toDetail(a){
				uni.navigateTo({
					url: "/pages/users/order/detail?type=1&id="+a.id
				})
			},
			//服务完成
			toFuwu() {
				const that = this;
				uni.showModal({
					title: "温馨提示",
					content: "是否要完成该订单服务？",
					async success(res) {
						if(res.confirm){
							const { code, data, msg } = await that.$api.serviceCompleteOrder({orderId: a.id,delType: 1, userId: uni.getStorageSync("userInfo").id});
							if(code == 200){
								uni.showToast({
									title: "提交成功"
								})
								setTimeout(()=>{
									that.getList();
								},2000)
							}else{
								uni.showToast({
									title: msg,
									icon: "none",
									position: "bottom"
								})
							}
						}
					}
				})
			},
			//申请订单
			toShengqing(a) {
				const that = this;
				uni.showModal({
					title: "温馨提示",
					content: "是否要申请退款？",
					async success(res) {
						if(res.confirm){
							const { code, data, msg } = await that.$api.refundOrder({orderId: a.id,delType: 1, userId: uni.getStorageSync("userInfo").id});
							if(code == 200){
								uni.showToast({
									title: "申请成功"
								})
								setTimeout(()=>{
									that.getList();
								},2000)
							}else{
								uni.showToast({
									title: msg,
									icon: "none",
									position: "bottom"
								})
							}
						}
					}
				})
			},
			//删除订单
			toDel(a) {
				const that = this;
				uni.showModal({
					title: "温馨提示",
					content: "是否要删除该订单？",
					async success(res) {
						if(res.confirm){
							const { code, data, msg } = await that.$api.deleteOrder({orderId: a.id,delType: 1, userId: uni.getStorageSync("userInfo").id});
							if(code == 200){
								uni.showToast({
									title: "删除成功"
								})
								setTimeout(()=>{
									that.getList();
								},2000)
							}else{
								uni.showToast({
									title: msg,
									icon: "none",
									position: "bottom"
								})
							}
						}
					}
				})
			},
			//取消订单
			toCancle(a) {
				const that = this;
				uni.showModal({
					title: "温馨提示",
					content: "是否要取消该订单？",
					async success(res) {
						if(res.confirm){
							const { code, data, msg } = await that.$api.cancelOrder({orderId: a.id,userId: uni.getStorageSync("userInfo").id});
							if(code == 200){
								uni.showToast({
									title: "取消成功"
								})
								setTimeout(()=>{
									that.getList();
								},2000)
							}else{
								uni.showToast({
									title: msg,
									icon: "none",
									position: "bottom"
								})
							}
						}
					}
				})
			},
			//数组对象去重
			arrayUnique (arr, name) {
				var hash = {};
				return arr.reduce(function (item, next) {
					hash[next[name]]
						? ""
						: (hash[next[name]] = true && item.push(next));
					return item;
				}, []);
			},
			async getList(type) {
				const { code, data } = await this.$api.orderList({
					userId: uni.getStorageSync("userInfo").id,
					status: Number(this.current)-1,
					limit: 20,
					page: this.page,
					userType: 1
				})
				if(type == 1){
					uni.hideLoading();
				}
				if(code == 200){
					// this.total = data.total;
					// this.orderList = this.arrayUnique([...this.orderList,...data.list],'id');
					this.total = data.length == 0?0:1;
					this.orderList = data;
				}
			},
			changeTabs(index){
				this.current = index
				this.page = 1;
				this.orderList = []
				this.getList();
			},
		}
	}
</script>

<style scoped lang="scss">
.order{
	.payPopup{
		padding: 40rpx;
		overflow: hidden;
		.title{
			font-weight: 500;
			font-size: 36rpx;
			color: #333333;
			text-align: center;
		}
		.btn{
			width: 100%;
			line-height: 98rpx;
			background: linear-gradient(0deg, #000000, #3D3B38);
			box-shadow: 0px 4rpx 18rpx 0px rgba(42,41,39,0.34);
			border-radius: 98rpx;
			font-weight: 400;
			text-align: center;
			font-size: 30rpx;
			color: #FFFFFF;
			margin-top: 30rpx;
		}
		.content{
			padding: 0 30rpx;
			background: #FFFFFF;
			border-radius: 20rpx;
			margin-top: 20rpx;
			.item{
				padding: 30rpx 0;
				display: flex;
				align-items: center;
				justify-content: space-between;
				font-weight: 400;
				font-size: 28rpx;
				color: #333333;
				border-top: 1px solid #EAEAEA;
				&:first-child{
					border-top-color: #FFFFFF;
				}
				.a{
					flex: 1;
					display: flex;
					align-items: center;
					image{
						width: 60rpx;
						height: 60rpx;
						margin-right: 10rpx;
					}
				}
				.b{
					font-size: 28rpx;
					color: #CCCCCC;
					display: flex;
					align-items: center;
					image{
						width: 32rpx;
						height: 32rpx;
						margin-left: 20rpx;
					}
					.img1{
						display: block;
					}
					.img2{
						display: none;
					}
				}
				&-on{
					.b{
						.img1{
							display: none;
						}
						.img2{
							display: block;
						}
					}
				}
			}
		}
	}
	.empty{
		padding: 150rpx 0;
		text-align: center;
		font-weight: 500;
		font-size: 28rpx;
		color: #222222;
		image{
			display: block;
			width: 460rpx;
			height: 400rpx;
			margin: 0 auto;
		}
	}
	&-tab{
		width: 100%;
	}
	&-list{
		padding: 0 25rpx 25rpx;
		overflow: hidden;
		.item{
			width: 100%;
			background: #FFFFFF;
			border-radius: 20rpx;
			margin-top: 20rpx;
			overflow: hidden;
			.a{
				height: 84rpx;
				padding: 0 25rpx;
				line-height: 84rpx;
				font-weight: 500;
				font-size: 28rpx;
				color: #666666;
				border-bottom: 1px solid #EAEAEA;
				text{
					float: right;
					color: #222222;
				}
			}
			.c{
				display: flex;
				align-items: center;
				justify-content: flex-end;
				padding: 0 25rpx 25rpx;
				overflow: hidden;
				.btn{
					width: 161rpx;
					line-height: 61rpx;
					background: #FFFFFF;
					border-radius: 61rpx;
					border: 1px solid #999999;
					margin-left: 20rpx;
					font-weight: 500;
					font-size: 24rpx;
					color: #666666;
					text-align: center;
					&-01{
						background: linear-gradient(0deg, #000000, #3D3B38);
						box-shadow: 0px 4rpx 18rpx 0px rgba(42,41,39,0.34);
						color: #FFFFFF;
					}
				}
			}
			.b{
				padding: 30rpx 25rpx;
				display: flex;
				align-items: center;
				image{
					width: 150rpx;
					height: 150rpx;
					border-radius: 10rpx;
					margin-right: 20rpx;
				}
				.info{
					flex: 1;
					.name{
						font-weight: 500;
						font-size: 30rpx;
						color: #333333;
						line-height: 46rpx;
					}
					.desc{
						font-weight: 500;
						font-size: 26rpx;
						color: #999999;
						margin-top: 20rpx;
					}
					.price{
						font-weight: 500;
						font-size: 28rpx;
						color: #222222;
						margin-top: 20rpx;
					}
				}
			}
		}
	}
}
</style>
