<template>	
	<view class="assemble">
		<fui-segmented-control :values="values" type="text" activeColor="#29d3b4" color="#fff"
			@click="segmented"></fui-segmented-control>

		<!--我的客户-->
		<scroll-view v-if="segmented_type == 1" scroll-y="true" :lower-threshold="lowerThreshold"
			@scrolltolower="loadMoreData_1" style="height: 100vh;">
			
			<!-- 搜索按钮区域 -->
			<view class="search_section">
				<view class="item">
					<view class="input_box" @click="openSearchPopup">
						<view class="input_box_text">校区/用户名/手机号/时间范围</view>
					</view>
					<view class="button" @click="openSearchPopup">搜索</view>
				</view>
			</view>
			<view class="card" v-for="(v,k) in tableList_1" :key="k">
				<view class="card-content">
					<view class="card-left" @click="clue_info(v)">
						<view style="display: flex;align-items: center;padding: 12rpx;">
							<view>
								<image :src="$util.img('/uniapp_src/static/images/index/myk.png')" class="card-image">
								</image>
							</view>
							<view class="card-text">{{ v.customerResource.name }}</view>
							<view class="card-con-span">{{ v.customerResource.phone_number }}</view>
						</view>
						<view class="card-con">
							所属校区：{{ v.customerResource.campus_name }}
						</view>
						<view class="card-con">
							来源：{{ v.customerResource.source }}
						</view>
						<view class="card-con">
							来源渠道：{{ v.customerResource.source_channel }} 
						</view>
					</view>
					<view class="card-right">
						<!-- 开单状态标签 -->
						<view :class="['status-tag',getOrderStatusClass(v.customerResource.order_status)]">
							{{ v.customerResource.order_status || '未开单' }}
						</view>
						<!-- 到访状态标签 -->
						<view class="visit-status">
							<view :class="['visit-tag',getVisitStatusClass(v.customerResource.first_visit_status)]">
								一访{{ v.customerResource.first_visit_status || '未到' }}
							</view>
							<view :class="['visit-tag',getVisitStatusClass(v.customerResource.second_visit_status)]">
								二访{{ v.customerResource.second_visit_status || '未到' }}
							</view>
						</view>
						<!--只有注册了member表的账号才可操作IM对话-->
						<view class="btn-item" v-if="v.customerResource.member_id" @click.stop>
							<image :src="$util.img('/uniapp_src/static/images/index/message.png')" class="image"
								@click.stop="openViewMyMessage(v)"></image>
						</view>
						<view class="btn-item" v-if="v.customerResource.phone_number" @click.stop>
							<image :src="$util.img('/uniapp_src/static/images/index/phone.png')" class="image"
								@click.stop="dialTel(v)">
							</image>
						</view>
					</view>
				</view>
				<view class="card-footer">
					<view class="card-date">
						<view class="card-con" v-if="v.customerResource.communication_time">
							{{ $util.formatToDateTime((v.customerResource.communication_time || ''), 'm-d H:i') }}
							拨打电话
						</view>
						<view class="card-con" v-else>
							暂未联系
						</view>
						<view style="margin-left: 30rpx;">
							<view style="display: flex;align-items: center;">
								<view class="title-x">
									是否有效：{{ v.sixSpeed && v.sixSpeed.efficacious === 1 ? '有效' : '无效' }}
								</view>
								<view style="padding: 12rpx;">
									<image v-if=" v.sixSpeed && v.sixSpeed.efficacious === 1"
										:src="$util.img('/uniapp_src/static/images/index/intention2.png')"
										class="drop-image-x"></image>
									<image v-else :src="$util.img('/uniapp_src/static/images/index/intention3.png')"
										class="drop-image-x"></image>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>

		<!--资源分配-->
		<scroll-view v-if="segmented_type == 2" scroll-y="true" :lower-threshold="lowerThreshold"
			@scrolltolower="loadMoreData_2" style="height: 100vh;">
			<view class="search_section">
				<view class="item">
					<view class="input_box" @click="openSearchPopup()">
						<view class="input_box_text">校区/用户名/手机号/时间范围</view>
					</view>
					<view class="button" @click="openSearchPopup()">搜索</view>
				</view>
				<!-- 批量操作控制栏 -->
				<view class="batch_control_bar">
					<view class="batch_actions_left">
						<view v-if="!batchMode" class="batch_toggle_btn" @click="enterBatchMode()">批量操作</view>
						<view v-if="batchMode" class="batch_toggle_btn cancel" @click="exitBatchMode()">取消</view>
						<view v-if="batchMode" class="batch_select_all" @click="toggleSelectAll()">
							<text class="checkbox" :class="{ checked: isAllSelected }">✓</text>
							<text class="select_all_text">全选</text>
						</view>
					</view>
					<view v-if="batchMode && selectedItems.length > 0" class="batch_actions_right">
						<view class="batch_assign_btn" @click="batchAssign()">
							批量分配 ({{ selectedItems.length }})
						</view>
					</view>
				</view>
			</view>
			<view class="card" v-for="(v,k) in tableList_2" :key="k" @click="batchMode ? toggleItemSelection(v) : null">
				<view class="card-content">
					<view class="card-left">
						<view style="display: flex;align-items: center;padding: 12rpx;">
							<!-- 批量选择复选框 -->
							<view v-if="batchMode" class="batch_checkbox" @click.stop="toggleItemSelection(v)">
								<text class="checkbox" :class="{ checked: selectedItems.includes(v.id) }">✓</text>
							</view>
							<view>
								<image :src="$util.img('/uniapp_src/static/images/index/myk.png')" class="card-image">
								</image>
							</view>
							<view class="card-text">{{v.customerResource.name}}</view>
							<!--            <view class="card-label">{{v.is_status == 1 ? '试听' : '成交'}}</view>-->
						</view>
						<view class="card-con">
							决策人：{{v.customerResource.name}} <span
								class="card-con-span">{{v.customerResource.decision_maker}}</span>
						</view>
						<view class="card-con">
							分配给：<span class="assigned-to" :class="v.shared_by_name === '未分配' ? 'unassigned' : 'assigned'">{{v.shared_by_name}}</span>
						</view>
					</view>
					<view class="card-right">
						<view class="btn-item" @click.stop="openAssign(v)">
							<view class="ling">分配</view>
						</view>
					</view>
				</view>
				<view class="card-footer">
					<view class="card-date">
						<view class="card-con" v-if="v.customerResource.communication_time">
							{{ $util.formatToDateTime((v.customerResource.communication_time || ''),'m-d H:i') }} 拨打电话
						</view>
						<view class="card-con" v-else>
							暂未联系
						</view>
						<view style="margin-left: 30rpx;">
							<view style="display: flex;align-items: center;">
								<view style="padding: 12rpx;">
									<image v-if="v.customerResource.initial_intent == 'high'"
										:src="$util.img('/uniapp_src/static/images/index/intention3.png')"
										class="drop-image-x"></image>
									<image v-else-if="v.customerResource.initial_intent == 'medium'"
										:src="$util.img('/uniapp_src/static/images/index/intention2.png')"
										class="drop-image-x"></image>
									<image v-else :src="$util.img('/uniapp_src/static/images/index/intention1.png')"
										class="drop-image-x"></image>
								</view>
								<view class="title-x">
									是否有效：{{ v.customerResource && v.customerResource.initial_intent_name || '' }}</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>

		<!-- 顶部弹出搜索组件 -->
		<view v-if="showSearchPopup" class="search_popup_mask" @tap="showSearchPopup=false">
			<view class="search_popup_content" @tap.stop>
				<view class="popup_search_content">
					<view class="popup_header">
						<view class="popup_title">筛选</view>
						<view class="popup_close" @tap="showSearchPopup=false">
							<text class="close_text">✕</text>
						</view>
					</view>
					
					<scroll-view :scroll-y="true" class="popup_scroll_view">
						<!-- 第一筛选区域 -->
						<view class="popup_filter_section">
							<view class="popup_filter_row">
								<view class="popup_filter_item">
									<text class="popup_filter_label">校区筛选</text>
									<input class="popup_filter_input" placeholder="校区筛选" v-model="searchForm.campus_name" />
								</view>
								<view class="popup_filter_item">
									<text class="popup_filter_label">姓名</text>
									<input class="popup_filter_input" placeholder="姓名" v-model="searchForm.name" />
								</view>
							</view>
							
							<view class="popup_filter_row">
								<view class="popup_filter_item">
									<text class="popup_filter_label">电话</text>
									<input class="popup_filter_input" placeholder="电话" v-model="searchForm.phone_number" />
								</view>
								<view class="popup_filter_item">
									<text class="popup_filter_label">来源</text>
									<picker :value="sourceIndex" :range="sourceOptions" @change="onSourceChange">
										<view class="popup_filter_picker">{{ sourceOptions[sourceIndex] }}</view>
									</picker>
								</view>
							</view>
							
							<view class="popup_filter_row" v-if="sourceIndex === 1">
								<view class="popup_filter_item full_width">
									<text class="popup_filter_label">来源渠道</text>
									<input class="popup_filter_input" placeholder="来源渠道" v-model="searchForm.source_channel" />
								</view>
							</view>
						</view>
						
						<!-- 第二筛选区域 -->
						<view class="popup_filter_section">
							<view class="popup_filter_row">
								<view class="popup_filter_item">
									<text class="popup_filter_label">课程检索</text>
									<input class="popup_filter_input" placeholder="课程检索" v-model="searchForm.course_search" />
								</view>
								<view class="popup_filter_item">
									<text class="popup_filter_label">到课类型</text>
									<picker :value="attendanceIndex" :range="attendanceOptions" @change="onAttendanceChange">
										<view class="popup_filter_picker">{{ attendanceOptions[attendanceIndex] }}</view>
									</picker>
								</view>
							</view>
							
							<view class="popup_filter_row">
								<view class="popup_filter_item">
									<text class="popup_filter_label">成交类型</text>
									<picker :value="dealIndex" :range="dealOptions" @change="onDealChange">
										<view class="popup_filter_picker">{{ dealOptions[dealIndex] }}</view>
									</picker>
								</view>
								<view class="popup_filter_item">
									<text class="popup_filter_label">资源有效类型</text>
									<picker :value="validIndex" :range="validOptions" @change="onValidChange">
										<view class="popup_filter_picker">{{ validOptions[validIndex] }}</view>
									</picker>
								</view>
							</view>
							
							<view class="popup_filter_row">
								<view class="popup_filter_item">
									<text class="popup_filter_label">沟通情况</text>
									<picker :value="communicationIndex" :range="communicationOptions" @change="onCommunicationChange">
										<view class="popup_filter_picker">{{ communicationOptions[communicationIndex] }}</view>
									</picker>
								</view>
								<view class="popup_filter_item">
									<text class="popup_filter_label">时间</text>
									<view class="popup_filter_input" @click="openDatePicker">
										{{ searchForm.time_range || '开始时间-结束时间' }}
									</view>
								</view>
							</view>
						</view>
					</scroll-view>
					
					<view class="popup_filter_buttons">
						<view class="popup_filter_btn reset_btn" @click="resetSearchOnly">重置</view>
						<view class="popup_filter_btn search_btn" @click="searchDataAndClose">搜索</view>
						<view class="popup_filter_btn close_btn" @click="closeSearchPopup">关闭</view>
					</view>
				</view>
			</view>
		</view>

		<!--下拉选择器-->
		<fui-select :show="select_show" :options="select_options" title="请选择员工" @confirm="getSales"
			@close="closeAssign"></fui-select>

		<!--    时间范围选择器-->
		<fui-date-picker range :show="date_picker_show" type="3" @change="changeDatePicker" :value="selectedDate"
			@cancel="cancelDatePicker" :z-index="1500"></fui-date-picker>

		<!--    我的客户搜索条件栏目-->
		<fui-drawer :show="showDrawer" direction="left" :maskClosable="true">
			<view class="fui-scroll__view">
				<view class="fui-title">筛选</view>
				<scroll-view scroll-y style="height: 720rpx;">
					<view class="drawer_box">

						<fui-list-cell>
							<view class="item">
								<view class="title">校区筛选</view>
								<view class="input_box">
									<fui-input :clearable="true" borderTop placeholder="校区筛选"
										v-model="showDrawerForm.campus_name"></fui-input>
								</view>
							</view>
						</fui-list-cell>


						<fui-list-cell>
							<view class="item">
								<view class="title">时间筛选</view>
								<view class="input_box">
									<fui-input :clearable="true" borderTop placeholder="开始时间-结束时间"
										@click="openDatePicker" v-model="showDrawerForm.shared_at_str"></fui-input>
								</view>
							</view>
						</fui-list-cell>

						<fui-list-cell>
							<view class="item">
								<view class="title">姓名筛选</view>
								<view class="input_box">
									<fui-input :clearable="true" borderTop placeholder="姓名筛选"
										v-model="showDrawerForm.name"></fui-input>
								</view>
							</view>
						</fui-list-cell>

						<fui-list-cell>
							<view class="item">
								<view class="title">电话筛选</view>
								<view class="input_box">
									<fui-input :clearable="true" borderTop placeholder="电话筛选"
										v-model="showDrawerForm.phone_number"></fui-input>
								</view>
							</view>
						</fui-list-cell>
					</view>
				</scroll-view>
				<view class="fui-btn__box">
					<view class="button clientbtn" @click="searchData()">搜索</view>
					<view class="button clientbtn" @click="closeShowDrawer()">关闭</view>
				</view>
			</view>
		</fui-drawer>

		<view style="height: 170rpx;"></view>
		<AQTabber />
	</view>
