<template>
	<view>
		<view class="page">
			<ul class="tab">
				<li v-for="(item, i) in tabs" :class="{active: curTab === tab.id}" @click="tabChange(tab.id)">{{ item.name }}</li>
			</ul>
			
			<template v-if="list.length">
				<view class="list">
					<view class="item" v-for="item in list" @click="toDetail(item)">
						<view class="metas">
							<view class="serial">编号：{{ item.putawayTime }}</view>
							<view class="date">提交时间：</view>
						</view>
						<view class="pro-name">
							<image class="icon" :src="item.logoUrl ? item.logoUrl : 'https://occupationlab.com/images/preschoolEdu/preSchool-icon.png'"></image>
							{{ item.companyName }}
						</view>
						<view class="info">
							<view class="texts">
								<view class="meta">
									采购内容：{{ item.oriPrice || 0 }}
								</view>
								<view class="meta">
									数量：{{ item.totalStocks || 0 }}
								</view>
							</view>
							<view class="type">组织采购</view>
						</view>
					</view>
				</view>
				<uni-load-more :status="status" />
			</template>
			<empty v-else text="暂无线索"></empty>
		</view>
	</view>
</template>

<script>
	import { myOrder } from '@/apis/modules/order.js'
	import Goods from '@/config/goods.js'
	export default {
		data() {
			return {
				Goods,
				curTab: '',
				tabs: [
					{
						id: '',
						name: '全部'
					},
					{
						id: 2,
						name: '组织'
					},
					{
						id: 1,
						name: '个人'
					},
				],
				reachBottom: 0, // 是否是上拉加载。0->否,1->是,-1->加载完所有数据
				status: 'more', // 上拉加载状态 more|loading|noMore
				searchTimer: null,
				keyword: '',
				list: [],
				page: 1,
				pageSize: 10,
			}
		},
		watch: {
			keyword () {
				clearTimeout(this.searchTimer)
				this.searchTimer = setTimeout(() => {
					this.initList()
				}, 500)
			}
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.initList()
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 1500)
		},
		// 上拉加载
		onReachBottom() {
			if (this.reachBottom >= 0) {
				this.reachBottom = 1
				this.status = 'loading'
				this.getList()
			}
		},
		onShow() {
			this.initList()
		},
		methods: {
			// 获取列表
			getList() {
				uni.showLoading({
					title: '加载中'
				})
				myOrder({
					status: this.curTab,
					page: this.page
				}).then(({ data }) => {
					uni.hideLoading()
					// 未加载完所有数据，并且不是筛选，则拼接list，否则直接赋值
					const list = data.records
					list.forEach(e => {
						if (e.pic) e.pic = e.pic.split(',')[0]
					})
					this.list = this.reachBottom > 0 ? [...this.list, ...list] : list
					this.page++ // 每次获取了数据后page+1
					const noMore = this.list.length === data.total // 是否加载完所有数据
					this.status = noMore ? 'noMore' : 'more' // 加载完了则设置为noMore
					this.reachBottom = noMore ? -1 : 0 // 加载完了则设置为-1
				}).catch(e => {
					uni.hideLoading()
				})
			},
			initList() {
				this.page = 1
				this.reachBottom = 0
				this.getList()
			},
			// tab切换
			tabChange(id) {
				this.curTab = id
				this.initList()
			},
			// 跳转详情
			toDetail(item) {
				this.$util.to(`../procureDetail/procureDetail?prodId=${item.prodId}`)
			},
		}
	}
</script>

<style scoped lang="scss">
.page {
	padding-bottom: 90px;
}
.list {
	margin-top: 20rpx;
	padding: 20rpx;
	.item {
		padding: 20rpx;
		margin-bottom: 20rpx;
		// border-bottom: 1px solid #f1f1f1;
		background-color: #fff;
	}
	.metas {
		display: flex;
		justify-content: space-between;
		margin-bottom: 20rpx;
	}
	.serial {
		font-size: 24rpx;
		color: #333;
	}
	.date {
		font-size: 24rpx;
		color: #ccc;
	}
	.info {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	.pro-name {
		display: flex;
		align-items: center;
		padding: 18rpx 0;
		font-size: 32rpx;
		font-weight: 600;
		color: #333;
		border-bottom: 1px solid #E6E8ED;
		.icon {
			width: 66rpx;
			min-width: 66rpx;
			height: 66rpx;
			margin-right: 20rpx;
			border-radius: 4px;
		}
	}
	.meta {
		margin: 10rpx 0;
		font-size: 24rpx;
		color: #999;
	}
	.type {
		font-size: 24rpx;
		color: #1fc133;
	}
}
</style>
