<template>
	<view class="content">
		<view class="header">
			<view class="title">
				我的订单
			</view>
		</view>

		<view class="navbar">
			<view v-for="(item, index) in navList" :key="index" class="nav-item"
				:class="{ current: tabCurrentIndex === index }" @click="tabClick(index)">{{ item.text }}</view>
		</view>

		<swiper :current="tabCurrentIndex" class="swiper-box" duration="300" @change="changeTab">
			<swiper-item class="tab-content" v-for="(tabItem, tabIndex) in navList" :key="tabIndex">
				<scroll-view class="list-scroll-content" scroll-y @scrolltolower="loadData">
					<!-- 空白页 -->
					<!-- #ifdef H5 -->
					<empty src="../../static/error/emptyOrder.png"
						v-if="tabItem.loaded === true && tabItem.orderList.length === 0"></empty>
					<!-- #endif -->
					<!-- #ifndef H5 -->
					<empty src="../static/error/emptyOrder.png"
						v-if="tabItem.loaded === true && tabItem.orderList.length === 0"></empty>
					<!-- #endif -->
					<!-- 订单列表 -->
					<view @click="navTo( '/pages/order/orderDetail?type=1&id=' + item.order_id )"
						v-for="(item, index) in tabItem.orderList" :key="index" class="order-item position-relative">
						<view class="i-top b-b flex">
							<view class="order-code flex">
								<view class="no">
									订单编号:
								</view>
								<view>
									{{ item.order_id }}
								</view>
							</view>
							<text class="font-size-sm" :style="{ color: item.stateTipColor }">{{ item._status._title }}</text>
							<!-- <text v-if="item.status === 4" class="del-btn iconfont icondelete" @click="deleteOrder(index)"></text> -->
						</view>

						<scroll-view v-if="item.cartInfo.length > 0" class="goods-box" scroll-x>
							<view v-for="(goodsItem, goodsIndex) in item.cartInfo" :key="goodsIndex" class="goods-item">
								<image class="goods-img" :src="goodsItem.productInfo.image" mode="aspectFill">
								</image>
								<view class="good-name clamp">
									{{goodsItem.productInfo.attrInfo.suk}}*{{goodsItem.cart_num}}
								</view>
							</view>
						</scroll-view>
						<view class="info-info">
							<view class="js">
								共{{item.cartInfo.length}}件
							</view>
							<view class="hj">
								合计:￥{{item.pay_price}}
							</view>
						</view>
						<!-- 底部操作栏 -->
						<view class="btm-btn-wrap flex">
							<view class="btm-left">
								下单时间：{{item._add_time.split(' ')[0].replace(/-/g,'/')}}
							</view>
							<view class="btm-right flex">
								<view class="btm-btn" @click.stop="cancelOrder(item)"
									v-if="item._status._title == '未支付'">
									取消订单
								</view>
								<view class="btm-btn ksps"
									@click.stop="navTo( '/pages/user/money/pay?type=1&ordid=' + item.order_id + '&money=' + item.pay_price)"
									v-if="item._status._title == '未支付'">
									前往支付
								</view>
								<view class="btm-btn ksps" @click.stop="orderTake(item, index)"
									v-if="item._status._title == '待收货'">
									确认收货
								</view>
								<view class="btm-btn ksps"
									@click.stop="navTo('/pages/order/orderRefund?id=' + item.order_id)"
									v-if="item._status._title == '未发货'||item._status._title == '未核销'">
									申请退款
								</view>
								<view class="btm-btn ksps"
									@click.stop="navTo('/pages/order/WriteOff?id=' + item.order_id)"
									v-if="item._status._title == '待核销'">
									核销码
								</view>
								<view class="btm-btn ksps" @click.stop="evaluate(item)"
									v-if="item._status._title == '待评价'">
									立即评价
								</view>
							</view>
						</view>
					</view>
					<uni-load-more v-if="!(tabItem.orderList.length==0&&tabItem.loaded === true)" :status="tabItem.loadingType"></uni-load-more>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	import {
		orderList,
		orderCancel,
		orderDel,
		orderTake
	} from '@/api/order.js';
	export default {
		data() {
			return {
				tabCurrentIndex: 0,
				navList: [{
						state: 0,
						text: '待支付',
						loadingType: 'more',
						orderList: [],
						page: 1, //当前页数
						limit: 10 //每次信息条数
					},
					{
						state: 1,
						text: '待收货',
						loadingType: 'more',
						orderList: [],
						page: 1, //当前页数
						limit: 10 //每次信息条数
					},
					{
						state: 3,
						text: '待评价',
						loadingType: 'more',
						orderList: [],
						page: 1, //当前页数
						limit: 10 //每次信息条数
					},
					{
						state: 4,
						text: '已完成',
						loadingType: 'more',
						orderList: [],
						page: 1, //当前页数
						limit: 10 //每次信息条数
					}
				]
			};
		},

		onLoad(options) {
			this.loadData();
		},
		// #ifdef APP-PLUS || H5
		onBackPress(e) {
			uni.switchTab({
				url: '/pages/user/user',
			});
			return true;
		},
		// #endif
		methods: {
			// 评价
			evaluate(item){
				if(item.cartInfo.length==1){
					uni.navigateTo({
						url: '/pages/order/evaluate?unique=' + item.cartInfo[0].unique
					});
				}else{
					this.navTo( '/pages/order/orderDetail?type=1&id=' + item.order_id )
				}
			},
			// 确认收货
			orderTake(item, index) {
				let obj = this;
				uni.showModal({
					title: '是否确认收货？',
					success: () => {
						orderTake({
								uni: item.order_id
							})
							.then(e => {
								obj.navList[obj.tabCurrentIndex].orderList.splice(index, 1);
								uni.showToast({
									title: '收货成功'
								});
							})
							.catch(e => {
								console.log(e);
							});
					}
				});
			},
			// 跳转页面
			navTo(url) {
				uni.navigateTo({
					url
				});
			},
			//获取订单列表
			loadData(source) {
				//这里是将订单挂载到tab列表下
				let index = this.tabCurrentIndex;
				let navItem = this.navList[index];
				let state = navItem.state;
				if (source === 'tabChange') {
					navItem.page = 1;
					navItem.orderList = [];
					navItem.loadingType = 'more';
					navItem.loaded=false;
					//tab切换只有第一次需要加载数据
				}
				if (navItem.loadingType === 'loading') {
					//防止重复加载
					return;
				}
				if (navItem.loadingType === 'noMore') {
					//防止重复加载
					return;
				}
				// 修改当前对象状态为加载中
				navItem.loadingType = 'loading';

				orderList({
						type: state,
						page: navItem.page,
						limit: navItem.limit
					})
					.then(({
						data
					}) => {
						let arr = data.map(e => {
							let b = this.orderStateExp(e.status);
							e.stateTip = b.stateTip;
							e.stateTipColor = b.stateTipColor;
							return e;
						});
						navItem.orderList = navItem.orderList.concat(arr);
						// console.log(navItem.orderList);
						navItem.page++;
						if (navItem.limit == 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);
					});
			},

			//swiper 切换
			changeTab(e) {
				this.tabCurrentIndex = e.target.current;
				this.loadData('tabChange');
			},
			//顶部tab点击
			tabClick(index) {
				this.tabCurrentIndex = index;
			},
			//删除订单
			deleteOrder(index) {
				uni.showLoading({
					title: '请稍后',
					mask: true
				});
				setTimeout(() => {
					this.navList[this.tabCurrentIndex].orderList.splice(index, 1);
					uni.hideLoading();
				}, 600);
			},
			//取消订单
			cancelOrder(item) {
				uni.showModal({
					title: '订单取消',
					content: '是否取消订单？',
					success: e => {
						if (e.confirm) {
							uni.showLoading({
								title: '请稍后',
								mask: true
							});
							orderCancel({
									id: item.order_id
								})
								.then(e => {
									uni.showToast({
										title: '取消成功',
										duration: 2000,
										position: 'top'
									});
								})
								.catch(e => {
									console.log(e);
								});
							//取消订单后删除待付款中该项
							let list = this.navList[this.tabCurrentIndex].orderList;
							let index = list.findIndex(val => val.id === item.id);
							index !== -1 && list.splice(index, 1);
							uni.hideLoading();
						}
					}
				});
			},

			//订单状态文字和颜色
			orderStateExp(state) {
				let stateTip = '',
					stateTipColor = '#3C82E6';
				switch (+state) {
					case 0:
						stateTip = '待付款';
						stateTipColor = '#3C82E6'
						break;
					case 1:
						stateTip = '待发货';
						stateTipColor = '#3C82E6'
						break;
					case 2:
						stateTip = '待收货';
						break;
					case 3:
						stateTip = '待评价';
						break;
					case 4:
						stateTip = '已完成';
						stateTipColor = '#999999';
						break;
					case 9:
						stateTip = '订单已关闭';
						stateTipColor = '#909399';
						break;

						//更多自定义
				}
				return {
					stateTip,
					stateTipColor
				};
			}
		}
	};
