<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-if="loadSelectBox"
				v-model="selectValue"
				:items="searchParam[parentPrgmId].snarEnergyList"
				item-text="ercNm"
				item-value="ercId"
				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: true,
		},
		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 {
			loadSelectBox: false,
		};
	},
	computed: {
		...mapState({
			searchParam: state => state.pageData,
		}),
		selectValue: {
			get() {
				return this.searchParam[this.parentPrgmId].snarEnergyCd;
			},
			set(value) {
				return this.setPageData({ snarEnergyCd: value });
			},
		},
	},
	async created() {
		if (this.autoLoad) {
			await this.initData();
		}
	},
	methods: {
		...mapMutations({ setPageData: 'setPageData' }),
		...mapActions({
			postApi: 'modules/list/postApi',
			postUpdateApi: 'modules/list/postUpdateApi',
			postApiReturn: 'modules/list/postApiReturn',
			chkOpenTabList: 'chkOpenTabList',
		}),
		async initData() {
			this.loadSelectBox = false;
			var snarEnergyList = await this.postApiReturn({
				apiKey: 'selectErcInfoList',
				resKey: 'snarEnergyData',
				sendParam: {
					useFg: '1',
				},
			});
			if (this.addAll) {
				snarEnergyList.unshift({
					ercNm: '전체',
					ercId: '',
				});
			}

			/*
      var tempIdx = 0;
      snarEnergyList.map(item => {
        item.idx = tempIdx++;
      })
      */

			this.setPageData({ snarEnergyCd: '', snarEnergyList: snarEnergyList });
			this.loadSelectBox = true;
		},
	},
};
</script>

<style></style>
