<template>
	<view class="container" :style="appThemeStyle">
		<mescroll-body ref="mescrollRef" :sticky="true" @init="mescrollInit" :down="{ native: true }"
			@down="downCallback" :up="upOption" @up="upCallback">
			<!-- 页面头部 -->
			<view class="header">
				<view class="search">
					<search :tips="options.search ? options.search : '搜索商品'" @event="handleSearch" />
				</view>
			</view>
			<view class="store-sort" v-if="tabbar.length>0">
				<view class="goodsType">
					<scroll-view class="typeList" scroll-x="true">
						<view v-for="(item,index) in tabbar" :key="index" class="typeItem" @click="onChage(item,1)"
							:style="{'color':item.category_id==isBanrdActive?'#F43B21':'#5A5A5A','background':item.category_id==isBanrdActive?'#FEF6F6':'#F7F8FA','border':item.category_id==isBanrdActive?'2rpx solid #F22029':'none'}">
							{{item.name}}
						</view>
					</scroll-view>
				</view>
				<view class="goodsType" v-if="goodsType && goodsType.length > 0">
					<scroll-view class="typeList" scroll-x="true">
						<view v-for="(item,index) in goodsType" :key="index" class="typeItem" @click="onChage(item,2)"
							:style="{'color':isActive==item.category_id?'#F43B21':'#5A5A5A','background':isActive==item.category_id?'#FEF6F6':'#F7F8FA','border':isActive==item.category_id?'2rpx solid #F22029':'none'}">
							{{item.name}}
						</view>
					</scroll-view>
				</view>
				<!-- 排序标签 -->
				<view class="store-box">
					<view class="sort-item" :class="{ active: sortType === 'all' }" @click="handleSortType('all')">
						<text>综合</text>
					</view>
					<view class="sort-item" :class="{ active: sortType === 'sales' }" @click="handleSortType('sales')">
						<text>销量</text>
					</view>
					<view class="sort-item sort-item-price" :class="{ active: sortType === 'price' }"
						@click="handleSortType('price')">
						<text>价格</text>
						<view class="price-arrow">
							<view class="icon up" :class="{ active: sortType === 'price' && !sortPrice }">
								<text class="iconfont icon-arrow-up"></text>
							</view>
							<view class="icon down" :class="{ active: sortType === 'price' && sortPrice }">
								<text class="iconfont icon-arrow-down"></text>
							</view>
						</view>
					</view>
					<view class="sort-item" :class="{ active: sortType === 'city' }" @click="handleSortType('city')">
						<text>同城达</text>
					</view>
					<!-- <view class="show-view btnOpera" @click="handleShowView">
						<text class="iconfont icon-view-tile" v-if="showView"></text>
						<text class="iconfont icon-view-list" v-else></text>
					</view> -->
				</view>
			</view>

			<!-- 商品列表 -->
			<view class="goods-list clearfix" :class="['column-' + (showView ? '1' : '2')]">
				<view class="goods-item" v-for="(item, index) in list.data" :key="index"
					@click="onTargetDetail(item.goods_id)">
					<!-- 单列显示 -->
					<view v-if="showView" class="dis-flex">
						<!-- 商品图片 -->
						<view class="goods-item-left">
							<image class="image" :src="item.goods_image"></image>
						</view>
						<view class="goods-item-right">
							<!-- 商品名称 -->
							<view class="goods-name">
								<text class="twoline-hide">{{ item.goods_name }}</text>
							</view>
							<view class="goods-item-desc">
								<!-- 商品卖点 -->
								<view class="desc-selling-point dis-flex">
									<text class="oneline-hide">{{ item.selling_point }}</text>
								</view>
								<!-- 商品销量 -->
								<view class="desc-goods-sales dis-flex">
									<text>已售{{ item.goods_sales }}件</text>
								</view>
								<!-- 商品价格 -->
								<view class="desc-footer">
									<text
										class="price-x">¥{{ item.goods_price_min>0?Number(item.goods_price_min):'' }}</text>
									<text class="price-y col-9"
										v-if="item.line_price_min > 0">¥{{ item.line_price_min>0?Number(item.line_price_min):'' }}</text>
								</view>
							</view>
						</view>
					</view>
					<!-- 多列显示 -->
					<view v-else class="">
						<!-- 商品图片 -->
						<view class="goods-image">
							<image class="image" mode="aspectFill" :src="item.goods_image"></image>
						</view>
						<view class="detail">
							<!-- 商品名称 -->
							<view class="goods-name">
								<text class="twoline-hide">{{ item.goods_name }}</text>
							</view>
							<view class="shipping">
								包邮
							</view>
							<view class="goodsInfo">
								<view class="oneTip tip">
									85英寸
								</view>
								<view class="twoTip tip">
									量子电视
								</view>
								<view class="threeTip tip">
									4K电视
								</view>
							</view>
							<!-- 商品价格 -->
							<view class="detail-price oneline-hide">
								<text
									class="goods-price f-30 col-m">￥{{ item.goods_price_min>0?Number(item.goods_price_min):'' }}
									<text class="delPrice">到手价</text></text>
								<text v-if="item.line_price_min > 0"
									class="line-price col-9 f-24">￥{{ item.line_price_min>0?Number(item.line_price_min):''  }}</text>
							</view>
							<!-- <view class="evaluate">
								200+条评论 99%好评率
							</view> -->
						</view>
					</view>
				</view>
				<u-empty text="暂无数据显示哦～" v-if="list.data.length==0" mode="list"></u-empty>
			</view>
		</mescroll-body>
	</view>
