<template>
	<view>
		<view class="search">
			<u-search placeholder="搜索商品名称" v-model="param.customerName" 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.isAsc == 'desc'" class="icon">&#xeb0a;</text>
					<text v-if="index > 0 && item.isAsc == 'asc'" class="icon">&#xeb0b;</text>
				</view>
				<view class="type" @click="show = true">
					<text>分类</text>
					<text class="icon">&#xe8f2;</text>
				</view>
				<view class="clear"></view>
			</view>
			<view class="item" v-for="(item, index) in list" :key="index" @click="detail(item.id)">
				<view class="title">广西京东自营店南宁分店</view>
				<image src="../../../static/news.jpg" mode="aspectFill" class="pic"></image>
				<view class="con">
					<view class="productName omit">{{ item.goodsName }}</view>
					<view class="desc">
						<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: '综合' }, { name: '日期' }, { name: '价格' }],
			list: [],
			param: { pageNo: 1, pageSize: 10 },
			loadMore: true
		};
	},
	onLoad() {
		this.getData();
	},
	methods: {
		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);
				}
			});
		},
		// 添加购物车
		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>
