<template>
	<view class="container all-type-list-wrapper">
		<commodity-list ref="childList" :empty-text="emptyText" :serverUrl="serverUrl" :typeId="typeId" v-if="isRequest"></commodity-list>
	</view>
</template>

<script>
	import commodityList from '@/components/cm-module/listTemplate/commodityList'
	
	export default{
		components:{
			commodityList
		},
		data(){
			return{
				serverUrl: '',
				emptyText: '',
				lastPageType: '',
				lastPageVal: '',
				isRequest:false,
				typeId:null
			}
		},		
		onLoad(option) {
			this.setServerUrl(option);
		},
		methods:{
			setServerUrl(option) {
				let self = this;
				const {type, from: value} = option;
				this.lastPageType = type;
				this.lastPageVal = value;
				uni.setNavigationBarTitle({title:value});
				uni.getStorage({
					key: 'commodity_id',
					success: function (res) {
						const emptyTxt = '正在上架中，敬请期待~';
						/**
						 *@商品分类列表传参类型
						 *@type  首页分类：0，首页商品模块1：1,首页商品模块2：2,首页商品模块3：3，底部导航分类：4
						 **/
						switch(type){
							case '0':
								self.typeId = res.data;
								self.serverUrl = '/search/query/product/classify';
								self.emptyText = '该分类暂时还没有商品哟，去逛逛别的吧~';
								break;
							case '1':	
								self.typeId = 1;
								self.serverUrl = '/search/query/product/preferred';
								self.emptyText = `${self.lastPageVal}商品${emptyTxt}`;
								break;
							case '2':
								self.typeId = 10;
								self.serverUrl = '/search/query/product/preferred';
								self.emptyText = `${self.lastPageVal}商品${emptyTxt}`;
								break;
							case '3':
								self.typeId = 100;
								self.serverUrl = '/search/query/product/preferred';
								self.emptyText = `${self.lastPageVal}${emptyTxt}`;
								break;
							case '4':
								self.typeId = res.data;
								self.serverUrl = '/search/query/product/tinyType';
								self.emptyText = '该分类暂时还没有商品哟，去逛逛别的吧~';
								break;
						}
						self.isRequest = true
					}
				})
			}
		},
		onShow() {
			let pages = getCurrentPages(),
				thisPage = pages[pages.length - 1];
			if(thisPage.refresh) {
				this.setServerUrl(thisPage.listData);
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: $sub-bg-color;
		.all-type-list-wrapper {
			display: flex;
			flex-direction: column;
		}
	}
</style>
