<template>
	<view>
		<view class="search">
			<u-search placeholder="搜索商品名称" v-model="param.goodsName" bgColor="white" @search="refresh()" :animation="true" actionText="取消" @clear="refresh()"></u-search>
			<view class="clear"></view>
		</view>
		<view class="goodsList">
			<view class="sort-list">
				<view class="sort-item" :class="{ active: item.check }" v-for="(item, index) in sort" :key="index" @click="select(item, index)">
					<text>{{ item.name }}</text>
					<text v-if="index > 0 && item.orderBy == 'desc'" class="icon">&#xeb0a;</text>
					<text v-if="index > 0 && item.orderBy == 'asc'" class="icon">&#xeb0b;</text>
				</view>
				<view class="type" @click="go()">
					<text>{{ param.goodsType || '分类' }}</text>
					<text class="icon">&#xe8f2;</text>
				</view>
			</view>
			<view class="item" v-for="(item, index) in list" :key="index" @click="detail(item.id)">
				<view class="title">{{ item.merchantName }}</view>
				<view class="goodsType omit">{{ item.goodsType }}</view>
				<image src="../../../static/news.jpg" mode="aspectFill" class="pic"></image>
				<view class="con">
					<view class="productName omit">{{ item.goodsName }}</view>
					<view class="desc omit">
						<text>{{ item.grossWeight }} {{ item.goodsUnits }}</text>
						<text>{{ item.tradeAreaName }}</text>
					</view>
					<view class="price">¥ {{ item.price }}</view>
					<view class="icon buy" @click.stop="addCar(item)" v-if="user.userType == 2">&#xe604;</view>
				</view>
				<view class="clear"></view>
			</view>
			<view class="loading" v-if="loadMore"><u-loadmore :status="loadMore ? 'loading' : 'nomore'" /></view>
			<u-empty v-if="!loadMore && list.length == 0"></u-empty>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			show: false,
			user: this.getUser(),
			sort: [
				{ name: '综合', sortType: 1, orderBy: 'desc', check: false },
				{ name: '日期', sortType: 15, orderBy: 'desc', check: false },
				{ name: '价格', sortType: 7, orderBy: 'desc', check: false }
			],
			list: [],
			param: { pageNo: 1, pageSize: 10 },
			loadMore: true
		};
	},
	onLoad() {
		this.getData();
		//选择商品分类
		uni.$on('productType', res => {
			this.param.goodsType = res.name;
			this.param.current = res.current;
			this.param.now = res.now;
			this.refresh();
		});
	},
	methods: {
		select(item, index) {
			this.sort.forEach(it => (it.check = false));
			item.check = true;
			if (index > 0) {
				this.orderBy = this.orderBy == 'desc' ? 'asc' : 'desc';
				item.orderBy = this.orderBy;
			}
			this.param.orderBy = item.orderBy;
			this.param.sortType = item.sortType;
			this.refresh();
		},
		getData() {
			this.http.request({
				url: '/level-one-server/app/TbGoodsTransit/getList',
				data: this.param,
				loading: 'false',
				success: res => {
					this.loadMore = parseInt(res.data.pageCount) > this.param.pageNo;
					this.list.push(...res.data.data);
				}
			});
		},
		//选择分类
		go() {
			uni.navigateTo({ url: '/pages/market/productType?current=' + this.param.current + '&now=' + this.param.now });
		},
		// 添加购物车
		addCar(goods) {
			let params = {
				buyUserId: this.user.id,
				enterpriseId: goods.merchantId,
				shopId: goods.shopId,
				tradeAreaId: goods.tradeAreaId,
				saleGoodsInfoId: goods.id,
				goodsImg: goods.goodsImg,
				goodsName: goods.goodsName,
				publishGoodsId: goods.id
			};
			this.http.request({
				url: '/level-one-server/app/TbGoodsCart/addGoodsInShopCart',
				data: params,
				contentType: 'application/json; charset=utf-8',
				method: 'POST',
				success: res => {
					uni.showToast({ title: '添加成功' });
				}
			});
		},
		detail(id) {
			uni.navigateTo({ url: '/pages/market/one/detail?id=' + id });
		},
		//刷新数据
		refresh() {
			this.loadMore = true;
			this.param.pageNo = 1;
			this.list = [];
			this.getData();
		}
	},
	//下拉刷新
	onPullDownRefresh() {
		setTimeout(() => {
			this.refresh();
			uni.stopPullDownRefresh();
		}, 1000);
	},
	//上拉加载
	onReachBottom() {
		if (this.loadMore) {
			this.param.pageNo++;
			this.getData();
		}
	}
};
</script>

<style lang="scss">
page {
	background-color: $pg;
}
.search {
	padding: 12px 12px 0px 12px;
}
</style>