<template>
	<view>
		<!-- tab标签 -->
		<view class="lf-p-r-32 lf-p-l-32">
			<u-tabs :list="tab_list" bg-color="#F6F6F6" :is-scroll="false" :current="current" @change="change"></u-tabs>
		</view>
		<swiper :style="{height: 'calc('+ windowHeight +'px - 110rpx)', width: '750rpx'}" :current="current"
			@change="swiperChange">
			<swiper-item v-for="(tab, tabIndex) in tab_list" :key="tabIndex">
				<scroll-view class="com" :scroll-y="true" :refresher-enabled="true" :refresher-triggered="isRefresher"
					@scrolltolower="onScrolltolower" @refresherrefresh="onRefresherrefresh">
					<view class="flex-direction justify-around list" v-for="(item, index) in tab.list" :key="item.id" @tap="goDetails(tabIndex,index)">
						<view class="lf-row-between">
							<view class="left" style="position: relative;display: flex;">
								<image :src="item.product.picture" mode="aspectFill"></image>
							</view>
							<view class="right">
								<view class="lf-line-2 title lf-font-28 lf-color-333" style="line-height: 40rpx;">{{item.product.title}}</view>
								<view class="lf-flex tips lf-m-t-10">
									<text class="progress margin-right-xs lf-color-gray">数量</text>
									<text class="bought lf-color-gray">x {{item.num}}</text>            
								</view>
								<view class="lf-row-between price lf-m-t-16" style="padding-right: 6rpx;">
									<lf-price :price="item.price" style="margin-top: 10rpx;" />
									<button class="lf-font-24 radius-order col-btn0" v-if="item.status == 0" @click="$url('/pages/order/confirm_order?goods_id='+item.id+'&btn_type=1')">立即付款</button>
									<button class="lf-font-24 radius-order col-btn0" v-if="item.status == 1" @click="$url('/pages/order/confirm_order?goods_id='+item.id+'&btn_type=2')">支付尾款</button>
									<button class="lf-font-24 radius-order col-btn1" v-if="item.status == 2 || item.status == 3">立即使用</button>
									<button class="lf-font-24 radius-order col-btn16" v-if="item.status == 16">已使用</button>
									<button class="lf-font-24 radius-order col-btn6" v-if="item.status == 6">等待审核</button>
									<button class="lf-font-24 radius-order col-btn7" v-if="item.status == 7">退款成功</button>
								</view>
							</view>
						</view>
						<view>
							<view class="solid-top flex justify-between align-center text-center">
								<view class="text-gray lf-font-28" style="padding: 20rpx;">
									{{$timer(item.created_at*1000)}}
								</view>
								<view :class="'col-status'+item.status" style="padding: 20rpx 24rpx 20rpx 20rpx;">
									{{item.status_text}}
								</view>
							</view>
						</view>
					</view>
					<!-- 空数据的情况 -->
					<view class="loading-more">
						<text v-if="tab.list.length"
							:class="{'loading-more-text': tab.loadingClass}">{{ tab.loadingText }}</text>
						<lf-nocontent v-else></lf-nocontent>
					</view>
					<!-- 回到顶部 -->
					<u-back-top :scroll-top="pageScrollTop" :custom-style="{background: 'rgba(51, 51 51, 0.3)'}">
					</u-back-top>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tab_list: [{
						name: '全部',
						type: '',
						list: [],
						loadingClass: true,
						loadingText: '正在加载中',
						page: 1,
						isPage: true
					}, {
						name: '待付款',
						type: 0,
						list: [],
						loadingClass: true,
						loadingText: '正在加载中',
						page: 1,
						isPage: true
					}, {
						name: '已付款',
						type: 1,
						list: [],
						loadingClass: true,
						loadingText: '正在加载中',
						page: 1,
						isPage: true
					},
					{
						name: '已完成',
						type: 16,
						list: [],
						loadingClass: true,
						loadingText: '正在加载中',
						page: 1,
						isPage: true
					},
					{
						name: '售后',
						type: 6,
						list: [],
						loadingClass: true,
						loadingText: '正在加载中',
						page: 1,
						isPage: true
					}
				],
				current: 0, // tab下表
				pageSize: 10,
				windowHeight: 0, // 屏幕可用高度
				showLogin: true,
				isRefresher: true
			}
		},
		onLoad(e) {
			this.windowHeight = getApp().globalData.windowHeight;
			// this.verifyUserInfo()
			this.getUserOrder()
		},
		methods: {
			// 检查当前用户登录状态
			verifyUserInfo(){
				let userInfo = uni.getStorageSync('userinfo') || {};
				if(!userInfo.id || !userInfo.nickname || !userInfo.avatar){
					if(this.showLogin){
						this.showLogin = false;
						this.$url('/pages/login/index?type=userinfo');
					}else{
						this.showLogin = true;
						this.$url('/pages/index/index', {type: 'switch'});
					}
				}
			},
			goDetails(tabIndex,index) {
				let item = this.tab_list[tabIndex].list[index]
				if (item.status == 1) {
					this.$url('/pages/order/unpay_details?order_id=' + item.id)
				}else if(item.status == 6){
					this.$url('/pages/order/refund_detail?order_id=' + item.id)
				}else {
					this.$url('/pages/order/order_details?order_id=' + item.id)
				}
			
			},
			//获取订单数据 
			getUserOrder(options = {}) {
				let per_page = this.pageSize;
				let tab_item = this.tab_list[this.current];
				this.$http(this.API.API_ORDERLIST, {
					status: this.tab_list[this.current].type,
					page: tab_item.page
				}).then(res => {
					let isPage = res.data.has_more_page;
					tab_item.isPage = isPage;
					if (!isPage) {
						tab_item.loadingClass = false;
						tab_item.loadingText = '没有更多数据啦~';
					}
					if(options.type == 'pageRefresh'){
						uni.stopPullDownRefresh();
					}else if(options.type == 'scrollRefresh'){
						this.isRefresher = false;
					}
					if (tab_item.page == 1) {
						tab_item.list = res.data.data;
					} else {
						tab_item.list.push(...res.data.data);
					}
					console.log(tab_item.list)
				})
			},

			// 切换tab
			change(index) {
				this.current = index;
				this.getUserOrder();
			},
			// 滑块下标值变化
			swiperChange(event) {
				this.current = event.detail.current;
				if (event.detail.source == '') return; // 如果是被动出发，没有事件类型则不做处理
				this.getUserOrder();
			},

			// 页面触底，加载下一页
			onScrolltolower(){
				let tab_item = this.tab_list[this.current];
				if(tab_item.isPage){
					tab_item.page = tab_item.page + 1;
					this.getUserOrder();
				}
			},
			// scroll-view 下拉刷新
			onRefresherrefresh(){
				this.isRefresher = true;
				this.refreshFn({type: 'scrollRefresh'});
			},
			// 下拉刷新处理
			refreshFn(options){
				let tab_item = this.tab_list[this.current];
				tab_item.page = 1;
				tab_item.isPage = true;
				tab_item.loadingClass = true;
				tab_item.list = []
				tab_item.loadingText = '正在加载中';
				this.getUserOrder(options);
			}
		},
		onPullDownRefresh() {
			this.refreshFn({type: 'pageRefresh'});
		}
	}