</template>

<script>
	import WxofficialMixin from '@/core/mixins/wxofficial'
	import MescrollMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins'
	import * as GoodsApi from '@/api/goods'
	import {
		getEmptyPaginateObj,
		getMoreListData
	} from '@/core/app'
	import Search from '@/components/search'

	const pageSize = 15
	const showViewKey = 'GoodsList-ShowView';
	import * as cstegory from '@/api/category'
	export default {
		components: {
			Search
		},
		mixins: [MescrollMixin, WxofficialMixin],
		data() {
			return {
				searchText: '',
				banrdType: [],
				keyName1: '',
				isBanrdActive: '',
				isActive: '',
				goodsType: [],
				keyName2: '',
				showView: false, // 列表显示方式 (true列表、false平铺)
				sortType: 'all', // 排序类型
				sortPrice: false, // 价格排序 (true高到低 false低到高)
				options: {}, // 当前页面参数
				list: getEmptyPaginateObj(), // 商品列表数据

				// 上拉加载配置
				upOption: {
					// 首次自动执行
					auto: true,
					// 每页数据的数量; 默认10
					page: {
						size: pageSize
					},
					// 数量要大于4条才显示无更多数据
					noMoreSize: 4,
				},
				tabbar: []
			}
		},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad(options) {
			console.log(options)
			// 记录options
			this.options = options
			this.searchText = options.search
			// 设置默认列表显示方式
			this.setShowView()
			// 设置微信公众号链接分享卡片内容
			this.setWxofficialShareData()
			this.getGoodsTypeList(options.categoryId)
		},

		methods: {
			getGoodsTypeList(category_id) {
				const that = this;
				that.goodsType = []
				return new Promise((resolve, reject) => {
					cstegory.childrenList({
							category_id: category_id
						})
						.then(res => {
							if (res.data.list.length > 0) {
								that.tabbar = res.data.list;
								res.data.list.forEach(item => {
									item.children.forEach(item_1 => {
										that.goodsType.push(item_1)
									})
								});
							}
						})
						.catch(reject)
				})
			},
			onChage(item, type) {
				if (type == 1) {
					if (item.category_id == this.isBanrdActive) {
						this.isBanrdActive = '';
						this.keyName1 = ''
					} else {
						this.isBanrdActive = item.category_id;
						this.keyName1 = item.name
					}
				}
				if (type == 2) {
					if (item.category_id == this.isActive) {
						this.isActive = '';
						this.keyName2 = ''
					} else {
						this.isActive = item.category_id;
						this.keyName2 = item.name
					}
				}

				this.getGoodsList()
			},
			/**
			 * 上拉加载的回调 (页面初始化时也会执行一次)
			 * 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10
			 * @param {Object} page
			 */
			upCallback(page) {
				const app = this
				// 设置列表数据
				app.getGoodsList(page.num)
					.then(list => {
						const curPageLen = list.data.length
						const totalSize = list.data.total
						app.mescroll.endBySize(curPageLen, totalSize)
					})
					.catch(() => app.mescroll.endErr())
			},

			// 设置默认列表显示方式
			setShowView() {
				this.showView = uni.getStorageSync(showViewKey) || false
			},

			/**
			 * 获取商品列表
			 * @param {number} pageNo 页码
			 */
			getGoodsList(pageNo = 1) {
				const app = this
				let keywords = []
				if (this.keyName1) {
					keywords.push(this.keyName1)
				}
				if (this.keyName2) {
					keywords.push(this.keyName2)
				}
				const param = {
					sortType: app.sortType,
					sortPrice: Number(app.sortPrice),
					categoryId: app.options.categoryId || 0,
					goodsName: app.options.search || '',
					page: pageNo,
					keywords: keywords.length > 0 ? keywords.join(',') : ''
				}
				console.log(param)
				return new Promise((resolve, reject) => {
					GoodsApi.list(param)
						.then(result => {
							// 合并新数据
							const newList = result.data.list
							app.list.data = getMoreListData(newList, app.list, pageNo)
							resolve(newList)
						})
						.catch(reject)
				})
			},

			// 切换排序方式
			handleSortType(newSortType) {
				const app = this
				const newSortPrice = newSortType === 'price' ? !app.sortPrice : true
				app.sortType = newSortType
				app.sortPrice = newSortPrice
				// 刷新列表数据
				app.list = getEmptyPaginateObj()
				app.mescroll.resetUpScroll()
			},

			// 切换列表显示方式
			handleShowView() {
				const app = this
				app.showView = !app.showView
				uni.setStorageSync(showViewKey, app.showView)
			},

			// 跳转商品详情页
			onTargetDetail(goodsId) {
				this.$navTo('pages/goods/detail', {
					goodsId
				})
			},

			/**
			 * 商品搜索
			 */
			handleSearch() {
				const searchPageUrl = 'pages/search/index?category_id=' + this.options.categoryId
				// 判断来源页面
				let pages = getCurrentPages()
				if (pages.length > 1 &&
					pages[pages.length - 2].route == searchPageUrl) {
					uni.navigateBack()
					return
				}
				// 跳转到商品搜索页
				this.$navTo(searchPageUrl)
			},

			// 设置微信公众号链接分享卡片内容
			setWxofficialShareData() {
				this.updateShareCardData({
					title: '商品列表'
				})
			},

		},

		/**
		 * 设置分享内容
		 */
		onShareAppMessage() {
			// 构建分享参数
			const app = this
			return {
				title: "商品列表",
				path: "/pages/goods/list?" + this.$getShareUrlParams(app.options)
			}
		},

		/**
		 * 分享到朋友圈
		 * 本接口为 Beta 版本，暂只在 Android 平台支持，详见分享到朋友圈 (Beta)
		 * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share-timeline.html
		 */
		onShareTimeline() {
			// 构建分享参数
			const app = this
			return {
				title: "商品列表",
				path: "/pages/goods/list?" + this.$getShareUrlParams(app.options)
			}
		}

	}
