<template>
	<view>
		<view style="position: relative;z-index: 999;height:150px;">
			<view style="position: fixed;width: 100%;background-color: white;">
				<u-tabs :list="tab" @click="clickSearchType" :lineHeight="5"></u-tabs>
				<view class="search">
					<u-search placeholder="搜索商品名或车牌号" v-model="param.goodsName" bgColor="#e2e2e2" @search="refresh()"
						:animation="true" actionText="取消" @clear="refresh()"></u-search>
					<view class="clear"></view>
				</view>
				<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>
		</view>
		<view class="goodsList">
			<view class="item" v-for="(item, index) in list" :key="index" @click="detail(item)">
				<view class="title">
					<text v-if="currentOrderType==2">{{ item.leaderName }}</text>
					<text v-if="currentOrderType==1">{{ item.shopName }}</text>
				</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>
				<view class="con">
					<view class="productName omit">
						<text v-if="currentOrderType==2">{{ item.goodsName }}</text>
						<text v-if="currentOrderType==1">{{ item.goodsNames }}</text>
					</view>
					<view class="desc">车牌号 <text style="font-weight: bold;margin-left: 5px;"> {{ item.veNo }}</text>
					</view>
					<view class="desc omit">
						<text v-if="currentOrderType==2">重量 {{ item.totalWeight }}{{ item.goodsUnit }}</text>
						<text v-if="currentOrderType==1">重量 {{ item.sumNetWt }} kg</text>
						<text>{{ item.tradeAreaName }}</text>
					</view>
					<view class="price" v-if="currentOrderType==2"> ¥ {{ item.resalePrice }}</view>
					<view class="price" v-if="currentOrderType==1"> ¥ {{ item.sumUpPrice }}</view>
					<view class="op">
						<view class="date">{{ item.createTime }}</view>
						<template>
							<view class="icon btn" v-if="user.userType == 3&&currentOrderType==2"
								@click.stop="addCar(item)">&#xe600;</view>
							<view class="btn" v-if="user.userType == 3" @click.stop="buy(item)">购买</view>
						</template>
					</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&&currentOrderType==2">
			<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">
			<view class="flex">
				<view class="f">
					<button class="btn" @click="buy({})">立即购买（{{ selects.length }}）</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tab: [{
						name: '整车',
						orderType: 1
					},
					{
						name: '非整车',
						orderType: 2
					},
				],
				currentOrderType: 1,
				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: 'resale_price',
						isAsc: 'desc',
						check: false
					}
				],
				list: [],
				param: {
					pageNo: 1,
					pageSize: 10
				},
				loadMore: true,
				selects: []
			};
		},
		onLoad(e) {

			//选择商品分类
			uni.$on('productType', res => {
				this.param.typeCode = res.no;
				this.param.goodsType = res.name;
				this.refresh();
			});
		},
		onShow() {
			this.init();
			this.query();
		},
		methods: {
			clickSearchType(e) {
				let orderType = e.orderType;
				if (this.currentOrderType == orderType) {
					return;
				}
				this.currentOrderType = orderType;
				this.init();
				this.query();
			},
			init() {
				this.list = [];
				this.param = {
					pageNo: 1,
					pageSize: 10
				}
			},
			query() {
				if (this.currentOrderType == 2) {
					this.getData();
				} else {
					this.getWholeData();
				}
			},
			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;
				}
				item.sortName=item.sortName=='resale_price'&&this.currentOrderType==1?item.sortName='sumUpPrice':item.sortName;
				this.param.orderBy = item.sortName + ' ' + this.isAsc;
				this.refresh();
			},
			getWholeData() {
				this.http.request({
					url: '/level-one-server/app/HtTradeSettlement/getOnSaleList',
					data: this.param,
					loading: 'false',
					success: res => {
						this.loadMore = parseInt(res.data.pageCount) > this.param.pageNo;
						this.list.push(...res.data.data);
					}
				});
			},
			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/purchaser/buy/detail?id=' + item.id + '&type=' + this.currentOrderType
				});
			},
			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) {
				let ids = undefined;
				if (item.id) {
					ids = item.id;
				} else {
					ids = this.selects.map(item => item.id)
				}
				if (this.currentOrderType == 2) {
					uni.navigateTo({
						url: '/pages/market/two/purchaser/buy/buy?orderId=' + ids
					});
					return;
				}
				uni.navigateTo({
					url: '/pages/market/two/purchaser/buy/buyWhole?ids=' + ids
				});
			},
			//刷新数据
			refresh() {
				this.loadMore = true;
				this.param.pageNo = 1;
				this.list = [];
				this.selects = [];
				this.query();
			}
		},
		//下拉刷新
		onPullDownRefresh() {
			setTimeout(() => {
				this.refresh();
				uni.stopPullDownRefresh();
			}, 1000);
		},
		//上拉加载
		onReachBottom() {
			if (this.loadMore) {
				this.param.pageNo++;
				this.query();
			}
		}
	};
</script>

<style lang="scss">
	page {
		background-color: $pg;
	}

	.goodsList .item .con {
		width: 98%;
	}

	.con {
		.btn {
			float: right;
			background-color: $main-color;
			height: 20px;
			position: relative;
			color: white;
			padding: 5px 20px;
			margin-left: 10px;
		}

		.icon {
			line-height: 20px;
		}
	}

	.mfooter {
		background-color: #ffffff00;
		border: 0px;

		.btn {
			background-color: #F44336;
		}
	}
</style>