</script>

<style lang="scss">
	.col-status2 {
		color: #0BCE5F!important;
	}
	.col-status3 {
		color: #0BCE5F!important;
	}
	.col-status0 {
		color: #FF0000!important;
	}
	.col-status1 {
		color: #FF0000!important;
	}
	.col-status16 {
		color: #1998FE!important;
	}
	.col-status6 {
		color: #555555!important;
	}
	.col-status7 {
		color: #555555!important;
	}
	.col-btn1 {
		color: #0BCE5F!important;
		border: 1px solid #0BCE5F!important;
	}
	.col-btn0 {
		color: #FF0000!important;
		border: 1px solid #FF0000!important;
	}
	.col-btn16 {
		color: #1998FE!important;
		border: 1px solid #1998FE!important;
	}
	.col-btn6 {
		color: #555555!important;
		border: 1px solid #555555!important;
	}
	.col-btn7 {
		color: #555555!important;
		border: 1px solid #555555!important;
	}
	page {
		background-color: #F6F6F6;
	}
	.radius-order {
		border-radius: 31.5rpx;
	}
	
	.font-400 {
		font-weight: 400;
	}

	.tag-self {
		position: absolute !important;
		top: 0 !important;
		border-radius: 20rpx 0 20rpx 0 !important;
		width: max-content;
		height: 32rpx !important;
	}

	.title {
		font-size: 28rpx;
		color: $u-content-color;
		height: 80rpx;
	}

	// tab
	.ctab {
		width: 100%;
		margin: 20rpx 0 0rpx 0rpx;
		padding: 0 22rpx;
	}

	// 商品列表
	.com {
		width: 100%;
		height: 100%;
		overflow: hidden;

		.list {
			border-radius: 20rpx;
			overflow: hidden;
			margin: 0 32rpx 30rpx 32rpx;
			background-color: #FFFFFF;
			// box-shadow: 0 10rpx 20rpx 0 rgba(0, 0, 0, 0.1);
			align-items: flex-start;

			.left {
				overflow: hidden;

				image {
					width: 210rpx;
					height: 210rpx;
					margin: 20rpx;
					border-radius: 20rpx;
				}
			}

			.right {
				overflow: hidden;
				width: 67%;

				.title {
					margin: 0rpx 20rpx 0rpx 0;
					color: #222222;
					font-size: 32rpx;
				}

				.tips {
					margin: 16rpx 0;
					overflow: hidden;

					.u-line-progress {
						width: 112rpx;
						overflow: hidden;
						margin-right: 20rpx;
					}

					.progress {
						color: #777777;
						font-size: 24rpx;
					}

					.bought {
						color: #777777;
						font-size: 24rpx;
						margin-right: 20rpx;
					}
				}

				.price {
					overflow: hidden;
					color: #FF0000;

					button {
						width: 150rpx;
						height: 63rpx;
						background: #fff;
						border-radius: 31.5rpx;
						font-size: 24rpx;
						color: #FF0000;
						margin: 0rpx 20rpx 0rpx 20rpx;
						border: 1px solid #FF0000;
						line-height: 60rpx;
						padding: 0;
					}
				}
			}
		}
	}
</style>
