<template>
	<view class="container">
		<view class="search-box">
			<uni-search-bar radius="5" placeholder="搜索" clearButton="auto" cancelButton="none" bgColor="#F8F8F8" @confirm="search"
			 class="search-bar" />
		</view>
		<scroll-view show-scrollbar="true" scroll-y="true" class="sort-left-box">
			<view class="sort-left-row" :class="sortFirstCode == item.cateCode ? 'sort-left-row-active' : ''" v-for="(item,index) in sortFirstList"
			 @click="checkFirstSort(item)" :key="index">
				<text>{{item.cateValue}}</text>
			</view>
		</scroll-view>
		<view class="sort-right-box">
			<scroll-view show-scrollbar="true" scroll-x="true" class="sort-right-row">
				<!-- <view style="padding-top: 20px;background-color: #909399;" v-if="sortSecondList.length<=0">
					<u-divider color="#909399" border-color="#909399">没有更多了</u-divider>
				</view> -->
				<view class="sort-right-col" :class="sortSecondCode == item.cateCode ? 'sort-right-col-active' : ''" v-for="(item,index) in sortSecondList"
				 :key="index" @click="checkSecondSort(item)">{{item.cateValue}}</view>
			</scroll-view>
			<scroll-view class="goods-box" scroll-y="true" @scrolltolower="handleLoadMore()">
				<view style="padding-top: 20px;background-color: #FFFFFF;" v-if="goodsList.length<=0">
					<u-divider color="#909399" border-color="#909399">没有更多了</u-divider>
				</view>
				<view class="goods-row" v-for="(item, index) in goodsList" :key="index" @click="goToGoodDetails(item)">
					<image class="goods-img" :src="item.imgPath"></image>
					<view class="goods-info">
						<view class="goods-name">{{item.productName}}</view>
						<view class="goods-details">{{item.productDescribe}}</view>
						<view class="goods-number">
							<text class="goods-icon">￥</text>
							<text class="goods-spec">{{item.bizPrice}}/{{item.unit}}</text>
							<text class="goods-sales">{{item.sellCount}}人付款</text>
						</view>
					</view>
					<view class="goods-cart">
						<view class="cart-button" @click.stop="addCart(item)">
							<view class="iconfont icongouwuche"></view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
		<u-modal v-model="modalShow" content="是否将该商品添加至购物车" @confirm="submitAddCart()" :async-close="true"
		 :show-cancel-button="true"></u-modal>
		<u-top-tips ref="uTips"></u-top-tips>
	</view>
</template>

<script>
	const NET = require('../../utils/request')
	const API = require('../../config/api')
	export default {
		data() {
			return {
				pageIndex: 1,
				isOver: false,
				goodName: '',
				sortFirstCode: '',
				sortFirstList: [],
				sortSecondCode: '',
				sortSecondList: [],
				goodsList: [],
				modalShow: false,
				goodItem: null,
			}
		},
		onShow() {
			this.getBaseData()
		},
		onPullDownRefresh() {
			this.getBaseData()
			uni.stopPullDownRefresh();
		},
		methods: {
			//  获取全部数据
			getBaseData() {
				this.pageIndex = 1
				this.sortFirstList = []
				this.sortSecondList = []
				this.goodsList = []
				NET.request(API.getSortList, {}, 'POST').then(res => {
					this.sortFirstList = res.data
					if (res.data.length) {
						this.checkFirstSort(res.data[0])
					} else {
						this.$refs.uTips.show({
							title: '获取商品分类失败',
							type: 'warning',
						})
					}
				}).catch(error => {
					this.$refs.uTips.show({
						title: '获取商品分类失败',
						type: 'warning',
					})
				})
			},
			//  切换一级分类
			checkFirstSort(item) {
				this.sortFirstCode = item.cateCode
				this.sortSecondList = []
				if (item.oneCategory.length) {
					this.sortSecondList = item.oneCategory
					this.checkSecondSort(item.oneCategory[0])
				}
			},
			//  切换二级分类
			checkSecondSort(item) {
				this.sortSecondCode = item.cateCode
				this.goodsList = []
				if (this.triggered = 'restore') {
					this.pageIndex = 1
					this.getGoodsList()
				}
			},
			//  搜索商品
			search(data) {
				this.goodName = data.value
				this.goodsList = []
				this.pageIndex = 1
				this.getGoodsList()
			},
			//  懒加载
			handleLoadMore() {
				if (!this.isOver) {
					this.pageIndex++
					this.getGoodsList()
				}
			},
			//  获取商品
			getGoodsList() {
				NET.request(API.getGoodsBySort, {
					name: this.goodName,
					oneId: this.sortFirstCode,
					twoId: this.sortSecondCode,
					pageIndex: this.pageIndex,
					pageSize: 10,
				}, 'POST').then(res => {
					this.isOver = res.data.list.length != 10
					this.goodsList = this.goodsList.concat(res.data.list)
				}).catch(res => {
					this.$refs.uTips.show({
						title: '获取商品列表失败',
						type: 'warning',
					})
				})
			},
			//  添加购物车
			addCart(item) {
				this.goodItem = {
					productId: item.id,
					productName: item.productName,
					imgUrl: item.imgPath,
					bizPrice: item.bizPrice,
					originalPrice: item.originalPrice,
					productType: item.productType,
					tenantCode: item.tenantCode,
					buyNum: 1
				}
				this.modalShow = true
			},
			//  提交购物车
			submitAddCart() {
				NET.request(API.addCart, this.goodItem, 'POST').then(res => {
					this.$refs.uTips.show({
						title: '加入购物车成功',
						type: 'success',
					})
					this.modalShow = false
				}).catch(res => {
					this.modalShow = false
					this.$refs.uTips.show({
						title: '加入购物车失败',
						type: 'warning',
					})
				})
			},
			//  跳转商品详情
			goToGoodDetails(item) {
				uni.navigateTo({
					url: '/pagesGood/goodDetails?goodId=' + item.id
				});
			}
		}
	}
