<!-- 自定义样式的时间范围选择器 -->
<!-- 使用插槽实现样式修改 -->
<template>
	<view class="custom-date-select">
		<uni-datetime-picker @change="handleSelect" v-model="dateRange" type="daterange">
			<view class="content">
				<view class="left date-box">
					<view class="date">
						{{dateRange[0]}}
					</view>
					<view class="icon">
						
					</view>
				</view>
				<view class="middle">
					-
				</view>
				<view class="right date-box">
					{{dateRange[1]}}
				</view>
			</view>
		</uni-datetime-picker>
	</view>
</template>

<script>
	import {
		defineComponent,
		watch,
		ref
	} from 'vue';
	import moment from 'moment';
	const props = {
		range: {
			type: Array,
			default: ()=>[]
		}
	}
	export default defineComponent({
		props,
		setup(props, {
			emit
		}) {
			const dateRange = ref([moment().subtract(1,'days').format('YYYY-MM-DD'), moment().format('YYYY-MM-DD')])
			watch(
				() => props.range,
				(newVal) => {
					if(newVal && newVal.length){
						dateRange.value = newVal
					}
				}, {
					immediate: true
				}
			)
			const handleSelect = () => {
				emit('dateChange', dateRange.value)
			}
			return {
				dateRange,
				handleSelect
			}
		}
	})
</script>

<style lang="scss" scoped>
	.custom-date-select{
		margin-top: 20rpx;
		
		::v-deep .content{
			display: flex;
			align-items: center;
			justify-content: space-between;
			
			.date-box{
				padding: 20rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				width: calc(46% - 40rpx);
				border-radius: 12rpx;
				background: #FFFFFF;
			}
		}
	}
</style>