<template>
	<view class="content">
		<z-nav-bar title="太湖英才" bgColor="#5188e5" fontColor="#fff" :backState="2000"></z-nav-bar>
		<view class="talents_module" :style="`top: ${42 + statusBarHeight}px;`">
			<view class="talents_tab">
				<view class="tab_item" @click="toggleFilter('city')">
					地区
					<text :class="['arrow', activeFilter === 'city' ? 'up' : 'down']"></text>
				</view>
				<view class="tab_item" @click="toggleFilter('department')">
					科室
					<text :class="['arrow', activeFilter === 'department' ? 'up' : 'down']"></text>
				</view>
			</view>
			<!-- 弹窗 -->
			<view class="talents_tan" v-if="activeFilter">
				<view class="tan_item tan_city" v-if="activeFilter=='city'">
					<scroll-view scroll-y="true" class="city_scroll scroll_left" :class="!cityStatus?'width50':''" :scroll-into-view="'country_' + selectedPath[0]">
						<view class="city_item"
						:class="countryIndex==index?'active':''" 
						v-for="(item,index) in country" :key="index" :id="'country_' + item.areaId" @click="click_country(item,index)">
							<text>{{item.title}}</text>
						</view>
					</scroll-view>
					<scroll-view scroll-y="true" class="city_scroll scroll_center" :class="!cityStatus?'width50':''" :scroll-into-view="'province_' + selectedPath[1]">
						<view class="city_item"
						:class="provinceIndex==index?'active':''" 
						v-for="(item,index) in province" :key="index" :id="'province_' + item.provId" @click="click_province(item,index)">
							<text>{{item.provName}}</text>
						</view>
					</scroll-view>
					<scroll-view scroll-y="true" class="city_scroll scroll_right" v-if="cityStatus" :scroll-into-view="'city_' + selectedPath[2]">
						<view class="city_item"
						:class="cityIndex==index?'active':''" 
						v-for="(item,index) in city" :key="index" :id="'city_' + item.cityId" @click="click_city(item,index)">
							<text>{{item.cityName}}</text>
						</view>
					</scroll-view>
				</view>
				
				<view class="tan_item tan_department" v-if="activeFilter=='department'">
					<view class="department_item" 
					:class="departmentIndex==index?'department_active':''" 
					v-for="(item,index) in chatAssistants" :key="index" @click="click_department(item.name,index)">
						<text>{{item.name}}</text>
					</view>
				</view>
			</view>
			<view class="name_search" v-if="!activeFilter">
				<uni-easyinput
					v-model="name"
					prefixIcon="search" 
					placeholder="按姓名搜索" 
					placeholderClass="name-placeholder"
					class="center-input"
				/>
				<button @click="getData(true)">查询</button>
			</view>
		</view>
		
		<!-- 遮罩层 -->
		<view v-if="activeFilter" class="overlay" @click="closeFilter"></view>
		<scroll-view scroll-y="true"
			:scroll-top="scrollTop"
			style="height: calc(100vh - 300rpx); margin-top: 185rpx; padding-bottom: 140rpx;"
			v-if="list&&list.length>0">
			<view class="talents_list">
				<view class="talents_item" v-for="(item,index) in list" :key="index" @click="goToDetail(item.id)">
					<image :src="item.icon" class="item_image" mode="aspectFit"></image>
					<view class="item_right">
						<view class="item_top">
							<text class="item_name">{{item.name}}</text>
							<text class="item_title">{{item.title}}</text>
						</view>
						<text class="item_con">{{item.introduce}}</text>
					</view>
				</view>
			</view>
		</scroll-view>
		<text class="null_text" v-else>{{null_text}}</text>
		<z-navigation></z-navigation>
	</view>
</template>

