<template>
	<v-row class="search-box" align="center" no-gutters>
		<v-col :cols="label ? textCols : ''">
			<v-text-field
				readonly
				v-model="selectValue"
				append-icon="mdi-magnify"
				class="v-input__custom"
				@click="dialogOpenCloseEvent(dialog)"
				outlined
				:hide-details="true"
			></v-text-field>
		</v-col>

		<v-dialog v-model="dialog" scrollable width="540px">
			<v-card style="height: 100%">
				<v-card-title class="pa-5 d-flex align-center justify-space-between">
					<span class="custom-title-4">{{ modalTitle }}</span>
					<v-btn
						icon
						tile
						:ripple="false"
						@click="dialogOpenCloseEvent(dialog)"
					>
						<v-icon>mdi-close</v-icon>
					</v-btn>
				</v-card-title>
				<div class="pa-5">
					<v-row align="center" no-gutters>
						<v-col :cols="2">
							<label for="" class="search-box-label">
								<v-icon x-small color="primary" class="mr-1"
									>mdi-record-circle</v-icon
								>
								검색
							</label>
						</v-col>
						<v-col :cols="6">
							<v-text-field
								append-icon="mdi-magnify"
								class="v-input__custom"
								outlined
								:hide-details="true"
								v-model="searchWord"
								@keyup.enter="search"
							></v-text-field>
						</v-col>
						<v-spacer></v-spacer>
						<v-col cols="4" class="text-right">
							<v-btn :ripple="false" @click="search()">
								조회
							</v-btn>
							<!-- <v-btn :ripple="false" @click="initSearch()">
                초기화
              </v-btn> -->
						</v-col>
					</v-row>
				</div>
				<v-divider></v-divider>
				<div :style="'height: calc(50vh)'">
					<!-- <div :style="{ height: 'calc(100% - 213px)' }"> -->
					<div ref="modalGridParent" class="h100 py-3">
						<component
							:ref="gridName + parentPrgmId"
							:is="loadGrid && dialog ? 'Grid' : null"
							:gridName="gridName"
							:dataPath="searchParam.modalData2.selectTagPop"
							:parentPrgmId="parentPrgmId"
							@getRowsData="getRowData"
							@dblClick="setUpdate()"
							@watchReadPlcPop="watchReadPlcPop"
						/>
					</div>
				</div>
				<v-card-actions class="pa-5 d-flex align-center justify-center">
					<v-btn :ripple="false" @click="setUpdate()">확인</v-btn>
					<v-btn :ripple="false" @click="dialogOpenCloseEvent(dialog)"
						>닫기</v-btn
					>
				</v-card-actions>
			</v-card>
		</v-dialog>
	</v-row>
</template>

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

