<template>
	<v-row
		class="search-box"
		align="center"
		no-gutters
		style="margin: 14px 0px 0px"
	>
		<v-col v-if="item.label" :cols="labelCols">
			<label for="" class="search-box-label">
				<v-icon x-small color="primary" class="mr-1">mdi-record-circle</v-icon>
				{{ item.label }}
			</label>
		</v-col>
		<v-col :cols="labelCols ? 11 - labelCols : ''">
			<v-select
				v-model="selectValue"
				:items="searchParam.cmCycleList"
				item-text="text"
				item-value="code"
				solo
				hide-details
				append-icon="mdi-chevron-down"
				class="v-select__custom"
			></v-select>
		</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,
		},
		item: {
			type: Object,
			require: true,
		},
	},
	data() {
		return {};
	},
	computed: {
		...mapState({
			searchParam(state) {
				if (!this.item.dataPath) {
					return state.pageData[this.parentPrgmId];
				} else {
					return state.pageData[this.parentPrgmId][this.item.dataPath];
				}
			},
		}),
		selectValue: {
			get() {
				return this.searchParam.cmCycle;
			},
			set(value) {
				let cmCycleObj = { cmCycle: value };
				if (!this.item.dataPath) {
					return this.setPageData(cmCycleObj);
				} else {
					return this.setDataPathPageData({
						pathKey: this.item.dataPath,
						data: cmCycleObj,
					});
				}
			},
		},
		labelCols() {
			let myCols = 0;
			if (this.item.label) {
				myCols = this.item.labelCols || '4';
			}
			return myCols;
		},
	},
	watch: {
		selectValue(value) {
			// 주기에 따른 오늘 기준 기본 날짜 세팅
			this.setDefaultDate(value);
		},
	},
	created() {
		this.setDefaultDate(this.searchParam.cmCycle);
	},
	async mounted() {},
	methods: {
		...mapMutations({
			setPageData: 'setPageData',
			setDataPathPageData: 'setDataPathPageData',
		}),
		...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, endDate, 'YYYY');
					break;

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

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

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

				default:
					break;
			}

			const dtObj = { fromDt: srartDate, toDt: endDate };
			if (!this.item.dataPath) {
				this.setPageData(dtObj);
			} else {
				this.setDataPathPageData({
					pathKey: this.item.dataPath,
					data: dtObj,
				});
			}
			// console.log(this.searchParam.cmCycle);
			// console.log(this.searchParam.dateRange);
		},
	},
};
</script>

<style></style>