<script>
import $http from "@/config/requestConfig.js";
export default {
	data() {
		return {
			name: '',
			region: '',  //地区
			department: '',  //科室
			list: [],
			null_text: '',
			departmentIndex: null,
			activeFilter: '',
			chatAssistants: [],  //科室数据
			country: [],  //国家数据
			province: [],  //省份数据
			city: [],  //城市数据
			
			countryIndex: 0,
			provinceIndex: null,
			cityIndex: null,
			
			countryTitle: '',
			provinceTitle: '',
			cityTitle: '',
			//城市模块是否显示
			cityStatus: false,
			selectedPath: [],  //存储选中路径
			scrollTop: 0,  //滚动位置
			isRefreshing: false,  //刷新状态
		}
	},
	onPullDownRefresh() {
		this.isRefreshing = true;
		console.log("下拉刷新");
		
		setTimeout(() => {
			this.region = '';
			this.getData();
			uni.stopPullDownRefresh();
			this.isRefreshing = false;
			console.log("下拉刷新已停止");
		}, 800);
	},
	onLoad() {
		uni.hideTabBar();
		this.getAllBaseArea();
		this.getChatAssistants();
	},
	onShow() {
		this.getData();
	},
	methods: {
		//获取数据
		getData(type) {
			uni.showLoading({
				title: '加载中'
			})
			this.$http.request({
				url: 'common/taihuTalent/getTaihuTalents',
				method: "POST",
				data: {
					name: this.name,
					region: this.region,
					department: this.department
				},
				header: {
					"Content-Type": "application/json",
				},
			})
			.then(res=> {
				uni.hideLoading();
				if (res.list&&res.list.length>0) {
					this.list = res.list;
					
					if(type){
						this.scrollTop = 0
						this.$nextTick(() => {
							this.scrollTop = 0.1;  // 确保触发滚动
						})
					}
				}else{
					this.list = [];
					this.null_text = '暂无数据';
				}
			});
		},
		//获取科室数据
		getChatAssistants() {
			this.$http.request({
				url: 'common/ragFlowApi/getChatAssistants',
				method: "POST",
				data: {},
				header: {
					"Content-Type": "application/json",
				},
			})
			.then(res=> {
				if (res.list&&res.list.length>0) {
					this.chatAssistants = res.list;
				}
			})
		},
		//获取国家
		getAllBaseArea() {
			this.$http.request({
				url: 'common/baseArea/getAllBaseArea',
				method: "POST",
				data: {},
				header: {
					"Content-Type": "application/json",
				},
			})
			.then(res=> {
				if (res.baseAreas&&res.baseAreas.length>0) {
					this.country = res.baseAreas;
					//如果第一位是中国默认请求
					if(this.country[0].code=='86'){
						this.countryTitle = this.country[0].title;
						this.getProvinceList();
					}
				}
			})
		},
		//获取中国省份
		getProvinceList() {
			this.$http.request({
				url: 'common/province/getProvinceList',
				method: "POST",
				data: {},
				header: {
					"Content-Type": "application/json",
				},
			})
			.then(res=> {
				if (res.provinceList&&res.provinceList.length>0) {
					this.province = res.provinceList;
				}
			})
		},
		//获取城市
		getCityList(provId) {
			this.$http.request({
				url: 'common/province/getCityList?provId='+provId,
				method: "POST",
				data: {},
				header: {
					"Content-Type": "application/json",
				},
			})
			.then(res=> {
				if (res.prov&&res.prov.length>0) {
					this.city = res.prov;
					this.cityStatus = true;
				}
			})
		},
		//点击国家
		click_country(data, index){
			this.provinceIndex = null;
			this.cityIndex = null;
			this.city = [];
			this.region = '';
			this.cityTitle = '';
			this.selectedPath = [data.areaId];
			
			//只有中国有二级，如果没有二级则为海外国家
			if(data.code!='86'){
				this.cityStatus = false;
				if(this.countryTitle == data.title){
					this.countryIndex = null;
					this.provinceIndex = null;
				}else{
					this.province = [];
					this.countryIndex = index;
					this.countryTitle = data.title;
					this.region = data.title;
				}
				this.activeFilter = '';
				this.getData();
			}else{
				this.countryIndex = index;
				this.countryTitle = data.title;
				this.getProvinceList();
			}
			
			console.log(this.countryIndex, this.countryTitle)
		},
		//点击省份
		click_province(data, index){
			this.provinceIndex = index;
			this.cityIndex = null;
			this.provinceTitle = data.provName;
			this.selectedPath = [this.selectedPath[0], data.provId];
			console.log(this.provinceTitle)
			this.getCityList(data.provId);
		},
		//点击城市
		click_city(data, index){
			this.selectedPath = [this.selectedPath[0], this.selectedPath[1], data.cityId];
			if(this.cityTitle == data.cityName){
				this.cityIndex = null;
				this.cityTitle = '';
				this.region = '';
			}else{
				this.cityIndex = index;
				this.cityTitle = data.cityName;
				this.region = this.provinceTitle + this.cityTitle;
			}
			this.activeFilter = '';
			this.getData();
			
			console.log(this.region)
		},
		//详情
		goToDetail(id){
			if (this.isRefreshing) return;
			uni.navigateTo({
				url: '/pages/talents/detail?id='+id,
			});
		},
		//点击
		toggleFilter(type) {
			this.activeFilter = this.activeFilter === type ? '' : type;
		},
		//关闭
		closeFilter() {
			this.activeFilter = '';
		},
		//点击科室类别
		click_department(name, index){
			if(this.department==name){
				this.departmentIndex = null;
				this.department = ''
			}else{
				this.departmentIndex = index;
				this.department = name;
			}
			
			this.activeFilter = '';
			this.getData();
		}
	},
}
</script>

