neo-datetime-pro.vue 30.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
<template>
	<view class="neo-datetime-pro">
		<!-- 触发器 -->
		<view 
			class="trigger" 
			:class="{ disabled, readonly }"
			@click="handleTriggerClick"
		>
			<text v-if="displayValue" class="trigger-value">{{ displayValue }}</text>
			<text v-else class="trigger-placeholder">{{ placeholder }}</text>
			<text v-if="clearable && displayValue && !disabled && !readonly" class="trigger-clear" @click.stop="handleClear">×</text>
		</view>
		
		<!-- 底部弹出层 -->
		<view v-if="visible" class="popup-overlay" @click="handleCancel">
			<view class="popup-content" :class="{ show: popupShow }" @click.stop>
				<!-- 头部 -->
				<view class="popup-header">
					<text class="popup-title">{{ popupTitle }}</text>
					<text class="popup-close" @click="handleCancel">×</text>
				</view>
				
				<!-- 快捷选项 -->
				<view v-if="shortcuts.length > 0 && isRangeType" class="shortcuts">
					<view 
						v-for="(shortcut, index) in shortcuts" 
						:key="index"
						class="shortcut-item"
						@click="handleShortcut(shortcut)"
					>
						{{ shortcut.text }}
					</view>
				</view>
				
				<!-- 主体内容 -->
				<view class="popup-body">
					<!-- 日期选择器 -->
					<view v-if="showDatePicker" class="date-picker">
						<!-- 年月切换 -->
						<view class="calendar-header">
							<text class="calendar-nav" @click="prevMonth">‹</text>
							<text class="calendar-title" @click="showYearMonthPicker = true">
								{{ currentYear }}{{ locale === 'zh' ? '年' : '-' }}{{ String(currentMonth).padStart(2, '0') }}{{ locale === 'zh' ? '月' : '' }}
							</text>
							<text class="calendar-nav" @click="nextMonth">›</text>
						</view>
						
						<!-- 星期标题 -->
						<view class="calendar-weekdays">
							<text 
								v-for="(day, index) in weekdays" 
								:key="index"
								class="weekday"
							>
								{{ day }}
							</text>
						</view>
						
						<!-- 日期网格 -->
						<view class="calendar-days">
							<view 
								v-for="(day, index) in calendarDays" 
								:key="index"
								class="day-cell"
								:class="getDayClass(day)"
								@click="handleDayClick(day)"
							>
								<text class="day-text">{{ day.day }}</text>
								<text v-if="showWeekNumber && day.weekNumber" class="week-number">W{{ day.weekNumber }}</text>
							</view>
						</view>
					</view>
					
					<!-- 时间选择器 -->
					<view v-if="showTimePicker" class="time-picker">
						<view 
							class="time-picker-header"
							:class="{ 'single-column': !showMinutes && !showSeconds, 'double-column': showMinutes && !showSeconds }"
						>
							<text class="time-label" v-if="showHours">{{ locale === 'zh' ? '时' : 'Hour' }}</text>
							<text class="time-label" v-if="showMinutes">{{ locale === 'zh' ? '分' : 'Minute' }}</text>
							<text class="time-label" v-if="showSeconds">{{ locale === 'zh' ? '秒' : 'Second' }}</text>
						</view>
						<picker-view 
							:value="timePickerValue" 
							class="time-picker-view"
							:class="{ 'single-column': !showMinutes && !showSeconds, 'double-column': showMinutes && !showSeconds }"
							@change="handleTimeChange"
						>
							<!-- 小时 -->
							<picker-view-column v-if="showHours">
								<view v-for="h in hours" :key="h" class="time-item">{{ String(h).padStart(2, '0') }}</view>
							</picker-view-column>
							<!-- 分钟 -->
							<picker-view-column v-if="showMinutes">
								<view v-for="m in minutes" :key="m" class="time-item">{{ String(m).padStart(2, '0') }}</view>
							</picker-view-column>
							<!-- 秒 -->
							<picker-view-column v-if="showSeconds">
								<view v-for="s in seconds" :key="s" class="time-item">{{ String(s).padStart(2, '0') }}</view>
							</picker-view-column>
						</picker-view>
					</view>
				</view>
				
				<!-- 底部按钮 -->
			<view class="popup-footer">
				<!-- 如果是第二步,显示"上一步"按钮 -->
				<view 
					v-if="currentStep === 2" 
					class="footer-btn footer-btn-back" 
					@click="handleBack"
				>
					{{ locale === 'zh' ? '上一步' : 'Back' }}
				</view>
				<view 
					v-else 
					class="footer-btn footer-btn-cancel" 
					@click="handleCancel"
				>
					{{ locale === 'zh' ? '取消' : 'Cancel' }}
				</view>
				
				<!-- 如果是 datetime/datetimerange 的第一步,显示"下一步" -->
				<view 
					v-if="(type === 'datetime' || type === 'datetimerange') && currentStep === 1"
					class="footer-btn footer-btn-confirm" 
					@click="handleNext"
				>
					{{ locale === 'zh' ? '下一步' : 'Next' }}
				</view>
				<!-- 否则显示"确定" -->
				<view 
					v-else
					class="footer-btn footer-btn-confirm" 
					@click="handleConfirm"
				>
					{{ locale === 'zh' ? '确定' : 'Confirm' }}
				</view>
			</view>
			</view>
		</view>
		
		<!-- 年月选择器 -->
		<view v-if="showYearMonthPicker" class="year-month-picker-overlay" @click="showYearMonthPicker = false">
			<view class="year-month-picker-content" @click.stop>
				<picker-view 
					:value="yearMonthPickerValue" 
					class="year-month-picker-view"
					@change="handleYearMonthChange"
				>
					<!-- 年份 -->
					<picker-view-column>
						<view v-for="y in years" :key="y" class="ym-item">{{ y }}{{ locale === 'zh' ? '年' : '' }}</view>
					</picker-view-column>
					<!-- 月份 -->
					<picker-view-column>
						<view v-for="m in 12" :key="m" class="ym-item">{{ String(m).padStart(2, '0') }}{{ locale === 'zh' ? '月' : '' }}</view>
					</picker-view-column>
				</picker-view>
				<view class="year-month-picker-footer">
					<view class="footer-btn footer-btn-cancel" @click="showYearMonthPicker = false">取消</view>
					<view class="footer-btn footer-btn-confirm" @click="confirmYearMonth">确定</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	name: 'NeoDatetimePro',
	props: {
		modelValue: {
			type: [String, Array, Date],
			default: ''
		},
		type: {
			type: String,
			default: 'date' // date, datetime, time, daterange, datetimerange
		},
		format: {
			type: String,
			default: 'YYYY-MM-DD'
		},
		placeholder: {
			type: String,
			default: '请选择'
		},
		disabled: {
			type: Boolean,
			default: false
		},
		readonly: {
			type: Boolean,
			default: false
		},
		clearable: {
			type: Boolean,
			default: true
		},
		shortcuts: {
			type: Array,
			default: () => []
		},
		disabledDate: {
			type: Function,
			default: null
		},
		minDate: {
			type: [String, Date],
			default: null
		},
		maxDate: {
			type: [String, Date],
			default: null
		},
		showWeekNumber: {
			type: Boolean,
			default: false
		},
		locale: {
			type: String,
			default: 'zh' // zh, en
		},
		startOfWeek: {
			type: Number,
			default: 0 // 0=Sunday, 1=Monday
		},
		currentTime: {
			type: [Date, String, Number],
			default: null // 当前时间,支持从接口获取。未传入时使用客户端时间
		}
	},
	data() {
		// 初始化时获取当前时间
		let now = new Date()
		if (this.currentTime) {
			if (this.currentTime instanceof Date) {
				now = this.currentTime
			} else if (typeof this.currentTime === 'number') {
				now = new Date(this.currentTime)
			} else if (typeof this.currentTime === 'string') {
				now = new Date(this.currentTime.replace(/-/g, '/'))
			}
		}
		return {
			visible: false,
			popupShow: false,
			showYearMonthPicker: false,
			currentStep: 1, // 当前步骤:1=选日期,2=选时间
			currentYear: now.getFullYear(),
			currentMonth: now.getMonth() + 1,
			selectedDate: null,
			selectedStartDate: null,
			selectedEndDate: null,
			selectingStart: true,
			selectedHour: 0,
			selectedMinute: 0,
			selectedSecond: 0,
			timePickerValue: [0, 0, 0],
			yearMonthPickerValue: [0, 0],
			hours: Array.from({ length: 24 }, (_, i) => i),
			minutes: Array.from({ length: 60 }, (_, i) => i),
			seconds: Array.from({ length: 60 }, (_, i) => i),
			// 性能优化:缓存今天的时间戳
			todayTimestamp: null,
			cachedToday: null
		}
	},
	computed: {
		isRangeType() {
			return this.type === 'daterange' || this.type === 'datetimerange'
		},
		hasTimeSelection() {
			return this.type === 'datetime' || this.type === 'datetimerange' || this.type === 'time'
		},
		// 性能优化:缓存解析后的最小日期
		parsedMinDate() {
			if (!this.minDate) return null
			const min = this.parseDate(this.minDate)
			min.setHours(0, 0, 0, 0)
			return min.getTime()
		},
		// 性能优化:缓存解析后的最大日期
		parsedMaxDate() {
			if (!this.maxDate) return null
			const max = this.parseDate(this.maxDate)
			max.setHours(0, 0, 0, 0)
			return max.getTime()
		},
		// 性能优化:缓存当前时间的 Date 对象
		now() {
			return this.cachedToday || this.getCurrentTime()
		},
		showDatePicker() {
			// 对于 datetime 和 datetimerange,只在第一步显示日期选择器
			if (this.type === 'datetime' || this.type === 'datetimerange') {
				return this.currentStep === 1
			}
			return this.type !== 'time'
		},
		showTimePicker() {
			// 对于 datetime 和 datetimerange,只在第二步显示时间选择器
			if (this.type === 'datetime' || this.type === 'datetimerange') {
				return this.currentStep === 2
			}
			return this.type === 'time'
		},
		// 根据 format 判断需要显示的时间部分
		showHours() {
			return this.format.includes('HH') || this.format.includes('hh') || this.format.includes('H') || this.format.includes('h')
		},
		showMinutes() {
			return this.format.includes('mm') || this.format.includes('m')
		},
		showSeconds() {
			return this.format.includes('ss') || this.format.includes('SS') || this.format.includes('s')
		},
		popupTitle() {
			// 对于 datetime 和 datetimerange,根据步骤显示不同标题
			if (this.type === 'datetime') {
				if (this.currentStep === 1) {
					return this.locale === 'zh' ? '选择日期' : 'Select Date'
				} else {
					return this.locale === 'zh' ? '选择时间' : 'Select Time'
				}
			}
			if (this.type === 'datetimerange') {
				if (this.currentStep === 1) {
					return this.locale === 'zh' ? '选择日期范围' : 'Select Date Range'
				} else {
					return this.locale === 'zh' ? '选择时间' : 'Select Time'
				}
			}
			
			const titles = {
				date: this.locale === 'zh' ? '选择日期' : 'Select Date',
				time: this.locale === 'zh' ? '选择时间' : 'Select Time',
				daterange: this.locale === 'zh' ? '选择日期范围' : 'Select Date Range'
			}
			return titles[this.type] || titles.date
		},
		weekdays() {
			const zhWeekdays = ['日', '一', '二', '三', '四', '五', '六']
			const enWeekdays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
			const days = this.locale === 'zh' ? zhWeekdays : enWeekdays
			
			// 根据 startOfWeek 调整顺序
			const result = []
			for (let i = 0; i < 7; i++) {
				result.push(days[(this.startOfWeek + i) % 7])
			}
			return result
		},
		years() {
			const currentYear = this.now.getFullYear()
			return Array.from({ length: 201 }, (_, i) => currentYear - 100 + i)
		},
		calendarDays() {
			const year = this.currentYear
			const month = this.currentMonth
			const firstDay = new Date(year, month - 1, 1)
			const lastDay = new Date(year, month, 0)
			const daysInMonth = lastDay.getDate()
			
			// 计算第一天是星期几(根据 startOfWeek 调整)
			let firstDayOfWeek = firstDay.getDay() - this.startOfWeek
			if (firstDayOfWeek < 0) firstDayOfWeek += 7
			
			const days = []
			
			// 上个月的日期
			const prevMonthLastDay = new Date(year, month - 1, 0).getDate()
			for (let i = firstDayOfWeek - 1; i >= 0; i--) {
				const day = prevMonthLastDay - i
				const date = new Date(year, month - 2, day)
				days.push({
					day,
					date,
					isCurrentMonth: false,
					isPrevMonth: true,
					isNextMonth: false
				})
			}
			
			// 当前月的日期
			for (let i = 1; i <= daysInMonth; i++) {
				const date = new Date(year, month - 1, i)
				days.push({
					day: i,
					date,
					isCurrentMonth: true,
					isPrevMonth: false,
					isNextMonth: false,
					weekNumber: this.showWeekNumber ? this.getWeekNumber(date) : null
				})
			}
			
			// 下个月的日期(补齐6行)
			const remainingDays = 42 - days.length
			for (let i = 1; i <= remainingDays; i++) {
				const date = new Date(year, month, i)
				days.push({
					day: i,
					date,
					isCurrentMonth: false,
					isPrevMonth: false,
					isNextMonth: true
				})
			}
			
			return days
		},
		displayValue() {
			if (this.isRangeType) {
				if (Array.isArray(this.modelValue) && this.modelValue.length === 2) {
					return `${this.formatDate(this.parseDate(this.modelValue[0]))} - ${this.formatDate(this.parseDate(this.modelValue[1]))}`
				}
				return ''
			} else {
				if (this.modelValue) {
					return this.formatDate(this.parseDate(this.modelValue))
				}
				return ''
			}
		}
	},
	watch: {
		modelValue: {
			handler(val) {
				this.initValue()
			},
			immediate: true
		},
		currentTime: {
			handler() {
				this.updateCachedToday()
			},
			immediate: true
		}
	},
	methods: {
		// 获取当前时间(支持从 prop 传入或使用客户端时间)
		getCurrentTime() {
			if (this.currentTime) {
				if (this.currentTime instanceof Date) {
					return this.currentTime
				}
				if (typeof this.currentTime === 'number') {
					return new Date(this.currentTime)
				}
				if (typeof this.currentTime === 'string') {
					return new Date(this.currentTime.replace(/-/g, '/'))
				}
			}
			return new Date()
		},
		// 性能优化:更新缓存的今天日期
		updateCachedToday() {
			const now = this.getCurrentTime()
			this.cachedToday = now
			const today = new Date(now.getFullYear(), now.getMonth(), now.getDate())
			this.todayTimestamp = today.getTime()
		},
		initValue() {
			if (this.isRangeType) {
				if (Array.isArray(this.modelValue) && this.modelValue.length === 2) {
					this.selectedStartDate = this.parseDate(this.modelValue[0])
					this.selectedEndDate = this.parseDate(this.modelValue[1])
				} else {
					this.selectedStartDate = null
					this.selectedEndDate = null
				}
			} else {
				if (this.modelValue) {
					this.selectedDate = this.parseDate(this.modelValue)
					if (this.showTimePicker) {
						this.selectedHour = this.selectedDate.getHours()
						this.selectedMinute = this.selectedDate.getMinutes()
						this.selectedSecond = this.selectedDate.getSeconds()
						this.updateTimePickerValue()
					}
				} else {
					this.selectedDate = null
				}
			}
		},
		handleTriggerClick() {
			if (this.disabled || this.readonly) return
			this.visible = true
			this.currentStep = 1 // 重置为第一步
			this.selectingStart = true
      // 派发弹窗打开事件
      this.$emit('popup-show');
			setTimeout(() => {
				this.popupShow = true
			}, 50)
			
			// 初始化年月选择器
			const yearIndex = this.years.indexOf(this.currentYear)
			this.yearMonthPickerValue = [yearIndex, this.currentMonth - 1]
		},
		handleClear() {
			this.$emit('update:modelValue', this.isRangeType ? [] : '')
			this.$emit('clear')
			this.$emit('change', this.isRangeType ? [] : '')
		},
		handleCancel() {
			this.popupShow = false
      // 派发弹窗关闭事件
      this.$emit('popup-hide');
			setTimeout(() => {
				this.visible = false
				this.currentStep = 1 // 重置步骤
			}, 300)
		},
		handleNext() {
			// 对于 datetime 类型,检查是否已选择日期
			if (this.type === 'datetime' && !this.selectedDate) {
				return
			}
			// 对于 datetimerange 类型,检查是否已选择日期范围
			if (this.type === 'datetimerange' && (!this.selectedStartDate || !this.selectedEndDate)) {
				return
			}
			// 进入第二步(选择时间)
			this.currentStep = 2
		},
		handleBack() {
			// 返回第一步(选择日期)
			this.currentStep = 1
		},
		handleConfirm() {
			let value
			
			if (this.type === 'time') {
				value = this.formatTime()
			} else if (this.isRangeType) {
				if (this.selectedStartDate && this.selectedEndDate) {
					const start = this.type === 'datetimerange' 
						? this.combineDatetime(this.selectedStartDate, this.selectedHour, this.selectedMinute, this.selectedSecond)
						: this.selectedStartDate
					const end = this.type === 'datetimerange'
						? this.combineDatetime(this.selectedEndDate, this.selectedHour, this.selectedMinute, this.selectedSecond)
						: this.selectedEndDate
					value = [this.formatDate(start), this.formatDate(end)]
				} else {
					return
				}
			} else {
				if (this.selectedDate) {
					const date = this.type === 'datetime'
						? this.combineDatetime(this.selectedDate, this.selectedHour, this.selectedMinute, this.selectedSecond)
						: this.selectedDate
					value = this.formatDate(date)
				} else {
					return
				}
			}
			
			this.$emit('update:modelValue', value)
			this.$emit('confirm', value)
			this.$emit('change', value)
			this.popupShow = false
      this.$emit('popup-hide');
			setTimeout(() => {
				this.visible = false
				this.currentStep = 1 // 重置步骤
			}, 300)
		},
		// 性能优化:防抖优化月份切换
		prevMonth() {
			// 清除之前的防抖定时器
			if (this._monthChangeTimer) {
				clearTimeout(this._monthChangeTimer)
			}
			this._monthChangeTimer = setTimeout(() => {
				if (this.currentMonth === 1) {
					this.currentYear--
					this.currentMonth = 12
				} else {
					this.currentMonth--
				}
			}, 50)
		},
		nextMonth() {
			// 清除之前的防抖定时器
			if (this._monthChangeTimer) {
				clearTimeout(this._monthChangeTimer)
			}
			this._monthChangeTimer = setTimeout(() => {
				if (this.currentMonth === 12) {
					this.currentYear++
					this.currentMonth = 1
				} else {
					this.currentMonth++
				}
			}, 50)
		},
		handleYearMonthChange(e) {
			this.yearMonthPickerValue = e.detail.value
		},
		confirmYearMonth() {
			this.currentYear = this.years[this.yearMonthPickerValue[0]]
			this.currentMonth = this.yearMonthPickerValue[1] + 1
			this.showYearMonthPicker = false
		},
		handleDayClick(day) {
			if (!day.isCurrentMonth) {
				// 点击上个月或下个月的日期,切换月份
				if (day.isPrevMonth) {
					this.prevMonth()
				} else if (day.isNextMonth) {
					this.nextMonth()
				}
				return
			}
			
			if (this.isDateDisabled(day.date)) {
				return
			}
			
			if (this.isRangeType) {
				if (this.selectingStart) {
					this.selectedStartDate = day.date
					this.selectedEndDate = null
					this.selectingStart = false
				} else {
					if (day.date < this.selectedStartDate) {
						this.selectedEndDate = this.selectedStartDate
						this.selectedStartDate = day.date
					} else {
						this.selectedEndDate = day.date
					}
					this.selectingStart = true
				}
			} else {
				this.selectedDate = day.date
				if (this.showTimePicker) {
					// 保持当前选择的时间
				}
			}
		},
		handleTimeChange(e) {
			this.timePickerValue = e.detail.value
			let columnIndex = 0
			
			// 根据显示的列动态获取值
			if (this.showHours) {
				this.selectedHour = this.hours[this.timePickerValue[columnIndex]]
				columnIndex++
			}
			if (this.showMinutes) {
				this.selectedMinute = this.minutes[this.timePickerValue[columnIndex]]
				columnIndex++
			}
			if (this.showSeconds) {
				this.selectedSecond = this.seconds[this.timePickerValue[columnIndex]]
			}
		},
		updateTimePickerValue() {
			const values = []
			
			// 根据显示的列动态构建值数组
			if (this.showHours) {
				values.push(this.hours.indexOf(this.selectedHour))
			}
			if (this.showMinutes) {
				values.push(this.minutes.indexOf(this.selectedMinute))
			}
			if (this.showSeconds) {
				values.push(this.seconds.indexOf(this.selectedSecond))
			}
			
			this.timePickerValue = values
		},
		handleShortcut(shortcut) {
			const value = this.getShortcutValue(shortcut.value)
			if (value) {
				this.selectedStartDate = this.parseDate(value[0])
				this.selectedEndDate = this.parseDate(value[1])
			}
		},
		getShortcutValue(value) {
			if (typeof value === 'function') {
				return value()
			}
			
			// 性能优化:使用缓存的当前时间
			const now = this.now
			const today = new Date(now.getFullYear(), now.getMonth(), now.getDate())
			
			switch (value) {
				case 'today':
					return [today, today]
				case 'yesterday':
					const yesterday = new Date(today)
					yesterday.setDate(yesterday.getDate() - 1)
					return [yesterday, yesterday]
				case 'last7days':
					const last7days = new Date(today)
					last7days.setDate(last7days.getDate() - 6)
					return [last7days, today]
				case 'last30days':
					const last30days = new Date(today)
					last30days.setDate(last30days.getDate() - 29)
					return [last30days, today]
				case 'thisweek':
					const thisWeekStart = new Date(today)
					const day = today.getDay()
					const diff = day - this.startOfWeek
					thisWeekStart.setDate(today.getDate() - (diff < 0 ? diff + 7 : diff))
					return [thisWeekStart, today]
				case 'thismonth':
					const thisMonthStart = new Date(today.getFullYear(), today.getMonth(), 1)
					return [thisMonthStart, today]
				default:
					return null
			}
		},
		getDayClass(day) {
			const classes = []
			
			if (!day.isCurrentMonth) {
				classes.push('other-month')
			}
			
			if (this.isDateDisabled(day.date)) {
				classes.push('disabled')
			}
			
			if (this.isToday(day.date)) {
				classes.push('today')
			}
			
			if (this.isRangeType) {
				if (this.isDateSelected(day.date, this.selectedStartDate) || 
				    this.isDateSelected(day.date, this.selectedEndDate)) {
					classes.push('selected')
				}
				if (this.isDateInRange(day.date)) {
					classes.push('in-range')
				}
			} else {
				if (this.isDateSelected(day.date, this.selectedDate)) {
					classes.push('selected')
				}
			}
			
			return classes.join(' ')
		},
		isToday(date) {
			// 性能优化:使用缓存的时间戳比较
			const dateTimestamp = new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime()
			return dateTimestamp === this.todayTimestamp
		},
		isDateSelected(date, selectedDate) {
			if (!selectedDate) return false
			return date.getFullYear() === selectedDate.getFullYear() &&
			       date.getMonth() === selectedDate.getMonth() &&
			       date.getDate() === selectedDate.getDate()
		},
		isDateInRange(date) {
			if (!this.selectedStartDate || !this.selectedEndDate) return false
			return date > this.selectedStartDate && date < this.selectedEndDate
		},
		isDateDisabled(date) {
			// 自定义禁用函数
			if (this.disabledDate && this.disabledDate(date)) {
				return true
			}
			
			// 性能优化:使用缓存的时间戳比较
			const timestamp = new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime()
			
			// 最小日期
			if (this.parsedMinDate && timestamp < this.parsedMinDate) {
				return true
			}
			
			// 最大日期
			if (this.parsedMaxDate && timestamp > this.parsedMaxDate) {
				return true
			}
			
			return false
		},
		getWeekNumber(date) {
			const d = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()))
			const dayNum = d.getUTCDay() || 7
			d.setUTCDate(d.getUTCDate() + 4 - dayNum)
			const yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1))
			return Math.ceil((((d - yearStart) / 86400000) + 1) / 7)
		},
		parseDate(value) {
			if (value instanceof Date) {
				return new Date(value)
			}
			if (typeof value === 'string') {
				// 支持多种格式
				return new Date(value.replace(/-/g, '/'))
			}
			return new Date()
		},
		formatDate(date) {
			if (!date) return ''
			
			const year = date.getFullYear()
			const month = String(date.getMonth() + 1).padStart(2, '0')
			const day = String(date.getDate()).padStart(2, '0')
			const hours = String(date.getHours()).padStart(2, '0')
			const minutes = String(date.getMinutes()).padStart(2, '0')
			const seconds = String(date.getSeconds()).padStart(2, '0')
			
			return this.format
				.replace(/YYYY/g, year)
				.replace(/MM/g, month)
				.replace(/DD/g, day)
				.replace(/HH/g, hours)
				.replace(/mm/g, minutes)
				.replace(/ss/g, seconds)
		},
		formatTime() {
			const parts = []
			
			if (this.showHours) {
				parts.push(String(this.selectedHour).padStart(2, '0'))
			}
			if (this.showMinutes) {
				parts.push(String(this.selectedMinute).padStart(2, '0'))
			}
			if (this.showSeconds) {
				parts.push(String(this.selectedSecond).padStart(2, '0'))
			}
			
			return parts.join(':')
		},
		combineDatetime(date, hour, minute, second) {
			const newDate = new Date(date)
			newDate.setHours(hour, minute, second, 0)
			return newDate
		}
	},
	// 性能优化:组件销毁时清理定时器
	beforeDestroy() {
		if (this._monthChangeTimer) {
			clearTimeout(this._monthChangeTimer)
		}
	},
	// Vue 3 兼容
	beforeUnmount() {
		if (this._monthChangeTimer) {
			clearTimeout(this._monthChangeTimer)
		}
	}
}
</script>

