<template>
	<view class="pages">
		<view class="search-wrap">
			<view class="search">
				<view class="searchTxt">
					<uni-search-bar placeholder="搜索你想发布的品类" @confirm="searchtype" @input="searchtype" ></uni-search-bar>
				</view>
			</view>	
		</view>
		<!-- 搜索 结束 -->
		<view class="typelist-wrap wrap">
			<view v-for="(item,index) in typelist" :key="index" class="typelist-block">
				<view class="select-bar" @click="typeclick(index)" :class="{open:index==ins}">
					<view class="select-bar-img-wrap">
						<image class="select-bar-img" :src="item.icon" mode=""></image>
					</view>
					<view class="select-bar-text">{{item.name}}</view>
					<view class="select-bar-arrow"></view>
				</view>
				<view class="typelist-item-wrap">
					<view v-for="(product,productindex) in item.children" :key="productindex" @click="publishitem(product)" class="typelist-item">
						<view class="productindex">{{productindex + 1}}</view>
						<view class="product">{{product.name}}</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { bus } from '../../../utils/bus.js';
	export default {
		data() {
			return {
				params:{
					token:'',
				},
				typelist:[],
				ins:null,
				
			}
		},
		onShow() {
			let serf = this;
			//检查登录,获取token
			// let loginRes = this.checkLogin('/pages/index/index', '2');
			// if(!loginRes){return false;}
			// serf.params.token=loginRes[0];			
		},
		onLoad() {
			
			//获取产品分类
			// ,{header: {Authorization:this.params.tokenhead+this.params.token}}
			this.$api.http.get(this.config.apiBaseurl+'/product/categoryTreeList').then(res => {
				console.log('产品分类',res);
				this.typelist = res.data.data;
			}).catch(err => {
				console.log(err)
			});

		},
		methods: {
			// 搜索
			searchtype(e){
				console.log(e)
				this.$api.http.get(this.config.apiBaseurl+'/product/categorySearch',{params:{key:e.value}}).then(res => {
					console.log('搜索结果',res);
					this.typelist = res.data.data;
				}).catch(err => {
					console.log(err)
				});
			},
			//分类点击
			typeclick(num){
				if(this.ins==num){
					this.ins = null;
					return
				}
				this.ins=num;
			},
			//选择发布
			publishitem(product){
				// console.log(product);
				bus.$emit('toDetailPage', {
					data: product
				});
				uni.navigateTo({
					url:`/pages/publish/publish`
				})
			},

		}
	}
</script>

<style scoped>
	/* @import url("./chosetype.css"); */
	.typelist-wrap{margin-top: 24rpx;}
	.typelist-item-wrap{height: 0;overflow: hidden;}
	.select-bar.open + .typelist-item-wrap{height: auto;}
	.typelist-item{display: flex;align-items: center;padding-left: 50rpx;height: 96rpx;border-bottom: 1px solid #eee;}
	.typelist-block .typelist-item:last-child{border-bottom: 0;}
	.typelist-item .productindex{width: 36rpx;height: 36rpx;line-height: 36rpx;margin-right: 25rpx;text-align: center;;background-color: #6BBC6D;color: #fff;}
	.select-bar-img-wrap{background-color: #6bbc6d;width: 0;}
	.search-wrap >>> .uni-searchbar__box{justify-content: left;border: 0;background-color: #F1F1F2!important;}
</style>
