<template>
	<view class="commonPageBox commonDetailPage">
		<z-nav-bar title="我的订单" :backState="2000"></z-nav-bar>
		<view class="cateList flexbox">
			<common-sticky itemStyle="width:20%; height: 68rpx;font-size:24rpx;" :list="ordersTabs" label="name"
				:currentCateIndex="currentCateIndex" @handleselectCate="ordersTabCLi"></common-sticky>
		</view>
		
		<view class="order_box" :style="`height: calc(100% - ${(80 + statusBarHeight) * 2}rpx);`">
			<common-list imgUrl="url" indexKey="orderSn" noDataIcon="data" :isScroll="true" :isOrderList="true" :axiosStatus="axiosStatus"
				:isLoadingHide="isLoadingHide" :isNoIcon="true" :isCondition="true" :dataList="newList"
				@hancleClick="toDetail" @lower="onReachBottom1" :pagination="pagination" label="title">
				<view slot="labelSlot" slot-scope="slotProps">
					<view class="orderInfo color_shandow">
						<view class="mainContent">
							<view class="btns" style="margin-top: 0rpx">
								<view class="flexbox opbtns" style="letter-spacing: 1rpx">
									<view style="
									  width: 100%;
									  display: flex;
									  align-items: center;
									  justify-content: space-between;
									  line-height: 30rpx;
									  color: #9b9b9b;
									  font-size: 26rpx;
									  font-weight: 400;
									">{{ slotProps.row.orderSn }}
								
										<u-tag @click="handleCopy(slotProps.row.orderSn, '订单编号')" text="复制" plain style="float: right" size="mini"
											type="success"></u-tag>
									</view>
								</view>
							</view>

							<view class="item">
								<view class="item_top" style="margin-top: 10rpx; margin-bottom: 0">
									<view class="source_app"></view>
									<view style="text-align: left">
										<text class="orderstatus" v-show="slotProps.row.orderStatus == 0">未付款</text>
										<text class="orderstatus" v-show="slotProps.row.orderStatus == 1">待发货</text>
										<text class="orderstatus" v-show="slotProps.row.orderStatus == 2">已发货</text>
										<text class="orderstatus" v-show="slotProps.row.orderStatus == 3">交易成功</text>
										<text class="orderstatus" v-show="slotProps.row.orderStatus == 4">交易失败</text>
										<text class="orderstatus" v-show="slotProps.row.orderStatus == 5">已过期</text>
									</view>
								</view>

								<view v-if="slotProps.row.orderType == 'vip'" class="bookinfolist">
									<image class="feng fengPoint" src="../../static/icon/vip.png" mode="aspectFill"></image>
									<view class="btns flexbox">
										<view class="booknameleft">
											{{ slotProps.row.vipBuyConfigEntity?slotProps.row.vipBuyConfigEntity.title:'' }}
											<text class="vip_year" v-if="slotProps.row.vipBuyConfigEntity.year">({{ slotProps.row.vipBuyConfigEntity.year }}年)</text>
										</view>

										<view style="
											line-height: 58rpx;
											color: #333;
											font-size: 26rpx;
											font-weight: 700;
										  ">
											<text style="font-size: 20rpx"></text>
										</view>
									</view>
									<view class="btns flexbox"
										style="color: #9f9f9f; margin-top: 10rpx; font-size: 24rpx">
										下单时间：{{ slotProps.row.createTime }}
									</view>
								</view>

								<view v-if="slotProps.row.orderType == 'point'" class="bookinfolist">
									<image class="feng fengPoint" src="/static/icon/pay_3.png" mode="aspectFill">
									</image>
									<view class="btns flexbox">
										<view class="booknameleft"> 充值 {{ slotProps.row.bookBuyConfigEntity.money }}天医币</view>

										<view style="
											line-height: 58rpx;
											color: #333;
											font-size: 26rpx;
											font-weight: 700;
										  ">
								
										</view>
									</view>
									<view class="btns flexbox"
										style="color: #9f9f9f; margin-top: 10rpx; font-size: 24rpx">
										下单时间：{{ slotProps.row.createTime }}
									</view>
								</view>

								<view v-if="slotProps.row.orderType == 'order'">
									<view v-for="(item2, index2) in slotProps.row.productList"
										:key="slotProps.row.orderId" class="bookinfolist">
										<view v-if="item2.product">
											<view class="feng" v-if="item2.product && item2.product.productImages">
												<image :key="Date.now()" style="width: 100%; height: 100%"
													mode="aspectFit" :src="item2.product.productImages"></image>
											</view>

											<view v-else class="feng" style="
											  color: #c0c4cc;
											  font-size: 22rpx;
											  line-height: 140rpx;
											  text-align: center;
											">暂无封面图</view>
											<view class="btns flexbox">
												<view class="booknameleft">
												{{ item2.product && item2.product.productName ? item2.product.productName : "" }}</view>

												<view style="
													line-height: 58rpx;
													color: #333;
													font-size: 26rpx;
													font-weight: 700;
												  ">
													<text style="font-size: 20rpx">￥</text>
													{{item2.product.price ? item2.product.price : ""}}
												</view>
											</view>
											<view class="btns flexbox" style="margin-top: 10rpx">
												<view class="left" style="color: #c0c4cc"></view>
												<view class="right flexbox opbtns" style="color: #c0c4cc">
													×{{ item2.quantity ? item2.quantity : "" }}
												</view>
											</view>
										</view>
										<view v-else>
											<view class="feng"></view>
											<view class="btns flexbox">
												<view class="booknameleft" style="color: #181818">未知商品</view>
												<view style="" class="right flexbox opbtns product_quantity">
													×{{ item2.quantity ? item2.quantity : "" }}
												</view>
											</view>
										</view>
									</view>
								</view>

								<view style="margin-top: 10rpx; overflow: hidden">
									<view class="btns flexbox" style="float: right; width: auto !important">
										<view class="right flexbox opbtns" style="
											width: auto;
											line-height: 44rpx;
											letter-spacing: 1rpx;
											display: flex;
											align-items: center;
										  ">
											<view style="
											  line-height: 46rpx;
											  color: #000;
											  font-size: 28rpx;
											  font-weight: 500;
											  margin-right: 10rpx;
											">实付款</view>
											<view style="
											  line-height: 46rpx;
											  color: #333;
											  font-size: 30rpx;
											  font-weight: 700;
											">
												<text style="font-size: 20rpx">￥</text>
												{{ slotProps.row.realMoney || slotProps.row.realMoney == 0 ? slotProps.row.realMoney: "" }}
											</view>
										</view>
									</view>
									<view class="btns flexbox" style="
									  margin-top: 0rpx;
									  float: right;
									  width: auto;
									  margin-left: 20rpx;" 
									  v-if="
									  (slotProps.row.addressId == 0 ||
										slotProps.row.addressId == null) &&
									  slotProps.row.orderType == 'order' &&
									  slotProps.row.jfDeduction > 0
									  ">
										<view class="right flexbox opbtns" style="
											line-height: 44rpx;
											letter-spacing: 1rpx;
											display: flex;
											align-items: center;
										  "><text style="color: #9b9b9b">（ </text>
											<view style="
											  line-height: 46rpx;
											  color: #9b9b9b;
											  font-size: 24rpx;
											  font-weight: 500;
											  margin-right: 10rpx;
											">积分抵扣</view>
											<view style="
											  line-height: 46rpx;
											  color: #9b9b9b;
											  font-size: 26rpx;
											  font-weight: 700;
											">
												<text style="font-size: 20rpx">￥</text>
												{{ slotProps.row.jfDeduction }}
											</view>
										</view><text style="color: #9b9b9b">）</text>
									</view>
								</view>

								<view class="operation_box boxShadow" v-if="slotProps.row.isShowMore == true">
									<view v-if="slotProps.row.orderStatus == 0"
										@click.stop="canceOrder(slotProps.row)">取消订单</view>
								</view>
								<view class="btns flexbox" style="margin-top: 10rpx">
									<view class="left" style="color: #c0c4cc" 
									 v-if="slotProps.row.orderStatus != 3"
									 @click.stop="openMore(slotProps.row, slotProps.rowIndex)">更多</view>
									 <view class="left" v-if="slotProps.row.orderStatus == 3"></view>
									<view class="right flexbox opbtns">
										<view class="orderstatusbtn orderstatusbtn_success"
											v-if="slotProps.row.orderStatus == 0"
											@click.stop="goPay(slotProps.row)">继续付款</view>
										<view class="orderstatusbtn" v-if="slotProps.row.orderStatus == 1">催发货</view>
										<view class="orderstatusbtn" v-if="slotProps.row.orderStatus == 2"
											@click.stop="seeExpressDetail(slotProps.row)">查看物流</view>
										<view class="orderstatusbtn" v-if="slotProps.row.orderStatus == 2"
											@click.stop="OverOrder(slotProps.row)">确认收货</view>
										<view class="orderstatusbtn" v-if="slotProps.row.orderStatus == 3">申请售后</view>
									</view>
								</view>

								<view style="border-bottom: 2rpx solid #e9e9e9; height: 50rpx"
									v-if="slotProps.rowIndex < slotProps.row.length - 1"></view>
							</view>
						</view>
					</view>
				</view>
			</common-list>
		</view>

		<u-action-sheet :closeOnClickAction="true" :closeOnClickOverlay="true" :actions="moreList" :show="isShowMore"
			cancelText="关闭" @close="isShowMore = false" @select="selectClick"></u-action-sheet>
	</view>
