<template>
	<v-row class="search-box" align="center" no-gutters>
		<v-col v-if="label" :cols="labelCols">
			<!-- <label for="" class="search-box-label"> -->
			<label for="" class="search-box-label">
				{{ label }}
			</label>
		</v-col>
		<v-col :cols="label ? textCols : ''">
			<!-- :value="textfield" -->
			<v-text-field
				readonly
				v-model="selectValue"
				append-icon="mdi-magnify"
				class="v-input__custom"
				:hide-details="true"
				@click="dialog = !dialog"
				outlined
			></v-text-field>
		</v-col>

		<!-- <v-row justify="center"> -->
		<v-dialog v-model="dialog" scrollable width="540px">
			<!-- <template v-slot:activator="{ on, attrs }">
        <v-btn color="primary" dark v-bind="attrs" v-on="on">공정/설비</v-btn>
      </template> -->
			<v-card>
				<v-card-title class="d-flex align-center justify-space-between">
					<span class="custom-title-4" v-if="label">검침개소</span>
					<v-btn icon tile :ripple="false" @click="dialog = !dialog">
						<v-icon>mdi-close</v-icon>
					</v-btn>
				</v-card-title>
				<div class="pa-5">
					<!-- <v-text-field label="위치정보 선택"></v-text-field> -->
					<v-text-field
						append-icon="mdi-magnify"
						class="v-input__custom"
						outlined
						hide-details
					></v-text-field>
				</div>
				<v-divider></v-divider>
				<div>
					<div ref="gridParent" style="height: calc(100% - 139px)">
						<component
							:ref="'rowGrid' + parentPrgmId"
							:is="loadGrid && dialog ? 'Grid' : null"
							:gridName="gridName"
							:dataPath="searchParam.modalData2.selectOutsideWeather"
							:parentPrgmId="parentPrgmId"
							@getRowsData="getRowData"
							@dblClick="setUpdate()"
						/>
					</div>
					<!-- <component
            :is="ftnPlcListTreeData.length > 0 ? 'Tree' : null"
            :tree-data="ftnPlcListTreeData"
          /> -->
				</div>
				<v-divider></v-divider>
				<v-card-actions class="justify-center">
					<v-btn @click="setUpdate()">확인</v-btn>
					<v-btn @click="dialog = false">닫기</v-btn>
					<!-- <v-btn
            color="green darken-1"
            text
            @click="dialog = false"
          >
            Disagree
          </v-btn>
          <v-btn
            color="green darken-1"
            text
            @click="dialog = false"
          >
            Agree
          </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,
			default: '공장/설비',
		},
		textCols: {
			type: Number,
			require: false,
			default: 7,
		},
		labelCols: {
			type: Number,
			require: false,
			default: 2,
		},
	},
	components: {
		Grid,
		// Tree
	},
	data() {
		return {
			// label: "공정/설비",
			labelPrepend: true,

			myModalKey: 'selectOutsideWeather',
			gridName: 'rowGrid',
			loadGrid: false,

			dialog: false,
			treeData: [], // 리턴받은 원본 트리 데이터
			modalDataKey: 'modalData2',
			treeExpandAll: true,
			activeRowData: {},
		};
	},
	computed: {
		...mapState({
			// searchParam: state => state.pageData,
			searchParam(state) {
				return state.pageData[this.parentPrgmId];
			},
		}),
		chkDialog() {
			// 모달 열기/닫기 값
			return this.dialog;
		},
		selectValue() {
			// 선택된 공장/설비 값
			var temp = [];
			if (this.searchParam.readPlcList.length > 0) {
				for (const item of this.searchParam.readPlcList) {
					temp.push(item.readPlcNm);
				}
			}
			return temp.join();
		},
	},
	watch: {
		chkDialog(val) {
			if (val) this.getModalGridData();
		},
	},

	beforeCreate() {
		this.$store.commit('setPageData', {
			modalData2: { selectOutsideWeather },
		});
	},
	created() {
		this.init();
		this.setPageData({
			readPlcList:
				localStorage.getItem(this.parentPrgmId + 'WeatherCheckedRow') != null
					? JSON.parse(
							localStorage.getItem(this.parentPrgmId + 'WeatherCheckedRow'),
					  )
					: {},
		});
	},
	methods: {
		...mapMutations({
			setPageData: 'setPageData',
			setModalGridData: 'setModalGridData',
			setModalGridColumn: 'setModalGridColumn',
			setModalGridOption: 'setModalGridOption',
		}),
		...mapActions({
			postApiReturn: 'modules/list/postApiReturn',
		}),
		init() {
			this.activeRowData = {};
			this.gridInit();
			// this.getTreeData();
			// this.lineCOption = chartOption;
		},
		// 공정/설비 트리 그리드 세팅
		gridInit() {
			this.setModalGridColumn({
				modalKey: this.myModalKey,
				gridKey: this.gridName,
				modalDataKey: this.modalDataKey,
				value: [
					{
						header: '검침개소',
						name: 'readPlc',
						align: 'center',
					},
					{
						header: '검침개소명',
						name: 'readPlcNm',
						align: 'center',
					},
					{
						header: '단위',
						name: 'unit',
						hidden: true,
					},
				],
			});
		},
		async getModalGridData() {
			this.loadGrid = false;

			const myOptions = {
				rowHeaders: ['checkbox'],
				rowHeight: 'auto',
			};

			const rowGridHeight = 490; // this.$refs.treeGridParent.offsetHeight - 30;

			this.setModalGridOption({
				modalKey: this.myModalKey,
				gridKey: this.gridName,
				modalDataKey: this.modalDataKey,
				// value: myOptionsTree
				value: Object.assign(
					Utility.defaultGridOption(rowGridHeight),
					myOptions,
				),
			});

			const res = await this.postApiReturn({
				apiKey: 'selectOutsideWeather',
				resKey: 'outsideWeatherData',
				sendParam: {
					blocId: this.userInfo.blocId,
				},
			});

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

			this.loadGrid = true;

			this.$nextTick(() => {
				if (this.searchParam.weatherIsMulti) {
					this.setGridCheckedRows();
				}
			});
		},
		setGridCheckedRows() {
			this.checkedRowDataList =
				this.searchParam.readPlcList.length > 0
					? this.searchParam.readPlcList
					: [];

			if (this.checkedRowDataList.length > 0) {
				for (var i = 0; i < this.checkedRowDataList.length; i++) {
					this.$refs['rowGrid' + this.parentPrgmId].checkEvt(
						this.checkedRowDataList[i],
						this.$refs['rowGrid' + this.parentPrgmId],
					);
				}
			}
		},
		getRowData(data) {
			if (this.searchParam.weatherIsMulti) {
				if (data._attributes.checked) {
					this.$refs['rowGrid' + this.parentPrgmId].uncheckEvt(
						data,
						this.$refs['rowGrid' + this.parentPrgmId],
					);
				} else {
					this.$refs['rowGrid' + this.parentPrgmId].checkEvt(
						data,
						this.$refs['rowGrid' + this.parentPrgmId],
					);
				}
			} else {
				this.activeRowData = data;
			}
		},
		setUpdate() {
			if (this.searchParam.weatherIsMulti) {
				this.checkedRowDataList = this.$refs[
					'rowGrid' + this.parentPrgmId
				].getCheckedRowsEvt();
				this.dialog = false;
				this.setPageData({ readPlcList: this.checkedRowDataList });
				localStorage.setItem(
					this.parentPrgmId + 'WeatherCheckedRow',
					JSON.stringify(this.searchParam.readPlcList),
				);
			}
		},
	},
};

const selectOutsideWeather = {
	rowGrid: {
		data: [],
		column: [], // myColumns,
		option: {}, // myOptions
	},
	eqpmYn: 1,
};
</script>

<style scoped lang="scss">
@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;
						}
					}
				}

				&__actions {
					padding: 30px 0;
				}
			}
		}
	}
}
</style>
