<template>
	<v-banner>
		<div
			class="d-flex align-center justify-space-between"
			style="cursor:pointer"
			@click="dialog = !dialog"
		>
			<v-card-subtitle class="pa-0">미사용개소</v-card-subtitle>
			<v-avatar
				size="36"
				:color="isDarkMode ? '#fb5a8b' : '#ff7b8b'"
				min-width="72"
				class="font-weight-bold text-color--white-0"
				>{{ pointNoUse }}</v-avatar
			>
		</div>
		<v-dialog v-model="dialog" scrollable width="600px">
			<v-card style="height: 100%">
				<v-card-title>
					<span class="custom-title-4">미사용 개소 조회</span>
				</v-card-title>
				<v-card-actions>
					<v-col>
						<v-text-field
							append-icon="mdi-magnify"
							class="v-input__custom"
							v-model="searchWord"
							@keyup.enter="typeEnterKey"
						></v-text-field>
					</v-col>
				</v-card-actions>
				<v-divider></v-divider>
				<v-card-text>
					<div ref="rowGridParent" style="height: 300px">
						<component
							:ref="gridName + parentPrgmId"
							:is="loadGrid && dialog ? 'Grid' : null"
							:gridName="gridName"
							:dataPath="searchParam.unUsedData.selectUnusedReadPlc"
							:parentPrgmId="parentPrgmId"
						/>
					</div>
				</v-card-text>
				<v-divider></v-divider>
				<v-card-actions>
					<v-spacer></v-spacer>
					<v-btn color="primary" dark>확인</v-btn>
					<v-btn color="primary" dark @click="dialog = false">닫기</v-btn>
				</v-card-actions>
			</v-card>
		</v-dialog>
	</v-banner>
</template>

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

export default {
	props: {
		parentPrgmId: {
			type: String,
			require: true,
		},
		pointNoUse: {
			type: Number,
			require: true,
		},
	},
	components: {
		Grid,
	},
	data() {
		return {
			modalDataKey: 'unUsedData',
			myModalKey: 'selectUnusedReadPlc',
			gridName: 'unUsedGrid',
			loadGrid: false,
			searchWord: '',
			dialog: false,
		};
	},
	computed: {
		...mapState({
			// searchParam: state => state.pageData,
			isDarkMode: 'isDarkMode',
			searchParam(state) {
				return state.pageData[this.parentPrgmId];
			},
		}),
		chkDialog() {
			// 모달 열기/닫기 값
			return this.dialog;
		},
	},
	watch: {
		async chkDialog(val) {
			if (val) {
				// 열릴 때
				await this.getData();
			} else {
				// 닫힐 때
			}
		},
	},
	beforeCreate() {
		if (this.$store.state.activeMenuInfo.prgmId == '0') {
			this.$store.state.activeMenuInfo.prgmId = this.$route.query.prgmId;
		}
		this.$store.commit('setPageData', {
			unUsedData: { selectUnusedReadPlc },
		});
		// console.log("2.vue::beforeCreate");
	},
	created() {
		this.init();
	},
	methods: {
		...mapMutations({
			setPageData: 'setPageData',
			setModalGridData: 'setModalGridData',
			setModalGridColumn: 'setModalGridColumn',
			setModalGridOption: 'setModalGridOption',
		}),
		...mapActions({
			postApiReturn: 'modules/list/postApiReturn',
			// setTree: "modules/list/setTree"
		}),
		init() {
			this.gridInit();
		},
		gridInit() {
			const gridHeight = 290;
			//const gridHeight = this.$refs.rowGridParent.offsetHeight - 30;

			const myOptions = {};
			this.setModalGridOption({
				modalKey: this.myModalKey,
				gridKey: this.gridName,
				modalDataKey: this.modalDataKey,
				value: Object.assign(Utility.defaultGridOption(gridHeight), myOptions),
			});
			this.setModalGridColumn({
				modalKey: this.myModalKey,
				gridKey: this.gridName,
				modalDataKey: this.modalDataKey,
				value: myColumns,
			});
		},
		async getData() {
			this.loadGrid = false;
			let sendParams = {
				readPlcNm: this.searchWord,
			};

			const res = await this.postApiReturn({
				apiKey: 'selectUnusedReadPlc',
				resKey: 'unusedData',
				sendParam: sendParams,
			});

			this.setModalGridData({
				modalKey: this.myModalKey,
				gridKey: this.gridName,
				modalDataKey: this.modalDataKey,
				value: res,
			});

			this.loadGrid = true;
		},
		// 검색
		typeEnterKey() {
			this.getData();
		},
	},
};

const selectUnusedReadPlc = {
	unUsedGrid: {
		data: [],
		column: [], // myColumns,
		option: {}, // myOptions
	},
};

const myColumns = [
	{ header: '사업장', name: 'blocNm', width: 100, align: 'center' },
	{ header: '에너지원', name: 'readObjNm', width: 100, align: 'center' },
	{ header: '검침개소명', name: 'readPlcNm', width: 240 },
	{ header: '값', name: 'useVal', width: 100, align: 'right' },
];
</script>

<style scoped>
.v-input {
	font-size: 12px;
}
.v-label {
	font-size: 10px;
}
</style>