</template>

<script>
	import AQTabber from "@/components/AQ/AQTabber.vue"
	import apiRoute from '@/api/apiRoute.js';
	import marketApi from '@/api/market.js';

	export default {
		components: {
			AQTabber,
		},
		data() {
			return {
				loading: false, //加载状态
				lowerThreshold: 100, //距离底部多远触发
				isReachedBottom: false, //防止重复加载|true=不可加载|false=可加载
				selectedDate: this.getCurrentDate(), // 初始化当前日期
				
				// 新增筛选相关数据
				showSearchPopup: false, // 搜索弹窗是否显示
				tempHideSearchPopup: false, // 临时隐藏搜索弹窗的状态
				searchForm: {
					campus_name: '',
					name: '',
					phone_number: '',
					source: '',
					source_channel: '',
					course_search: '',
					attendance_type: '',
					deal_type: '',
					valid_type: '',
					communication_status: '',
					time_range: ''
				},
				
				// 选择器相关
				sourceIndex: 0,
				sourceOptions: ['全部', '线上', '线下'],
				attendanceIndex: 0,
				attendanceOptions: ['全部', '一访未到', '一访已到', '二访未到', '二访已到', '未到访'],
				dealIndex: 0,
				dealOptions: ['全部', '已成交', '未成交'],
				validIndex: 0,
				validOptions: ['全部', '有效', '无效'],
				communicationIndex: 0,
				communicationOptions: ['全部', '已沟通', '未沟通'],
				//筛选条件
				filteredData: {
					page: 1, //当前页码
					limit: 10, //每页返回数据条数
					total: 10, //数据总条数
					type: '0', //0=全部,1=待跟进,2=7天,3=30天
					is_gh: '2', //1=资源分配,2=我的客户
				},

				//顶部Tab栏
				values: [{
						id: 1,
						name: '我的客户'
					},
					// {
					//   id: 2,
					//   name: '资源分配'
					// }
				],
				segmented_type: 1, //当前选中的Tab栏目|1=我的客户,2=资源分配

				userInfo: {}, //用户信息
				//我的客户列表相关
				//筛选条件
				filteredData_1: {
					page: 1, //当前页码
					limit: 10, //每页返回数据条数
					total: 10, //数据总条数
					shared_by: '', //共享人ID|0=未分配
					shared_at_str: '', //共享时间|开始时间(Y-m-d)-结束时间(Y-m-d)
					shared_at_arr: [], //时间范围数组格式
					phone_number: '', //客户资源表-手机号
					name: '', //客户资源表-用户姓名
					campus_name: '', //校区名称
					source: '', //来源
					source_channel: '', //来源渠道
					attendance_type: '', //到课类型
					deal_type: '', //成交类型
					valid_type: '', //资源有效类型
				},
				//数据列表
				tableList_1: [], //表格数据

				//资源分配列表相关
				//筛选条件
				filteredData_2: {
					page: 1, //当前页码
					limit: 10, //每页返回数据条数
					total: 10, //数据总条数
					shared_by: '0', //共享人ID|0=未分配
					shared_at_str: '', //共享时间|[开始时间(Y-m-d),结束时间(Y-m-d)]
					phone_number: '', //客户资源表-手机号
					name: '', //客户资源表-用户姓名
					campus_name: '',
				},
				//数据列表
				tableList_2: [], //表格数据

				// 批量操作相关
				batchMode: false, // 是否进入批量操作模式
				selectedItems: [], // 已选择的资源ID列表
				isAllSelected: false, // 是否全选

				//员工select选择器
				select_show: false, //是否展示选择器
				select_item: {}, //当前选中的选项
				select_options: [
					// {
					//   text: '张三',
					//   value: '1',
					// }
				], //选择器可选项

				//时间选择器相关
				date_picker_show: false, //是否展示时间选择器

				//抽屉搜索条件展示窗相关
				showDrawer: false, //是否展示|false=否
				showDrawerForm: {
					campus_name: '',
					shared_at_str: '', //共享时间|[开始时间(Y-m-d),结束时间(Y-m-d)]
					phone_number: '', //客户资源表-手机号
					name: '', //客户资源表-用户姓名
				},


			}
		},
		onLoad(options) {},
		onShow() {
			this.init() //初始化
		},
		//下拉刷新
		async onPullDownRefresh() {
			//重置为第一页
			if (this.segmented_type == 1) {
				//我的客户
				await this.resetFilteredData_1()
				await this.getList_1()
			} else {
				//资源分配
				await this.resetFilteredData_2()
				await this.getList_2()
			}


		},
		methods: {
			getCurrentDate() {
				const now = new Date();
				const year = now.getFullYear();
				const month = String(now.getMonth() + 1).padStart(2, '0');
				const day = String(now.getDate()).padStart(2, '0');
				return `${year}-${month}-${day}`; // 示例：2024-04-15
			},
			//初始化
			async init() {
				await this.getUserInfo();
				if (this.segmented_type == 1) {
					await this.getList_1();
				} else {
					await this.getList_2();
				}
			},

			//获取用户信息
			async getUserInfo() {
				let res = await apiRoute.getPersonnelInfo({})
				if (res.code != 1) {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
					return
				}

				this.userInfo = res.data

				this.filteredData_1.shared_by = this.userInfo.id //共享人ID始终是当前登录的员工

				//判断用户是不是销售经理,是就展示"资源分配"
				if (this.userInfo.role_key_arr.includes('market_manager')) {
					this.values = [{
							id: 1,
							name: '我的客户'
						},
						{
							id: 2,
							name: '资源分配'
						}
					]
					//是经理的情况下,获取这个校区的全部用户(身份是销售的)
					// await this.getPersonnelAll();
				} else {
					this.values = [{
						id: 1,
						name: '我的客户'
					}, ]
				}
			},


			//获取全部员工
			async getPersonnelAll(campus = '') {
				let res = await apiRoute.common_getPersonnelAll({
					personnel_id: this.userInfo.id, //客户id
					account_type: 'market', //账号类型|teacher=老师,market=销售
					campus: campus
				})
				if (res.code != 1) {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
					return
				}

				let arr = []
				res.data.forEach((v, k) => {
					arr.push({
						text: v.name,
						value: v.id,
					})
				})
				this.select_options = arr
				console.log('员工', this.select_options)
			},

			//获取列表-我的客户相关
			//加载更多(下一页)
			loadMoreData_1() {
				//判断是否加载
				if (!this.isReachedBottom) {
					this.isReachedBottom = true; //设置为不可请求状态
					this.getList_1();
				}
			},
			//重置为第一页
			async resetFilteredData_1() {
				this.isReachedBottom = false; // 重置状态，以便下次触发加载更多

				this.filteredData_1.page = 1 //当前页码
				this.filteredData_1.limit = 10 //每页返回数据条数
				this.filteredData_1.total = 10 //数据总条数
			},
			//获取列表-我的客户
			async getList_1() {
				this.loading = true

				let data = {
					...this.filteredData_1
				}

				//判断是否还有数据
				if (this.filteredData_1.page * this.filteredData_1.limit > this.filteredData_1.total) {
					this.loading = false
					return
				}

				this.filteredData_1.shared_by = this.userInfo.id //共享人ID始终是当前登录的员工

				if (data.page == 1) {
					this.tableList_1 = []
				}

				let res = await apiRoute.xs_resourceSharingIndex(data)
				this.loading = false
				this.isReachedBottom = false;
				if (res.code != 1) {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
					return
				}

				this.tableList_1 = this.tableList_1.concat(res.data.data); // 使用 concat 方法 将新数据追加到数组中

				console.log('列表1', this.tableList_1)
				this.filteredData_1.total = res.data.total
				this.filteredData_1.page++

			},

			//获取列表-资源分配相关
			//加载更多(下一页)
			loadMoreData_2() {
				//判断是否加载
				if (!this.isReachedBottom) {
					this.isReachedBottom = true; //设置为不可请求状态
					this.getList_2();
				}
			},
			//重置为第一页
			async resetFilteredData_2() {
				this.isReachedBottom = false; // 重置状态，以便下次触发加载更多
				this.filteredData_2.page = 1 //当前页码
				this.filteredData_2.limit = 10 //每页返回数据条数
				this.filteredData_2.total = 10 //数据总条数
			},
			//获取列表-资源分配
			async getList_2() {
				this.loading = true

				let data = {
					...this.filteredData_2
				}

				//判断是否还有数据
				if (this.filteredData_2.page * this.filteredData_2.limit > this.filteredData_2.total) {
					this.loading = false
					return
				}

				if (data.page == 1) {
					this.tableList_2 = []
				}

				let res = await apiRoute.xs_resourceSharingIndex(data)
				this.loading = false
				this.isReachedBottom = false;
				if (res.code != 1) {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
					return
				}

				console.log(123123, res)

				this.tableList_2 = this.tableList_2.concat(res.data.data); // 使用 concat 方法 将新数据追加到数组中

				console.log('列表2', this.tableList_2)
				this.filteredData_2.total = res.data.total
				this.filteredData_2.page++
			},


			//切换tag列表
			async segmented(param) {
				this.segmented_type = param.id //1=我的客户,2=资源分配

				//重置筛选抽屉中的表单数据
				this.showDrawerForm.shared_at_str = '' //共享时间|[开始时间(Y-m-d),结束时间(Y-m-d)]
				this.showDrawerForm.phone_number = '' //客户资源表-手机号
				this.showDrawerForm.name = '' //客户资源表-用户姓名
				this.campus_name = ''
				if (this.segmented_type == 1) {
					this.filteredData_1.shared_at_str = '' //共享时间|[开始时间(Y-m-d),结束时间(Y-m-d)]
					this.filteredData_1.phone_number = '' //客户资源表-手机号
					this.filteredData_1.name = '' //客户资源表-用户姓名
					this.filteredData_1.campus_name = ''

					//我的客户
					//重置为第一页
					await this.resetFilteredData_1()
					await this.getList_1()
				} else {
					this.filteredData_2.shared_at_str = '' //共享时间|[开始时间(Y-m-d),结束时间(Y-m-d)]
					this.filteredData_2.phone_number = '' //客户资源表-手机号
					this.filteredData_2.name = '' //客户资源表-用户姓名
					this.filteredData_2.campus_name = ''
					//资源分配
					//重置为第一页
					await this.resetFilteredData_2()
					await this.getList_2()
				}

			},

			//跳转页面-我的消息
			openViewMyMessage(item) {
				let from_id = this.userInfo.id //发送者的id
				let to_id = item.customerResource.id //接收者ID
				this.$navigateTo({
					url: `/pages/common/im_chat_info?from_id=${from_id}&to_id=${to_id}`
				})
			},

			//拨打电话
			async dialTel(item) {

				let tel = item.customerResource.phone_number

				if (!tel) {
					uni.showToast({
						title: '电话号码为空',
						icon: 'none'
					});
					return;
				}

				uni.makePhoneCall({
					phoneNumber: tel,
					success: () => {
						let param = {
							staff_id: this.userInfo.id, //员工id
							resource_id: item.customerResource.id, //资源ID
							resource_type: '', //资源类型（如设备、文件、系统等）
							communication_type: 'phone', //沟通类型: phone-电话, email-邮件, meeting-会议, other-其他
							communication_result: 'success', //沟通结果: success-成功, failure-失败, pending-待定
							remarks: null, //备注
							tag: null, //标签
						}
						apiRoute.xs_communicationRecordsAdd(param).then(ressuccess=>{
							if(ressuccess.code != 1){
								uni.showToast({
									title: res.msg,
									icon: 'none'
								})
							}
						})
					}
				})
			},

			//客户详情
			clue_info(item) {
				let resource_sharing_id = item.id ///共享资源表id
				this.$navigateTo({
					url: `/pages/market/clue/clue_info?resource_sharing_id=${resource_sharing_id}`
				})
			},

			//公海-分配客户给员工相关
			//打开分配列表
			openAssign(item) {
				console.log(item)
				this.getPersonnelAll(item.customerResource.campus)
				this.select_item = item
				this.select_show = true
			},
			//关闭分配列表
			closeAssign(item) {
				this.select_item = {}
				this.select_show = false
				this.select_options = this.select_options.map(v => ({
					...v,
					checked: false
				}));
			},
			//公海-分配客户给员工（支持单个和批量）
			async getSales(e) {
				let select_item = {
					...this.select_item
				}
				console.log('选中', e, select_item)
				this.closeAssign() //关闭选择窗
				
				// 检查是否为批量操作
				if (select_item.isBatch && select_item.selectedIds) {
					// 批量分配
					await this.batchAssignResources(select_item.selectedIds, e.options.value)
				} else {
					// 单个分配
					let param = {
						resource_sharing_id: select_item.id, //共享资源表id
						shared_by: e.options.value, //分给员工的id
					}
					let res = await apiRoute.xs_resourceSharingAssign(param)
					if (res.code != 1) {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						return
					}
					uni.showToast({
						title: res.msg,
						icon: 'success'
					})
					//延迟1s执行
					setTimeout(() => {
						let param = {
							id: 2,
							index: 1,
							name: "资源分配",
						}
						this.segmented(param)
					}, 1000)
				}
			},
			
			// 批量分配资源
			async batchAssignResources(selectedIds, assigneeId) {
				let successCount = 0
				let failCount = 0
				
				for (let resourceId of selectedIds) {
					try {
						let param = {
							resource_sharing_id: resourceId,
							shared_by: assigneeId
						}
						let res = await apiRoute.xs_resourceSharingAssign(param)
						if (res.code == 1) {
							successCount++
						} else {
							failCount++
						}
					} catch (error) {
						failCount++
					}
				}
				
				// 显示批量操作结果
				if (failCount === 0) {
					uni.showToast({
						title: `成功分配 ${successCount} 个资源`,
						icon: 'success'
					})
				} else {
					uni.showToast({
						title: `成功分配 ${successCount} 个，失败 ${failCount} 个`,
						icon: 'none'
					})
				}
				
				// 退出批量模式并刷新列表
				this.exitBatchMode()
				setTimeout(() => {
					let param = {
						id: 2,
						index: 1,
						name: "资源分配",
					}
					this.segmented(param)
				}, 1000)
			},

			//时间选择器相关
			//监听-时间选择器结果
			changeDatePicker(e) {
				console.log('时间', e)
				let time_range = `${e.startDate.result} ~ ${e.endDate.result}`
				this.searchForm.time_range = time_range
				this.showDrawerForm.shared_at_str = time_range
				this.cancelDatePicker()
			},
			//打开时间选择器
			openDatePicker() {
				console.log('打开时间选择器')
				this.date_picker_show = true
				// 临时隐藏搜索弹窗，避免层级冲突
				this.tempHideSearchPopup = this.showSearchPopup
				this.showSearchPopup = false
			},
			//关闭时间选择
			cancelDatePicker() {
				this.date_picker_show = false
				// 恢复搜索弹窗显示状态
				if (this.tempHideSearchPopup) {
					this.showSearchPopup = true
					this.tempHideSearchPopup = false
				}
			},

			//检索数据
			async searchData() {
				if (this.segmented_type == 1) {
					this.filteredData_1.shared_at_str = this.showDrawerForm.shared_at_str //共享时间|[开始时间(Y-m-d),结束时间(Y-m-d)]
					this.filteredData_1.phone_number = this.showDrawerForm.phone_number //客户资源表-手机号
					this.filteredData_1.name = this.showDrawerForm.name //客户资源表-用户姓名
					this.filteredData_1.campus_name = this.showDrawerForm.campus_name //客户资源表-用户姓名
					//我的客户
					await this.resetFilteredData_1()
					await this.getList_1()
				} else {
					this.filteredData_2.shared_at_str = this.showDrawerForm.shared_at_str //共享时间|[开始时间(Y-m-d),结束时间(Y-m-d)]
					this.filteredData_2.phone_number = this.showDrawerForm.phone_number //客户资源表-手机号
					this.filteredData_2.name = this.showDrawerForm.name //客户资源表-用户姓名
					this.filteredData_2.campus_name = this.showDrawerForm.campus_name //客户资源表-用户姓名
					//资源分配
					await this.resetFilteredData_2()
					await this.getList_2()
				}
				this.closeShowDrawer()
			},

			//打开抽屉筛选弹窗
			openShowDrawer() {
				this.showDrawer = true
			},
			//关闭抽屉筛选弹窗
			closeShowDrawer() {
				this.showDrawer = false
			},
			
			// 新增筛选方法
			// 打开搜索弹窗
			openSearchPopup() {
				console.log('打开搜索弹窗', this.showSearchPopup)
				this.showSearchPopup = true
				console.log('设置后的状态', this.showSearchPopup)
			},
			
			// 搜索并关闭弹窗
			searchDataAndClose() {
				console.log('执行搜索，表单数据:', this.searchForm)
				this.searchDataFromPopup()
				this.showSearchPopup = false
			},
			
			// 从弹窗搜索数据
			async searchDataFromPopup() {
				// 根据当前标签页选择对应的筛选数据对象
				const currentFilterData = this.segmented_type == 1 ? this.filteredData_1 : this.filteredData_2
				
				// 将弹窗搜索表单数据映射到筛选数据
				currentFilterData.campus_name = this.searchForm.campus_name
				currentFilterData.name = this.searchForm.name
				currentFilterData.phone_number = this.searchForm.phone_number
				
				// 处理时间范围：转换为后端需要的shared_at_arr格式
				if (this.searchForm.time_range && this.searchForm.time_range.includes(' ~ ')) {
					const timeArray = this.searchForm.time_range.split(' ~ ')
					currentFilterData.shared_at_arr = timeArray
					currentFilterData.shared_at_str = this.searchForm.time_range
				} else {
					currentFilterData.shared_at_arr = []
					currentFilterData.shared_at_str = ''
				}
				
				// 处理其他搜索条件（如果后端支持）
				if (this.searchForm.source && this.searchForm.source !== '全部') {
					currentFilterData.source = this.searchForm.source
				}
				if (this.searchForm.source_channel) {
					currentFilterData.source_channel = this.searchForm.source_channel
				}
				if (this.searchForm.attendance_type && this.searchForm.attendance_type !== '全部') {
					currentFilterData.attendance_type = this.searchForm.attendance_type
				}
				if (this.searchForm.deal_type && this.searchForm.deal_type !== '全部') {
					currentFilterData.deal_type = this.searchForm.deal_type
				}
				if (this.searchForm.valid_type && this.searchForm.valid_type !== '全部') {
					currentFilterData.valid_type = this.searchForm.valid_type
				}
				
				console.log('映射后的筛选数据:', currentFilterData)
				
				// 根据当前标签页重置数据并获取列表
				if (this.segmented_type == 1) {
					// 我的客户
					await this.resetFilteredData_1()
					await this.getList_1()
				} else {
					// 资源分配
					await this.resetFilteredData_2()
					await this.getList_2()
				}
			},
			
			// 重置搜索并关闭弹窗
			resetSearchAndClose() {
				this.resetSearch()
				this.showSearchPopup = false
			},
			
			// 仅重置搜索（不关闭弹窗）
			resetSearchOnly() {
				this.resetSearch()
			},
			
			// 关闭搜索弹窗
			closeSearchPopup() {
				this.showSearchPopup = false
			},
			
			// 来源选择器变化
			onSourceChange(e) {
				this.sourceIndex = e.detail.value
				this.searchForm.source = this.sourceOptions[this.sourceIndex]
				// 如果不是线上，清空来源渠道
				if (this.sourceIndex !== 1) {
					this.searchForm.source_channel = ''
				}
			},
			
			// 到课类型选择器变化
			onAttendanceChange(e) {
				this.attendanceIndex = e.detail.value
				this.searchForm.attendance_type = this.attendanceOptions[this.attendanceIndex]
			},
			
			// 成交类型选择器变化
			onDealChange(e) {
				this.dealIndex = e.detail.value
				this.searchForm.deal_type = this.dealOptions[this.dealIndex]
			},
			
			// 资源有效类型选择器变化
			onValidChange(e) {
				this.validIndex = e.detail.value
				this.searchForm.valid_type = this.validOptions[this.validIndex]
			},
			
			// 沟通情况选择器变化
			onCommunicationChange(e) {
				this.communicationIndex = e.detail.value
				this.searchForm.communication_status = this.communicationOptions[this.communicationIndex]
			},
			
			// 重置搜索
			async resetSearch() {
				this.searchForm = {
					campus_name: '',
					name: '',
					phone_number: '',
					source: '',
					source_channel: '',
					course_search: '',
					attendance_type: '',
					deal_type: '',
					valid_type: '',
					communication_status: '',
					time_range: ''
				}
				this.sourceIndex = 0
				this.attendanceIndex = 0
				this.dealIndex = 0
				this.validIndex = 0
				this.communicationIndex = 0
				
				// 重置当前标签页的筛选数据
				if (this.segmented_type == 1) {
					// 我的客户
					await this.resetFilteredData_1()
					await this.getList_1()
				} else {
					// 资源分配
					await this.resetFilteredData_2()
					await this.getList_2()
				}
			},
			
			// 获取开单状态样式类
			getOrderStatusClass(status) {
				return status === '已开单' ? 'status-closed' : 'status-open'
			},
			
			// 获取到访状态样式类
			getVisitStatusClass(status) {
				return status === '已到' ? 'visit-arrived' : 'visit-not-arrived'
			},
			
			// 批量操作相关方法
			// 进入批量操作模式
			enterBatchMode() {
				this.batchMode = true
				this.selectedItems = []
				this.isAllSelected = false
			},
			
			// 退出批量操作模式
			exitBatchMode() {
				this.batchMode = false
				this.selectedItems = []
				this.isAllSelected = false
			},
			
			// 切换单个项目的选择状态
			toggleItemSelection(item) {
				const index = this.selectedItems.indexOf(item.id)
				if (index > -1) {
					this.selectedItems.splice(index, 1)
				} else {
					this.selectedItems.push(item.id)
				}
				this.updateAllSelectedState()
			},
			
			// 切换全选状态
			toggleSelectAll() {
				if (this.isAllSelected) {
					this.selectedItems = []
					this.isAllSelected = false
				} else {
					this.selectedItems = this.tableList_2.map(item => item.id)
					this.isAllSelected = true
				}
			},
			
			// 更新全选状态
			updateAllSelectedState() {
				this.isAllSelected = this.selectedItems.length === this.tableList_2.length && this.tableList_2.length > 0
			},
			
			// 批量分配
			batchAssign() {
				if (this.selectedItems.length === 0) {
					uni.showToast({
						title: '请先选择要分配的资源',
						icon: 'none'
					})
					return
				}
				// 获取第一个选择项目的校区信息用于获取员工列表
				const firstSelectedItem = this.tableList_2.find(item => this.selectedItems.includes(item.id))
				if (firstSelectedItem) {
					this.getPersonnelAll(firstSelectedItem.customerResource.campus)
					this.select_item = { isBatch: true, selectedIds: this.selectedItems }
					this.select_show = true
				}
			}
		}
	}
