<template>
	<view class="container">
		<view class="order-tab">
			<u-tabs :list="tabList" :is-scroll="false" :current="tabIndex" @change="changeTabs" font-size="30" active-color="#52A63A"
			 inactive-color="#666666" :bold="false" height="90"></u-tabs>
		</view>
		<scroll-view class="order-list" scroll-y="true" @scrolltolower="handleLoadMore(1)" v-if="tabIndex==0">
			<orderItem v-for="(item, index) in orderList1" :key="index" :orderData="item" :tabIndex="tabIndex + 1"></orderItem>
		</scroll-view>
		<scroll-view class="order-list" scroll-y="true" @scrolltolower="handleLoadMore(2)" v-else-if="tabIndex==1">
			<orderItem v-for="(item, index) in orderList2" :key="index" :orderData="item" :tabIndex="tabIndex + 1" @reasetList="reasetList()"></orderItem>
		</scroll-view>
		<scroll-view class="order-list" scroll-y="true" @scrolltolower="handleLoadMore(3)" v-else-if="tabIndex==2">
			<orderItem v-for="(item, index) in orderList3" :key="index" :orderData="item" :tabIndex="tabIndex + 1" @reasetList="reasetList()"></orderItem>
		</scroll-view>
		<scroll-view class="order-list" scroll-y="true" @scrolltolower="handleLoadMore(4)" v-else-if="tabIndex==3">
			<orderItem v-for="(item, index) in orderList4" :key="index" :orderData="item" :tabIndex="tabIndex + 1" @reasetList="reasetList()"></orderItem>
		</scroll-view>
		<scroll-view class="order-list" scroll-y="true" @scrolltolower="handleLoadMore(5)" v-else-if="tabIndex==4">
			<orderItem v-for="(item, index) in orderList5" :key="index" :orderData="item" :tabIndex="tabIndex + 1" @reasetList="reasetList()"></orderItem>
		</scroll-view>
		<u-top-tips ref="uTips"></u-top-tips>
	</view>
</template>

<script>
	const NET = require('@/utils/request')
	const API = require('@/config/api')
	import orderItem from '@/pagesMain/orderItem.vue'
	export default {
		components: {
			orderItem
		},
		data() {
			return {
				tabIndex: 0,
				tabList: [{
					name: '全部'
				}, {
					name: '待付款'
				}, {
					name: '待发货'
				}, {
					name: '待收货'
				}, {
					name: '待评价'
				}, ],
				pageIndex1: 1,
				isOver1: false,
				orderList1: [],
				pageIndex2: 1,
				isOver2: false,
				orderList2: [],
				pageIndex3: 1,
				isOver3: false,
				orderList3: [],
				pageIndex4: 1,
				isOver4: false,
				orderList4: [],
				pageIndex5: 1,
				isOver5: false,
				orderList5: [],
			}
		},
		onLoad(options) {
			this.tabIndex = options.type - 1
			this.getOrderList(options.type)
		},
		onReady(options) {},
		onPullDownRefresh() {
			this['isOver' + (this.tabIndex + 1)] = false
			this['pageIndex' + (this.tabIndex + 1)] = 1
			this['orderList' + (this.tabIndex + 1)] = []
			this.getOrderList(this.tabIndex + 1, 'refresh')
		},
		methods: {
			//  切换tab
			changeTabs(index) {
				this.tabIndex = index;
				this.reasetList()
			},
			//  刷新数据
			reasetList() {
				this['isOver' + (this.tabIndex + 1)] = false
				this['pageIndex' + (this.tabIndex + 1)] = 1
				this['orderList' + (this.tabIndex + 1)] = []
				this.getOrderList(this.tabIndex + 1)
			},
			//  懒加载
			handleLoadMore(type) {
				if (!this['isOver' + type]) {
					this['pageIndex' + type]++
					this.getOrderList(type)
				}
			},
			//  获取全部商品
			getOrderList(type, refresh) {
				NET.request(API.getOrderList + this['pageIndex' + type] + '/10', {
					flag: type,
				}, 'GET').then(res => {
					if (refresh == 'refresh') {
						uni.stopPullDownRefresh();
					}
					this['isOver' + type] = res.data.list.length != 10
					this['orderList' + type] = this['orderList' + type].concat(res.data.list)
				}).catch(error => {
					this.$refs.uTips.show({
						title: error.data.msg,
						type: 'warning',
					})
				})
			},
		}
	}
</script>

<style lang="less" scoped>
	page {
		width: 100%;
		height: 100%;
	}

	.container {
		width: 100%;
		height: 100%;
		float: left;
		background-color: #f7f7f7;
		position: absolute;

		.order-tab {
			width: 100%;
			height: 45px;
			float: left;
		}

		.order-list {
			width: calc(100% - 30px);
			height: calc(100% - 46px);
			float: left;
			margin: 0 15px;
			box-sizing: border-box;
			padding-top: 15px;
		}
	}
</style>
