<template>
	<view class="app-container">
		<view class="top-container">
			<view class="tab">
				<view class="item" :class="screenType === 0 ? 'current':''" @tap="changeScreen(0)">综合</view>
				<view class="item" :class="screenType === 1 ? 'current':''" @tap="changeScreen(1)">销量</view>
				<view class="item" :class="screenType === 2 || screenType === 3 ? 'current':''"  @tap="changeScreen(2)">价格
					<image src="@/static/icon/price_1.png" v-if="screenType === 2"></image>
					<image src="@/static/icon/price_2.png" v-if="screenType === 3"></image>
					<image src="@/static/icon/price_0.png" v-if="screenType != 2 && screenType != 3"></image>
				</view>
				<view class="item" :class="screenType === 4 ? 'current':''" @tap="changeScreen(4)">上架时间</view>
			</view>
			<view class="icon">
				<image src="@/static/icon/show_1.png" v-if="showType == 1" @tap="showType = 2"></image>
				<image src="@/static/icon/show_2.png" v-if="showType == 2" @tap="showType = 1"></image>
			</view>
		</view>
		<view class="goods-waterfall-list" v-show="showType == 1">
			<view class="left">
				<block v-for="(item, index) in goodsList" :key='index'>
					<view class="item" v-if="index%2==0" @tap="toGoodsDetail(item.goodsId)">
						<view class="image">
							<image :src="item.imgUrl" mode="aspectFill" class="img"></image>
							<image :src="item.logo" mode="aspectFill" class="water" v-if="item.isShowWater"></image>
						</view>
						<view class="content">
							<view class="title ellipsis-2">{{item.goodsName}}</view>
							<view class="tags" v-if="item.tags1 && item.tags1.length > 0">
								<view class="it" v-for="(it, idx) in item.tags1" :key="idx">{{it}}</view>
							</view>
							<view class="price">
								<view class="price-1">¥{{item.goodsPrice | numToFixed}}</view>
								<view class="price-2">¥{{item.orgGoodsPrice | numToFixed}}</view>
							</view>
							<view class="tags2" v-if="item.tags2 && item.tags2.length > 0">
								<view class="it" v-for="(it, idx) in item.tags2" :key="idx">{{it}}</view>
							</view>
							<view class="text">销量：{{item.soldNum}}</view>
						</view>
					</view>
				</block>
			</view>
			<view class="right">
				<block v-for="(item, index) in goodsList" :key='index'>
					<view class="item" v-if="index%2==1" @tap="toGoodsDetail(item.goodsId)">
						<view class="image">
							<image :src="item.imgUrl" mode="aspectFill" class="img"></image>
							<image :src="item.logo" mode="aspectFill" class="water" v-if="item.isShowWater"></image>
						</view>
						<view class="content">
							<view class="title ellipsis-2">{{item.goodsName}}</view>
							<view class="tags" v-if="item.tags1 && item.tags1.length > 0">
								<view class="it" v-for="(it, idx) in item.tags1" :key="idx">{{it}}</view>
							</view>
							<view class="price">
								<view class="price-1">¥{{item.goodsPrice | numToFixed}}</view>
								<view class="price-2">¥{{item.orgGoodsPrice | numToFixed}}</view>
							</view>
							<view class="tags2" v-if="item.tags2 && item.tags2.length > 0">
								<view class="it" v-for="(it, idx) in item.tags2" :key="idx">{{it}}</view>
							</view>
							<view class="text">销量：{{item.soldNum}}</view>
						</view>
					</view>
				</block>
			</view>
		</view>
		
		<view class="goods-row-list" v-show="showType == 2">
			<block v-for="(item, index) in goodsList" :key='index'>
				<view class="item" @tap="toGoodsDetail(item.goodsId)">
					<view class="image">
						<image :src="item.imgUrl" mode="aspectFill" class="img"></image>
						<image :src="item.logo" mode="aspectFill" class="water" v-if="item.isShowWater"></image>
					</view>
					<view class="right">
						<view>
							<view class="title ellipsis-2">{{item.goodsName}}</view>
							<view class="tags" v-if="item.tags1 && item.tags1.length > 0">
								<view class="it" v-for="(it, idx) in item.tags1" :key="idx">{{it}}</view>
							</view>
						</view>
						<view>
							<view class="bottom">
								<view class="price">
									<view class="price-1">¥{{item.goodsPrice | numToFixed}}</view>
									<view class="price-2">¥{{item.orgGoodsPrice | numToFixed}}</view>
								</view>
								<view class="text">销量：{{item.soldNum}}</view>
							</view>
						</view>
					</view>
				</view>
			</block>
		</view>
		<no-data v-if="!goodsList.length" :showText="'暂无商品'"></no-data>
		<loading-text v-if="goodsList.length"  :loading="loading" :noMore="noMore" ></loading-text>
		
		<!-- <drag-button :isDock="true" :customBar="true" ref="dragButton"></drag-button> -->
	</view>