</script>

<style lang="scss">
	.content {
		height: 100%;
		background: linear-gradient(180deg, rgba(#3C82E6, 0.3), rgba(#5395F5, 0));
		background-size: 100% 645rpx;
		background-repeat: no-repeat;
	}

	.header {
		padding-top: var(--status-bar-height);

		.title {
			height: 50px;
			font-size: 18px;
			line-height: 50px;
			text-align: center;
			font-weight: bold;
		}
	}

	.swiper-box {
		height: calc(100% - 90px);
	}

	.list-scroll-content {
		height: 100%;
		padding-top: 30rpx;
	}

	.navbar {
		display: flex;
		height: 40px;
		padding: 6px;
		background: $bg-gradual;
		box-shadow: 0 1px 5px rgba(0, 0, 0, 0.06);
		position: relative;
		z-index: 10;
		margin: 0 30rpx;
		border-radius: 99rpx;

		.nav-item {
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 28px;
			font-size: 15px;
			line-height: 28px;
			color: #FFF;
			position: relative;

			&.current {
				color: $base-color;
				background-color: #FFF;
				border-radius: 99rpx;
			}
		}
	}

	.order-item {
		padding-left: 30rpx;
		padding-right: 30rpx;
		margin: 0 30rpx;
		margin-bottom: 30rpx;
		border-radius: 30rpx;
		background: #fff;
		color: #A8ADBF;

		.i-top {
			height: 80rpx;
			.order-code{
				font-size: $font-sm;
				.no{
					padding: 10rpx 10rpx;
					background-color: #FFEAE5;
					color: #FD5B23;
					line-height: 1;
					border-top-right-radius: 10rpx;
					border-bottom-left-radius: 10rpx;
					margin-right: 10rpx;
					
				}
			}
			
			.del-btn {
				padding: 10rpx 0 10rpx 36rpx;
				font-size: $font-lg;
				position: relative;

				&:after {
					content: '';
					width: 0;
					height: 30rpx;
					border-left: 1px solid $border-color-dark;
					position: absolute;
					left: 20rpx;
					top: 50%;
					transform: translateY(-50%);
				}
			}
		}

		.info-info {
			width: 215rpx;
			height: 200rpx;
			position: absolute;
			top: 80rpx;
			right: 35rpx;
			display: flex;
			flex-direction: column;
			justify-content: flex-end;
			align-items: flex-end;
			font-size: 30rpx;
			font-weight: 500;

			.hj {
				padding-top: 40rpx;
				color: $font-color-dark;
				padding-bottom: 20rpx;
			}
		}

		/* 多条商品 */
		.goods-box {
			height: 200rpx;
			padding: 20rpx 0;
			white-space: nowrap;
			padding-right: 250rpx;

			.goods-item {
				width: 120rpx;
				height: 180rpx;
				display: inline-block;
				margin-right: 24rpx;
			}

			.goods-img {
				display: block;
				width: 120rpx;
				height: 120rpx;
			}

			.good-name {
				font-size: 24rpx;
				text-align: center;
				width: 120rpx;
				padding-top: 10rpx;
				margin-right: 0;
				color: $font-color-dark;
			}
		}

		.btm-btn-wrap {
			height: 90rpx;
			width: 100%;
			font-size: 24rpx;
			font-weight: 500;
			color: #A3A8BB;
			line-height: 1;

			.btm-right {
				justify-content: flex-end;
			}

			.btm-btn {
				width: 144rpx;
				padding: 10rpx 0;
				border: 2px solid #ededed;
				border-radius: 28rpx;
				font-size: 26rpx;
				font-weight: 500;
				text-align: center;
				margin-left: 10rpx;
				color: #999999;
			}

			.ksps {
				border-color: #4589ec;
				color: #4589ec;
			}
		}
	}

	/deep/ .empty-content {
		background: transparent;
	}
</style>
