<template>
	<v-row class="search-box">
		<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].evtCd2List"
				item-text="commCdNm"
				item-value="idx"
				solo
				outlined
				:hide-details="true"
				append-icon="mdi-chevron-down"
				class="v-select__custom"
			></v-select>
		</v-col>
	</v-row>
</template>

<script>
import { mapState, mapMutations, mapActions } from 'vuex';
export default {
	props: {
		parentPrgmId: {
			type: String,
			require: true,
		},
		autoLoad: {
			type: Boolean,
			require: false,
			default: false,
		},
		label: {
			type: String,
			require: false,
			default: '소분류',
		},
		addAll: {
			type: Boolean,
			require: false,
			default: false,
		},
		textCols: {
			type: Number,
			require: false,
			default: 7,
		},
		labelCols: {
			type: Number,
			require: false,
			default: 4,
		},
	},
	data() {
		return {};
	},
	computed: {
		...mapState({
			searchParam: state => state.pageData,
		}),
		reLoadFlag() {
			var returnValue = false;

			if (this.searchParam[this.parentPrgmId].evtCd1 != null) {
				returnValue = this.searchParam[this.parentPrgmId].evtCd1;
			}

			return returnValue;
		},
		selectValue: {
			get() {
				return this.searchParam[this.parentPrgmId].evtCd2;
			},
			set(value) {
				return this.setPageData({ evtCd2: value });
			},
		},
		evtCd1Flag() {
			return this.searchParam[this.parentPrgmId].evtCd1;
		},
	},
	watch: {
		async reLoadFlag() {
			await this.initData();
		},
	},
	async created() {
		if (this.autoLoad && this.reLoadFlag) {
			await this.initData();
		}
	},
	methods: {
		...mapMutations({ setPageData: 'setPageData' }),
		...mapActions({
			postApi: 'modules/list/postApi',
			postUpdateApi: 'modules/list/postUpdateApi',
			postApiReturn: 'modules/list/postApiReturn',
			setTree: 'modules/list/setTree',
			chkOpenTabList: 'chkOpenTabList',
		}),
		async initData() {
			var evtCd2List = await this.postApiReturn({
				apiKey: 'selectCommCd',
				resKey: 'commCdData',
				sendParam: {
					commGrpCd: 'EM_EVT2',
					userDefVal1: this.searchParam[this.parentPrgmId].evtCd1List[
						this.searchParam[this.parentPrgmId].evtCd1
					].commCd,
					useFg: '1',
				},
			});

			if (this.addAll) {
				evtCd2List.unshift({
					commCdNm: '전체',
					commCd: '',
				});
			}

			var tempIdx = 0;
			evtCd2List.map(item => {
				item.idx = tempIdx++;
			});

			this.setPageData({ evtCd2: 0, evtCd2List: evtCd2List });
		},
	},
};
</script>

<style></style>
