<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="30" lineHeight="1"
					lineColor="#E96737" :activeStyle="{
										color: '#E96737',
										transform: 'scale(1)'
									}" :inactiveStyle="{
										color: '#333',
										transform: 'scale(1)'
									}" itemStyle="padding-left: 15px; padding-right: 15px; height: 44px;">
				</u-tabs>
				<view class="test"></view>
			</view>
			<view class="order-list">
				<view class="order-list-data">
					<view class="order-list-data-item" v-for="(item, index) in list" :key="index">
						<view class="order-list-data-item__info">
							<view class="flex order-list-data-item__info__title">
								<view class="number">
									<text>订单编号：</text>
									<text>{{ item.orderId }}</text>
								</view>
								<view class="success" v-if="item.status.value == 1">{{ item.status.desc }}</view>
								<view v-else>{{ item.status.desc }}</view>
							</view>
							<navigator :url="`/pages/order/detail?id=${ item.orderId }`" hover-class="navigator-hover">
								<view class="flex order-list-data-item__info__detail"
									v-for="(items, index) in item.items" :key="index">
									<view class="order-list-data-item__info__detail__left">
										<view class="img">
											<image class="img" :src="items.picUrl" mode="aspectFill">
											</image>
										</view>
									</view>
									<view class="order-list-data-item__info__detail__right">
										<view class="title">{{ items.title }}</view>
										<view class="flex num">
											<view class="sku" v-if="!items.properties"></view>
											<view class="sku" v-if="items.properties">规格：{{ items.properties }}</view>
											<view class="">数量：{{ items.goodsNum }}</view>
										</view>
									</view>
								</view>
							</navigator>

							<view class="order-list-data-item__info__total">
								<view>共{{ item.orderNum }}个商品</view>
								<view class="money">
									<text>实付：</text>
									<text>￥{{ $numberFormat(item.payAmt) }}</text>
								</view>
							</view>

							<view class="order-list-data-item__info__btn" v-if="item.status.value == 0">
								<text @click="cancelOrder(item.orderId)">取消订单</text>
								<text @click="payOrder(item)">去支付</text>
							</view>
						</view>
					</view>
				</view>
				<view class="flex empty" v-if="!list.length && !loading">
					<u-empty text="订单为空" mode="order" />
				</view>
			</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]
							})
							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()
								}
							})
						}
					}
				})
			},

			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 = '-1'
				}
				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 {
			background-color: #F8F8F8;
			margin: 0rpx 10rpx 0;
			padding-top: 100rpx;

			&-data {

				navigator {
					margin-bottom: 10rpx;
				}

				navigator:last-child {
					margin-bottom: 0;
				}

				&-item {
					margin-bottom: 10rpx;
					padding: 20rpx 30rpx;
					background-color: #FFFFFF;
					justify-content: space-between;
					background-color: #FFFFFF;
				}

				&-item {
					&__info {

						&__title {
							justify-content: space-between;

							.number {
								line-height: 40rpx;
							}

							.success {
								color: $uni-text-color;
							}
						}

						&__detail {
							padding: 20rpx 0;
							justify-content: space-between;
							border-bottom: 1px solid rgba(236, 236, 236, 100);

							&__left {
								display: flex;
								height: 170rpx;

								.img {
									image {
										width: 170rpx;
										height: 170rpx;
										margin-right: 20rpx;
									}
								}
							}

							&__right {
								flex: 1;
								display: flex;
								height: 170rpx;
								flex-direction: column;
								justify-content: space-between;
								align-items: flex-end;
								
								.num{
									width: 100%;
									justify-content: space-between;
									.sku{
										color: #8C8C8C;
									}
								}
							}

						}

						&__detail:last-child {
							border: none;
						}

						&__total {
							display: flex;
							align-items: center;
							justify-content: flex-end;
							padding: 10rpx 0;

							.money {
								font-weight: bold;
								margin-left: 54rpx;
							}
						}

						&__btn {
							display: flex;
							align-items: center;
							justify-content: flex-end;
							padding-top: 10rpx;
							border-top: 1px solid rgba(236, 236, 236, 100);

							text {
								display: block;
								box-sizing: border-box;
								margin: 0 0 0 40rpx;
								width: 160rpx;
								height: 60rpx;
								line-height: 60rpx;
								text-align: center;
								font-size: 24rpx;
								border-radius: 8rpx;
								border: none;
							}

							text:first-child {
								background-color: #fff;
								line-height: 56rpx;
								border: 2rpx solid $uni-bg-color;
							}

							text:last-child {
								background-color: $uni-bg-color;
								color: #FFFFFF;
							}
						}
					}
				}
			}
		}

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