<template>
	<view>
		<view class="page">
			<ul class="tab">
				<li :class="{active: curTab === ''}" @click="tabChange('')">全部</li>
				<li :class="{active: curTab === 0}" @click="tabChange(0)">已上架</li>
				<li :class="{active: curTab === 2}" @click="tabChange(2)">已下架</li>
				<li :class="{active: curTab === 3}" @click="tabChange(3)">草稿</li>
				<li :class="{active: curTab === 4}" @click="tabChange(4)">审核中</li>
			</ul>
			
			<template v-if="list.length">
				<view class="list">
					<view v-for="item in list" @click="toDetail(item)">
						<view class="metas">
							<view class="time">2313</view>
							<view class="status">上架中</view>
						</view>
						<view class="info">
							<image class="pic" :src="item.coverUrl"></image>
							<view class="texts">
								<view class="name">{{ item.customerName }}</view>
								<view class="meta">
									市场价：20.00元
									<text class="sell">售价：10元</text>
								</view>
								<view class="meta">
									库存：1000
									已售：500
								</view>
								<view class="btns">
									<!-- <view class="btn" @click.stop="toDetail(item)">查看</view> -->
									<view class="btn off" @click.stop="changeStatus(item, 0)">下架</view>
									<view class="btn del" @click.stop="del(item)">删除</view>
									<view class="btn" @click.stop="toEdit(item)">编辑</view>
								</view>
							</view>
						</view>
					</view>
				</view>
				<uni-load-more :status="status" />
			</template>
			<empty v-else text="暂无商品"></empty>
			
			<uni-icons class="plus" type="plus-filled" size="60" color="#007eff" @click="$util.to('../addGoods/addGoods')"></uni-icons>
		</view>
	</view>
</template>

<script>
	import { page, batchDelete, prodStatus } from '@/apis/modules/goods.js'
	export default {
		data() {
			return {
				curTab: '',
				tabs: [],
				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: '加载中'
				})
				page({
					status: this.curTab
				}).then(({ data }) => {
					uni.hideLoading()
					// 未加载完所有数据，并且不是筛选，则拼接list，否则直接赋值
					this.list = this.reachBottom > 0 ? [...this.list, ...data.records] : data.records
					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()
			},
			// 上架下架
			async changeStatus(item, status) {
				await prodStatus(item.prodId, status)
				this.initList()
			},
			// 跳转详情
			toDetail(item) {
				this.$util.to(`../addGoods/addGoods?id=${item.prodId}&show=1`)
			},
			// 跳转编辑
			toEdit(item) {
				this.$util.to(`../addGoods/addGoods?id=${item.prodId}`)
			},
		}
	}
</script>

<style scoped lang="scss">
.page {
	padding-bottom: 90px;
}
.list {
	margin-top: 20rpx;
	li {
		padding: 20rpx;
		margin-bottom: 20rpx;
		// border-bottom: 1px solid #f1f1f1;
		background-color: #fff;
	}
	.metas {
		display: flex;
		justify-content: space-between;
		margin-bottom: 20rpx;
	}
	.time {
		font-size: 24rpx;
		color: #333;
	}
	.status {
		font-size: 24rpx;
		color: #333;
	}
	.info {
		display: flex;
		align-items: center;
	}
	.pic {
		width: 150rpx;
		height: 150rpx;
		margin-right: 20rpx;
	}
	.name {
		font-size: 30rpx;
		color: #333;
	}
	.meta {
		display: flex;
		margin: 10rpx 0;
		font-size: 28rpx;
		color: #999;
	}
	.btns {
		text-align: right;
	}
	.btn {
		padding: 8rpx 20rpx;
		margin-left: 20rpx;
		font-size: 28rpx;
		color: #fff;
		background-color: $uni-primary;
		border-radius: 4px;
		&.del {
			background-color: #f00;
		}
		&.off {
			background-color: #ccc;
		}
	}
}
</style>
