<template>
	<view class="container">
		<view class="top-box">
			<view class="input-item">
				<image class="icon" src="https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/image/search.png" mode=""></image>
				<input class="" placeholder="请输入讲者姓名" placeholder-class="placeholder" v-model="searchName" />
			</view>
			<view class="filter-item" @click="showFilter = true">
				<image class="w32 h32 mr4" src="https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/image/icon_select.png" mode=""></image>
				<text>筛选</text>
			</view>
		</view>
		<!-- {{utils.getDictLabelName("doctorPosition",item.doctorPosition)}} -->
		<!-- 筛选标签栏 -->
		<view class="tab">
			<view class="filter-bar">
				<view class="filter-tabs">
					<view class="tab-item" :class="{ active: currentTab === item.value }" v-for="(item, index) in tabs"
						:key="index" @click="switchTab(item.value)">
						{{ item.label }}
					</view>
				</view>
				<view class="filter-divider"></view>
				<view class="filter-btn" @click="showMemberPopup = true">
					<text>{{ selectedMemberOption || '全员' }}</text>
					<image class="w32 h32" src="https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/image/icon_down.png" mode=""></image>
				</view>
			</view>
			<!-- 成员筛选弹窗 -->
		<view class="member-popup-overlay" v-if="showMemberPopup" @click="showMemberPopup = false"></view>
		<view class="member-popup" v-if="showMemberPopup">
			<view class="member-options">
				<view class="member-option" :class="{ active: selectedMember === 'all' }" @click="selectMember('all', '全员')">
					<text>全员</text>
				</view>
				<view class="member-option" :class="{ active: selectedMember === 'option1' }" @click="selectMember('option1', '选项1')">
					<text>选项1</text>
				</view>
				<view class="member-option" :class="{ active: selectedMember === 'option2' }" @click="selectMember('option2', '选项2')">
					<text>选项2</text>
				</view>
				<view class="member-option" :class="{ active: selectedMember === 'option3' }" @click="selectMember('option3', '选项3')">
					<text>选项3</text>
				</view>
			</view>
		</view>
		</view>
		
		<!-- 任务列表 -->
		<scroll-view class="content" scroll-y>
			<view class="task-card" v-for="(item, index) in taskList" :key="index" @click="showDetail(item)">
				<view class="card-header">
					<view class="card-title">
						<text>{{ item.doctorName||'未命名' }}</text>
						<text class="title-little" v-if="item.account_type">{{item.account_type}}</text>
						<view class="lable" v-if="item.jobTitle">{{item.jobTitle}}</view>
					</view>
					<view class="status-tag"
						:class="item.status === 0 ? 'reviewing' : item.status === 1 ? 'approved' : item.status === 2 ? 'rejected' : 'manual'">
						{{ item.status === 0 ? '待审核' : item.status === 1 ? '已通过' : item.status === 2 ? '已驳回' : '转人工审核' }}
					</view>
				</view>


				<view class="row">
					<image class="icon" src="https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/image/icon_hospital.png" mode=""></image>
					<text>{{ item.institution ||'-'}}</text>
					<view class="line"></view>
					<text>{{ item.department ||'-'}}</text>
				</view>
				<view class="row">
					<image class="icon" src="https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/image/icon_user.png" mode=""></image>
					<view class="txt-container">
						<text class="txt" :class="{ 'expanded': expandedItems[index] }">
							<text v-for="(value,idx) in (item.doctorProducts || [])" :key="idx">
								{{ value.companyUserName }}({{ value.productCode }})<span
									v-if="idx < (item.doctorProducts || []).length - 1">、</span>
							</text>
						</text>
					</view>
					<image v-if="item.doctorProducts && item.doctorProducts.length > 0" class="icon expand-icon"
						:class="{ 'expanded': expandedItems[index] }" @click.stop="expand(index)"
						src="https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/image/icon_expand.png" mode=""></image>
				</view>

				<view class="card-warning" v-if="item.warning">
					<text class="warning-icon">⚠</text>
					<text>{{ item.warning }}</text>
				</view>

				<view class="card-rejection" v-if="item.rejectReason">
					<image class="w28 h28 mr8" src="https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/image/icon_wrong.png" mode=""></image>
					<text>驳回原因: {{ item.rejectReason }}</text>
				</view>
				<view class="card-tags">
					<view class="tag-item points-tag" v-if="item.productGroup" @click.stop="showCorrelation=true">
						<!-- 默认产品组：五级 -->
						{{ item.productGroup }}
					</view>
				</view>
				<view class="apply-button" v-if="item.isApply==0" @click.stop="applyLevel(item)">定级申请</view>
			</view>
		</scroll-view>

		<!-- 身份筛选 -->
		<view class="filter-popup" v-if="showFilter" @click="closeFilter">
			<view class="filter-content" @click.stop>
				<view class="filter-header">
					<view class="filter-title">筛选</view>
					<image class="filter-close-btn" src="https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/image/icon_close.png" @click="closeFilter" mode="">
					</image>
				</view>

				<!-- 身份筛选 -->
				<view class="filter-group">
					<view class="group-label">账号身份</view>
					<view class="tabs-container">
						<view v-for="(tab, index) in identity" :key="index"
							:class="['tab-item', activeTab === tab.dictValue ? 'active' : '']"
							@click="switchFilterTab(tab.dictValue)">
							<text class="tab-text">{{ tab.dictLabel }}</text>
							<!-- <text v-if="tab.dictValue" class="tab-count">{{ tab.dictValue }}</text> -->
						</view>
					</view>
				</view>

				<!-- 操作按钮 -->
				<view class="filter-actions">
					<view class="reset-btn" @click="resetFilters">重置</view>
					<view class="confirm-btn" @click="confirmFilters">确定</view>
				</view>
			</view>
		</view>

		<!-- 关联产品 -->
		<view class="filter-popup" v-if="showCorrelation" @click="showCorrelation = false">
			<view class="filter-content correlation-content" @click.stop>
				<view class="filter-header">
					<view class="filter-title">关联产品</view>
					<image class="filter-close-btn" src="https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/image/icon_close.png" @click="showCorrelation = false"
						mode="">
					</image>
				</view>

				<!-- 注意：这里移出了 filter-header -->
				<view class="popup-content">
					<view class="head-item item">
						<view class="left">小蜜蜂</view>
						<view class="right">关联产品</view>
					</view>
					<view class="item" v-for="(row, index) in productData" :key="index">
						<view class="left">{{ row.assistant }}</view>
						<view class="right">
							<text v-for="(product, idx) in row.products" :key="idx" class="product-tag">
								{{ product }}<span v-if="idx < row.products.length - 1">、</span>
							</text>
						</view>
					</view>
				</view>
			</view>
		</view>


		<Server />
	</view>
