<template>
	<v-row class="search-box" align="center">
		<v-col :cols="option.labelCols" class="py-0">
			<label for="" class="search-box-label">
				<v-icon v-if="item.iconShow" small
					:class="['mr-1', item.required ? 'icon-orange' : 'icon-blue']">$icoBulletPoint</v-icon>
				{{ option.labelContent }}
			</label>
		</v-col>
		<v-col :cols="option.textCols" class="py-0">
			<v-text-field
				readonly
				v-model="selectValue"
				append-icon="mdi-magnify"
				:class="['v-select__custom', customClass]"
				@click="dialogOpenCloseEvent(dialog)"
				outlined
				:hide-details="true"
				:required="item.required || false"
				:disabled="item.disabled || false"
			>
			<template v-slot:append>
				<!-- Custom SVG icon -->
				<v-icon>$icoSearch</v-icon>
			</template>
			</v-text-field>
		</v-col>

		<v-dialog v-model="dialog" scrollable width="1000px">
			<v-card style="height: 100%">
				<v-card-title class="pa-5 d-flex align-center justify-space-between">
					<span class="custom-title-4">{{ option.modalTitle }}</span>
					<v-icon @click="dialogOpenCloseEvent(dialog)">mdi-close</v-icon>
				</v-card-title>
				<v-row align="end" class="px-5 pb-3" no-gutters>
					<v-col :cols="2.5" class="mr-2">
						<v-row>
							<v-col :cols="12" class="py-0">
								<label for="" class="search-box-label">
									<!-- <v-icon small 
									:class="['mr-1', item.required ? 'icon-orange' : 'icon-blue']"
									>$icoBulletPoint</v-icon> -->
									검색
								</label>
							</v-col>
							<v-col :cols="11" class="py-0 pr-3">
								<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>
							<a-button icon="search" :ripple="false" type="primary" @click="search()"
								class="search-button">조회</a-button>
						</v-row>
					</v-col>
				</v-row>
				<!-- <div :style="'height: calc(65vh)'"> -->
				<div :style="'height: 620px'" class="px-5">
					<!-- <div :style="{ height: 'calc(100% - 213px)' }"> -->
					<div ref="modalGridParent" class="h100 py-3">
						<!-- :is="loadGrid && dialog ? 'Grid' : null" -->
						<component :is="loadGrid && dialog ? 'Grid' : null" :gridName="grid_01"
							:dataPath="searchParam.modalData2.evtObjPop" :parentPrgmId="parentPrgmId"
							@getRowsData="getRowData" @dblClick="setUpdate($event)" />
					</div>
				</div>
				<v-card-actions class="pa-5 d-flex align-center justify-end">
					<a-button type="default" class="mr-2" :ripple="false"
						@click="dialogOpenCloseEvent(dialog)">닫기</a-button>
					<a-button type="primary" class="mr-2" :ripple="false" @click="setUpdate('init')">초기화</a-button>
					<a-button type="primary" :ripple="false" @click="setUpdate($event)">확인</a-button>
				</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,
		},
		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: '검색',
		},
		item: {
			type: Object,
			require: false,
		},
		bindingData: {
			type: String,
			require: false,
		},
		customClass: {
			type: String,
			require: false,
		},
		// bindingFlag:{
		//   type:Boolean,
		//   require: false,
		//   default: false
		// }
	},
	components: {
		Grid,
		Utility,
	},
	data() {
		return {
			dialog: false,

			loadGrid: false,
			grid_01: 'grid_01',
			myModalKey: 'evtObjPop',
			modalDataKey: 'modalData2',

			searchWord: '',
			selectedData: {},
			textFieldData: '',
			formFlag: this.item.formFg != undefined ? this.item.formFg : true,

			option: {
				labelCols:
					this.item !== undefined
						? this.item.labelCols !== undefined
							? this.item.labelCols
							: this.labelCols
						: this.labelCols,
				labelContent:
					this.item !== undefined
						? this.item.labelContent !== undefined
							? this.item.labelContent
							: this.labelContent
						: this.labelContent,
				textCols:
					this.item !== undefined
						? this.item.textCols !== undefined
							? this.item.textCols
							: this.textCols
						: this.textCols,
				modalTitle:
					this.item !== undefined
						? this.item.modalTitle !== undefined
							? this.item.modalTitle
							: this.modalTitle
						: this.modalTitle,
			},
		};
	},
	computed: {
		...mapState({
			searchParam(state) {
				return state.pageData[this.parentPrgmId];
			},
			myBindingData(state) {
				if (!this.bindingData) {
					return state.pageData[this.parentPrgmId]['rowGridSelectData'];
				} else {
					return state.pageData[this.parentPrgmId][this.bindingData][
						'rowGridSelectData'
					];
				}
			},
		}),
		selectValue: {
			get() {
				if (this.formFlag) {
					if (!this.item) {
						return this.textFieldData;
					}
					// else if(this.item.disableContent){
					//   return "NONE";
					// }
					else {

						return this.myBindingData
							? this.myBindingData[this.item.valueNm]
							: this.textFieldData;
					}
				} else {
					// grid와 바인딩 안했을 때
					return this.textFieldData;
				}
			},
			set(value) {
				// if(this.item && this.item.disableContent){
				//   return "NONE";
				// }
				return value;
			},
		},
	},
	watch: {
		async dialog(val) {
			if (val) {
				await this.getGridData();
			}
		},
	},

	beforeCreate() {
		this.$store.commit('setPageData', {
			modalData2: { evtObjPop },
		});
	},
	created() {
		this.init();
	},
	mounted() { },
	methods: {
		...mapMutations({
			setPageData: 'setPageData',
			setModalGridData: 'setModalGridData',
			setModalGridColumn: 'setModalGridColumn',
			setModalGridOption: 'setModalGridOption',
		}),
		...mapActions({
			postApiReturn: 'modules/list/postApiReturn',
			setTree: 'modules/list/setTree',
		}),
		init() {
			this.gridInit();
		},
		search() {
			this.getGridData();
		},
		initSearch() {
			this.searchWord = '';
			this.getGridData();
		},
		gridInit() {
			const myOptions = {
				columnOptions: {
					resizable: true,
				},
				header: {
					height: 38,
				},
			};
			this.setModalGridOption({
				modalKey: this.myModalKey,
				gridKey: this.grid_01,
				modalDataKey: this.modalDataKey,
				value: Object.assign(
					// Utility.defaultGridOption(this.$refs.modalGridParent.offsetHeight - 60, myOptions),
					Utility.defaultGridOption(570, myOptions),
					myOptions,
				),
			});

			this.setModalGridColumn({
				modalKey: this.myModalKey,
				gridKey: this.grid_01,
				modalDataKey: this.modalDataKey,
				value: [
					{ header: 'TAG ID', name: 'tagId' },
					{ header: 'TAG 명', name: 'tagNm' },
				],
			});
		},
		async getGridData() {
			this.loadGrid = false;

			const res = await this.postApiReturn({
				apiKey: 'selectTagBaseInfo',
				resKey: 'tagBaseInfoData',
				sendParam: {
					tagNmLike: this.searchWord,
				},
			});

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

			this.loadGrid = true;
		},
		getRowData(data) {
			this.selectedData = { ...data };
		},
		setUpdate(v) {
			// 초기화
			if (v == 'init') {
				this.selectedData.tagId = null;
				this.selectedData.tagNm = null;
			}
			this.dialog = false;
			if (!this.item) {
				this.textFieldData = this.selectedData.tagId;
			} else {
				// let formFg = this.item.formFg != undefined ? this.item.formFg : this.formFg;
				if (this.formFlag) {
					if (this.item.bindNm) {
						this.myBindingData[this.item.bindNm] = this.selectedData.tagNm;
					} else {
						this.myBindingData.tagNm = this.selectedData.tagNm;
					}
					// this.myBindingData.tagNm = this.selectedData.tagNm;
					const dt = {
						columnName: this.item.valueNm,
						value: this.selectedData.tagId,
					};
					this.$emit('gridEditingFinish', dt);
					if (this.item.valueNm2 !== undefined) {
						const dt = {
							columnName: this.item.valueNm2,
							value: this.selectedData[this.item.valueNm2],
						};
						this.$emit('gridEditingFinish', dt);
					}
					if (this.item.bindNm !== undefined) {
						const dt = {
							columnName: this.item.bindNm,
							value: this.selectedData.tagNm,
						};
						this.$emit('gridEditingFinish', dt);
					}
				} else {
					// grid와 binding 안되어있는 경우
					this.textFieldData = this.selectedData.tagNm;
					this.setPageData({
						tagId: this.selectedData.tagId,
						// [this.item.tagNm]: this.selectedData.tagNm
					});

				}

			}
		},
		dialogOpenCloseEvent(val) {
			// if(this.item && this.item.disableContent){
			//   this.dialog = false;
			//   return;
			// }
			this.dialog = !val;
		},
	},
};

var evtObjPop = {
	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;
	}
}
</style>
