<template>
	<view>
		<u-navbar title="我的订单" :border="true" :placeholder="true" :autoBack="true" bgColor="#fff" />
		<view class="order">
			<!-- 订单导航 -->
			<view class="flex order-state-search">
				<u-tabs @change="changeTab" :scrollable="false" :list="statusArr" lineWidth="20" lineHeight="4"
					lineColor="#E96737" :activeStyle="{
										color: '#333',
										transform: 'scale(1.1)',
										width: '50px',
										
									}" :inactiveStyle="{
										color: '#666666',
										transform: 'scale(1)',
										width: '50px'
									}" itemStyle="padding-left: 15px; padding-right: 15px; height: 44px;text-align: center;">
				</u-tabs>
			</view>
			<!-- 订单列表 -->
			<view class="order-list">
				<view class="order-list-item" v-for="(item, index) in list" :key="index">
					<view class="order-list-item__state">
						<text class="success">{{ item.status && item.status.desc }}</text>
					</view>
					<view class="order-list-item__goods">
						<view class="order-list-item__goods-item" v-for="(items, indexs) in item.items" :key="indexs">
							<image :src="items.picUrl" mode="aspectFit"></image>
							<view class="info">
								<view class="info-title ells">{{ items.title }}</view>
								<view class="info-num flex">
									<view class="info-num-price">￥{{ $numberFormat(85) }}</view>
									<view class="info-num-goods">x{{ items.goodsNum }}</view>
								</view>
							</view>
						</view>
					</view>
					<view class="order-list-item-price flex">
						<view class="order-list-item-price__num">共计<text>{{ item.orderNum }}</text>商品，</view>
						<view class="order-list-item-price__total">总金额：<text>￥{{ $numberFormat(item.payAmt) }}</text></view>
					</view>
					<view class="order-list-item-btn flex">
						<view class="order-list-item-btn-item flex logistics" v-if="item.status.value == 2 || item.status.value == 3" @click="toLogistics(item)"><text>查看物流</text></view>
						<view class="order-list-item-btn-item flex cancel" v-if="item.status.value == 0" @click="cancelOrder(item.orderId)"><text>取消订单</text></view>
						<view class="order-list-item-btn-item flex detail" @click="toDetail(item)"><text>查看详情</text></view>
						<view class="order-list-item-btn-item flex pay"v-if="item.status.value == 0"  @click="payOrder(item)"><text>去支付</text></view>
					</view>
				</view>
			</view>
			<view class="flex empty" v-if="!list.length && !loading">
				<u-empty text="订单为空" mode="order" />
			</view>
		</view>
	</view>
</template>

<script>
	import env from '../../config/env.js'
	import $http from '@/utils/request.js'
	export default {
		data() {
			return {
				loading: false,
				pageNum: 1,
				total: 100,
				list: [],
				status: null,
				statusArr: [{
					name: '全部'
				}, {
					name: '待付款',
				}, {
					name: '待发货'
				}, {
					name: '待收货'
				}, {
					name: '已完成'
				}, ],
			};
		},
		onLoad(opthios) {},
		onShow() {
			this.pageList()
		},
		methods: {
			getList() {
				uni.showLoading({
					title: '加载中'
				});
				this.loading = true
				$http.post(`/api/v1/mp/user/deliver/order/list?pageNum=${ this.pageNum }&pageSize=20`, {
					status: this.status
				}).then(res => {
					uni.hideLoading();
					this.loading = false
					if (res.code == 0) {
						res.rows.forEach(item => {
							let items = item.items
							items.forEach(item => {
								let picUrlArr = item.picUrl.split(',')
								item.picUrl = env.filePublic + picUrlArr[0] + '?imageView2/2/w/170'
							})
							item.status = JSON.parse(item.status)
						})
						this.total = res.total
						this.list = this.list.concat(res.rows)
					}
				}).catch(() => {
					uni.hideLoading();
					this.loading = false
				})
			},

			pageList() {
				this.pageNum = 1
				this.list = []
				this.getList()
			},

			cancelOrder(id) {
				let _this = this
				uni.showModal({
					title: '提示',
					content: '您确认要取消订单吗？',
					success(res) {
						if (res.confirm) {
							$http.post('/api/v1/mp/user/deliver/order/cancel', {
								orderId: id
							}).then(res => {
								if (res.code == 0) {
									uni.$u.toast('订单取消成功');
									_this.pageList()
								}
							})
						}
					}
				})
			},
			
			toDetail(item) {
				uni.navigateTo({
					url: `/packageGoods/order/detail?id=${ item.orderId }`
				})
			},
			
			// 查看物流
			toLogistics(item) {
				uni.navigateTo({
					url: `/packageGoods/order/logistics?id=${ item.orderId }`
				});
			},

			payOrder(item) {
				let _this = this
				let payIng = false
				if (payIng) return
				uni.showLoading({
					title: '加载中'
				});
				$http.post('/api/v1/mp/user/deliver/order/pay', {
					orderId: item.orderId,
					payType: 2
				}).then(ele => {
					uni.hideLoading();
					payIng = true
					if (ele.code == 0) {
						uni.requestPayment({
							timeStamp: ele.data.timeStamp,
							nonceStr: ele.data.nonceStr,
							package: ele.data.package,
							signType: ele.data.signType,
							paySign: ele.data.paySign,
							success() {
								uni.showToast({
									title: '支付成功',
									icon: 'success',
									duration: 2000
								})
								_this.pageList()
							},
							fail() {
								payIng = false
							}
						})
					} else {
						payIng = false
						uni.$u.toast('支付失败!');
					}
				}).catch(() => {
					payIng = false
					uni.$u.toast('支付失败!');
					uni.hideLoading();
				})
			},

			changeTab(e) {
				if (e.index == 0) {
					this.status = null
				} else if (e.index == 1) {
					this.status = '0'
				} else if (e.index == 2) {
					this.status = '1,4'
				} else if (e.index == 3) {
					this.status = '2'
				} else if (e.index == 4) {
					this.status = '3'
				}
				this.pageList()
			},
		},

		onReachBottom() {
			// 判断是否有数据
			if (this.total > this.pageNum * 20) {
				setTimeout(() => {
					++this.pageNum
					this.getList()
				}, 500)
			} else {
				uni.$u.toast('没有更多数据了')
			}
		},
	}
