<template>
	<view>
		<view class="page">
			<ul class="tab">
				<li :class="{active: curTab === ''}" @click="tabChange('')">全部</li>
				<li :class="{active: curTab === 0}" @click="tabChange(0)">已上架({{ status0 }})</li>
				<li :class="{active: curTab === 2}" @click="tabChange(2)">已下架({{ status2 }})</li>
				<li :class="{active: curTab === 3}" @click="tabChange(3)">草稿({{ status3 }})</li>
				<li :class="{active: curTab === 4}" @click="tabChange(4)">审核中({{ status4 }})</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="time">{{ item.putawayTime }}</view>
							<view :class="['status', {grounding: !item.status}]">{{ Goods.goodsStatus.find(e => e.id === item.status).name }}</view>
						</view>
						<view class="info">
							<image class="pic" :src="item.pic"></image>
							<view class="texts">
								<view class="name">{{ item.prodName }}</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> -->
									<template v-if="item.status === 2 || item.status === 3">
										<view class="btn del" @click.stop="del(item)">删除</view>
										<view class="btn" @click.stop="toEdit(item)">编辑</view>
									</template>
									<view v-else-if="!item.status" class="btn off" @click.stop="changeStatus(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'
	import Goods from '@/config/goods.js'
	export default {
		data() {
			return {
				Goods,
				curTab: '',
				tabs: [],
				reachBottom: 0, // 是否是上拉加载。0->否,1->是,-1->加载完所有数据
				status: 'more', // 上拉加载状态 more|loading|noMore
				searchTimer: null,
				keyword: '',
				list: [],
				page: 1,
				pageSize: 10,
				status0: 0,
				status2: 0,
				status3: 0,
				status4: 0,
			}
		},
		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.status0 = 0
			this.status2 = 0
			this.status3 = 0
			this.status4 = 0
			this.initList()
		},
		methods: {
			// 获取列表
			getList() {
				uni.showLoading({
					title: '加载中'
				})
				page({
					status: this.curTab
				}).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()
			},
			// 下架
			async changeStatus(row, status) {
				const that = this
				uni.showModal({
					title: '提示',
					content: `确定要下架吗？`,
					async success(res) {
						if (res.confirm) {
							await prodStatus(row.prodId, 2)
							that.initList()
						}
					}
				})
			},
			// 删除
			del(row) {
				const that = this
				uni.showModal({
					title: '提示',
					content: '确定要删除吗？',
					async success(res) {
						if (res.confirm) {
							await batchDelete([row.prodId])
							that.$util.sucMsg('删除成功')
							that.initList()
						}
					}
				})
			},
			// 跳转详情
			toDetail(item) {
				this.$util.to(`../addGoods/addGoods?prodId=${item.prodId}&show=1`)
			},
			// 跳转编辑
			toEdit(item) {
				this.$util.to(`../addGoods/addGoods?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;
	}
	.time {
		font-size: 24rpx;
		color: #333;
	}
	.status {
		font-size: 24rpx;
		color: #333;
	}
	.grounding {
		color: #1fc133;
	}
	.info {
		display: flex;
		align-items: center;
	}
	.pic {
		width: 280rpx;
		height: 180rpx;
		margin-right: 20rpx;
		border-radius: 8px;
	}
	.texts {
		width: calc(100% - 300rpx);
	}
	.name {
		font-size: 30rpx;
		color: #333;
	}
	.meta {
		display: flex;
		margin: 10rpx 0;
		font-size: 24rpx;
		color: #999;
	}
	.btns {
		display: flex;
		justify-content: flex-end;
		margin-top: 20rpx;
	}
	.btn {
		padding: 10rpx 50rpx;
		margin-left: 20rpx;
		font-size: 28rpx;
		color: #fff;
		background-color: $uni-primary;
		border-radius: 20px;
		&.del {
			background-color: #ff982e;
		}
		&.off {
			background-color: #e23636;
		}
	}
}
</style>