</script>

<style lang="less" scoped>
	page {
		width: 100%;
		height: 100%;
	}

	.container {
		width: 100%;
		height: calc(100% - 70px);
		float: left;

		.search-box {
			width: 100%;
			height: 58px;
			box-sizing: border-box;
			padding: 2px 12px 1px 12px;
			border-bottom: 1px solid #DDDDDD;

			/deep/.search-bar {
				.uni-searchbar__box {
					border-color: #F8F8F8;
				}
			}
		}

		.sort-left-box {
			width: 80px;
			height: calc(100% - 58px);
			float: left;
			overflow-y: auto;

			.sort-left-row {
				width: 100%;
				height: 45px;
				box-sizing: border-box;
				padding-left: 9px;
				border-left: 3px solid #FFFFFF;
				font-size: 15px;
				font-family: PingFang SC;
				color: #333333;
				line-height: 16px;
				align-items: center;
				display: flex;
			}

			.sort-left-row-active {
				color: #51A539;
				border-left: 3px solid #51A539;
			}
		}

		.sort-right-box {
			width: calc(100% - 81px);
			height: calc(100% - 58px);
			float: left;
			border-left: 1px solid #DDDDDD;

			.sort-right-row {
				width: 100%;
				height: 44px;
				padding: 9px 2px 0px 7px;
				box-sizing: border-box;
				white-space: nowrap;
				float: left;

				.sort-right-col {
					height: 26px;
					margin-right: 10px;
					display: inline-block;
					border: 1px solid #DCDCDC;
					border-radius: 13px;
					padding: 0 25px;
					box-sizing: border-box;
					font-size: 12px;
					font-family: PingFang SC;
					color: #666666;
					line-height: 24px;
				}

				.sort-right-col-active {
					color: #FFFFFF;
					background: #52A63A;
					border: 1px solid #51A539;
				}
			}

			.goods-box {
				width: 100%;
				height: calc(100% - 44px) !important;
				padding: 6px 0 0px 0;
				box-sizing: border-box;
				float: left;
				overflow-y: auto;

				.goods-row {
					width: calc(100% - 24px);
					height: 104px;
					float: left;
					padding: 11px 11px 11px 7px;
					box-sizing: border-box;
					margin: 4px 0 12px 12px;
					background: #FFFFFF;
					box-shadow: 0px 1px 5px 0px rgba(102, 102, 102, 0.43);
					border-radius: 15px;
					position: relative;

					.goods-img {
						width: 84px;
						height: 84px;
						float: left;
					}

					.goods-info {
						width: calc(100% - 108px);
						height: 84px;
						box-sizing: border-box;
						padding-left: 6px;
						float: left;

						.goods-name {
							width: calc(100% + 24px);
							height: 18px;
							font-size: 15px;
							font-family: PingFang SC;
							color: #333333;
							line-height: 18px;
							white-space: nowrap;
							text-overflow: ellipsis;
							overflow: hidden;
						}

						.goods-details {
							height: 28px;
							font-size: 12px;
							font-family: PingFang SC;
							color: #999999;
							line-height: 14px;
							overflow: hidden;
							text-overflow: ellipsis;
							display: -webkit-box;
							-webkit-line-clamp: 2;
							-webkit-box-orient: vertical;
							word-wrap: break-word;
							margin: 4px 0 12px 0;
						}

						.goods-number {
							height: 16px;
							font-family: PingFang SC;
							line-height: 16px;

							.goods-icon {
								font-size: 12px;
								color: #52A63A;
							}

							.goods-spec {
								font-size: 15px;
								color: #52A63A;
								margin-right: 15px;
							}

							.goods-sales {
								font-size: 12px;
								color: #999999;
							}
						}
					}

					.goods-cart {
						width: 24px;
						height: 84px;
						float: left;

						.cart-button {
							width: 24px;
							height: 24px;
							margin-top: 30px;
							background-color: #52A63A;
							border-radius: 50%;
							text-align: center;

							.iconfont {
								color: #FFFFFF;
								font-size: 18px;
								line-height: 24px;
							}
						}
					}
				}
			}
		}
	}
</style>