</template>

<script>
    import utils from '@/utils/common.js'
	import Server from '@/components/Server.vue'
	import {
		speakerList
	} from '@/api/speaker'

	export default {
		components: {
			Server
		},
		data() {
			return {
				// userInfo: JSON.parse(uni.getStorageSync('userInfo') || '{}'),
			    userInfo :{},
				productData: [{
						assistant: '王小明',
						products: ['302', '403', '202', '301']
					},
					{
						assistant: '李洋',
						products: ['301']
					}
				],
				showCorrelation: false, //显示关联产品弹窗
				activeTab: '',
				// 状态标签
				identity: {},
				statusBarHeight: uni.getSystemInfoSync().statusBarHeight + 'px',
				currentTab: '',
				showFilter: false,
				dateRange: {
					startDate: '',
					endDate: ''
				},
				tempDateRange: {
					startDate: '',
					endDate: ''
				},
				searchName: '',
				tabs: [{
						label: '全部',
						value: ''
					},
					{
						label: '待审核',
						value: '0'
					},
					{
						label: '已通过',
						value: '1'
					},
					{
						label: '已驳回',
						value: '2'
					}
				],
				taskList: [],
				expandedItems: {},
				// 搜索防抖定时器
				searchTimer: null,
				// 成员筛选弹窗
				showMemberPopup: false,
				// 选中的成员选项
				selectedMember: 'all',
				// 选中的成员选项文本
				selectedMemberOption: '全员'
			}
		},
		watch: {
			showFilter(newVal) {
				if (newVal) {
					// 打开弹窗时，同步临时日期范围为当前日期范围
					this.tempDateRange = {
						startDate: this.dateRange.startDate,
						endDate: this.dateRange.endDate
					}
				}
			},
			// 监听搜索关键词变化，添加防抖处理
			searchName(newVal) {
				// 清除之前的定时器
				if (this.searchTimer) {
					clearTimeout(this.searchTimer)
				}
				// 设置新的定时器，300ms后执行搜索
				this.searchTimer = setTimeout(() => {
					this.loadData()
				}, 300)
			}
		},
		async onLoad() {
			this.userInfo = uni.getStorageSync('userInfo') || '{}';
			this.loadData();
			try {
				this.identity = await utils.getDicts("doctor_account_identity_type");//学术头衔
				console.log("identity内容", this.identity)
			} catch (e) {
				console.log('获取字典数据失败:', e)
				this.identity = []
			}
		},
		onReachBottom() {
			this.loadMore()

		},
		methods: {
			expand(index) {
				this.$set(this.expandedItems, index, !this.expandedItems[index])
			},
			goBack() {
				uni.navigateBack()
			},
			switchTab(value) {
				this.currentTab = value
				this.loadData()
			},
			// 弹窗筛选栏切换
			switchFilterTab(value) {
				this.activeTab = value
			},
			closeFilter() {
				// 关闭弹窗时，恢复临时日期范围为当前日期范围
				this.tempDateRange = {
					startDate: this.dateRange.startDate,
					endDate: this.dateRange.endDate
				}
				this.showFilter = false
			},
			onStartDateChange(e) {
				this.tempDateRange.startDate = e.detail.value
			},
			onEndDateChange(e) {
				this.tempDateRange.endDate = e.detail.value
			},
			resetFilters() {
				this.activeTab = '';
			},
			confirmFilters() {
				// 验证日期范围
				if (this.tempDateRange.startDate && this.tempDateRange.endDate) {
					if (new Date(this.tempDateRange.startDate) > new Date(this.tempDateRange.endDate)) {
						uni.showToast({
							icon: 'none',
							title: '开始时间不能大于结束时间'
						})
						return
					}
				}
				this.dateRange = {
					startDate: this.tempDateRange.startDate,
					endDate: this.tempDateRange.endDate
				}
				this.showFilter = false
				this.loadData()
			},
			// 查看详情
			showDetail(item) {
				console.log("item内容", item)
				uni.navigateTo({
					url: `/pages_speaker/lecturerDetail?id=${item.id }`
				})
			},
			async loadData() {
				console.log("activeTab内容", this.currentTab)
				try {
					uni.showLoading({
						title: '加载中...'
					})
					console.log("userInfo内容", this.userInfo)
					const res = await speakerList({
						companyId: this.userInfo.companyId,
						page: 1,
						pageSize: 20,
						doctorName: this.searchName,
						status: this.currentTab
					})
					uni.hideLoading()
					if (res.code === 200 && res.rows) {
						// 直接使用接口返回的原始数据
						this.taskList = res.rows
					} else {
						this.taskList = this.getDefaultData()
					}
				} catch (e) {
					uni.hideLoading()
					console.error('加载数据失败', e)
					this.taskList = this.getDefaultData()
				}
			},
			async loadMore() {
				// 加载更多数据
			},
			// 定级申请
			applyLevel(item) {
				console.log('定级申请', item)
				uni.navigateTo({
					url: `/pages_speaker/gradeApplication?id=${item.id}&auditInstanceId=${item.auditInstanceId || item.id}&doctorId=${item.doctorId}`
				})
			},
			getDefaultData() {
				return []
			},
			// 选择成员选项
			selectMember(value, label) {
				this.selectedMember = value
				this.selectedMemberOption = label
				this.showMemberPopup = false
				// 这里可以添加根据选择的成员选项过滤数据的逻辑
				this.loadData()
			}
		}
	}