<style lang="scss" scoped>
@import '@/static/mixin.scss';
.content{
	height: 100%;
	overflow: auto;
	background-color: #fff;
}
.talents_list{
	margin: 0 30rpx 20rpx;
}
.talents_item{
	border: 1rpx solid $themeColor;
	border-radius: 15rpx;
	margin-bottom: 20rpx;
	padding: 25rpx;
	display: flex;
	align-items: center;
}
.item_image{
	display: block;
	width: 180rpx;
	height: 200rpx;
	background: #f3f3f3;
}
.item_right{
	width: calc(100% - 200rpx);
	margin-left: 30rpx;
}
.item_top{
	display: flex;
	align-items: center;
	line-height: 40rpx;
}
.item_name{
	font-size: 34rpx;
	color: #333;
	font-weight: bold;
}
.item_title{
	font-size: 32rpx;
	color: #999;
	padding-left: 20rpx;
}
.item_con{
	font-size: 30rpx;
	color: #666;
	margin-top: 10rpx;
	line-height: 40rpx;
	display: -webkit-box;
	-webkit-line-clamp: 3;
	-webkit-box-orient: vertical;
	overflow: hidden;
	text-overflow: ellipsis;
}
.null_text{
	display: block;
	text-align: center;
	font-size: 30rpx;
	color: #999;
	padding-top: 300rpx;
}


.talents_module{
	width: 100%;
	position: fixed;
	z-index: 99;
	top: 0;
	left: 0;
}
.talents_tab {
	width: 100%;
	height: 80rpx;
	display: flex;
	justify-content: space-around;
	background: #f3f3f3;
}
.tab_item {
  display: flex;
  align-items: center;
}
.arrow {
  display: inline-block;
  width: 0;
  height: 0;
  border-left: 10rpx solid transparent;
  border-right: 10rpx solid transparent;
  margin-left: 10rpx;
}
.arrow.down {
  border-top: 10rpx solid #666;
}
.arrow.up {
  border-bottom: 10rpx solid #666;
}
.overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 9;
}
.talents_tan{
	width: 100%;
	background: #fff;
	font-size: 28rpx;
	
	.tan_department{
		display: flex;
		flex-wrap: wrap;
		padding-bottom: 1rpx;
		
		.department_item{
			display: flex;
			justify-content: center;
			width: 50%;
			height: 85rpx;
			line-height: 85rpx;
			border-bottom: 1rpx solid #f3f3f3;
			border-right: 1rpx solid #f3f3f3;
			
			text{
				color: #333;
			}
		}
		.department_item:nth-of-type(2n){
			border-right: 0;
		}
		.department_item:nth-last-child(-n+2){
			border-bottom: 0;
		}
		.department_active{
			
			text{
				color: $themeColor;
				font-weight: bold;
			}
		}
	}
	
	.tan_city{
		display: flex;
		align-items: center;
		
		.city_scroll{
			width: 33.3%;
			height: 395rpx;
		}
		
		.scroll_left,.scroll_center{
			border-right: 1rpx solid #f3f3f3;
			box-sizing: border-box;
		}
		.width50{
			width: 50% !important;
		}
	}
}
.city_item{
	padding-left: 20rpx;
	line-height: 78rpx;
	border-bottom: 1rpx solid #f3f3f3;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
}
.city_item:last-child{
	border-bottom: 0;
}
.active{
	text{
		color: $themeColor;
		font-weight: bold;
	}
}

.name_search{
	background-color: #fff;
	padding: 20rpx 30rpx;
	display: flex;
	align-items: center;
	
	/deep/.is-input-border{
		background-color: #f3f3f3;
		border-radius: 50rpx;
		height: 60rpx;
		line-height: 30rpx;
		padding: 15rpx;
		font-size: 28rpx;
		color: #666;
	}
	/deep/.uni-easyinput__content-input{
		
	}
	.name-placeholder{
		font-size: 28rpx;
		text-align: center;
		color: #666;
	}
	
	button{
		background-color: $themeBgColor;
		font-size: 26rpx;
		line-height: 36rpx;
		border-radius: 15rpx;
		color: #fff;
		padding: 5rpx 20rpx;
		margin-left: 15rpx;
	}
}
</style>