<template>
	<view class="container-main">
		<view class="type-tree">
			<view class="tree-item" v-for="(item,index) in treeList" :key="index"
				:class="currenIndex == index ? 'activeTree' : '' " @click="clickTree(index)">
				{{item.name}}
			</view>
		</view>
		<view class="data-rows">
			<u-sticky>
				<view class="header-search">
					<u-icon name="search" size="35" color="#9DA2AB"></u-icon>
					<u-input v-model="keyword" placeholder="请输入关键字"
						:custom-style="{marginLeft:'20rpx',fontSize:'30rpx'}" :height="40" :clearable="false" />
					<view class="search-submit" @click="searchData()">搜索</view>
				</view>
			</u-sticky>
			<scroll-view scroll-y class="scrollView" @scrolltolower="onreachBottom">
				<view class="list-item" @click="getInfo()">
					<view class="item-data" v-if="currenIndex == 0">
						<view class="data-title">111</view>
						<view class="data-subtitle">222</view>
					</view>
					<view class="item-data" v-if="currenIndex == 1">
						<view class="data-title">333</view>
						<view class="data-subtitle">444</view>
					</view>
					<view class="item-data" v-if="currenIndex == 2">
						<view class="data-title">555</view>
						<view class="data-subtitle">555</view>
					</view>
					<view class="item-data" v-if="currenIndex == 3">
						<view class="data-title">666</view>
						<view class="data-subtitle">777</view>
					</view>
					<view class="item-data" v-if="currenIndex == 4">
						<view class="data-title">888</view>
						<view class="data-subtitle">888</view>
					</view>
					<view class="item-icon">
						<u-icon name="arrow-right" size="35" color="#DCE3EC"></u-icon>
					</view>
				</view>
				<u-loadmore :status="status" :load-text="loadText" margin-top="20" />
			</scroll-view>
		</view>
	</view>
</template>