</script>
<style lang="stylus">
	.placeholder {
		color: #C8C9CC !important;
		font-size: 28rpx;
		color: #C8C9CC;
	}
</style>
<style lang="scss" scoped>
	.container {
		min-height: 100vh;
		background: #f5f5f5;
		display: flex;
		flex-direction: column;

		.top-box {
			position: relative;
			z-index: 99;
			padding: 16rpx 32rpx;
			display: flex;
			align-items: center;
			background: #ffffff;

			.input-item {
				display: flex;
				align-items: center;
				flex: 1;
				height: 72rpx;
				background: #F7F8FA;
				border-radius: 38rpx 38rpx 38rpx 38rpx;

				.icon {
					width: 26rpx;
					height: 26rpx;
					margin: 0 10rpx 0 28rpx;
				}
			}

			.filter-item {
				margin-left: 24rpx;
				display: flex;
				align-items: center;
				font-size: 28rpx;
				color: #999999;
			}
		}
	}

	.status-bar {
		width: 100%;
		background: #fff;
	}

	.header {
		position: relative;
		height: 88rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background: #fff;
		border-bottom: 1rpx solid #f0f0f0;

		.back-btn {
			position: absolute;
			left: 24rpx;
			width: 40rpx;
			height: 40rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.title {
			font-size: 36rpx;
			font-weight: bold;
			color: #333;
		}

		.header-right {
			position: absolute;
			right: 24rpx;
			display: flex;
			align-items: center;
			gap: 16rpx;

			.more-icon {
				font-size: 32rpx;
				color: #333;
			}
		}
	}

	.tab {
		position: relative;
			z-index: 99;

		.filter-bar {
			display: flex;
			align-items: center;
			background: #fff;
			padding: 0 40rpx;

			.filter-tabs {
				flex: 1;
				display: flex;
				align-items: center;
				gap: 64rpx;
				overflow-x: auto;

				.tab-item {
					padding: 24rpx 0;
					font-size: 28rpx;
					color: #999999;
					white-space: nowrap;
					position: relative;

					&.active {
						color: #333333;
						font-weight: 500;

						&::after {
							content: '';
							position: absolute;
							bottom: 0;
							left: 0;
							right: 0;
							height: 6rpx;
							background: #388BFF;
							border-radius: 3rpx 3rpx 3rpx 3rpx;
						}
					}
				}
			}

			.filter-divider {
				width: 1rpx;
				height: 40rpx;
				background: #e0e0e0;
				margin: 0 16rpx;
			}

			.filter-btn {
				display: flex;
				align-items: center;
				padding: 24rpx 0;
				font-size: 28rpx;
				color: #666666;

				.filter-icon {
					font-size: 32rpx;
					color: #333;
				}
			}
		}
	}


	.content {
		flex: 1;
		padding: 24rpx;
		box-sizing: border-box;
	}

	.task-card {
		background: #fff;
		border-radius: 16rpx;
		padding: 24rpx;
		margin-bottom: 24rpx;

		.card-header {
			display: flex;
			align-items: flex-start;
			justify-content: space-between;
			margin-bottom: 16rpx;

			.card-title {
				flex: 1;
				font-size: 32rpx;
				font-weight: 600;
				color: #333;
				display: flex;
				align-items: center;

				.title-little {
					font-size: 24rpx;
					color: #666666;
					margin-left: 36rpx;
				}

				.lable {
					margin-left: 16rpx;
					padding: 2rpx 12rpx;
					font-weight: 500;
					font-size: 22rpx;
					color: #C89743;
					background: #FFF6E5;
					border-radius: 8rpx 8rpx 8rpx 8rpx;
				}
			}

			.status-tag {
				padding: 8rpx 16rpx;
				border-radius: 20rpx;
				font-size: 24rpx;

				// &.pending {
				// 	background: #E3F2FD;
				// 	color: #2196F3;
				// }

				&.reviewing {
					background: #FFFBEA;
					color: #FAAB0C;
				}

				&.approved {
					background: #E6FAEF;
					color: #07C160;
				}

				&.rejected {
					background: #FFF4F5;
					color: #CF3546;
				}

				&.manual {
					background: #E3F2FD;
					color: #2196F3;
				}
			}
		}

		.card-tags {
			display: flex;
			align-items: center;
			flex-wrap: wrap;
			gap: 12rpx;
			margin-top: 20rpx;

			.tag-item {
				padding: 8rpx 16rpx;
				background: #FFFAF4;
				border-radius: 8rpx;
				font-size: 24rpx;
				color: #5D410F;
				display: flex;
				align-items: center;

				&.points-tag {
					color: #388BFF;
					background: transparent;
					border-radius: 8rpx 8rpx 8rpx 8rpx;
					border: 2rpx solid rgba(56, 139, 255, 0.4);
				}
			}
		}

		.row {
			display: flex;
			align-items: center;
			margin-bottom: 20rpx;
			font-size: 28rpx;
			color: #666666;

			.icon {
				width: 32rpx;
				height: 32rpx;
				margin-right: 16rpx;
			}

			.expand-icon {
				margin-left: 8rpx;
				transition: transform 0.3s;

				&.expanded {
					transform: rotate(180deg);
				}
			}

			.txt-container {
				flex: 1;
				overflow: hidden;
			}

			.txt {
				display: block;
				overflow: hidden;
				white-space: nowrap;
				text-overflow: ellipsis;

				&.expanded {
					white-space: normal;
					word-wrap: break-word;
				}
			}

			.line {
				width: 2rpx;
				height: 28rpx;
				background: #EAEBEE;
				border-radius: 0rpx 0rpx 0rpx 0rpx;
				margin: 0 24rpx;
			}
		}

		.apply-button {
			padding: 12rpx 40rpx;
			font-weight: 500;
			font-size: 28rpx;
			color: #FFFFFF;
			background: #388BFF;
			border-radius: 34rpx 34rpx 34rpx 34rpx;
			width: fit-content;
			margin-left: auto;
		}

		.card-warning {
			display: flex;
			align-items: center;
			gap: 8rpx;
			padding: 12rpx;
			background: #FFF3E0;
			border-radius: 8rpx;
			margin-bottom: 16rpx;
			font-size: 26rpx;
			color: #FF9800;

			.warning-icon {
				font-size: 28rpx;
			}
		}

		.card-rejection {
			display: flex;
			align-items: center;
			gap: 8rpx;
			padding: 10rpx 20rpx;
			background: #FFF4F5;
			border-radius: 8rpx;
			margin-bottom: 16rpx;
			font-size: 26rpx;
			color: #F44336;

			.rejection-icon {
				font-size: 28rpx;
			}
		}
	}

	.filter-popup {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background: rgba(0, 0, 0, 0.5);
		z-index: 999;
		display: flex;
		align-items: flex-end;
	}

	.filter-content {
		width: 100%;
		background: #fff;
		border-radius: 24rpx 24rpx 0 0;
		padding: 32rpx;
		display: flex;
		flex-direction: column;
		max-height: 80vh;

		.popup-content {
			.item {
				height: 88rpx;
				display: flex;
				align-items: center;
				font-weight: 400;
				font-size: 28rpx;
				color: #333333;
				padding-left: 32rpx;
			}

			.head-item {
				font-weight: 600;
				color: #666666;
			}

			.left {
				width: 20%;
			}

			.right {
				flex: 1;
			}


		}

		/* 限制最大高度 */
		.filter-header {
			display: flex;
			align-items: center;
			justify-content: center;
			margin-bottom: 32rpx;
			position: relative;
			flex-shrink: 0;

			/* 防止标题区域被压缩 */
			.filter-title {
				font-size: 32rpx;
				font-weight: bold;
				color: #333;
				text-align: center;
			}

			.filter-close-btn {
				position: absolute;
				right: 0;
				width: 44rpx;
				height: 44rpx;
			}
		}

		.filter-group {
			margin-bottom: 32rpx;

			.group-label {
				font-size: 28rpx;
				font-weight: bold;
				color: #333;
				margin-bottom: 20rpx;
			}

			.tabs-container {
				display: inline-flex;
				align-items: center;
				gap: 32rpx;
				flex-wrap: wrap;

				.tab-item {
					display: flex;
					align-items: center;
					gap: 8rpx;
					padding: 12rpx 0;
					position: relative;

					.tab-text {
						font-size: 28rpx;
						color: #666666;
					}

					.tab-count {
						font-size: 24rpx;
						color: #999999;
						background: #f5f6fa;
						padding: 2rpx 8rpx;
						border-radius: 12rpx;
						min-width: 32rpx;
						text-align: center;
						border: 2rpx solid transparent;
					}

					&.active {
						color: #333333;
						background: rgba(56, 139, 255, 0.15);
						border-radius: 70rpx 70rpx 70rpx 70rpx;
						border: 2rpx solid #388BFF;
						padding: 14rpx 32rpx;

					}
				}
			}
		}

		.filter-actions {
			display: flex;
			gap: 24rpx;
			margin-top: 40rpx;
			padding-top: 24rpx;
			flex-shrink: 0;

			.reset-btn,
			.confirm-btn {
				flex: 1;
				height: 80rpx;
				line-height: 80rpx;
				text-align: center;
				border-radius: 200rpx 200rpx 200rpx 200rpx;
				font-size: 28rpx;
			}

			.reset-btn {
				background: #fff;
				color: #388BFF;
				border: 2rpx solid #388BFF;
			}

			.confirm-btn {
				background: #388BFF;
				color: #fff;
			}
		}
	}

	/* 成员筛选弹窗样式 */
	.member-popup-overlay {
		position: fixed;
		top: 240rpx;
		left: 0;
		right: 0;
		bottom: 0;
		background: rgba(0, 0, 0, 0.5);
		z-index: 8;
	}

	.member-popup {
		position: fixed;
		top: 160rpx;
		left: 0;
		right: 0;
		background: #fff;
		border-radius: 0;
		padding: 32rpx;
		width: 100%;
		box-sizing: border-box;
		box-shadow: none;
		z-index: 9;
	}

	.member-options {
		display: flex;
		flex-wrap: wrap;
		gap: 24rpx;
		border-radius: 70rpx 70rpx 70rpx 70rpx;
		justify-content: flex-start;
	}

	.member-option {
		padding: 24rpx 14rpx;
		border-radius: 70rpx;
		font-size: 28rpx;
		color: #666666;
		background: #ffffff;
		border: 1rpx solid #e8e8e8;
		transition: all 0.2s ease;
		text-align: center;
		min-width: 214rpx;
		box-sizing: border-box;

		&.active {
			background: rgba(56,139,255,0.15);
			color: #ffffff;
			color: #388BFF;
			border: 1rpx solid #388BFF ;
		}
	}
</style>