</script>

<style lang="less" scoped>
	.assemble {
		width: 100%;
		height: 100vh;
		background-color: #292929;
		overflow: auto;

		.search_section {
			margin-top: 20rpx;
			padding: 10rpx 20rpx;
			display: flex;
			flex-direction: column;

			.item {
				height: 60rpx;
				display: flex;
				justify-content: center;

				.input_box {
					width: 75%;

					.input_box_text {
						height: 60rpx;
						line-height: 60rpx;
						background-color: #fff;
						padding-left: 20rpx;
						color: #cccccc;
						font-size: 28rpx;
					}
				}
			}
		}
	}
	
	// 搜索弹窗样式
	.search_popup_mask {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background: rgba(0, 0, 0, 0.6);
		z-index: 999;
		display: flex;
		flex-direction: column;
	}
	
	.search_popup_content {
		background: #fff;
		border-bottom-left-radius: 24rpx;
		border-bottom-right-radius: 24rpx;
		animation: slideDown 0.3s ease-out;
		width: 100%;
		max-height: 80vh;
		overflow: hidden;
		display: flex;
		flex-direction: column;
	}
	
	@keyframes slideDown {
		from {
			transform: translateY(-100%);
		}
		to {
			transform: translateY(0);
		}
	}
	
	// 弹窗搜索内容样式
	.popup_search_content {
		padding: 0;
		background: #fff;
		min-height: 60vh;
		max-height: 80vh;
		display: flex;
		flex-direction: column;
		border-bottom-left-radius: 24rpx;
		border-bottom-right-radius: 24rpx;
		overflow: hidden;
	}
	
	.popup_header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 32rpx;
		border-bottom: 1px solid #f0f0f0;
	}
	
	.popup_title {
		font-size: 32rpx;
		font-weight: 600;
		color: #333;
	}
	
	.popup_close {
		width: 60rpx;
		height: 60rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		
		.close_text {
			font-size: 32rpx;
			color: #999;
		}
	}
	
	.popup_scroll_view {
		flex: 1;
		padding: 32rpx;
		overflow-y: auto;
	}
	
	.popup_filter_section {
		margin-bottom: 32rpx;
		
		&:last-child {
			margin-bottom: 0;
		}
	}
	
	.popup_filter_row {
		display: flex;
		gap: 20rpx;
		margin-bottom: 24rpx;
		
		&:last-child {
			margin-bottom: 0;
		}
	}
	
	.popup_filter_item {
		flex: 1;
		display: flex;
		flex-direction: column;
		gap: 12rpx;
		
		&.full_width {
			flex: 1;
		}
		
		.popup_filter_label {
			font-size: 26rpx;
			color: #666;
			font-weight: 500;
		}
		
		.popup_filter_input {
			height: 72rpx;
			line-height: 72rpx;
			padding: 0 16rpx;
			border: 1px solid #ddd;
			border-radius: 8rpx;
			font-size: 28rpx;
			color: #333;
			background: #fff;
			
			&::placeholder {
				color: #999;
			}
		}
		
		.popup_filter_picker {
			height: 72rpx;
			line-height: 72rpx;
			padding: 0 16rpx;
			border: 1px solid #ddd;
			border-radius: 8rpx;
			font-size: 28rpx;
			color: #333;
			background: #fff;
			position: relative;
			
			&::after {
				content: '▼';
				position: absolute;
				right: 16rpx;
				font-size: 20rpx;
				color: #999;
			}
		}
	}
	
	.popup_filter_buttons {
		display: flex;
		gap: 20rpx;
		padding: 32rpx;
		margin-top: auto;
		border-top: 1px solid #f0f0f0;
		background: #fff;
		border-bottom-left-radius: 24rpx;
		border-bottom-right-radius: 24rpx;
	}
	
	.popup_filter_btn {
		flex: 1;
		height: 72rpx;
		line-height: 72rpx;
		text-align: center;
		border-radius: 8rpx;
		font-size: 28rpx;
		font-weight: 600;
		
		&.search_btn {
			background: #29d3b4;
			color: #fff;
		}
		
		&.reset_btn {
			background: #f5f5f5;
			color: #666;
			border: 1px solid #ddd;
		}
		
		&.close_btn {
			background: #666;
			color: #fff;
		}
	}

	.search {
		width: 92%;
		margin: auto;
		display: flex;
		margin-top: 20rpx;
	}

	.selected {
		background: #294e48;
		border-radius: 16rpx;
		padding: 12rpx 8rpx;
		text-align: center;
		color: #29d3b4;
		margin-left: 5rpx;
	}

	.not-selected {
		margin-left: 5rpx;
		background: #ffffff;
		border: 4rpx #959595 solid;
		border-radius: 16rpx;
		padding: 12rpx 8rpx;
		text-align: center;
	}

	.card {
		width: 92%;
		margin: 8rpx auto;
		background: #434544;
		border-radius: 16rpx;
		display: flex;
		flex-direction: column;
		
		.card-content {
			display: flex;
			justify-content: space-between;
			
			.card-left {
				flex: 1;
				padding-right: 10rpx;
			}
			
			.card-right {
				min-width: 120rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: flex-start;
				padding: 12rpx;
				
				// 开单状态标签
				.status-tag {
					font-size: 20rpx;
					padding: 8rpx 16rpx;
					border-radius: 16rpx;
					color: #fff;
					margin-bottom: 8rpx;
					text-align: center;
					min-width: 60rpx;
					
					&.status-closed {
						background-color: #52c41a; // 绿色-已开单
					}
					
					&.status-open {
						background-color: #ff4d4f; // 红色-未开单
					}
				}
				
				// 到访状态容器
				.visit-status {
					display: flex;
					flex-direction: column;
					gap: 6rpx;
					margin-bottom: 12rpx;
					
					.visit-tag {
						font-size: 18rpx;
						padding: 6rpx 12rpx;
						border-radius: 12rpx;
						color: #fff;
						text-align: center;
						min-width: 60rpx;
						
						&.visit-arrived {
							background-color: #1890ff; // 蓝色-已到
						}
						
						&.visit-not-arrived {
							background-color: #8c8c8c; // 灰色-未到
						}
					}
				}
				
				.btn-item {
					margin-bottom: 12rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					width: 80rpx;
					height: 80rpx;
					background-color: rgba(255, 255, 255, 0.1);
					border-radius: 50%;
					
					&:last-child {
						margin-bottom: 0;
					}
					
					.image {
						width: 60rpx;
						height: 60rpx;
					}
				}
			}
		}
		
		.card-footer {
			width: 100%;
			border-top: 1rpx solid rgba(255, 255, 255, 0.1);
			padding: 12rpx;
			box-sizing: border-box;
			background-color: rgba(0, 0, 0, 0.1);
			border-radius: 0 0 16rpx 16rpx;
		}
	}

	.card-image {
		width: 60rpx;
		height: 60rpx;
	}

	.card-text {
		font-size: 32rpx;
		padding-left: 16rpx;
		color: #fff;
	}

	.card-label {
		margin-left: 30rpx;
		background: #76575a;
		width: 130rpx;
		padding: 8rpx 20rpx;
		color: #c9595a;
		text-align: center;
		border-radius: 0 50rpx 50rpx 40rpx;
	}

	.card-con {
		font-size: 30rpx;
		padding: 8rpx 10rpx 8rpx 16rpx;
		color: #fff;
	}

	.card-con-span {
		font-size: 24rpx;
		color: #a47332;
		padding-left: 12rpx;
	}

	.drop-image-x {
		width: 20rpx;
		height: 20rpx;
	}

	.title-x {
		font-size: 28rpx;
		color: #fff;
		padding-left: 10rpx;
	}

	.card-date {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.image {
		width: 60rpx;
		height: 60rpx;
	}

	.neck {
		width: 60rpx;
		height: 60rpx;
		border-radius: 50%;
		background: #f59a23;
		color: #fff;
		line-height: 60rpx;
		text-align: center;
	}

	//领取按钮
	.ling {
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 10rpx;
		color: #fff;
		width: 100rpx;
		padding: 10rpx 0;
		background-color: #F59A23;
	}

	//抽屉搜索栏
	.fui-scroll__view {
		margin-top: 60rpx;
		width: 520rpx;
		flex: 1;
		overflow: hidden;

		.fui-title {
			padding: 40rpx 32rpx;
			padding-bottom: 20rpx;
			box-sizing: border-box;
			font-weight: bold;
		}

		.drawer_box {
			.item {
				display: flex;
				flex-direction: column;
				gap: 15rpx;

				.title {}

				.input_box {
					border: 1px solid #292929;
					width: 450rpx;

					::v-deep .fui-input__wrap {
						padding: 10rpx !important;
						height: 100%;
					}
				}
			}
		}

		.fui-btn__box {
			padding: 40rpx 0;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			gap: 30rpx;
		}

	}

	.button {
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 5rpx;
		font-size: 24rpx;
		color: #fff;
		background-color: #29d3b4;
		width: 20%;
		height: 100%;


	}

	.clientbtn {
		width: 200px;
		height: 40px;
		font-size: 16px;
		border-radius: 5px;
	}

	/* 批量操作样式 - 优化为黑色主题 */
	.batch_control_bar {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 20rpx 24rpx;
		background: rgba(41, 41, 41, 0.95);
		border-top: 1px solid #404040;
		margin-top: 20rpx;
		border-radius: 12rpx;
		backdrop-filter: blur(10rpx);
	}

	.batch_actions_left {
		display: flex;
		align-items: center;
		gap: 24rpx;
	}

	.batch_toggle_btn {
		padding: 16rpx 32rpx;
		border-radius: 24rpx;
		font-size: 28rpx;
		background: linear-gradient(135deg, #29d3b4, #26c3a4);
		color: #fff;
		box-shadow: 0 4rpx 12rpx rgba(41, 211, 180, 0.3);
		transition: all 0.3s ease;
		
		&.cancel {
			background: linear-gradient(135deg, #666, #555);
			box-shadow: 0 4rpx 12rpx rgba(102, 102, 102, 0.3);
		}
	}

	.batch_select_all {
		display: flex;
		align-items: center;
		gap: 12rpx;
		padding: 8rpx 0;
	}

	.checkbox {
		width: 36rpx;
		height: 36rpx;
		border: 2rpx solid #666;
		border-radius: 8rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 24rpx;
		color: transparent;
		background: #404040;
		transition: all 0.3s ease;
		
		&.checked {
			background: linear-gradient(135deg, #29d3b4, #26c3a4);
			border-color: #29d3b4;
			color: #fff;
			box-shadow: 0 2rpx 8rpx rgba(41, 211, 180, 0.4);
		}
	}

	.select_all_text {
		font-size: 28rpx;
		color: #fff;
		font-weight: 500;
	}

	.batch_actions_right {
		display: flex;
		align-items: center;
	}

	.batch_assign_btn {
		padding: 16rpx 32rpx;
		border-radius: 24rpx;
		font-size: 28rpx;
		background: linear-gradient(135deg, #ff6b35, #ff5722);
		color: #fff;
		box-shadow: 0 4rpx 12rpx rgba(255, 107, 53, 0.3);
		font-weight: 600;
		transition: all 0.3s ease;
	}

	.batch_checkbox {
		margin-right: 20rpx;
		padding: 8rpx;
	}

	/* 分配状态样式 */
	.assigned-to {
		font-weight: 600;
		
		&.unassigned {
			color: #ff6b35;
		}
		
		&.assigned {
			color: #29d3b4;
		}
	}
</style>