export default {
	props: {
		parentPrgmId: {
			type: String,
			require: true,
		},
		label: {
			type: String,
			require: false,
		},
		labelCols: {
			type: Number,
			require: false,
			default: 4,
		},
		labelContent: {
			type: String,
			require: false,
			default: '태그',
		},
		textCols: {
			type: Number,
			require: false,
			default: 7,
		},
		modalTitle: {
			type: String,
			require: false,
			default: '태그명',
		},
		modalContent: {
			type: String,
			require: false,
			default: '태그명 검색',
		},
	},
	components: {
		Grid,
		Utility,
	},
	data() {
		return {
			dialog: false,

			loadGrid: false,
			gridName: 'grid_01',
			myModalKey: 'selectTagPop',
			modalDataKey: 'modalData2',

			searchWord: '',
			selectedData: {},
			textFieldData: '',
			searchedData: [],
		};
	},
	computed: {
		...mapState({
			searchParam(state) {
				return state.pageData[this.parentPrgmId];
			},
			isDarkMode: 'isDarkMode',
		}),
		chkDialog() {
			// 모달 열기/닫기 값
			return this.dialog;
		},
		selectValue() {
			const tagData = this.searchParam.tagData;
			return tagData.tagNm;
		},
		chkReadPlc() {
			return this.searchParam.readPlcId;
		},
	},
	watch: {
		async dialog(val) {
			if (val) {
				await this.getGridData();
			}
		},
		chkReadPlc() {
			// console.log(this.searchParam.readPlcId);
			if (!this.dialog) {
				this.getGridData();
			}
		},
	},
	beforeCreate() {
		this.$store.commit('setPageData', {
			modalData2: { selectTagPop },
		});
	},
	created() {
		this.init();
		this.setPageData({
			tagId:
				localStorage.getItem(this.parentPrgmId + 'CheckedRow2') != null
					? JSON.parse(localStorage.getItem(this.parentPrgmId + 'CheckedRow2'))
					: {},
		});
	},
	mounted() {},
	methods: {
		...mapMutations({
			setPageData: 'setPageData',
			setModalGridData: 'setModalGridData',
			setModalGridColumn: 'setModalGridColumn',
			setModalGridOption: 'setModalGridOption',
		}),
		...mapActions({
			postApiReturn: 'modules/list/postApiReturn',
			setTree: 'modules/list/setTree',
		}),
		init() {
			this.activeRowData = {};
			this.gridInit();
		},
		watchReadPlcPop() {
			if (!this.dialog) {
				this.getGridData();
			}
		},
		search() {
			this.getGridData();
		},
		initSearch() {
			this.searchWord = '';
			this.getGridData();
		},
		gridInit() {
			const myOptions = {
				columnOptions: {
					resizable: true,
				},
			};
			this.setModalGridOption({
				modalKey: this.myModalKey,
				gridKey: this.gridName,
				modalDataKey: this.modalDataKey,
				value: Object.assign(
					// Utility.defaultGridOption(this.$refs.modalGridParent.offsetHeight - 60, myOptions),
					Utility.defaultGridOption(400, myOptions),
					myOptions,
				),
			});
			this.setModalGridColumn({
				modalKey: this.myModalKey,
				gridKey: this.gridName,
				modalDataKey: this.modalDataKey,
				value: [
					// { header: "TAG ID", name: "tagId" },
					{ header: 'TAG 명', name: 'tagNm' },
				],
			});
			this.getGridData();
		},
		async getGridData() {
			this.loadGrid = false;
			let searchparams = {
				readPlc: '',
			};
			searchparams['readPlc'] =
				this.searchParam.readPlcId.readPlcId == undefined
					? ''
					: this.searchParam.readPlcId.readPlcId;
			searchparams['searchWord'] = this.searchWord;

			const res = await this.postApiReturn({
				apiKey: 'selectTagNmList',
				resKey: 'tagNmListData',
				sendParam: searchparams,
			});
			this.searchedData = res;

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

			this.loadGrid = true;
			if (res.length > 0 && !this.dialog) {
				this.setPageData({ tagData: res[0] });
			} else if (res.length == 0 && !this.dialog) {
				this.setPageData({ tagData: { tagId: 'noSearchTagId' } });
			}

			this.$nextTick(() => {
				if (res.length > 0) {
					try {
						this.$refs['grid_01' + this.parentPrgmId].focus({
							rowKey: 0,
							columnName: 'tagNm',
							setScroll: true,
						});
					} catch (error) {}
				}
			});
		},
		getRowData(data) {
			this.selectedData = { ...data };
		},
		setUpdate() {
			this.dialog = false;
			this.setPageData({ tagData: this.selectedData });
		},
		dialogOpenCloseEvent(val) {
			this.searchWord = '';
			this.dialog = !val;
		},
	},
};

var selectTagPop = {
	grid_01: {
		data: [],
		column: [],
		option: {},
	},
};
</script>

<style lang="scss" scoped>
::v-deep {
	.v-dialog {
		overflow-y: hidden !important;
	}

	.tui-grid-table-container {
		.tui-grid-table {
			border-right-style: solid !important;
			border-right-color: rgba(255, 255, 255, 0.1) !important;
		}
	}

	.tui-grid-cell.tui-grid-cell-has-tree
		.tui-grid-tree-extra-content
		+ .tui-grid-cell-content:before {
		content: none !important;
	}
}

@each $theme in dark, light {
	.v-application.#{$theme}-mode {
		.v-dialog {
			.v-card {
				&__title {
					color: map-deep-get($color, 'white', '0');
					@if $theme == dark {
						background-color: #2d3355;
						.v-btn {
							background-color: #2d3355;
						}
					} @else {
						background-color: #3f4d7d;
						.v-btn {
							background-color: #3f4d7d;
						}
					}
				}
			}
		}
	}
}
</style>
