<template>
	<u-popup :maskCloseAble="maskCloseAble" mode="bottom" :popup="false" v-model="timeshow" length="auto"
		:safeAreaInsetBottom="safeAreaInsetBottom" @close="close" :z-index="uZIndex">
		<view class="u-datetime-picker">
			<view class="u-picker-header" @touchmove.stop.prevent="">
				<view class="u-btn-picker u-btn-picker--tips" :style="{ color: cancelColor }" hover-class="u-opacity"
					:hover-stay-time="150" @tap="getResult('cancel')">{{cancelText}}</view>
				<view class="u-picker__title">{{ title }}</view>
				<view class="u-btn-picker u-btn-picker--primary" :style="{ color: moving ? cancelColor : confirmColor }"
					hover-class="u-opacity" :hover-stay-time="150" @touchmove.stop="" @tap.stop="getResult('confirm')">
					{{confirmText}}
				</view>
			</view>
			<view class="u-picker-body">
				<picker-view :value="valueArr" @change="change" class="u-picker-view" @pickstart="pickstart"
					@pickend="pickend">
					<picker-view-column v-if="!reset && params.year" style="flex:6">
						<view class="u-column-item" v-for="(item, index) in years" :key="index">
							{{ item }}
							<text class="u-text" v-if="showTimeTag"></text>
						</view>
					</picker-view-column>
					<picker-view-column v-if="!reset && params.month" style="flex:5">
						<view class="u-column-item" v-for="(item, index) in months" :key="index">
							{{ formatNumber(item) }}
							<text class="u-text" v-if="showTimeTag"></text>
						</view>
					</picker-view-column>
					<picker-view-column v-if="!reset && params.day" style="flex:5">
						<view class="u-column-item" v-for="(item, index) in days" :key="index">
							{{ formatNumber(item) }}
							<text class="u-text" v-if="showTimeTag"></text>
						</view>
					</picker-view-column>
					<view class="u-column-item">
						<text class="u-text">一</text>
					</view>
					<picker-view-column v-if="!reset && params.endmonth" style="flex:5">
						<view class="u-column-item" v-for="(item, index) in months" :key="index">
							{{ formatNumber(item) }}
							<text class="u-text" v-if="showTimeTag"></text>
						</view>
					</picker-view-column>
					<picker-view-column v-if="!reset && params.endday" style="flex:5">
						<view class="u-column-item" v-for="(item, index) in enddays" :key="index">
							{{ formatNumber(item) }}
							<text class="u-text" v-if="showTimeTag"></text>
						</view>
					</picker-view-column>
				</picker-view>
			</view>
		</view>
	</u-popup>
</template>