</template>

<script>
	// import dragButton from '@/components/drag-button.vue';
	
	export default {
		// components:{
		// 	dragButton
		// },
		data() {
			return {
				screenType: '',
				goodsList: [],
				pageNum: 1,
				pageSize: 8,
				noMore: false,
				loading: false,
				showType: 1
			}
		},
		
		onShow() {
			// this.$refs.dragButton.init();
		},
		
		onLoad() {
			this.getGoodsList();
		},
		
		// 下拉刷新
		onPullDownRefresh() {
			this.pageNum = 1;
			this.getGoodsList();
		},
		
		// 上拉加载
		onReachBottom() {
			this.getGoodsList(1);
		},
		
		methods: {
			// 切换筛选类型
			changeScreen(type) {
				if(type != 2) {
					if(this.screenType !== type) {
						this.screenType = type;
					}else {
						this.screenType = '';
					}
				}else {
					if(this.screenType != 2 && this.screenType != 3) {
						this.screenType = 2;
					}else if(this.screenType == 2) {
						this.screenType = 3;
					}else {
						this.screenType = '';
					}
				}
				this.pageNum = 1;
				this.getGoodsList();
			},
			
			// 获取商品列表
			getGoodsList(loadMore) {
				if(this.noMore && loadMore)return;
				this.noMore = false
				if(!loadMore){
					this.pageNum = 1;
				}else{
					this.loading = true;
				}
				this.$axios({
					url: '/goods/list/sort/page',
					method: 'get',
					params: {
						pageNum: this.pageNum,
						pageSize: this.pageSize,
						categoryId: '',
						sort: this.screenType
					},
					isLoading: !loadMore
				}).then(res => {
					res.data.records.forEach(item => {
						if(item.logo && item.logoStartTime) {
							item.isShowWater = this.$compareTime(item.logoStartTime, item.logoEndTime);
						}else {
							item.isShowWater = false;
						}
					})
					let _list = res.data.records;
					let pageTotal = res.data.pages;
					if(this.pageNum >= pageTotal){
						this.noMore = true;
					}
					if (_list.length) {
						this.pageNum += 1
					}
					if (loadMore) {
						this.goodsList = this.goodsList.concat(_list);
						this.loading = false;
					} else {
						this.goodsList = _list;
					}
					
					uni.stopPullDownRefresh();
				})
			},
			
			toGoodsDetail(id) {
				uni.navigateTo({
					url: '/packageGoods/pages/detail?id=' + id
				})
			}
		}
	}
</script>

<style lang="scss">
	.app-container {
		background: #F4F2F2;
		padding-top: 88rpx;
		box-sizing: border-box;
	}
	.top-container {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		background: #FFFFFF;
		display: flex;
		padding: 0 20rpx;
		align-items: center;
		box-sizing: border-box;
		.tab {
			flex: 1;
			display: flex;
			padding: 0 80rpx 0 30rpx;
			box-sizing: border-box;
			justify-content: space-between;
			.item {
				height: 88rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 30rpx;
				color: #666666;
				&.current {
					color: #FF3F42;
				}
				image {
					width: 18rpx;
					height: 30rpx;
					display: block;
					margin-left: 10rpx;
				}
			}
		}
		.icon {
			padding-right: 10rpx;
			image {
				width: 36rpx;
				height: 36rpx;
				display: block;
			}
		}
	}
	
</style>