<script>

	export default {
		data() {
			return {
				keyword: '',
				status: 'nomore',
				loadText: {
					loading: '努力加载中',
					nomore: '已经到底了~'
				},
				list: [],
				total: 0,
				queryParams: {
					pageNum: 1,
					pageSize: 10,
				},
				currenIndex: null,
				treeList: [{
						name: '国家食品安全示范城市',
						areaType: 0,
						tabName: '示范城市',
						type: 3
					},
					{
						name: '省级食品安全示范县',
						areaType: 1,
						tabName: '示范县区',
						type: 4
					},
					{
						name: '食品安全示范街',
					},
					{
						name: '食品安全示范店',
					},
					{
						name: '食品生产主体责任示范企业',
					}
				],
				cityList: [],
				cityTpye: [],
			};
		},
		onLoad() {
			
			//初始化
			this.clickTree(0)
		},
		methods: {
			//详情
			getInfo() {
				if (this.currenIndex == 0 || this.currenIndex == 1) {
					uni.navigateTo({
						url: `/sub-public/shifan-chuangjian/city-county-info/index`
					})
				} else if (this.currenIndex == 2) {
					uni.navigateTo({
						url: `/sub-public/shifan-chuangjian/street-info/info`
					})
				} else if (this.currenIndex == 3) {
					uni.navigateTo({
						url: `/sub-public/shifan-chuangjian/foodStore/info/index`
					})
				} else {
					uni.navigateTo({
						url: `/sub-public/shifan-chuangjian/enterprise-info/index`,
					})
				}
			},
			//关键字搜索
			searchData() {
				this.reset()
				if (this.currenIndex == 0) {
					this.queryParams.areaType = 0
					if(this.keyword == ''){
						this.queryParams.name =  undefined
					}else{
						let name = this.cityList.filter((item) => item.dictLabel == this.keyword)
						this.queryParams.name =  name.length > 0 ? name[0].dictValue :-1
					}
					this.getCityOrCount()
				} else if (this.currenIndex == 1) {
					this.queryParams.areaType = 1
					if(this.keyword == ''){
						this.queryParams.name =  undefined
					}else{
						let type = this.cityTpye.filter((item) => item.dictLabel == this.keyword)
						this.queryParams.name = type.length > 0 ? type[0].dictValue  :-1
					}
					this.getCityOrCount()
				} else if (this.currenIndex == 2) {
					this.queryParams.blockName = this.keyword
					this.getStreet()
				} else if (this.currenIndex == 3) {
					this.queryParams.canteenName = this.keyword
					this.getShop()
				} else {
					this.queryParams.enterpriseName = this.keyword
					this.getEnterpriseData()
				}
			},
			//翻译
			filterCity(name, title) {
				let list = [];
				if (title == "城市") {
					list = this.cityList;
				} else {
					list = this.cityTpye;
				}
				let cityName = list.filter((item) => item.dictValue == Number(name));
				if (cityName.length > 0) {
					return cityName[0].dictLabel;
				}
			},
			// 触底加载
			onreachBottom() {
				if (this.queryParams.pageNum < this.total) {
					if (this.status == 'loading') return
					this.queryParams.pageNum = this.queryParams.pageNum + 1;
					if (this.currenIndex == 0 || this.currenIndex == 1) {
						this.getCityOrCount();
					} else if (this.currenIndex == 2) {
						this.getStreet()
					} else if (this.currenIndex == 3) {
						this.getShop()
					} else {
						this.getEnterpriseData()
					}
				}
			},
			// 左侧分类
			clickTree(index) {
				if (this.currenIndex == index) return
				let item = this.treeList[index]
				this.keyword = ''
				this.reset()
				if (index == 0 || index == 1) {
					this.queryParams.areaType = item.areaType
					this.getCityOrCount()
				} else if (index == 2) {
					this.getStreet()
				} else if (index == 3) {
					this.getShop()
				} else {
					this.getEnterpriseData()
				}
				this.currenIndex = index
			},
			//示范企业
			getEnterpriseData() {
				this.status = 'loading'
				this.filerList([])
			},
			//示范店
			getShop() {
				this.status = 'loading'
				this.filerList([])
			},
			// 示范街
			getStreet() {
				this.status = 'loading'
				this.filerList([])
			},
			// 获取城市/县区列表
			getCityOrCount() {
				this.status = 'loading'
					this.filerList([])
			},
			filerList(res) {
				this.list = res;
				this.status = 'nomore'
			},
			// 重置
			reset() {
				this.list = []
				this.total = 0
				this.queryParams = {
					pageNum: 1,
					pageSize: 10,
				}
			},
			clickCell(item) {
				uni.navigateTo({
					url: item.path
				})
			}
		},
	}
</script>

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

	.container-main {
		height: 100%;
		display: flex;
		align-items: center;

		&>view {
			height: 100%;
		}

		.type-tree {
			background: #F5F7FA;

			.tree-item {
				width: 180rpx;
				box-sizing: border-box;
				padding: 20rpx;
				font-size: 28rpx;
				font-weight: 400;
				color: #34373B;
			}

			.activeTree {
				background: #fff;
				color: #3976F1;
			}
		}

		.data-rows {
			box-sizing: border-box;
			padding: 20rpx;
			flex: 1;
			background: #fff;

			.header-search {
				box-sizing: border-box;
				padding: 20rpx;
				border-radius: 16rpx;
				border: 1px solid #DCE3EC;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.search-submit {
					font-size: 32rpx;
					font-weight: 500;
					color: #3976F1;
				}
			}

			.scrollView {
				height: 94.5%;

				.list-item {
					display: flex;
					align-items: center;
					justify-content: space-between;
					min-height: 160rpx;
					border-bottom: 1px solid #DCE3EC;
					box-sizing: border-box;
					padding: 20rpx 0;

					.item-data {
						flex: 1;

						.data-title {
							font-size: 34rpx;
							font-weight: normal;
							color: #34373B;
						}

						.data-subtitle {
							width: 500rpx;
							margin-top: 20rpx;
							font-size: 32rpx;
							font-weight: 400;
							color: #B0B4BB;
							overflow: hidden;
							white-space: nowrap;
							text-overflow: ellipsis;
						}
					}

					.item-icon {
						width: 40rpx;
					}
				}
			}
		}

	}
</style>