<template>
	<v-row class="search-box" align="center" no-gutters>
		<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-text-field
				readonly
				v-model="selectValue"
				append-icon="mdi-magnify"
				class="v-input__custom"
				@click="dialog = !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>
					<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.modalData.selectUserPop"
							:parentPrgmId="parentPrgmId"
							@getRowsData="getRowData"
							@dblClick="setUpdate()"
						/>
					</div>
				</v-card-text>
				<v-divider></v-divider>
				<v-card-actions>
					<v-spacer></v-spacer>
					<v-btn color="primary" dark @click="setUpdate()">확인</v-btn>
					<v-btn color="primary" dark @click="dialog = false">닫기</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,
		},
		labelCols: {
			type: Number,
			require: false,
			default: 4,
		},
		textCols: {
			type: Number,
			require: false,
			default: 7,
		},
	},
	components: {
		Grid,
	},
	data() {
		return {
			label: '사용자',
			modalDataKey: 'modalData',
			myModalKey: 'selectUserPop',
			gridName: 'rowGrid',
			loadGrid: false,
			searchWord: '',
			dialog: false,
		};
	},
	computed: {
		...mapState({
			// searchParam: state => state.pageData,
			searchParam(state) {
				return state.pageData[this.parentPrgmId];
			},
		}),
		chkDialog() {
			// 모달 열기/닫기 값
			return this.dialog;
		},
		selectValue() {
			// 선택된 공장/설비 값
			return this.searchParam.selecUserList.length > 0
				? this.searchParam.selecUserList.map(item => item.userNm)
				: null;
		},
	},
	watch: {
		async chkDialog(val) {
			if (val) {
				// 열릴 때
				await this.getData();
				// console.log("chkDialog: ", val);
				if (this.searchParam.selecUserList.length > 0) {
					for (var i = 0; i < this.searchParam.selecUserList.length; i++) {
						this.$refs[this.gridName + this.parentPrgmId].checkEvt(
							this.searchParam.selecUserList[i],
							this.$refs[this.gridName + this.parentPrgmId],
						);
					}
				}
				// this.$refs[this.gridName + this.parentPrgmId].setCheck(
				//   this.searchParam.selecUserListRowKeys
				// );
			} else {
				// 닫힐 때
				this.setPageData({
					myUserList:
						this.searchParam.selecUserList.length > 0
							? this.searchParam.selecUserList.map(item => item.userNo)
							: '',
				});
			}
		},
	},

	beforeCreate() {
		this.$store.commit('setPageData', {
			modalData: { selectUserPop },
		});
		// 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 = 490; // this.$refs.rowGridParent.offsetHeight - 30;

			const myOptions = {
				rowHeaders: ['checkbox'],
			};
			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 = {
				userNm: this.searchWord,
				userNo: '',
				userLoginId: '',
				deptId: '',
			};

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

			this.setModalGridData({
				modalKey: this.myModalKey,
				gridKey: this.gridName,
				modalDataKey: this.modalDataKey,
				value: res,
			});
			this.loadGrid = true;
		},
		// 공정/설비 트리 row 클릭이벤트
		getRowData(data) {
			//this.activeRowData = data;
			if (data._attributes.checked) {
				this.$refs[this.gridName + this.parentPrgmId].uncheckEvt(
					data,
					this.$refs[this.gridName + this.parentPrgmId],
				);
			} else {
				this.$refs[this.gridName + this.parentPrgmId].checkEvt(
					data,
					this.$refs[this.gridName + this.parentPrgmId],
				);
			}
		},
		setUpdate() {
			// console.log("이게뭐야", this.$refs[this.gridName + this.parentPrgmId])
			this.dialog = false;
			this.setPageData({
				selecUserList: this.$refs[
					this.gridName + this.parentPrgmId
				].getCheckedRowsEvt(),
			});
			// this.setPageData({
			//   selecUserListRowKeys: this.$refs[
			//     this.gridName + this.parentPrgmId
			//   ].getCheckedRowKeys()
			// });
		},
		// 검색
		typeEnterKey() {
			this.getData();
		},
	},
};

const selectUserPop = {
	rowGrid: {
		data: [],
		column: [], // myColumns,
		option: {}, // myOptions
	},
	eqpmYn: 1,
};

const myColumns = [
	{ header: '사용자NO', name: 'userNo', align: 'center' },
	{ header: '사용자ID', name: 'userLoginId' },
	{ header: '사용자명', name: 'userNm' },
	{ header: '부서명', name: 'deptNm' },
];
</script>

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