</script>

<style lang="scss" scoped>
	::v-deep .u-empty {
		padding: 100rpx 0;
	}

	// 页面头部
	.header {
		background-color: #fff;

		// 搜索框
		.search {
			width: 100%;
			padding: 0 30rpx;

			.search-wrapper {
				padding: 0;
				width: 100%;
			}

		}

		// 切换显示方式
		.show-view {
			width: 60rpx;
			height: 60rpx;
			font-size: 36rpx;
			color: #505050;
			display: flex;
			align-items: center;
		}
	}

	// 排序组件
	.store-sort {
		position: sticky;
		top: var(--window-top);

		color: #000;
		z-index: 99;

		.store-box {
			display: flex;
			padding: 20rpx 0;
			font-size: 28rpx;
			background: #fff;

			.sort-item {
				flex-basis: 25%;
				display: flex;
				justify-content: center;
				align-items: center;
				height: 50rpx;

				&.active {
					color: $main-bg;
				}
			}

			.sort-item-price .price-arrow {
				margin-left: 20rpx;
				font-size: 24rpx;
				color: #000;

				.icon {
					&.active {
						color: $main-bg;
					}

					&.up {
						margin-bottom: -16rpx;
					}

					&.down {
						margin-top: -16rpx;
					}
				}

			}
		}
	}

	// 商品列表
	.goods-list {
		padding: 4rpx;
		box-sizing: border-box;
	}

	// 单列显示
	.goods-list.column-1 {
		.goods-item {
			width: 100%;
			height: 280rpx;
			margin-bottom: 12rpx;
			padding: 20rpx;
			box-sizing: border-box;
			background: #fff;
			line-height: 1.6;

			&:last-child {
				margin-bottom: 0;
			}
		}

		.goods-item-left {
			display: flex;
			width: 300rpx;
			background: #fff;
			align-items: center;

			.image {
				display: block;
				width: 240rpx;
				height: 240rpx;
			}
		}

		.goods-item-right {
			position: relative;
			flex: 1;

			.goods-name {
				margin-top: 10rpx;
				min-height: 68rpx;
				line-height: 1.3;
				white-space: normal;
				color: #484848;
				font-size: 26rpx;
			}
		}

		.goods-item-desc {
			margin-top: 8rpx;
		}

		.desc-selling-point {
			width: 400rpx;
			font-size: 24rpx;
			color: #e49a3d;
		}

		.desc-goods-sales {
			color: #999;
			font-size: 24rpx;
		}

		.desc-footer {
			font-size: 24rpx;

			.price-x {
				margin-right: 16rpx;
				color: $main-bg;
				font-size: 30rpx;
			}

			.price-y {
				text-decoration: line-through;
			}
		}
	}

	// 平铺显示
	.goods-list.column-2 {
		.goods-item {
			width: 50%;
		}
	}

	.goods-item {
		float: left;
		box-sizing: border-box;
		padding: 6rpx;

		.goods-image {
			position: relative;
			width: 100%;
			height: 0;
			padding-bottom: 100%;
			overflow: hidden;
			background: #fff;

			&:after {
				content: '';
				display: block;
				margin-top: 100%;
			}

			.image {
				position: absolute;
				width: 100%;
				height: 100%;
				top: 0;
				left: 0;
				-o-object-fit: cover;
				object-fit: cover;
			}
		}

		.detail {
			padding: 8rpx;
			background: #fff;

			.goods-name {
				min-height: 68rpx;
				line-height: 1.3;
				white-space: normal;
				color: #484848;
				font-size: 26rpx;
				margin-bottom: 4rpx;
			}

			.detail-price {
				.goods-price {
					margin-right: 8rpx;
				}

				.line-price {
					text-decoration: line-through;
				}
			}
		}
	}

	.btnOpera {
		width: 33rpx;
		height: 33rpx;
		position: relative;
		top: 6rpx;
	}

	.shipping {
		width: 44rpx;
		background: #FFFFFF;
		border-radius: 4rpx;
		opacity: 1;
		border: 1rpx solid #F21A1C;
		font-size: 16rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		color: #F21A1C;
		line-height: 24rpx;
		text-align: center;
		margin: 6rpx 0 12rpx 0;
	}

	.goodsInfo {
		display: flex;
		align-items: center;
		margin-bottom: 10rpx;

		.tip {
			height: 28rpx;
			font-size: 20rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			color: #494949;
			line-height: 28rpx;
			padding: 0 10rpx;
		}

		.oneTip {
			border-right: 1rpx solid #C9C9C9;
		}

		.twoTip {
			border-right: 1rpx solid #C9C9C9;
		}
	}

	.delPrice {
		font-size: 20rpx;
		font-weight: 500;
		color: #F21A1C;
	}

	.evaluate {
		height: 28rpx;
		font-size: 20rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		color: #949494;
		line-height: 28rpx;
		margin-bottom: 20rpx;
	}

	.goodsType {
		background-color: #fff;
		padding: 0 30rpx;
		height: 90rpx;
		display: flex;
		align-items: center;

		.text {
			padding-right: 30rpx;
		}

		.typeList {
			white-space: nowrap;

			.typeItem {
				margin-right: 20rpx;
				display: inline-block;
				width: 124rpx;
				height: 54rpx;
				border-radius: 82rpx 82rpx 82rpx 82rpx;
				opacity: 1;
				// border: 2rpx solid #F22029;
				font-size: 24rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				line-height: 54rpx;
				text-align: center;
			}
		}
	}
</style>
