<template>
	<view class="container">
		<view class="empty" v-if="orderLists.length<=0 && loadingType != 'loading'">
			<image class="empty-img" src="../../static/empty-coupon.png" mode=""></image>
			<view class="empty-text">暂无订单</view>
		</view>
		<view class="order-list">
			<view class="item" v-for="(item, index) in orderLists" :key="index" @click="goDetails(item)">
				<view class="order-top">
					<view class="num">
						订单编号：{{item.orderSn}}
					</view>
					<view class="status" :style="{'color':statusTextColor[item.state]}">
						{{statusText[item.state]}}
					</view>
				</view>
				<view class="goods-info">
					<image class="img" :src="item.goodsPath" mode="aspectFill"></image>
					<view class="info">
						<view class="title">{{item.attrValue}}</view>
						<view class="spec">
							<view>{{item.attrName}}:{{item.denomination}}</view>
							<view>x{{item.payNumber}}</view>
						</view>
						<view class="price">
							支付积分：{{item.payIntegral}}
						</view>
					</view>
				</view>
				<view class="btn" v-if="item.state === 200">
					<view class="btn-item pay-btn" @click.stop="goCoupon(item)">
						查看卡券
					</view>
				</view>
			</view>
		</view>
		<uni-load-more :status="loadingType" v-if="orderLists.length>0"></uni-load-more>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				orderLists: [],
				loadingType: 'more',
				statusTextColor:{
					100:'#FF0520',200:'#FF6205',300:'#686868'
				},
				statusText:{
					100:'待兑换',200:'兑换成功',300:'兑换失败'
				},
				page: 1,
				isLogin: true
			};
		},
		onLoad() {
		},
		onPullDownRefresh () {
			console.log('下拉刷新');
		},
		onReachBottom () {
			this.getList()
		},
		onShow() {
			this.isLogin = this.$store.state.isLogin
		},
		onTabItemTap () {
			if (!this.$store.state.isLogin) {
				this.$router.push('Login')
				return
			}
			this.orderLists = []
			this.page = 1
			this.loadingType = 'loading'
			this.getList()
		},
		methods: {
			getList() {
				if (this.loadingType == 'noMore') {
					return
				}
				this.loadingType = 'loading';
				let params = {'page': this.page, 'pageSize':'10'}
				this.$net.post('/orders/list', params).then((res)=>{
					if (res && res.code==200) {
						this.orderLists = this.orderLists.concat(res.data.list)
						let paginate = res.data.paginate
						if (res.data.list.length < 10) {
							this.loadingType = 'noMore'
						} else {
							this.loadingType = 'more'
							this.page++
						}
					} else {
						this.loadingType = 'noMore'
						this.toast(res.message)
					}
				})
			},
			goDetails(item) {
				this.$router.push('OrderDetail',{'orderId':item.orderSn})
			},
			goCoupon(item) {
				this.$router.push('Coupon', {'orderId':item.orderSn})
			}
		},
		watch: {
			isLogin(newVal, oldVal) {
				console.log(newVal, oldVal);
				if (!oldVal && newVal) {
					this.orderLists = []
					this.page = 1
					this.loadingType = 'loading'
					this.getList()
				}
				if (oldVal && !newVal) {
					this.orderLists = []
				}
			}
		}
	}
</script>

<style>
	page{
		background-color: #F5F5F5;
	}
</style>
<style lang="scss" scoped>
.container {
	background-color: #F5F5F5;
	width: 100%;
	height: 100%;
	.order-list {
		padding: 24rpx;
		.item {
			border-radius: 16rpx;
			background-color: #FFFFFF;
			margin-bottom: 24rpx;
			padding: 32rpx 24rpx;
			.order-top {
				display: flex;
				justify-content: space-between;
				.num {
					font-size: 28rpx;
					color: #686868;
					line-height: 40rpx;
				}
				.status {
					font-size: 28rpx;
					line-height: 40rpx;
				}
			}
			.goods-info {
				display: flex;
				margin-top: 32rpx;
				.img {
					height: 200rpx;
					width: 200rpx;
					border-radius: 8rpx;
					background-color: #D8D8D8;
				}
				.info {
					margin: 4rpx 0 0 16rpx;
					position: relative;
					.title {
						font-size: 28rpx;
						color: #333333;
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
						line-height: 40rpx;
						width: 438rpx;
					}
					.spec {
						width: 438rpx;
						display: flex;
						justify-content: space-between;
						margin-top: 20rpx;
						view {
							font-size: 26rpx;
							color: #666666;
						}
					}
					.price {
						margin-top: 56rpx;
						width: 438rpx;
						text-align: right;
						font-size: 28rpx;
						font-weight: bolder;
						color: #252525;
					}
				}
			}
			.btn {
				width: 654rpx;
				display: flex;
				justify-content: flex-end;
				.btn-item {
					margin-top: 40rpx;
					height: 62rpx;
					width: 176rpx;
					border-radius: 32rpx;
					text-align: center;
					font-size: 24rpx;
					line-height: 64rpx;
				}
				.pay-btn {
					border: 2rpx solid #FF0520;
					color: #FF0520;
					margin-left: 24rpx;
				}
				.other-btn {
					border: 2rpx solid #C9C9C9;
					color: #686868;
				}
			}
		}
	}
	.empty {
		padding: 432rpx 136rpx 0;
		.empty-img {
			height: 280rpx;
			width: 480rpx;
		}
		.empty-text {
			font-size: 28rpx;
			color: #BFBFBF;
			margin-top: 48rpx;
			width: 480rpx;
			text-align: center;
		}
	}
}

</style>
