<template>
	<view class="border-box">
		<view class="content">
			<view class="top-box">
				<view class="back-box x-f">
					<image src="/static/image/device/back_icon32.png"></image>
					<view @click="goBack()">返回</view>
				</view>
				<view class="tab-box">
					<view v-for="(item,index) in tabs" :key="index" :class="aIndex==item.val?'item active':'item'"
						@click="getTab(item)">
						{{item.name}}
					</view>
				</view>
				<view :style="{width: width < height ?'18px':'138px'}"></view>
			</view>
			<view class="btn-box x-bc" :class="{ 'flex-box': width < height }">
				<view class="x-f dl">
					<view class="search">
						<image src="/static/image/device/search_icon25.png"></image>
						<input type="text" placeholder="请输入客户姓名或者手机号" v-model="keyword" @confirm="rightTap" />
					</view>
					<view class="dateBox">
						<picker mode="date" @change="createTimesChange" style="width: 100%;">
							<view class="date x-bc">
								<text class="text" :style="{color:createTimes!=='' ?'#222':'#999'}">
									{{createTimes!=='' ? createTimes: '请选择服务时间'}}
								</text>
								<image src="/static/image/device/calendar_icon24.png"></image>
							</view>
						</picker>
					</view>
				</view>
				<view class="x-f dr">
					<view class="rest" @click="clear">重置</view>
					<view class="export" @click="toExport">
						<image src="/static/image/device/cp_download_icon24.png"></image>
						导出
					</view>
				</view>
			</view>
			<view class="user-list" :class="{ 'flex-box': width < height }">
				<view class="table-title x-f">
					<view class="col col-1">序号</view>
					<view class="col col-2">客户账号</view>
					<view class="col col-2">名称</view>
					<view class="col col-1">性别</view>
					<!-- <view class="col col-2">客户关系</view> -->
					<!-- <view class="col col-1">年龄</view> -->
					<view class="col col-2">手机号</view>
					<view class="col col-3">上次服务时间</view>
					<view class="col col-2">服务类型</view>
					<view class="col col-2">推荐产品</view>
					<view class="col col-3">操作</view>
				</view>

				<view class="table-item" v-for="(item,index) in dataList" :key="index">
					<view class="col col-1">{{index+1}}</view>
					<view class="col col-2">{{item.username||'--'}}</view>
					<view class="col col-2">{{item.nickName||'--'}}</view>
					<view class="col col-1">{{item.sex==1?"男":"女"}}</view>
					<!-- <view class="col col-2">--</view> -->
					<!-- <view class="col col-1">{{item.birthday||'--'}}</view> -->
					<view class="col col-2">{{item.phone||'--'}}</view>
					<view class="col col-3">{{item.createTimes||'--'}}</view>
					<view class="col col-2">体质辨识</view>
					<view class="col col-2">{{item.product||'无'}}</view>
					<view class="col col-3 btn-box">
						<view class="btn" @click="goList(item.userId)">体质辨识</view>
						<view class="btn">开方详情</view>
					</view>
				</view>

			</view>
			<view v-if="showDetail" class="mask">
				<view class="popup-container2">
					<view class="title-t">温馨提示</view>
					<view class="title-r">检测报告请下载芳华未来APP查看</view>
					<view class="btn-r" @click="closeP">确认</view>
				</view>
			</view>
			<view v-if="showList" class="mask">
				<!-- 弹窗内容 -->
				<view class="popup-container">
					<!-- 弹窗头部 -->
					<view class="popup-header">
						<text class="popup-title">选择体质辨识</text>
						<view class="close-btn" @click="onCloseList">
							<image src="/static/image/device/close_icon32.png"></image>
						</view>
					</view>
					<!-- 弹窗主体内容 -->
					<view class="popup-content">
						<view class="userList-box">
							<view class="userList" v-if="reportList.length>0">
								<view class="item" v-for="(item,index) in reportList" :key="index">
									<view class="title x-f">
										<view>报告日期：{{item.createTimes||''}}</view>
										<view style="margin-left: 30px;">报告编号：{{item.numberId||''}}</view>
									</view>
									<view class="right-box">
										<view class="rest" @click="onOpenDetail(item)">
											查看
										</view>
									</view>
								</view>
							</view>

						</view>
						<view v-if="reportList.length==0" class="isNull">暂无数据</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	import {
		selectUsernameOrPhoneOrTime,
		getExport,
		selectNotGeneratedReport,
		selectSuccessGeneratedReport
	} from '@/api/user.js'
	import {
		getDictByKey
	} from '@/api/common.js'
	import {
		tr
	} from "date-fns/locale";
	export default {
		mixins: [MescrollMixin], // 使用mixin
		data() {
			return {
				height: uni.getSystemInfoSync().screenHeight,
				width: uni.getSystemInfoSync().screenWidth,
				statusBarHeight: uni.getSystemInfoSync().statusBarHeight,
				hosLevelOptions: [],
				tabs: [{
						val: '',
						name: '全部客户'
					},
					{
						val: '0',
						name: '待问诊客户'
					},
					{
						val: '1',
						name: '购买客户'
					},

				],
				total: 10,
				aIndex: '',
				createTimes: '',
				keyword: '',
				type: 0,
				mescroll: null,
				downOption: { //下拉刷新
					use: true,
					auto: false // 不自动加载 (mixin已处理第一个tab触发downCallback)
				},
				upOption: {
					onScroll: false,
					use: true, // 是否启用上拉加载; 默认true
					page: {
						pae: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 10, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					textNoMore: "已经到底了",
					empty: {
						icon: 'https://cos.his.cdwjyyh.com/fs/20240423/cf4a86b913a04341bb44e34bb4d37aa2.png',
						tip: '暂无数据'
					}
				},
				dataList: [],
				reportList: [],
				showList: false,
				showDetail: false
			}
		},
		onLoad(options) {
			this.selectUsernameOrPhoneOrTime()
			//this.getDictByKey("sys_hospital_level");
		},
		onShow() {

		},
		methods: {
			goBack() {
				// uni.navigateBack({
				// 	delta:1
				// })	
				// uni.redirectTo({
				// 	url: '/pages/index/index'
				// });
				uni.navigateBack({
				  delta:1
				})
				//设置50ms延时跳转目标界面
				// setTimeout(() => {
				// 	uni.navigateTo({
				// 	 url: '/pages/index/index',
				// 	})
				// }, 50);
			},
			onCloseList() {
				this.showList = false
			},
			onOpenDetail(item) {
				this.showDetail = true
				this.showList = false
			},
			closeP() {
				this.showDetail = false
			},
			getTab(item) {
				this.aIndex = item.val
				if (item.val == '0') {
					this.selectNotGeneratedReport()
				} else {
					this.selectUsernameOrPhoneOrTime()
				}
			},
			rightTap(e) {
				this.keyword = e.target.value
				this.selectUsernameOrPhoneOrTime()
			},
			clear() {
				this.keyword = ''
				this.createTimes = ''
				this.selectUsernameOrPhoneOrTime()
			},
			createTimesChange(e) {
				console.log('e', e)
				this.createTimes = e.detail.value
				this.selectUsernameOrPhoneOrTime()
			},
			goList(id) {
				var data = {
					userId: id
				}
				selectSuccessGeneratedReport(data).then(res => {
						if (res.code == 200) {
							this.reportList = res.data
							this.reportList.forEach(item => {
							  item.createTimes = this.$timeFormat(item.createTimes, 'yyyy年mm月dd日')
							})
							this.showList = true
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
						}
					},
					rej => {}
				);

			},
			selectNotGeneratedReport() {
				var data = {
					nickName: this.keyword,
					phone: this.keyword,
					createTimes: this.createTimes
				}
				selectNotGeneratedReport(data).then(res => {
						if (res.code == 200) {
							console.log('res', res)
							this.dataList = res.data
							this.total = res.data.length
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
						}
					},
					rej => {}
				);
			},
			selectUsernameOrPhoneOrTime() {
				var data = {
					nickName: this.keyword,
					phone: this.keyword,
					createTimes: this.createTimes
				}
				// var user=uni.getStorageSync('userInfo');
				selectUsernameOrPhoneOrTime(data).then(res => {
						if (res.code == 200) {
							console.log('res', res)
							this.dataList = res.data
							this.total = res.data.length
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
						}
					},
					rej => {}
				);
			},
			toExport() {
				var data = {
					nickName: this.keyword,
					phone: this.keyword,
					createTimes: this.createTimes
				}
				// var user=uni.getStorageSync('userInfo');
				getExport(data).then(res => {
						if (res.code == 200) {
							uni.showToast({
								icon: 'success',
								title: "导出成功",
							});
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
						}
					},
					rej => {}
				);
			},
			typeChange(item) {
				this.type = item.index
				console.log(this.type)
				this.mescroll.resetUpScroll()
			},
			navTo(url) {
				uni.navigateTo({
					url: url
				})
			},
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			/*下拉刷新的回调 */
			downCallback() {
				this.mescroll.resetUpScroll()
			},
			/*上拉加载的回调*/
			upCallback(page) {
				//联网加载数据
				var that = this;
				var data = {
					type: this.type + 1,
					pageNum: page.num,
					pageSize: page.size
				};
				getMyDoctorList(data).then(res => {
					if (res.code == 200) {
						if (page.num == 1) {
							res.data.list.forEach(function(value, index, array) {
								value.prices = JSON.parse(value.priceJson)
							});
							that.dataList = res.data.list;

						} else {
							that.dataList = that.dataList.concat(res.data.list);

						}
						that.mescroll.endBySize(res.data.list.length, res.data.total);

					} else {
						uni.showToast({
							icon: 'none',
							title: "请求失败",
						});
						that.dataList = null;
						that.mescroll.endErr();
					}
				});
			},

		}
	}
</script>

<style lang="scss">
	page {

		height: 100%;
		background: #f6f6f6;
	}
</style>
<style scoped lang="scss">
	.border-box {
		width: 100vw;
		height: 100vh;
		overflow: hidden;
		background-image: url(/static/image/device/ipad_yjf_boder.png);
		background-repeat: no-repeat;
		position: relative;
		background-size: 100% 100%;
		box-sizing: border-box;
		padding: 10px;
	}

	.content {
		// height: 100%;
		width: 100%;
		height: 100%;
		background-image: url(/static/image/device/inner_page_bg.png);
		background-repeat: no-repeat;
		background-size: 100% 100%;
		position: relative;
		padding: 30px;
		.top-box {
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-bottom: 50px;

	.back-box {
		width: 102px;
		height: 47px;
		background: #FFFFFF;
		border-radius: 6px 6px 6px 6px;
		border: 1px solid #8F6726;
		// padding: 12px 8px;
		justify-content: center;
		view {
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 18px;
			color: #8F6726;
			text-align: center;
			margin-left: 10px;
		}
		// position: absolute;
		// left: 30px;
		image {
			width: 24px;
			height: 24px;
		}
	}

			.tab-box {
				display: flex;
				align-items: center;
				justify-content: center;

				.item {
					display: flex;
					align-items: center;
					justify-content: center;
					width: 144px;
					height: 43px;
					background: #FBF4EE;
					border-radius: 4px;
					border: 1px solid rgba(143,103,38,0.3);
					font-family: Source Han Serif CN, Source Han Serif CN;
					font-weight: 500;
					font-size: 24px;
					color: #8F6726;
					text-align: center;
					margin-right: 26px;

					&.active {
						background: linear-gradient(180deg, #E3B379 0%, #8F6726 100%);
						border: 1px solid #8F6726;
				font-weight: bold;
						font-size: 24px;
						color: #FFFFFF;
					}

					&:last-child {
						margin-right: 0;
					}
				}
			}
		}

		.btn-box {
			&.flex-box{
				flex-direction: column;
				.dl{
					width: 100%;
					justify-content: left;
				}
				.dr{
					margin-top: 20px;
					width: 100%;
					justify-content: right;
				}
				
			}
			image {
				width: 18px;
				height: 18px;
			}

			input {
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 18px;
				color: #9B9B9B;
				text-align: left;
			}

			.search {
				width: 311px;
				height: 47px;
				background: #FFFFFF;
				border-radius: 6px 6px 6px 6px;
				border: 1px solid #8F6726;
				display: flex;
				align-items: center;
				padding: 15px;
				margin-right: 16px;
				box-sizing: border-box;
                uni-input{
					width: 100%;
				}
				image {
					margin-right: 10px;
				}
			}

			.dateBox {
				width: 230px;
				height: 47px;
				background: #FFFFFF;
				border-radius: 6px 6px 6px 6px;
				border: 1px solid #8F6726;
				padding: 15px;
				display: flex;
				align-items: center;
				justify-content: space-between;

				image {
					margin-left: 10px;
				}

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

				.text {
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 18px;
					text-align: left;
				}
			}

			.rest {
				width: 83px;
				height: 47px;
				background: #FFFFFF;
				border-radius: 6px 6px 6px 6px;
				border: 1px solid #8F6726;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size:18px;
				color: #8F6726;
				display: flex;
				align-items: center;
				justify-content: center;
			}

			.export {
				display: flex;
				align-items: center;
				justify-content: center;
				width: 108px;
				height: 47px;
				background: #8F6726;
				border-radius: 6px 6px 6px 6px;
				color: #fff;
				font-size: 18px;
				margin-left: 16px;

				image {
					margin-right: 5px;
				}
			}
		}

		.user-list {
			border-radius: 16px 16px 0 0;
			background: #FFFFFF;
			margin-top: 30px;
			width: 100%;
			/* 横向滚动：适配小屏设备，避免列挤压 */
			height: calc(100vh - 270px);
			overflow-y: auto;
			overflow-x: auto;
            &.flex-box{
				height: calc(100vh - 370px);
			}
			&::-webkit-scrollbar {
				display: block !important;
				width: 10px !important;
				/* 滚动条宽度 */
			}

			&::-webkit-scrollbar-thumb {
				background: #8F6726 !important;
				/* 滚动条滑块颜色 */
				border-radius: 5px !important;
				/* 滑块圆角 */
			}

			.table-title {
				position: sticky;
				top: 0;
				/* 滚动时吸附在顶部 */
				z-index: 10;
				width: 100%; /* 确保宽度占满容器 */
				min-width: max-content; /* 标准写法 */
				background: #E9DDCD;
				height: 47px;
				justify-content: space-between;
				view {
					background: #E9DDCD;
					line-height: 47px;
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					font-size: 16px;
					color: #8F6726;
					text-align: center;
				}
			}
		}

		.table-item {
			background: #FFFFFF;
			height: 47px;
			display: flex;
			align-items: center;
			justify-content: space-between;
			// min-width: 1200px;
			width:100%; /* 确保宽度占满容器 */
 min-width: max-content; /* 关键：如果内容超出容器宽度，表头宽度跟随内容撑开 */
			/* 最小宽度：确保小屏可横向滚动，列不挤压 */
			.btn-box {
				width: 100%;

				.btn {
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 16px;
					color: #A14212;
					text-align: center;

					&:last-child {
						margin-left: 16px;
					}
				}
			}

		}
	}

	/* 遮罩层样式 */
	.mask {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.5);
		display: flex;
		justify-content: center;
		align-items: center;
		z-index: 999;
		animation: fadeIn 0.3s ease;
	}

	.isNull {
		text-align: center;
		color: #8F6726;
		font-size: 18px;
	}

	/* 弹窗容器样式 */
	.popup-container2 {
		//width: 50%;
		height: 250px;
		background: #FBF4EE;
		border-radius: 12px;
		box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
		animation: scaleIn 0.3s ease;
		overflow: hidden;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: space-between;
		padding: 30px 50px;
	
		.title-t {
			font-size: 24px;
			font-weight: bold;
			color: #8F6726;
			text-align: center;
		}
	
		.title-r {
			font-size: 20px;
			color: #8F6726;
			text-align: center;
		}
	
		.btn-r {
			text-align: center;
			background: #8F6726;
			font-size: 20px;
			color: #FBF4EE;
			border-radius: 12px;
			width: 60%;
			height: 50px;
			line-height: 50px;
		}
	}

	/* 弹窗容器样式 */
	.popup-container {
		width: 80%;
		// min-width: 840px;
		background: #FBF4EE;
		border-radius: 16px;
		box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
		animation: scaleIn 0.3s ease;
		overflow: hidden;
	}

	/* 弹窗头部样式 */
	.popup-header {
		padding: 20px;
		display: flex;
		justify-content: space-between;
		align-items: center;
		background: #E9DDCD;

		.goback {
			background: #FFFFFF;
			border-radius: 4px;
			border: 1px solid #8F6726;
			padding: 10px 16px;
			font-size: 24px;
			color: #8F6726;
			text-align: center;
		}
	}

	.popup-title {
		flex: 1;
		font-family: Source Han Serif CN, Source Han Serif CN;
		font-weight: bold;
		font-size: 24px;
		color: #8F6726;
		text-align: center;
	}

	.close-btn {
		display: flex;
		justify-content: center;
		align-items: center;
		transition: all 0.2s ease;

		image {
			width: 24px;
			height: 24px;
		}
	}

	/* 弹窗内容样式 */
	.popup-content {
		// width: 90%;
		padding: 40px 30px;
		font-size: 24px;
		color: #666;
		line-height: 1.6;
		background: #FBF4EE;
		display: flex;
		flex-direction: column;
		align-items: center;
		margin: auto;

		.form-item {
			display: flex;
			align-items: center;
			margin-bottom: 30px;

			.label {
				width: 164px;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 28px;
				color: #8F6726;
				text-align: right;
			}

			.form-input {
				width: 540px;
				height: 64px;
				box-sizing: border-box;
				background: #FFFFFF;
				border-radius: 8px;
				border: 2px solid #8F6726;
				padding: 15px 26px;
			}

			.text-input {
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 24px;
				color: #9B9B9B;
				text-align: left;
			}

			.r-box {
				width: 540px;
				height: 64px;
			}

			.picker-box {

				// width: 540px;
				// height: 64px;
				// box-sizing: border-box;
				// background: #FFFFFF;
				// border-radius: 8px;
				// border: 2px solid #8F6726;
				// padding: 15px 26px;
				.date {
					image {
						width: 32px;
						height: 32px;
					}
				}

			}

		}
	}

	.userList-box {
		// margin-top: 30px;
		width: 100%;
		background: #FFFFFF;
		border-radius: 16px;
		overflow: hidden;
	}

	.userList {
		width: 100%;
		height: 330px;
		overflow-y: auto;

		&::-webkit-scrollbar {
			display: block !important;
			width: 10px !important;
			/* 滚动条宽度 */
		}

		&::-webkit-scrollbar-thumb {
			background: #8F6726 !important;
			/* 滚动条滑块颜色 */
			border-radius: 5px !important;
			/* 滑块圆角 */
		}

		.item {
			width: 100%;
			padding: 17px 22px;
			display: flex;
			align-items: center;
			justify-content: space-between;
			border-bottom: 1px solid #8F6726;

			.title {
				flex: 1;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 18px;
				color: #8F6726;
				text-align: left;
			}

			.right-box {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.rest {
					display: flex;
					align-items: center;
					justify-content: center;
					width: 59px;
					height: 33px;
					border-radius: 6px 6px 6px 6px;
					border: 1px solid #8F6726;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 16px;
					color: #8F6726;
					margin-right: 16px;
				}

				.select {
					display: flex;
					align-items: center;
					justify-content: center;
					width: 80px;
					height: 44px;
					border-radius: 8px;
					border: 2px solid #8F6726;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 20px;
					color: #8F6726;

					&.active {
						background: #8F6726;
						color: #fff;
					}
				}
			}

			&:last-child {
				border-bottom: none;
			}
		}
	}

	.footer {
		position: fixed;
		bottom: 80px;
		right: 80px;
	}

	/* 统一列样式：控制对齐、溢出、内边距 */
	.col {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		padding: 0 10px;
		line-height: 47px;
		/* 适配多行文本 */
		font-size: 16px;
		color: #8F6726;
		white-space: nowrap;
		/* 禁止换行（可选，根据需求调整） */
		overflow: hidden;
		text-overflow: ellipsis;
		/* 长文本省略号 */
	}

	/* 列宽规则：按内容重要性分配宽度（核心优化） */
	.col-1 {
		flex: 0 0 60px;
		
	}

	/* 窄列：序号、性别、年龄（内容短） */
	.col-2 {
		flex: 0 0 120px;
		
	}

	/* 中列：客户账号、客户关系（内容中等） */
	.col-3 {
		flex: 0 0 180px;
		
	}

	/* 宽列：名称、手机号、时间（内容长） */
	.col-4 {
		flex: 0 0 220px;
		
	}
</style>