<template>
	<view>
		<view class="flex-between-center">
			<view @click="onOpenStartTime" class="select-time">
				<text v-if="!startTimeValue">选择开始时间</text>
				<text v-else style="color: black;">{{ startTimeValue }}</text>
			</view>
			<text class="fs-30" style="color: #999999;">-</text>
			<view @click="onOpenEndTime" class="select-time">
				<text v-if="!endTimeValue">选择结束时间</text>
				<text v-else style="color: black;">{{ endTimeValue }}</text>
			</view>
		</view>

		<!-- 选择开始时间 -->
		<u-datetime-picker :show="startTimeShow" v-model="startTime" title="开始时间" mode="date" :formatter="formatter"
			:closeOnClickOverlay="true" @confirm="onConfirmStartTime" @close="startTimeShow = false"
			@cancel="startTimeShow = false"></u-datetime-picker>
		<!-- 选择结束时间 -->
		<u-datetime-picker :show="endTimeShow" v-model="endTime" title="结束时间" mode="date" :formatter="formatter"
			:closeOnClickOverlay="true" @confirm="onConfirmEndTime" @close="endTimeShow = false"
			@cancel="endTimeShow = false"></u-datetime-picker>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				startTimeShow: false,
				startTimeValue: '',
				startTime: new Date().getTime(),
				endTimeShow: false,
				endTime: new Date().getTime(),
				endTimeValue: '',
				startTimeNumber: 0,
				endTimeNumber: 0,
			}
		},
		methods: {
			formatter(type, value) {
				if (type === 'year') {
					return `${value}年`
				}
				if (type === 'month') {
					return `${value}月`
				}
				if (type === 'day') {
					return `${value}日`
				}
				if (type === 'hour') {
					return `${value}点`
				}
				if (type === 'minute') {
					return `${value}分`
				}
				return value
			},
			onOpenStartTime() {
				this.startTimeShow = true
			},
			onOpenEndTime() {
				if (this.startTimeValue == '') {
					this.$wskj.toast('请先选择开始时间')
					return
				}
				this.endTimeShow = true
			},
			onConfirmStartTime(e) {
				console.log(e)
				this.startTimeNumber = e.value
				this.startTimeShow = false
				this.startTimeValue = this.$wskj.timeFormat(e.value, 'yyyy-mm-dd')
				this.$emit('onConfirmStartTime', this.startTimeValue)
			},
			onConfirmEndTime(e) {
				console.log(e)
				this.endTimeNumber = e.value
				if (this.startTimeNumber > this.endTimeNumber) {
					this.$wskj.toast('结束时间必须大于开始时间')
					return
				}
				this.endTimeShow = false
				this.endTimeValue = this.$wskj.timeFormat(e.value, 'yyyy-mm-dd')
				this.$emit('onConfirmEndTime', this.endTimeValue)
			},
		}
	}
</script>

<style scoped>
	.select-time {
		width: 300rpx;
		height: 84rpx;
		text-align: center;
		line-height: 84rpx;
		border-radius: 5px;
		font-size: 30rpx;
		color: #999999;
	}
</style>