<template>
	<view class="page">
		<navbar bgColor="#DC2828"/>

		<view class="category">
			
			<view class="tabs">
				<uv-tabs 
				:list="category" 
				:activeStyle="{color : '#f00', fontWeight : 600}" 
				lineColor="#f00"
				:inactiveStyle="{color: 'rgba(0,0,0,.8)'}" 
				lineHeight="8rpx" 
				lineWidth="50rpx" 
				keyName="title"
				:current="current"
				@click="clickTabs"></uv-tabs>
			</view>
			
			
			<uv-vtabs 
			:chain="chain" 
			:list="category[current].children" 
			keyName="title"
			:key="category[current].id"
			:current="currentChildren"
			:barItemBadgeStyle="{right:'20px',top:'12px'}" 
			@change="change">
				<uv-vtabs-item>
					<!-- <view class="category-title">
						租赁
					</view>
					<view class="list">
						<view class="item" v-for="(item,index) in list" :key="index"
						@click="$utils.navigateTo(`/pages_order/product/productDetail?id=${item.id}`)">
							<view class="item-image">
								<image 
								:src="item.image" 
								mode="aspectFill"></image>
							</view>
							<view class="item-unit">
								<text class="text">{{item.name}}</text>
							</view>
						</view>
					</view> -->
					
					<productItem
					:btn="false"
					@click="$utils.navigateTo(`/pages_order/product/productDetail?id=${item.id}`)"
					:item="item" v-for="(item, index) in list" :key="index"/>
					
					
				</uv-vtabs-item>
			</uv-vtabs>
		</view>
		
		
		<quick-order-entry 
		ref="quickOrderEntry"
		/>
		
		<kefu/>

		<tabber select="category" />
	</view>
</template>

<script>
	import tabber from '@/components/base/tabbar.vue'
	import productItem from '@/components/productItem.vue'
	import selectCityPopup from '@/components/selectCityPopup.vue'
	export default {
		components: {
			tabber,
			productItem,
			selectCityPopup,
		},
		data() {
			return {
				current : 0,
				currentChildren : 0,
				category: [],
				list: [],
				chain: false,
				value: 0,
			}
		},
		computed: {
			selectCategory(){
				
				if(!this.category[this.current] || !this.category[this.current].children){
					return {}
				}
				
				return this.category[this.current].children[this.currentChildren] || this.category[this.current]
			},
		},
		onLoad() {
			this.getCategory()
		},
		onShow() {
			if(uni.getStorageSync('token')){
			   this.$refs.quickOrderEntry.refresh();
			}
		},
		methods: {
			clickTabs({index}){
				this.current = index
				this.currentChildren = 0
				this.$nextTick(() => {
					this.getProductList()
				})
			},
			// 获取分类列表
			getCategory() {
				this.$api('getClassify', res => {
					if(res.code == 200) {
						this.category = res.result
						if(this.category.length > 0) {
							this.$nextTick(() => {
								this.getProductList()
							})
						}
					}
				})
			},
			// 获取商品列表
			getProductList(shopIconId) {
				this.$api('getProductList', {
					shopClassId: this.selectCategory.id,
					pageNo: 1,
					pageSize: 99999,
					cityId : this.selectCity.id,
				}, res => {
					if(res.code == 200) {
						this.list = res.result.records
					}
				})
			},
			change(index) {
				this.currentChildren = index;
				if(this.selectCategory.id) {
					this.$nextTick(() => {
						this.getProductList()
					})
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.page{
		/deep/ .uv-vtabs{
			height: calc(100vh - 360rpx) !important;
		}
		/deep/ .uv-vtabs__bar{
			height: calc(100vh - 360rpx) !important;
		}
		/deep/ .uv-vtabs__content{
			height: calc(100vh - 360rpx) !important;
		}
	}
	.category {
		font-size: 30rpx;
		color: #333;
		.category-title{
			position: relative;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 120rpx;
			
			&::before,
			&::after {
				position: absolute;
				top: 50%;
				content: '';
				width: 10%;
				border-top: 2rpx solid black;
			}
			
			&::before {
				left: 25%;
			}
			
			&::after {
				right: 25%;
			}
		}
		.list{
			display: flex;
			flex-wrap: wrap;
			margin: 0 auto;
			width: 490rpx;
			.item {
				padding: 10rpx 20rpx;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				margin-bottom: 20rpx;
				.item-image {
					width: 120rpx;
					height: 120rpx;
					image{
						height: 100%;
						width: 100%;
						border-radius: 50%;
					}
				}
			
				.item-unit {
					font-size: 24rpx;
					margin-top: 15rpx;
					color: #555;
				}
			}
			
			.gap {
				padding: 0 30rpx;
			}
		}
		
	}
</style>