</template>

<script>
	import $http from "@/config/requestConfig.js";
	import {
		setPay,
		setPayAssign,
		setWXPay
	} from "@/config/utils";
	import {
		mapState,
		mapMutations
	} from "vuex";
	export default {
		data() {
			return {
				axiosStatus: null,
				isShowMore: false,
				come: "3",
				isShowTab: false,
				isLoadingHide: false,
				moreList: [{
					name: "取消订单",
					key: "false",
				}, ],
				currentCateIndex: 0,
				pagination: {
					page: 1, //页码
					limit: 20, //每页显示
					total: 0, //总条数
				},
				playData: {},
				options: {},
				windowWidth: 0,
				bookid: null,
				booklistpage: 1,
				productList: [],
				productInfo: {},
				page: 1,
				pageSize: 10,
				total: 0,
				status: 3,
				bfaid: null,
				ordersTabs: [{
						name: "全部",
						value: -1,
						badge: {},
					},
					{
						name: "待付款",
						value: 0,
						badge: {},
					},
					{
						name: "待发货",
						value: 1,
						badge: {},
					},
					{
						name: "待收货",
						value: 2,
						badge: {},
					},
					{
						name: "已完成",
						value: 3,
						badge: {},
					},
				],
				selectOrderInfo: {},
				ordersListTab: 1,
				newestpage: 1,
				newList: [],
				map: {}
			};
		},
		onUnload() {
			uni.getStorageSync("orderStatus", 0)
		},
		onPullDownRefresh() {
			uni.stopPullDownRefresh();

			this.newestpage = 1;
			this.newList = [];
			this.getBookList(this.ordersListTab, false);
		},
		onReachBottom() {
			this.newestpage++;
			this.getBookList(this.ordersListTab, false);
		},
		onLoad(e) {
			this.windowWidth = uni.getSystemInfoSync().windowWidth;
			this.options = e;
		},
		onShow() {
			uni.setStorageSync("orderStatus", 0)
			var status = uni.getStorageSync("orderStatus");
			this.newList = [];
			this.pagination = {
				page: 1, //页码
				limit: 20, //每页显示
				total: 0, //总条数
			};
			this.$nextTick(async () => {
				await this.ordersTabCLi(this.ordersTabs[status], status);
			});
		},
		computed: {
			...mapState(["userInfo"]),
		},
		methods: {
			...mapMutations(["setLoadingShow"]),
			selectClick(index) {
				if (index.key == "false") {
					this.isShowMore = false;
					this.canceOrder(this.selectOrderInfo);
				}
			},
			openMore(row, index) {
				this.selectOrderInfo = row;
				this.isShowMore = true;
			},
			handleCopy(value, title) {
				this.$commonJS.handleCopy(value, title);
			},
			onReachBottom1() {
				// 上拉加载
				// 当列表数量不大于或等于总数量，则再次调用接口请求数据
				if (this.newList.length >= this.pagination.total) return;
				this.pagination.page++;
				this.getBookList(this.ordersListTab, false);
			},
			// 切换tab状态
			ordersTabCLi(data, index) {
				this.axiosStatus = 0
				this.currentCateIndex = index;
				this.pagination = {
					page: 1, //页码
					limit: 20, //每页显示
					total: 0, //总条数
				};

				this.ordersListTab = data.value;
				this.newestpage = 1;
				this.newList = [];
				this.getBookList(this.ordersListTab, false);
			},
			// 订单详情
			toDetail(val) {
				uni.navigateTo({
					url: "/pages/detail/orderLCont?orderId=" +
						val.orderId +
						"&orderType=" +
						val.orderStatus +
						"&orderSn=" +
						val.orderSn,
				});
			},
			//初始化获取数据
			getBookList(flag, refreshflag) {
				this.isLoadingHide = false;
				var that = this;
				// 顶部红点数量
				$http.request({
					url: "common/buyOrder/getBuyOrderNumByStatus",
					method: "POST",
					data: {
						userId: that.userInfo.id,
						come: this.come,
					},
					header: {
						"Content-Type": "application/json",
					},
				})
				.then((res) => {
					//订单状态
					// * 0: 待付款
					// * 1: 待发货
					// * 2: 已发货
					// * 3：已完成
					// * 4: 交易失败
					// * 5: 已过期
					that.map = res.data;

					that.ordersTabs.map((e) => {
						if (e.value == 0 || e.value == 1 || e.value == 2) {
							var numList = that.map.filter((v) => v.order_status == e.value);
							console.log("num at line 771:", numList);
							if (numList.length > 0) {
								e.badge = {
									value: numList[0].num,
								};
							}
						} else {
							e.badge = {};
						}
					});
					that.$forceUpdate();
				})
				.catch((e) => {
					console.log(e);
				});
				
				uni.showLoading({
					title: '加载中'
				});
				//订单数据
				var params = {
					userId: this.userInfo.id,
					come: this.come,
					orderStatus: flag == -1 ? "" : flag, //传null为全部，订单状态  0-未付款 1-待发货 2-已发货 3-交易成功 4-交易失败 5-过期
					...this.pagination
				}
				this.$http.request({
					url: "common/buyOrder/commonBuyOrderList",
					method: "POST",
					data: params,
					header: {
						"Content-Type": "application/json",
					},
				})
				.then((res) => {
					uni.hideLoading();
					that.pagination.total = res.data.total;
					if (res.data.total == 0) {
						this.isLoadingHide = true;
					}
					if (res.code == 0 && res.data && res.data.records.length > 0) {
						that.newList = that.newList.concat(res.data.records);
					} else {
						
					}
					if (res.data.records.length != 10) {
						that.status = 1;
					} else {
						that.status = 0;
					}
				})
				.catch((e) => {
					
				});
				this.axiosStatus = 1
			},
			// 支付
			goPay(payItem) {
				if (payItem.paymentMethod == 2) {
					console.log("阿里支付");
					setPay({
							typePay: "alipay",
							subject: "order",
							totalAmount: payItem.realMoney,
							type: 2,
							relevanceoid: payItem.orderSn,
							customerId: this.userInfo.id,
						},
						(res) => {
							if (res.success) {
								uni.showToast({
									title: "支付成功",
								});
							} else {
								uni.showToast({
									title: "支付失败",
									icon: "none"
								});
							}
							this.getOrderList();
						}
					);
				} else if (payItem.paymentMethod == 1) {
					console.log("微信支付");
					if (this.isAndorid == false) {
						uni.showModal({
							title: "提示",
							content: "很抱歉，苹果系统暂不支持微信支付",
							showCancel: false,
						});
						return false;
					} else {
						let data1 = {
							orderSn: payItem.orderSn,
							buyOrderId: null,
							totalAmount: payItem.realMoney,
						};
						setWXPay(data1, (res) => {
							if (res.success) {
								uni.showToast({
									title: "支付成功",
								});
							} else {
								if (res.data.errMsg.indexOf("User canceled") != -1) {
									uni.showToast({
										title: "用户取消支付",
										icon: "none"
									});
								} else {
									uni.showToast({
										title: "支付失败",
										icon: "none"
									});
								}
							}
						});
					}
				} else if (payItem.paymentMethod == 3) {
					// 苹果充值
					console.log("苹果二次支付");
					if (this.isAndorid) {
						uni.showModal({
							title: "提示",
							showCancel: false,
							content: "很抱歉，当前订单属于苹果系统内购订单，安卓系统无法完成支付操作，您可切换到苹果系统进行支付，也可以取消该订单，并重新下单",
						});
					} else {
						this.iphonepay(payItem);
					}
				}
			},
			// 取消订单
			canceOrder(item) {
				uni.showModal({
					title: "提示",
					content: "确定要取消订单吗？",
					confirmText: "取消订单",
					cancelText: "考虑一下",
					confirmColor: "#c96713",
					cancelColor: "#555",
					success: (res) => {
						if (res.confirm) {
							this.$http
								.get("book/buyOrder/cancelOrder?orderSn=" + item.orderSn)
								.then((res) => {
									if (res.code == 0) {
										uni.showToast({
											icon: "none",
											title: "取消订单成功",
										});
										this.newestpage = 1;
										this.newList = [];
										this.getBookList(this.ordersListTab, false);
									}
								});
						}
					},
				});
			},
			// 查看物流
			seeExpressDetail(item) {
				// 直接展示详情
				uni.navigateTo({
					url: "./deliverDetail?orderSn=" +
						item.orderSn +
						"&expressOrderSn=" +
						item.expressList[0].expressOrderSn,
				});
			},
			// 确认收货
			OverOrder(item) {
				uni.showModal({
					title: "提示",
					content: "确认收货？",
					success: (res) => {
						let data = {
							orderId: item.orderId,
							orderStatus: "3",
						};
						if (res.confirm) {
							uni.showLoading();
							$http.request({
								url: "book/buyOrder/update",
								method: "POST",
								data,
								header: {
									"Content-Type": "application/json",
								},
							})
							.then((res) => {
								if (res.code == 0) {
									uni.hideLoading();
									uni.showToast({
										title: "收货成功",
										icon: "success",
										duration: 1000,
									});
									this.newestpage = 1;
									this.newList = [];
									this.getBookList(this.ordersListTab, false);
								}
							});
						} else if (res.cancel) {}
					},
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
@import "@/static/mixin.scss";

.ordersTabs {
	width: 730rpx;
	position: fixed;
	background-color: #f7faf9;
	z-index: 100;

	.ordersdefine {
		display: inline-block;
		padding: 20rpx 0 20rpx 0;
		// margin: 40rpx 0 15rpx 0;
		width: 140rpx;
		text-align: center;
		font-size: 30rpx;
		position: relative;

		.ordersnum {
			position: absolute;
			top: 8rpx;
			right: 0rpx;
			background-color: red;
			// border:1rpx solid #a3a3a3;
			border-radius: 40rpx;
			width: 28rpx;
			height: 28rpx;
			font-size: 20rpx;
			font-weight: 500;
			color: rgb(255, 255, 255);
		}
	}

	.ordStyle {
		font-weight: bold;
	}
}

.orderstatusbtn {
	width: 160rpx !important;
	height: 60rpx;
	line-height: 55rpx !important;
	color: #1d1d1d;
	border: 1px solid #a0a0a0;
	border-radius: 30rpx;
	// padding: 0 10rpx;
	text-align: center;
	margin-left: 20rpx;
	font-size: 28rpx;
}

.orderstatusbtn_success {
	color: #f5342b;
	border-color: #f5342b;
}

.flexbox {
	display: flex;
}

.container {
	padding: 0 10px;
}

.star {
	display: inline-block;
	width: 20px;
	height: 20px;
	margin-right: 10rpx;
}

.starGray {
	background: url(../../static/icon/star_greey.png) no-repeat;
	background-size: contain;
}

.starLight {
	background: url(../../static/icon/star_light.png) no-repeat;
	background-size: contain;
}

.cuIcon-emoji {
	background: url(../../static/biaoqing.png) no-repeat;
	background-size: contain;
	display: block;
	margin-right: 20rpx;
	width: 30px;
}

.cuIcon-keyboard {
	background: url(../../static/biaoqing.png) no-repeat;
	background-size: contain;
	display: block;
	width: 30px;
}

.tanchu {
	padding: 40rpx 30rpx 40rpx 30rpx;
	position: relative;

	.dp_title {
		font-size: 32rpx;
		margin-bottom: 50rpx;
		color: #555;
		text-align: center;
		font-weight: bold;
	}

	.dp_add {
		position: absolute;
		top: 40rpx;
		right: 30rpx;
		font-size: 22rpx;
		background-color: #fd6004;
		color: #fff;
		border-radius: 10rpx;
		padding: 5rpx 10rpx;

		.u-icon {
			display: inline-block;
			margin-right: 5rpx;
		}
	}
}

.quesheng {
	text-align: center;
	margin-top: 100rpx;
	color: #8b8a91;
	padding-bottom: 20rpx;
	padding-top: 20rpx;
}

.orderInfo {
	position: relative;
	justify-content: space-between;
	background-color: #fff;
	padding: 20rpx 20rpx;
	border: 1px splid #999;
	box-sizing: border-box;
	border-radius: 10rpx;
	margin-bottom: 20rpx;

	.bookinfolist {
		width: 100%;
		margin-bottom: 20rpx;
		overflow: hidden;
	}

	.bookinfolist:nth-last-child(1) {
		margin-bottom: 0 !important;
	}

	.operation_box {
		position: absolute;
		bottom: 20rpx;
		left: 40rpx;
	}
}

.mainContent {
	background-color: #fff;

	.item {
		box-sizing: border-box;
		border-radius: 20rpx;
	}

	.feng {
		background-color: #fafafa;
		margin: 0rpx 22rpx 0 0;
		height: 140rpx;
		width: 140rpx;
		float: left;
		border-radius: 14rpx;
		// border: 1rpx solid #e9e9e9;
	}

	.point_box {
		.booknameleft {
			line-height: 70rpx !important;
		}
	}

	.fengPoint {
		background-color: #fff;
		height: 100rpx;
		width: 100rpx;
	}

	.item_top {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: space-between;

		margin-bottom: 25rpx;

		.source_app {
			font-size: 26rpx;
			font-weight: bold;
			line-height: 26rpx;
		}
	}

	.orderstatus {
		line-height: 24rpx;
		font-size: 24rpx;
		color: #f5342b;
		text-align: right;
		letter-spacing: 2rpx;
	}

	/deep/.description {
		overflow: hidden;
		color: #666;
		text-overflow: -o-ellipsis-lastline;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 5;
		line-clamp: 5;
		-webkit-box-orient: vertical;
		font-size: 26rpx;
		margin-bottom: 20rpx;
		margin-top: 10rpx;
		height: 172rpx;

		img {
			width: 100% !important;
		}
	}

	.btns {
		font-size: 28rpx;
		justify-content: space-between;
		align-items: center;

		.booknameleft {
			font-size: 32rpx;
			color: #070707;
			letter-spacing: 0.5rpx;
			font-weight: 600;
		}

		.left {
			width: 300rpx;
			color: #a1a1a1;
		}

		.right {
			width: 350rpx;
			display: flex;
			justify-content: flex-end;
			align-items: center;
		}

		.price {
			width: 120rpx;
		}

		.gzicon {
			margin: 0 0 0 20rpx;
			height: 40rpx;
			width: 40rpx;
		}
	}
}

.mb30 {
	margin-bottom: 30rpx;
	overflow: hidden;
}

.mytabs {
	height: 80rpx;

	.item {
		border-radius: 10rpx;
		background-color: #c6ead0;
		width: 48%;
		// margin: 0 10rpx;
		text-align: center;
		color: #3c7f56;
		font-size: 36rpx;
		line-height: 80rpx;
	}

	.item.active {
		background-color: #3c7f56;
		color: #fff;
	}

	.item2 {
		margin-right: 20rpx;
	}
}

/deep/.u-tabs__wrapper__nav__item__text {
	// font-size: 32rpx !important;
}

.cateList {
	width: 100%;
}

/deep/.u-tabs__wrapper__nav__item {
	padding: 0 !important;
}
.order_box {
	padding: 20rpx;
}

/deep/.order_box .list_item {
	// border-bottom: none;
	padding: 0 !important;
	padding-right: 0 !important;
	border: none !important;
}

.product_quantity {
	width: auto;
	color: #9b9b9b;
	font-size: 26rpx;
}
.vip_year{
	padding-left: 20rpx;
	color: #f5342b;
	font-size: 30rpx;
}
</style>