<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,
		},
		textCols: {
			type: Number,
			require: false,
			default: 8,
		},
		labelCols: {
			type: Number,
			require: false,
			default: 4,
		},
	},
	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("주기에 따른 오늘 기준 기본 날짜 세팅");
			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_WEEK':
					endDate = today;
					srartDate = Utility.setBeforetDate(
						this.searchParam[this.parentPrgmId],
						endDate,
						'YYYYMMDD',
					);
					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;
			}
			this.setPageData({ fromDt: srartDate, toDt: endDate });
			// console.log(this.searchParam[this.parentPrgmId].cmCycle);
			// console.log(this.searchParam[this.parentPrgmId].dateRange);
		},
	},
};
</script>

<style></style>