</script>

<style lang="scss" scoped>
	.order {
		padding-bottom: 40rpx;
		
		// 订单导航
		&-state-search {
			position: relative;
			width: 100%;
			position: fixed;
			z-index: 100;
			background-color: #FFFFFF;
			box-shadow: 0 5rpx 5rpx #ececec;
		}

		// 订单列表
		&-list {
			margin: 0rpx 34rpx 0;
			padding-top: 110rpx;

			&-item {
				background: #FFFFFF;
				border-radius: 6rpx;
				margin-bottom: 22rpx;
				padding: 20rpx 34rpx;
				
				// 订单状态
				&__state {
					font-size: 30rpx;
					line-height: 30rpx;
					margin-bottom: 22rpx;
					
					.success {
						color: #FF1818;
					}
				}
				
				// 商品列表
				&__goods {
					margin-bottom: 20rpx;
					
					&-item{
						display: flex;
						padding: 22rpx;
						box-shadow: 0px 0px 8px 0px rgba(26, 35, 113, 0.08);
						border-radius: 4rpx;
						margin-bottom: 20rpx;
						
						image {
							width: 220rpx;
							height: 200rpx;
							border-radius: 12rpx;
							margin-right: 24rpx;
						}
						
						.info {
							display: flex;
							flex-direction: column;
							justify-content: space-between;
							flex: 1;
							padding-bottom: 34rpx;
							
							&-title {
								font-weight: bold;
								line-height: 40rpx;
							}
							
							&-num {
								justify-content: space-between;
								
								&-price {
									font-size: 24rpx;
								}
								
								&-goods {
									color: #999999;
								}
							}
						}
					}
					
					&-item:last-child {
						margin-bottom: 0;
					}
				}
				
				// 订单价格
				&-price {
					justify-content: flex-end;
					padding-bottom: 18rpx;
					border-bottom: 1px solid #E5E5E5;
					margin-bottom: 22rpx;
					
					&__num {
						text {
							color: #F9822C;
						}
					}
					
					&__total {
						text {
							color: #F24E4E;
						}
					}
				}
				
				// 操作按钮
				&-btn {
					justify-content: flex-end;
					
					&-item {
						width: 200rpx;
						height: 66rpx;
						font-size: 30rpx;
						border-radius: 33rpx;
						margin-left: 44rpx;
							
						text {
							line-height: 30rpx;
						}
					}
					
					.logistics {
						border: 1px solid #F9822C;
						color: #F9822C;
					}
					
					.detail {
						background-color: rgb(249, 130, 44);
						color: #FFFFFF;
					}
					
					.cancel {
						border: 1px solid #c4c6c9;
						color: #c4c6c9;
					}
					
					.pay {
						background-color: #5ac725;
						color: #fff;
					}
					
					&-item:first-child {
						margin-left: 0;
					}
				}
			}
			
			&-item:last-child {
				margin-bottom: 0;
			}
		}

		// 空状态
		.empty {
			height: 60vh;
		}
	}
</style>
