import {Component, OnInit, EventEmitter, Output, Input} from '@angular/core'; declare const moment: any; @Component({ selector: 'dateTimeSearchGroup', templateUrl: './template.html', styleUrls: ['./style.less'] }) export default class DateTimeSearchGroup implements OnInit { private data: any = {}; ngOnInit(): void { } startDateValue: string; @Output() startDateChange = new EventEmitter(); @Input() get startDate() { return this.startDateValue; } set startDate(value) { this.startDateValue = value; this.startDateChange.emit(this.startDateValue); this.data.searchType == 'range' && (this.data.startDate = this.startDate); } endDateValue: string; @Output() endDateChange = new EventEmitter(); @Input() get endDate() { return this.endDateValue; } set endDate(value) { this.endDateValue = value; this.endDateChange.emit(this.endDateValue); this.data.searchType == 'range' && (this.data.endDate = this.endDate); } searchTypeValue: string; @Output() searchTypeChange = new EventEmitter(); @Input() get searchType() { return this.searchTypeValue; } set searchType(value) { this.searchTypeValue = value; this.searchTypeChange.emit(this.searchTypeValue); this.data.searchType = this.searchType; if (this.searchType == 'range') { this.data.startDate = this.startDate; this.data.endDate = this.endDate; } } changeType(value) { this.data.searchType = value; this.setValue(); if (this.data.searchType != 'range') { this.data.startDate = ''; this.data.endDate = ''; } } setStartDate(value) { this.data.startDate = value; this.setValue(); } setEndDate(value) { this.data.endDate = value; this.setValue(); } setValue() { let startDate = ''; let endDate = ''; if (this.data.searchType == 'range') { startDate = this.data.startDate; endDate = this.data.endDate; } else if (this.data.searchType == 'month') { startDate = moment().subtract(1, 'months').format('YYYY-MM-DD'); endDate = moment().format("YYYY-MM-DD"); } else if (this.data.searchType == 'quarter') { const quarter = moment().quarter(); startDate = moment().month((quarter - 1) * 3 + 1).format('YYYY-MM-DD'); endDate = moment().month((quarter - 1) * 3 + 3).format("YYYY-MM-DD"); } else if (this.data.searchType == 'year') { startDate = moment().subtract(5, 'months').format('YYYY-MM-DD'); endDate = moment().format("YYYY-MM-DD"); } this.startDate = startDate; this.endDate = endDate; this.searchType = this.data.searchType; } }