<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.isAsc == 'desc'" class="icon">&#xeb0a;</text>
					<text v-if="index > 0 && item.isAsc == '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)">
				<view class="title">{{ item.createName }}</view>
				<image src="../../../static/news.jpg" mode="aspectFill" class="pic"></image>
				<view class="con">
					<view class="productName omit">{{ item.goodsName }}</view>
					<view class="price">¥ {{ item.resalePrice }}</view>
					<view class="icon btn" v-if="user.userType == 3" @click.stop="addCar(item)">&#xe600;</view>
					<view class="btn" v-if="user.userType == 3" @click.stop="buy(item)">购买</view>
				</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>
		<productType v-model="show"></productType>
		<button class="addBtn" @click="goCar()">
			<text class="icon">&#xe600;</text>
			<view class="bag animated" :class="{ bounce: add }" v-if="cars > 0">{{ cars }}</view>
		</button>
	</view>
</template>

<script>
export default {
	data() {
		return {
			user: this.getUser(),
			show: false,
			isAsc: 'desc',
			cars: 0,
			add: false,
			sort: [
				{ name: '综合', sortName: 'id', isAsc: 'desc', check: false },
				{ name: '日期', sortName: 'create_time', isAsc: 'desc', check: false },
				{ name: '数量', sortName: 'goods_quantity', isAsc: 'desc', check: false }
			],
			list: [],
			param: { pageNo: 1, pageSize: 10 },
			loadMore: true
		};
	},
	onLoad(e) {
		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.isAsc = this.isAsc == 'desc' ? 'asc' : 'desc';
				item.isAsc = this.isAsc;
			}
			this.param.orderBy = item.sortName + ' ' + this.isAsc;
			this.refresh();
		},
		getData() {
			this.http.request({
				url: '/level-two-server/app/TbOrders/getLevelTwoList',
				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 });
		},
		detail(item) {
			uni.navigateTo({ url: '/pages/market/two/detail?orderId=' + item.id });
		},
		addCar(item) {
			this.add = true;
			this.http.request({
				url: '/level-two-server/app/TbOrdersCart/add',
				method: 'POST',
				data: { orderId: item.id },
				success: res => {
					this.cars++;
					setTimeout(() => {
						this.add = false;
					}, 500);
					uni.showToast({ title: '添加成功' });
				}
			});
		},
		goCar() {
			uni.navigateTo({ url: '/pages/market/two/purchaser/order/cart' });
		},
		buy(item) {
			uni.navigateTo({ url: '/pages/market/two/purchaser/buy/buy?orderId=' + item.id });
		},
		//刷新数据
		refresh() {
			this.loadMore = true;
			this.param.pageNum = 1;
			this.list = [];
			this.getData();
		}
	},
	//下拉刷新
	onPullDownRefresh() {
		setTimeout(() => {
			this.refresh();
			uni.stopPullDownRefresh();
		}, 1000);
	},
	//上拉加载
	onReachBottom() {
		if (this.loadMore) {
			this.param.pageNum++;
			this.getData();
		}
	}
};
</script>

<style lang="scss">
page {
	background-color: $pg;
}
.con {
	.btn {
		float: right;
		background-color: $main-color;
		color: #000;
		height: 20px;
		position: relative;
		color: white;
		padding: 5px 20px;
		margin-left: 10px;
	}
	.icon {
		line-height: 30px;
		line-height: 20px;
	}
}
</style>
