<template>
	<view>
		<view class="top">
			<!--商品搜索-->
			<view class="search">
				<u-search v-model="param.title" :animation="true" action-text="取消" placeholder="输入搜索内容" shape="square" @search="search" @clear="clear"></u-search>
			</view>
			<!--分类筛选-->
			<u-sticky>
				<!--分类筛选-->
				<view class="menu">
					<u-dropdown>
						<u-dropdown-item v-model="options.value1" title="全部分类" :options="options1" @change="change($event, 1)"></u-dropdown-item>
						<u-dropdown-item v-model="options.value2" title="综合排序" :options="options2" @change="change($event, 2)"></u-dropdown-item>
						<u-dropdown-item v-model="options.value3" title="筛选" :options="options3" @change="change($event, 3)"></u-dropdown-item>
					</u-dropdown>
				</view>
			</u-sticky>
		</view>
		<!--列表数据-->
		<view class="list">
			<view class="item" v-for="(item, index) in list" :key="index" @click="detail(item)">
				<image :src="ip + item.pic"></image>
				<view class="title omit">{{ item.title }}</view>
				<view class="price">¥{{ item.price }}</view>
			</view>
			<view class="clear"></view>
			<view class="loading" v-show="showLoadMore"><u-loading></u-loading></view>
			<u-empty class="noData" text="没有数据" :show="list.length == 0"></u-empty>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			ip: this.$http.urls.ip,
			list: [],
			param: { pageNum: 1, orderByColumn: 'id', isAsc: 'desc', typeId: '' },
			showLoadMore: true,
			options: { value1: 1, value2: 2, value3: 3 }, //options选中项的value值
			//商品分类
			options1: [{ label: '全部分类', value: '' }],
			//综合排序
			options2: [
				{
					label: '综合排序',
					value: 'id-desc'
				},
				{
					label: '时间降序',
					value: 'createTime-desc'
				},
				{
					label: '时间升序',
					value: 'createTime-asc'
				},
				{
					label: '价格降序',
					value: 'price-desc'
				},
				{
					label: '价格升序',
					value: 'price-asc'
				}
			],
			//筛选
			options3: [
				{
					label: '按销量',
					value: 'sells-desc'
				}
			]
		};
	},
	onLoad() {
		this.getData();
		this.getType();
	},
	methods: {
		getType() {
			this.$http.request({
				url: this.$http.urls.goods_type,
				loading: 'false',
				success: res => {
					res.data.data.forEach(item => {
						this.options1.push({ label: item.name, value: item.id });
					});
				}
			});
		},
		//获取数据
		getData() {
			this.$http.request({
				url: this.$http.urls.goods_list,
				data: this.param,
				loading: 'false',
				success: res => {
					this.showLoadMore = res.data.pages > this.param.pageNum ? true : false;
					res.data.rows.forEach(item => {
						this.list.push(item);
					});
				}
			});
		},
		//点击下拉菜单， tag当前点击标识(1，商品分类，2，综合排序和筛选)
		change(e, tag) {
			if (tag === 1) {
				this.param.typeId = e;
			} else {
				this.param.orderByColumn = e.split('-')[0];
				this.param.isAsc = e.split('-')[1];
			}
			this.refresh();
		},
		//点击键盘搜索按钮触发
		search() {
			this.param.typeId = '';
			this.refresh();
		},
		//清空搜索内容
		clear() {
			this.refresh();
		},
		//详情
		detail(item) {
			console.log("item.tbGoodsType.name:"+item.tbGoodsType.name);
			uni.navigateTo({
				url: 'details?id=' + item.id + '&goodsType=' + item.tbGoodsType.name
			});
		},
		//刷新数据
		refresh() {
			this.param.pageNum = 1;
			this.list = [];
			this.getData();
		}
	},
	//下拉刷新
	onPullDownRefresh() {
		setTimeout(() => {
			uni.stopPullDownRefresh();
			this.refresh();
		}, 1000);
	},
	//上拉加载
	onReachBottom() {
		if (this.showLoadMore) {
			this.param.pageNum++;
			this.getData();
		}
	}
};
</script>

<style lang="scss">
.top {
	background-color: white;
	.search {
		padding: 12px;
	}
	.menu {
		background-color: white;
		border-bottom: 1px solid #d8d8d8;
	}
}
.list {
	padding: 0px 10px 20px 10px;
	.item {
		background-color: white;
		margin-top: 5px;
		border-radius: 3px;
		width: 49%;
		padding: 10px;
		float: left;
		text-align: center;
		margin-right: 5px;
		image {
			width: 100px;
			height: 100px;
			border-radius: 7px;
		}
		&.item:nth-child(even) {
			margin-right: 0px;
		}
		.title {
			padding-top: 8px;
			font-size: 13px;
			text-align: left;
			color: #252525;
		}
		.price {
			padding-top: 8px;
			font-size: 15px;
			text-align: left;
			color: $theme-color;
		}
	}
}
</style>
