<template>
	<view class="pad-x20" v-if="isLoading">
		<!-- 头部 -->
		<status-nav :ifReturn="true" navBarTitle="我的发布"></status-nav>
		<!-- 商品分类 -->
		<goods-cate @changeCateEv="changeCateEv"></goods-cate>
		<!-- 商品列表 -->
		<goods-list :goodsList="goodsList" :isOperate="true" @changeStateEv="changeStateEv"></goods-list>
		<!-- 没有更多 -->
		<view class="no-more mar-s20 font24 color-99">没有更多数据了</view>
		<!-- 发布按钮 -->
		<release-btn></release-btn>
	</view>
</template>
<script>
	import goodsCate from '@/components/goods-cate/goods-cate.vue';
	import goodsList from '@/components/goods-list/goods-list.vue';
	import releaseBtn from '@/components/release-btn/release-btn.vue';
	export default {
		components:{
			goodsCate,
			goodsList,
			releaseBtn
		},
		data() {
			return {
				statusHeight:uni.getSystemInfoSync().statusBarHeight, //状态栏高度
				goodsList:[], //商品列表
				page:1, //页数
				size:10, //条数
				total:0, //总数
				isLoading:false, //是否加载完成
				flag:true, //改变状态
				category_id:0, //分类id
			}
		},
		onShow() {
			// 查询商品列表
			this.getGoodsList(this.category_id);
		},
		onReachBottom(e) {
			if(this.goodsList.length<this.total){
				this.page++;
				// 查询商品列表
				this.getGoodsList(this.category_id);
			}
		},
		onPullDownRefresh() {
			this.page = 1;
			// 查询商品列表
			this.getGoodsList(this.category_id);	
			// 关闭下拉刷新
			uni.stopPullDownRefresh();
		},
		methods: {
			// 查询商品列表
			getGoodsList(id){
				uni.showLoading({
					title:'加载中'
				})
				let params = {
					page:this.page,
					size:this.size,
					category_id:id
				}
				if(this.page==1) this.goodsList = [];
				this.$requst.get('/api/v1/user/goods',params).then(res=>{
					if(res.code == 0){
						console.log(res,'我的发布列表')
						this.total = res.data.total;
						let goodsArr = [];
						res.data.list.forEach(item=>{
							let obj = {
								id:item.id,
								cover:item.cover,
								name:item.title,
								original_price:item.original_price,
								price:item.price,
								status:item.status
							}
							goodsArr.push(obj)
						})
						this.goodsList = this.goodsList.concat(goodsArr);
					}
					uni.hideLoading();
					this.isLoading = true;
				})
			},
			
			// 分类选择事件
			changeCateEv(category_id) {
				this.category_id = category_id;
				// 查询商品列表
				this.getGoodsList(this.category_id);
			},
			
			// 更改显示状态
			changeStateEv(id,index){
				if(this.flag){
					this.flag =false;
					this.$requst.post('/api/v1/goods/set-status',{id:id,status:this.goodsList[index].status==0?1:0}).then(res=>{
						if(res.code == 0){
							console.log(res,'改变状态');
							if(this.goodsList[index].status==0){
								this.goodsList[index].status = 1;
								this.$toolAll.tools.showToast('已下架');
							}else{
								this.goodsList[index].status = 0;
								this.$toolAll.tools.showToast('已上架');
							}
						}
					})
					setTimeout(()=>{
						this.flag = true;
					},500)
				}
			},
			
			// 去详情页
			goDetail(id){
				uni.navigateTo({
					url:`/pages/index/detail?id=${id}`
				})
			},
		}
	}
</script>
<style scoped>
	/* 没有更多 */
	.no-more{
		text-align: center;
		line-height: 2;
	}
</style>