<template>
    <view>
        <view class="search-data">
            <text v-for="(val, index) in year" :key="index" :class="val.startShow ? 'active' : ''"
                @click="onSeachYear(val, index)">{{ val.lable }}</text>
        </view>
    </view>
</template>
<script>
export default {
    data(){
        return{
            year: [{
				lable: '日',
				startShow: false
			}, {
				lable: '周',
				startShow: false
			}, {
				lable: '月',
				startShow: false
			}],
        }
    },
    methods: {
        onSeachYear(val, index) {
            console.log(val, index,99999999);
			this.year.map((value, i) => i === index ? value.startShow = !value.startShow : value.startShow = false)
			let now = new Date();
			let year = now.getFullYear();
			let month = now.getMonth() + 1; // 月份是从0开始的，所以需要加1
			let day = now.getDate();
			if (index == 0 && val.startShow) {
				let theDay = `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;
				let beginTime = theDay + ' 00:00:01'
				let endTime = theDay + ' 23:59:59'
                this.$emit("time",beginTime,endTime) 
				this.$emit("queryData") 
			}
			else if (index == 1 && val.startShow) {
				let dayOfWeek = now.getDay();
				let firstDayOfWeek = new Date(year, now.getMonth(), day - dayOfWeek + 1);
				let lastDayOfWeek = new Date(year, now.getMonth(), day + (7 - dayOfWeek));
				let monday = this.formatDate(firstDayOfWeek);
				let sunday = this.formatDate(lastDayOfWeek);
				let beginTime = monday + ' 00:00:01'
				let endTime = sunday + ' 23:59:59'
                this.$emit("time",beginTime,endTime) 
				this.$emit("queryData") 
			}
			else if (index == 2 && val.startShow) {
				let beginTime = this.getCurrentMonthFirst() + ' 00:00:01'
				let endTime = this.getCurrentMonthLast() + ' 23:59:59'
                this.$emit("time",beginTime,endTime) 
				this.$emit("queryData") 
			}
			else {
				let beginTime = ''
				let endTime = ''
				// type == 1 ? this.queryData() : this.analysisQueryData()
                this.$emit("time",beginTime,endTime) 
				this.$emit("queryData") 
			}

		},
        formatDate(date) {
			let year = date.getFullYear();
			let month = date.getMonth() + 1;
			let day = date.getDate();
			month = month < 10 ? '0' + month : month;
			day = day < 10 ? '0' + day : day;
			return year + '-' + month + '-' + day;
		},
		getCurrentMonthFirst() {
			// 获取当月第一天数据
			let date = new Date()
			date.setDate(1)
			let month = parseInt(date.getMonth() + 1)
			let day = date.getDate()
			if (month < 10) month = '0' + month
			if (day < 10) day = '0' + day
			return date.getFullYear() + '-' + month + '-' + day
		},
		getCurrentMonthLast() {
			// 获取当月最后一天数据
			let date = new Date()
			let year = date.getFullYear()
			let month = date.getMonth() + 1
			month = month < 10 ? '0' + month : month
			let day = new Date(year, month, 0)
			return year + '-' + month + '-' + day.getDate()
		},
    }
}
</script>
<style lang="scss" scoped>
.search-data {
	color: #666666;
	border: 1px solid #EEEEEE;

	.active {
		color: #FFFFFF;
		background: linear-gradient(132deg, #2DD9FF 0%, #2D6DFF 100%);
	}

	text {
		padding: 5px 6px;
		border-left: 1px solid #EEEEEE;
	}

	text:first-child {
		border-left: none
	}
}
</style>