<script>
	export default {
		name: 'timepicker',
		props: {
			// picker中需要显示的参数
			params: {
				type: Object,
				default () {
					return {
						year: true,
						month: true,
						day: true,
						endday: true,
						endmonth: true,
					};
				}
			},
			// 模式选择，region-地区类型，time-时间类型，selector-单列模式，multiSelector-多列模式
			mode: {
				type: String,
				default: 'time'
			},
			// 年份开始时间
			startYear: {
				type: [String, Number],
				default: 2023
			},
			// 年份结束时间
			endYear: {
				type: [String, Number],
				default: 2100
			},
			// "取消"按钮的颜色
			cancelColor: {
				type: String,
				default: '#606266'
			},
			// "确定"按钮的颜色
			confirmColor: {
				type: String,
				default: '#2979ff'
			},
			// 默认显示的时间，2025-07-02 || 2025-07-02 13:01:00 || 2025/07/02
			defaultTime: {
				type: String,
				default: ''
			},
			// 时间模式时，是否显示后面的年月日中文提示
			showTimeTag: {
				type: Boolean,
				default: true
			},
			safeAreaInsetBottom: {
				type: Boolean,
				default: false
			},
			// 是否允许通过点击遮罩关闭Picker
			maskCloseAble: {
				type: Boolean,
				default: true
			},
			// 通过双向绑定控制组件的弹出与收起
			timepicker: {
				type: Boolean,
				default: false
			},
			// 弹出的z-index值
			zIndex: {
				type: [String, Number],
				default: 0
			},
			// 顶部标题
			title: {
				type: String,
				default: ''
			},
			// 取消按钮的文字
			cancelText: {
				type: String,
				default: '取消'
			},
			// 确认按钮的文字
			confirmText: {
				type: String,
				default: '确认'
			}
		},
		data() {
			return {
				timeshow: false,
				years: [],
				months: [],
				days: [],
				enddays: [],
				year: 0,
				month: 0,
				day: 0,
				endmonth: 0,
				endday: 0,
				reset: false,
				startDate: '',
				endDate: '',
				valueArr: [],
				moving: false // 列是否还在滑动中，微信小程序如果在滑动中就点确定，结果可能不准确
			};
		},
		mounted() {
			this.init();
		},
		computed: {
			yearAndMonth() {
				return `${this.year}-${this.month}`;
			},
			yearAndendMonth() {
				return `${this.year}-${this.endmonth}`;
			},
			uZIndex() {
				// 如果用户有传递z-index值，优先使用
				return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
			},
		},
		watch: {
			// watch监听月份的变化，实时变更日的天数，因为不同月份，天数不一样
			// 一个月可能有30，31天，甚至闰年2月的29天，平年2月28天
			yearAndMonth(val) {
				if (this.params.year) {
					this.setDays();
				}
			},
			yearAndendMonth(val) {
				if (this.params.year) {
					this.setendDays();
				}
			},
			// 微信和QQ小程序由于一些奇怪的原因(故同时对所有平台均初始化一遍)，需要重新初始化才能显示正确的值
			timepicker(n) {
				this.timeshow = this.timepicker
				if (n) {
					this.reset = true;
					setTimeout(() => this.init(), 10);
				}
			}
		},
		methods: {
			// 标识滑动开始，只有微信小程序才有这样的事件
			pickstart() {
				// #ifdef MP-WEIXIN
				this.moving = true;
				// #endif
			},
			// 标识滑动结束
			pickend() {
				// #ifdef MP-WEIXIN
				this.moving = false;
				// #endif
			},
			// 小于10前面补0，用于月份，日期，时分秒等
			formatNumber(num) {
				return +num < 10 ? '0' + num : String(num);
			},
			// 生成递进的数组
			generateArray: function(start, end) {
				// 转为数值格式，否则用户给end-year等传递字符串值时，下面的end+1会导致字符串拼接，而不是相加
				start = Number(start);
				end = Number(end);
				end = end > start ? end : start;
				// 生成数组，获取其中的索引，并剪出来
				return [...Array(end + 1).keys()].slice(start);
			},
			getIndex: function(arr, val) {
				let index = arr.indexOf(val);
				// 如果index为-1(即找不到index值)，~(-1)=-(-1)-1=0，导致条件不成立
				return ~index ? index : 0;
			},
			//日期时间处理
			initTimeValue() {
				// 格式化时间，在IE浏览器(uni不存在此情况)，无法识别日期间的"-"间隔符号
				let fdate = this.defaultTime.replace(/\-/g, '/');
				fdate = fdate && fdate.indexOf('/') == -1 ? `2020/01/01 ${fdate}` : fdate;
				let time = null;
				if (fdate) time = new Date(fdate);
				else time = new Date();
				// 获取年日月时分秒
				this.year = time.getFullYear();
				this.month = Number(time.getMonth()) + 1;
				this.day = time.getDate();
				this.endmonth = Number(time.getMonth()) + 1;
				this.endday = time.getDate();
				// this.second = time.getSeconds();
			},
			init() {
				this.valueArr = [];
				this.reset = false;
				if (this.mode == 'time') {
					this.initTimeValue();
					if (this.params.year) {
						this.valueArr.push(0);
						this.setYears();
					}
					if (this.params.month) {
						this.valueArr.push(0);
						this.setMonths();
					}
					if (this.params.day) {
						this.valueArr.push(0);
						this.setDays();
					}
					if (this.params.endmonth) {
						this.valueArr.push(0);
						this.setendMonths();
					}
					if (this.params.endday) {
						this.valueArr.push(0);
						this.setendDays();
					}
				}
				this.$forceUpdate();
			},
			// 设置picker的某一列值
			setYears() {
				// 获取年份集合
				this.years = this.generateArray(this.startYear, this.endYear);
				// 设置this.valueArr某一项的值，是为了让picker预选中某一个值
				this.valueArr.splice(this.valueArr.length - 1, 1, this.getIndex(this.years, this.year));
			},
			setMonths() {
				this.months = this.generateArray(1, 12);
				this.valueArr.splice(this.valueArr.length - 1, 1, this.getIndex(this.months, this.month));
			},
			setDays() {
				let totalDays = new Date(this.year, this.month, 0).getDate();
				this.days = this.generateArray(1, totalDays);
				let index = 0;
				// 这里不能使用类似setMonths()中的this.valueArr.splice(this.valueArr.length - 1, xxx)做法
				// 因为this.month和this.year变化时，会触发watch中的this.setDays()，导致this.valueArr.length计算有误
				if (this.params.year && this.params.month) index = 2;
				else if (this.params.month) index = 1;
				else if (this.params.year) index = 1;
				else index = 0;
				// 当月份变化时，会导致日期的天数也会变化，如果原来选的天数大于变化后的天数，则重置为变化后的最大值
				// 比如原来选中3月31日，调整为2月后，日期变为最大29，这时如果day值继续为31显然不合理，于是将其置为29(picker-column从1开始)
				if (this.day > this.days.length) this.day = this.days.length;
				this.valueArr.splice(index, 1, this.getIndex(this.days, this.day));
			},
			setendMonths() {
				this.months = this.generateArray(1, 12);
				this.valueArr.splice(this.valueArr.length - 1, 1, this.getIndex(this.months, this.endmonth));
			},
			setendDays() {
				let totalDays = new Date(this.year, this.endmonth, 0).getDate();
				this.enddays = this.generateArray(1, totalDays);
				let index = 0;
				// 这里不能使用类似setMonths()中的this.valueArr.splice(this.valueArr.length - 1, xxx)做法
				// 因为this.month和this.year变化时，会触发watch中的this.setDays()，导致this.valueArr.length计算有误
				if (this.params.year && this.params.endmonth) index = 4;
				else if (this.params.endmonth) index = 3;
				else if (this.params.year) index = 3;
				else index = 0;
				// 当月份变化时，会导致日期的天数也会变化，如果原来选的天数大于变化后的天数，则重置为变化后的最大值
				// 比如原来选中3月31日，调整为2月后，日期变为最大29，这时如果day值继续为31显然不合理，于是将其置为29(picker-column从1开始)
				if (this.endday > this.enddays.length) this.endday = this.enddays.length;
				this.valueArr.splice(index, 1, this.getIndex(this.enddays, this.endday));
			},
			close() {
				this.$emit('input', false);
			},
			// 用户更改picker的列选项
			change(e) {
				this.valueArr = e.detail.value;
				let i = 0;
				// 这里使用i++，是因为this.valueArr数组的长度是不确定长度的，它根据this.params的值来配置长度
				// 进入if规则，i会加1，保证了能获取准确的值
				if (this.params.year) this.year = this.years[this.valueArr[i++]];
				if (this.params.month) this.month = this.months[this.valueArr[i++]];
				if (this.params.day) this.day = this.days[this.valueArr[i++]];
				if (this.params.endmonth) this.endmonth = this.months[this.valueArr[i++]];
				if (this.params.endday) this.endday = this.enddays[this.valueArr[i++]];
			},
			// 用户点击确定按钮
			getResult(event = null) {
				let result = {};
				// 只返回用户在this.params中配置了为true的字段
				if (this.params.year) result.year = this.formatNumber(this.year || 0);
				if (this.params.month) result.month = this.formatNumber(this.month || 0);
				if (this.params.day) result.day = this.formatNumber(this.day || 0);
				if (this.params.endmonth) result.endmonth = this.formatNumber(this.endmonth || 0);
				if (this.params.endday) result.endday = this.formatNumber(this.endday || 0);
				// const newObj = {};
				// for (let prop in result) {
				// 	if (result.hasOwnProperty(prop) && (prop === 'year' || prop === 'month')) {
				// 		newObj[prop] = result[prop];
				// 	}
				// }
				this.$emit('timeconfirm', result);
				this.close();
			},
			// 获取时间戳
			getTimestamp() {
				// yyyy-mm-dd为安卓写法，不支持iOS，需要使用"/"分隔，才能二者兼容
				let time = this.year + '/' + this.month + '/' + this.day + ' ' + this.hour + ':' + this.minute + ':' + this
					.second;
				return new Date(time).getTime() / 1000;
			}
		}
	};
