<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.LeaderName }}</view>
				<view class="check" @click.stop="selected(item)" v-if="user.userType == 3">
					<text class="check icon" v-if="item.check" style="color: #4581fb">&#xe631;</text>
					<text class="check icon" v-else>&#xe60c;</text>
				</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="desc">车牌号：{{ item.veNo }}</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()" v-if="user.userType == 3">
			<text class="icon">&#xe600;</text>
			<view class="bag animated" :class="{ bounce: add }" v-if="cars > 0">{{ cars }}</view>
		</button>
		<view class="mfooter" v-if="selects.length > 0&&user.userType == 3">
			<view class="flex">
				<view class="f">
					<button class="btn" @click="buy({})">立即购买（{{ selects.length }}）</button>
				</view>
			</view>
		</view>
	</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,
			selects: []
		};
	},
	/* onShow(){
		this.refresh();
	}, */
	onLoad(e) {
		this.getData();
		//选择商品分类
		uni.$on('productType', res => {
			this.param.typeCode = res.no;
			this.param.goodsType = res.name;
			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);
				}
			});
		},
		//多选购买
		selected(item) {
			item.check = !item.check;
			this.$forceUpdate();
			this.selects = this.list.filter(item => item.check);
		},
		//选择分类
		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) {
			if (item.id) {
				uni.navigateTo({
					url: '/pages/market/two/purchaser/buy/buy?orderId=' + item.id
				});
			} else {
				uni.navigateTo({
					url: '/pages/market/two/purchaser/buy/buy?orderId=' + this.selects.map(item => item.id)
				});
			}
		},
		//刷新数据
		refresh() {
			this.loadMore = true;
			this.param.pageNum = 1;
			this.list = [];
			this.selects=[];
			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;
	}
}
.mfooter {
	background-color: #ffffff00;
	border: 0px;
	.btn{
		background-color: #F44336;
	}
}
</style>