<style scoped>
.neo-datetime-pro {
	width: 100%;
}

/* 触发器 */
.trigger {
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 4rpx 32rpx;
	background: var(--neo-color-bg-elevated, #fff);
	border: 2rpx solid var(--neo-color-border, rgba(0, 0, 0, 0.08));
	border-radius: 12rpx;
	min-height: 44rpx;
	box-sizing: border-box;
	transition: all 0.3s;
	cursor: pointer;
	user-select: none;
}

.trigger:active:not(.disabled):not(.readonly) {
	opacity: 0.8;
}

.trigger.disabled {
	opacity: 0.5;
	cursor: not-allowed;
	pointer-events: none;
}

.trigger.readonly {
	background: var(--neo-color-bg-base, #f5f7fa);
	cursor: default;
}

.trigger-value {
	font-size: 28rpx;

}

.trigger-placeholder {
	font-size: 28rpx;
	color: var(--neo-color-text-tertiary, #94a3b8);
}

.trigger-clear {
	font-size: 40rpx;
	color: var(--neo-color-text-tertiary, #94a3b8);
	padding: 0 8rpx;
}

/* 弹出层 */
.popup-overlay {
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	background: rgba(0, 0, 0, 0.5);
	z-index: 9999;
	display: flex;
	align-items: flex-end;
}

.popup-content {
	width: 100%;
	max-height: 80vh;
	background: var(--neo-color-bg-elevated, #fff);
	border-radius: 24rpx 24rpx 0 0;
	transform: translateY(100%);
	transition: transform 0.3s;
}

.popup-content.show {
	transform: translateY(0);
}

.popup-header {
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 32rpx;
	border-bottom: 2rpx solid var(--neo-color-border, rgba(0, 0, 0, 0.08));
}

.popup-title {
	font-size: 32rpx;
	font-weight: 600;
	color: var(--neo-color-text-primary, #1e293b);
}

.popup-close {
	font-size: 48rpx;
	color: var(--neo-color-text-tertiary, #94a3b8);
	padding: 0 8rpx;
}

.popup-body {
	max-height: 60vh;
	overflow-y: auto;
	padding: 24rpx;
}

/* 快捷选项 */
.shortcuts {
	display: flex;
	flex-wrap: wrap;
	gap: 16rpx;
	padding: 24rpx;
	border-bottom: 2rpx solid var(--neo-color-border, rgba(0, 0, 0, 0.08));
}

.shortcut-item {
	padding: 12rpx 24rpx;
	background: var(--neo-color-bg-base, #f5f7fa);
	border-radius: 8rpx;
	font-size: 24rpx;
	color: var(--neo-color-text-secondary, #64748b);
	transition: all 0.2s;
}

.shortcut-item:active {
	background: var(--neo-color-primary, #3b82f6);
	color: #fff;
}

/* 日历 */
.date-picker {
	margin-bottom: 24rpx;
}

.calendar-header {
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 24rpx 0;
}

.calendar-nav {
	font-size: 36rpx;
	color: var(--neo-color-text-primary, #1e293b);
	padding: 8rpx 24rpx;
	cursor: pointer;
}

.calendar-title {
	font-size: 28rpx;
	font-weight: 600;
	color: var(--neo-color-text-primary, #1e293b);
	padding: 8rpx 16rpx;
}

.calendar-weekdays {
	display: grid;
	grid-template-columns: repeat(7, 1fr);
	gap: 8rpx;
	margin-bottom: 16rpx;
}

.weekday {
	text-align: center;
	font-size: 24rpx;
	color: var(--neo-color-text-tertiary, #94a3b8);
	padding: 8rpx 0;
}

.calendar-days {
	display: grid;
	grid-template-columns: repeat(7, 1fr);
	gap: 8rpx;
}

.day-cell {
	aspect-ratio: 1;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	border-radius: 8rpx;
	cursor: pointer;
	position: relative;
	transition: all 0.2s;
}

.day-text {
	font-size: 28rpx;
	color: var(--neo-color-text-primary, #1e293b);
}

.week-number {
	font-size: 20rpx;
	color: var(--neo-color-text-tertiary, #94a3b8);
	position: absolute;
	top: 4rpx;
	right: 4rpx;
}

.day-cell.other-month .day-text {
	color: var(--neo-color-text-tertiary, #94a3b8);
}

.day-cell.today {
	background: var(--neo-color-bg-hover, #f8f9fa);
}

.day-cell.selected {
	background: var(--neo-color-primary, #3b82f6);
}

.day-cell.selected .day-text {
	color: #fff;
}

.day-cell.in-range {
	background: rgba(59, 130, 246, 0.1);
}

.day-cell.disabled {
	opacity: 0.3;
	cursor: not-allowed;
}

.day-cell.disabled:active {
	transform: none;
}

.day-cell:active:not(.disabled) {
	transform: scale(0.95);
}

/* 时间选择器 */
.time-picker {
	margin-top: 24rpx;
}

.time-picker-header {
	display: flex;
	padding: 16rpx 0;
	border-bottom: 2rpx solid var(--neo-color-border, rgba(0, 0, 0, 0.08));
}

.time-picker-header.single-column {
	padding-left: 30%;
	padding-right: 30%;
}

.time-picker-header.double-column {
	padding-left: 15%;
	padding-right: 15%;
}

.time-label {
	font-size: 26rpx;
	font-weight: 600;
	color: var(--neo-color-text-secondary, #64748b);
	flex: 1;
	text-align: center;
}

.time-picker-view {
	height: 400rpx;
}

.time-picker-view.single-column {
	/* 只显示一列时居中 */
	padding: 0 30%;
}

.time-picker-view.double-column {
	/* 显示两列时 */
	padding: 0 15%;
}

.time-item {
	display: flex;
	align-items: center;
	justify-content: center;
	height: 80rpx;
	font-size: 32rpx;
	color: var(--neo-color-text-primary, #1e293b);
}

/* 年月选择器 */
.year-month-picker-overlay {
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	background: rgba(0, 0, 0, 0.5);
	z-index: 10000;
	display: flex;
	align-items: flex-end;
}

.year-month-picker-content {
	width: 100%;
	background: var(--neo-color-bg-elevated, #fff);
	border-radius: 24rpx 24rpx 0 0;
}

.year-month-picker-view {
	height: 500rpx;
	padding: 24rpx 0;
}

.ym-item {
	display: flex;
	align-items: center;
	justify-content: center;
	height: 80rpx;
	font-size: 32rpx;
	color: var(--neo-color-text-primary, #1e293b);
}

.year-month-picker-footer {
	display: flex;
	gap: 24rpx;
	padding: 24rpx;
	border-top: 2rpx solid var(--neo-color-border, rgba(0, 0, 0, 0.08));
}

/* 底部按钮 */
.popup-footer {
	display: flex;
	gap: 24rpx;
	padding: 24rpx;
	border-top: 2rpx solid var(--neo-color-border, rgba(0, 0, 0, 0.08));
}

.footer-btn {
	flex: 1;
	height: 88rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	border-radius: 12rpx;
	font-size: 28rpx;
	font-weight: 600;
	transition: all 0.2s;
}

.footer-btn-cancel,
.footer-btn-back {
	background: var(--neo-color-bg-base, #f5f7fa);
	color: var(--neo-color-text-secondary, #64748b);
}

.footer-btn-confirm {
	background: var(--neo-color-primary, #3b82f6);
	color: #fff;
}

.footer-btn:active {
	opacity: 0.8;
	transform: scale(0.98);
}
</style>