</script>

<style lang="scss">
	// 定义混入指令，用于在非nvue环境下的flex定义，因为nvue没有display属性，会报错
	@mixin vue-flex($direction: row) {
		/* #ifndef APP-NVUE */
		display: flex;
		flex-direction: $direction;
		/* #endif */
	}

	.u-datetime-picker {
		position: relative;
		z-index: 999;
	}

	.u-picker-view {
		height: 100%;
		box-sizing: border-box;
	}

	.u-picker-header {
		width: 100%;
		height: 90rpx;
		padding: 0 40rpx;
		@include vue-flex;
		justify-content: space-between;
		align-items: center;
		box-sizing: border-box;
		font-size: 30rpx;
		background: #fff;
		position: relative;
	}

	.u-picker-header::after {
		content: '';
		position: absolute;
		border-bottom: 1rpx solid #eaeef1;
		-webkit-transform: scaleY(0.5);
		transform: scaleY(0.5);
		bottom: 0;
		right: 0;
		left: 0;
	}

	.u-picker__title {
		color: $u-content-color;
	}

	.u-picker-body {
		padding-top: 20rpx;
		width: 100%;
		height: 500rpx;
		overflow: hidden;
		background-color: #fff;
	}

	.u-column-item {
		@include vue-flex;
		align-items: center;
		justify-content: center;
		font-size: 30rpx;
		color: $u-main-color;
		padding: 0 8rpx;
	}

	.u-text {
		font-size: 24rpx;
		padding-left: 8rpx;
	}

	.u-btn-picker {
		padding: 16rpx;
		box-sizing: border-box;
		text-align: center;
		text-decoration: none;
	}

	.u-opacity {
		opacity: 0.5;
	}

	.u-btn-picker--primary {
		color: $u-type-primary;
	}

	.u-btn-picker--tips {
		color: $u-tips-color;
	}
</style>
