<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>
			<!--分类筛选-->
			<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>
		</view>
		<!--列表数据-->
		<view class="list">
			<view class="goods_item" v-for="(item, index) in list" :key="index" @click="detail(item)">
				<image :src="ip + item.pic" mode="aspectFill"></image>
				<view class="title omit">
					<text>{{ item.title }}</text>
				</view>
				<view class="price">
					<text>¥{{ item.price }}</text>
					<text class="day">/天</text>
					<text class="icon" v-show="item.mType == 0">&#xe61e;</text>
					<text class="icon" v-show="item.mType == 1">&#xe61c;</text>
				</view>
			</view>
			<view class="clear"></view>
			<view class="loading"><u-loadmore :status="loadMore ? 'loading' : 'nomore'" /></view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			ip: this.$http.urls.ip,
			list: [],
			param: { pageNum: 1, orderByColumn: 'id', isAsc: 'desc', mType: '' },
			loadMore: true,
			options: { value1: '', value2: 'id-desc', value3: '' }, //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: 'id-desc'
				}
			]
		};
	},
	onLoad() {
		this.getData();
		this.getType();
	},
	methods: {
		getType() {
			this.$http.request({
				url: this.$http.urls.dictType + 'm_type',
				loading: 'false',
				success: res => {
					console.log('zx:' + JSON.stringify(res));
					res.data.data.forEach(item => {
						this.options1.push({ label: item.dictLabel, value: item.dictValue });
					});
				}
			});
		},
		//获取数据
		getData() {
			this.$http.request({
				url: this.$http.urls.goods_list,
				data: this.param,
				loading: 'false',
				success: res => {
					this.loadMore = 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.mType = e;
			} else {
				this.param.orderByColumn = e.split('-')[0];
				this.param.isAsc = e.split('-')[1];
			}
			this.refresh();
		},
		//点击键盘搜索按钮触发
		search() {
			this.param.mType = '';
			this.refresh();
		},
		//清空搜索内容
		clear() {
			this.refresh();
		},
		//详情
		detail(item) {
			uni.navigateTo({ url: '/pages/goods/detail?id=' + item.id });
		},
		//刷新数据
		refresh() {
			this.loadMore = true;
			this.param.pageNum = 1;
			this.list = [];
			this.getData();
		}
	},
	//下拉刷新
	onPullDownRefresh() {
		setTimeout(() => {
			uni.stopPullDownRefresh();
			this.refresh();
		}, 1000);
	},
	//上拉加载
	onReachBottom() {
		if (this.loadMore) {
			this.param.pageNum++;
			this.getData();
		}
	}
};
</script>

<style lang="scss">
page {
	background-color: $page;
}
.top {
	background-color: white;
	.search {
		padding: 12px;
	}
	.menu {
		background-color: white;
		border-bottom: 1px solid #d8d8d8;
	}
}
.list {
	padding: 0px 10px 20px 10px;
}
</style>
