<template>
	<v-row class="search-box" align="center" no-gutters>
		<v-col v-if="label" :cols="labelCols">
			<label for="" class="search-box-label">
				<v-icon x-small color="primary" class="mr-1">mdi-record-circle</v-icon>
				{{ label }}
			</label>
		</v-col>
		<v-col :cols="label ? textCols : ''">
			<v-select
				v-model="selectValue"
				:items="searchParam[parentPrgmId].cmCycleList"
				label="주기를 선택하세요"
				item-text="text"
				item-value="value"
				solo
				outlined
				:hide-details="true"
				append-icon="mdi-chevron-down"
				class="v-select__custom"
			></v-select>
			<!-- @change="updateBlocCode($event)" -->
		</v-col>
	</v-row>
</template>

<script>
import { mapState, mapMutations, mapActions } from 'vuex';
import Utility from '~/plugins/utility';

export default {
	props: {
		parentPrgmId: {
			type: String,
			require: true,
		},
		pickerMode: {
			type: String,
			require: true,
		},
		labelCols: {
			type: Number,
			require: false,
			default: 4,
		},
		textCols: {
			type: Number,
			require: false,
			default: 8,
		},
		label:{
			type: String,
			require: false,
			default: '주기'
		}
	},
	data() {
		return {
			// label: '주기',
			labelPrepend: true,
		};
	},
	computed: {
		...mapState({
			searchParam: state => state.pageData,
		}),
		selectValue: {
			get() {
				return this.searchParam[this.parentPrgmId].cmCycle;
			},
			set(value) {
				return this.setPageData({ cmCycle: value });
			},
		},
	},
	watch: {
		selectValue(value) {
			// 주기에 따른 오늘 기준 기본 날짜 세팅
			this.setDefaultDate(value);
		},
	},
	created() {
		this.setDefaultDate(this.searchParam[this.parentPrgmId].cmCycle);
	},
	async mounted() {},
	methods: {
		...mapMutations({ setPageData: 'setPageData' }),
		...mapActions({}),
		setDefaultDate(value) {
			// console.log("주기에 따른 오늘 기준 기본 날짜 세팅");
			if(this.searchParam[this.parentPrgmId].dateInitedFlag){
				return;
			}
			const today = Utility.setFormatDate('today', 'YYYYMMDD');
			let srartDate = '';
			let endDate = '';
			// console.log(value);
			switch (value) {
				case 'CYC_YEAR':
					endDate = Utility.setFormatDate(today, 'YYYY');
					srartDate = Utility.setBeforetDate(
						this.searchParam[this.parentPrgmId],
						endDate,
						'YYYY',
					);
					break;

				case 'CYC_MONTH':
					endDate = Utility.setFormatDate(today, 'YYYYMM');
					srartDate = Utility.setBeforetDate(
						this.searchParam[this.parentPrgmId],
						endDate,
						'YYYYMM',
					);
					break;

				case 'CYC_DAY':
					endDate = today;
					srartDate = Utility.setBeforetDate(
						this.searchParam[this.parentPrgmId],
						endDate,
						'YYYYMMDD',
					);
					break;

				case 'CYC_HOUR':
					endDate = today;
					srartDate = today;
					break;

				default:
					break;
			}
			if (
				this.pickerMode == undefined ||
				this.pickerMode == null ||
				this.pickerMode == ''
			) {
				this.setPageData({ fromDt: srartDate, toDt: endDate });
			} else if (this.pickerMode == 'single') {
				this.setPageData({ fromDt: endDate, toDt: srartDate });
			} else {
				this.setPageData({ fromDt: srartDate, toDt: endDate });
			}
			// console.log(this.searchParam[this.parentPrgmId].cmCycle);
			// console.log(this.searchParam[this.parentPrgmId].dateRange);
		},
	},
};
</script>

<style></style>
