<template>
	<div class="l-layout">
		<common-page-title />
		<v-row ref="searchFilter" class="mt-2">
			<v-col :cols="12">
				<v-card class="searchFilter">
					<v-row align="center" no-gutters>
						<v-col :cols="3">
							<!-- 설비종류 -->
							<component :is="'SelectBox'" :propsValue="selectValue02" :itemList="selectValueList02"
								:label="'FAB'" @update:propsValue="selectValue02 = $event" :labelCols="12"
								:textCols="12" :customClass="'select-large'" />
						</v-col>
						<v-col :cols="3">
							<!-- 설비종류 -->
							<component :is="'SelectBox'" :propsValue="selectValue01" :itemList="selectValueList01"
								:label="'설비종류'" @update:propsValue="selectValue01 = $event" :labelCols="12"
								:textCols="12" :customClass="'select-large'" />
						</v-col>
						<v-col :cols="3">
							<!-- 설비종류 -->
							<component :is="'SelectBox'" :propsValue="selectValue03" :itemList="selectValueList03"
								:label="'설비그룹'" @update:propsValue="selectValue03 = $event" :labelCols="12"
								:textCols="12" :customClass="'select-large'" />
						</v-col>
						<v-col :cols="3">
							<!-- 평가 -->
							<component :is="'SelectBox'" :propsValue="selectValue06" :itemList="selectValueList06"
								:label="'평가'" @update:propsValue="selectValue06 = $event" :labelCols="12" :textCols="12"
								:customClass="'select-large'" />
						</v-col>


					</v-row>
					<v-row align="end" no-gutters>
						<v-col :cols="3">
							<!-- 설비종류 -->
							<component ref="EqpmSelectPop" :is="'EqpmSelectPop'" :label="'설비'" :valueNm="'eqpmNm'"
								:parentPrgmId="myPrgmId" :eqpmGrpDisabled="true" :fabDisabled="true" :isMulti="true"
								:labelCols="12" :textCols="12" :customClass="'input-large'" />
						</v-col>
						<v-col :cols="3">
							<!-- 설비종류 -->
							<component :is="'SelectBox'" ref="SelectBox1" :propsValue="selectValue05"
								:itemList="selectValueList05" :label="'가이드명'"
								@update:propsValue="selectValue05 = $event" :labelCols="12" :textCols="12"
								:customClass="'select-large'" />
						</v-col>
						<v-col :cols="3">
							<!-- 대상일 -->
							<component :is="'Datepicker'" :parentPrgmId="myPrgmId" :label="'대상일'" :labelCols="12"
								:textCols="12" :customClass="'datepicker-large'" />
						</v-col>
						<v-col :cols="3" class="text-right">
							<!-- 조회버튼 -->
							<BtnSearch @click="search" size="large" />
						</v-col>
					</v-row>
				</v-card>
			</v-col>
		</v-row>
		<v-row ref="contents" class="pt-3">
			<v-col :cols="12" style="height: 100%">
				<v-card class="pb-5">
					<div class="d-flex align-center justify-space-between pa-4">
						<v-card-title class="pa-0">설비별 현황 리스트</v-card-title>
					</div>
					<div class="px-4" style="height:calc(100% - 76px)">
						<div ref="gridParent" class="w100 h100">
							<component :ref="gridName" :is="loadGrid ? 'Grid' : null" :gridName="gridName"
								:parentPrgmId="myPrgmId" :columnClickEventFlag="true" @columnClick="columnClick" />
						</div>
					</div>
				</v-card>
			</v-col>
		</v-row>
		<component ref="EnrgEffcEqpmDetailPop" :is="'EnrgEffcEqpmDetailPop'" :parentPrgmId="myPrgmId" />
		<component ref="EnrgEffcGdIdxDetPop" :is="'EnrgEffcGdIdxDetPop'" :parentPrgmId="myPrgmId" />
	</div>
</template>

<script>
import mixinGlobal from '@/mixin/global.js';
import { resize } from '@/mixin/resize.js';
import { mapActions } from 'vuex';
import BtnSearch from '~/components/common/button/BtnSearch';
import Buttons from '~/components/common/button/Buttons';
import SelectBox from '@/components/common/select/SelectBox';
import Grid from '~/components/common/Grid';
import Utility from '~/plugins/utility';
import Datepicker from '~/components/common/Datepicker';
import EqpmSelectPop from '~/components/common/modal/EqpmSelectPop';
import EnrgEffcEqpmDetailPop from '~/components/common/modal/EnrgEffcEqpmDetailPop';
import EnrgEffcGdIdxDetPop from '~/components/common/modal/EnrgEffcGdIdxDetPop';

let myTitle;
let myPrgmId;
let paramsKey;
// let params;
// let routeCheck = false;

export default {
	mixins: [mixinGlobal, resize],
	async asyncData(context) {
		const myState = context.store.state;
		myPrgmId = context.route.query.prgmId;
		await context.store.commit('setActiveMenuInfo', myState.menuData[myPrgmId]);
		myTitle = await myState.activeMenuInfo.menuNm;
	},
	meta: {
		title: () => {
			return myTitle;
		},
		prgmId: myPrgmId,
		closable: true,
	},
	components: {
		Utility,
		Grid,
		SelectBox,
		Buttons,
		BtnSearch,
		Datepicker,
		EnrgEffcEqpmDetailPop,
		EqpmSelectPop,
		EnrgEffcGdIdxDetPop,
	},
	data() {
		return {
			myPrgmId: myPrgmId,
			selectValue01: null,
			selectValue02: null,
			selectValue03: null,
			selectValue04: { eqpmId: [] },
			selectValue05: null,
			selectValue06: null,
			selectValueList01: [],
			selectValueList02: [],
			selectValueList03: [],
			selectValueList04: [],
			selectValueList05: [],
			selectValueList06: [],
			initedFlag: false,
			loadGrid: false,
			gridName: 'rowGrid',
			popDialog: false,
			eqpmFlag: false,
			eqpmKindFlag: false,
			eqpmGrpFlag: false,
			eqpmGdIdxFlag: false,
			fabFlag: false,
			routeData: {},
		};
	},
	computed: {
		chkIsFind() {
			// 조회 플래그
			return this.pageData.isFind;
		},
		fabId() {
			return this.pageData.fabId;
		},
		fabNm() {
			return this.pageData.fabNm;
		},
		eqpmKindId() {
			return this.pageData.eqpmKindId;
		},
		eqpmGrpId() {
			return this.pageData.eqpmGrpId;
		},
		eqpmGrpNm() {
			return this.pageData.eqpmGrpNm;
		},
		fromDt() {
			return this.pageData.fromDt;
		},
		checkedRows() {
			return this.pageData.checkedRows;
		},
		eqpmInfo() {
			return this.pageData.eqpmInfo;
		},
	},
	watch: {
		$route(to, from) {
			// routeCheck=true;
			if (to.query.prgmId == myPrgmId) {
				if (paramsKey != to.params.key) {
					if (to.params.key == undefined) {
					} else {
						// params=to.params;
						paramsKey = to.params.Key;
						document.getElementById('refresh').click();
					}
				}
			}
		},
		chkIsFind(val) {
			if (val) this.search();
		},
		fabId(val) { },
		fabNm(val) { },
		eqpmKindId(val) { },
		eqpmGrpId(val) { },
		eqpmGrpNm(val) { },
		fromDt(val) { },
		async selectValue02(val) {
			if (this.initedFlag) {
				this.setPageData({
					fabId: val,
					eqpmId: [],
					eqpmNm: null,
					eqpmIdList: [],
					// isFind:true
				});
				this.selectValue04 = { eqpmId: [] };
				// await this.getEqpm();
				// if(this.eqpmFlag){
				//   this.setPageData({ isFind: true });
				//   this.eqpmFlag=false;
				// }
			}
		},
		async selectValue01(val) {
			if (this.initedFlag) {
				this.setPageData({
					eqpmKindId: val,
					eqpmGrpId: null,
					eqpmId: [],
					eqpmNm: null,
					eqpmIdList: [],
				});
				this.selectValue04 = { eqpmId: [] };
				await this.getEqpmGrp();
				await this.getEqpm(this.selectValueList03);
			}
		},
		async selectValue03(val) {
			if (this.initedFlag) {
				this.setPageData({
					eqpmGrpId: val,
					eqpmId: [],
					eqpmNm: null,
					eqpmIdList: [],
				});
				this.selectValue04 = { eqpmId: [] };
				// await this.getEqpm();
				// if(this.eqpmFlag){
				//   this.setPageData({ isFind: true });
				this.eqpmGrpFlag = true;
				// }
				// await this.getEqpm();
				await this.getGdIdx();
			}
		},
		async selectValue06(val) {
			if (this.initedFlag) {
				this.setPageData({
					eqpmGrpId: null,
					eqpmId: [],
					eqpmNm: null,
					eqpmIdList: [],
					isFind: true,
				});
			}
		},
		checkedRows(val) {
			if (this.initedFlag) {
				var temp = this.pageData.checkedRows.map(item => {
					return item.eqpmId;
				});
				this.selectValue04 = { eqpmId: temp };
			}
		},
		eqpmInfo(val) {
			if (this.initedFlag) {
				this.setPageData({ eqpmId: val.eqpmId, eqpmNm: val.eqpmNm });
			}
		},

		// async selectValue04(val) {
		// 	if (this.initedFlag) {
		//     console.log("val : ",val)
		// 		this.setPageData({ isFind: true });
		// 	}
		// },
		selectValue04: {
			deep: true,
			handler(newVal, oldVal) {
				this.setPageData({ isFind: true });
			},
		},
		async selectValue05(val) {
			if (this.initedFlag) {
				this.setPageData({ isFind: true });
			}
		},
	},
	async beforeCreate() {
		myPrgmId = this.$route.query.prgmId;
		await this.$store.dispatch('chkOpenTabList', {
			key: 'create',
			prgmId: myPrgmId,
			defaultData: defaultData,
		});
	},
	created() {
		var data = this.$route.params;
		// this.setPageData({
		//   eqpmGrpId : data.eqpmGrpId,
		//   eqpmGrpNm : data.eqpmGrpNm,
		//   fabId : data.fabId,
		//   fabNm : data.fabNm,
		//   eqpmKindId : data.eqpmKindId,
		//   fromDt : data.fromDt

		// });
		this.routeData = {
			eqpmGrpId: data.eqpmGrpId,
			eqpmGrpNm: data.eqpmGrpNm,
			fabId: data.fabId,
			fabNm: data.fabNm,
			eqpmKindId: data.eqpmKindId,
			fromDt: data.fromDt,
			okFg: data.okFg
		};
	},
	async mounted() {
		await this.init();
		this.initedFlag = true;
		this.routeData = {};
	},
	beforeDestroy() {
		// routeCheck=false;
		this.chkOpenTabList({ key: 'destroy', prgmId: myPrgmId });
	},
	methods: {
		...mapActions({
			getCodeList: 'modules/search/getCodeList',
			getBlocMstrList: 'modules/search/getBlocMstrList',
		}),
		async init() {
			await this.getFab();
			await this.getEqpmKind();
			await this.getEqpmGrp();
			// await this.getEqpm();
			await this.getGdIdx();
			await this.getOkFg();

			if (this.routeData.fromDt != null) {
				this.setPageData({
					fromDt: this.routeData.fromDt,
				});
			} else {
				this.setFromDt();
			}
			await this.gridInit();
		},
		async search() {
			await this.getRowGridData();
			this.setPageData({
				isFind: false,
			});
		},
		setFromDt() {
			this.pageData.fromDt = Utility.setBeforetDate(
				this.pageData,
				this.pageData.toDt,
				'YYYYMMDD',
			);
			// this.pageData.toDt = Utility.setAftertDate(this.pageData, this.pageData.fromDt, "YYYYMMDD");
		},
		async getEqpmKind() {
			let res = await this.postApiReturn({
				apiKey: 'selectEqpmKindCodeList',
				resKey: 'eqpmKindCodeLists',
				sendParam: {},
			});
			if (res.length > 0) {
				this.selectValueList01 = await res.map(item => {
					return {
						text: item.eqpmKindNm,
						value: item.eqpmKindId,
					};
				});
				this.selectValueList01.unshift({
					text: '전체',
					value: null,
				});
				if (
					this.routeData.eqpmKindId == null ||
					this.routeData.eqpmKindId == ''
				) {
					this.selectValue01 = this.selectValueList01[0].value;
				} else if (
					this.routeData.eqpmKindId != null ||
					this.routeData.eqpmKindId != ''
				) {
					this.selectValue01 = this.routeData.eqpmKindId;
				}
			} else {
				this.selectValueList01 = [];
				this.selectValue01 = null;
			}
			this.setPageData({
				eqpmKindList: this.selectValueList01,
				eqpmKindId: this.selectValue01,
			});
		},
		async getFab() {
			let res = await this.postApiReturn({
				apiKey: 'selectFabCodeList',
				resKey: 'fabCodeLists',
				sendParam: {},
			});

			if (res.length > 0) {
				this.selectValueList02 = await res.map(item => {
					return {
						text: item.eccNm,
						value: item.eccId,
					};
				});
				this.selectValueList02.unshift({
					text: '전체',
					value: null,
				});
				if (this.routeData.fabId != null && this.routeData.fabId != '') {
					this.selectValue02 = this.routeData.fabId;
				} else if (this.routeData.fabId == null || this.routeData.fabId == '') {
					this.selectValue02 = this.selectValueList02[0].value;
				}
			} else {
				this.selectValueList02 = [];
				this.selectValue02 = null;
			}
			this.setPageData({
				fabIdList: this.selectValueList02,
				fabId: this.selectValue02,
			});
		},
		async getEqpmGrp() {
			//설비그룹
			let res = await this.postApiReturn({
				apiKey: 'selectEqpmGrpCodeList',
				resKey: 'eqpmGrpCodeLists',
				sendParam: { eqpmKindId: this.selectValue01 },
			});
			if (res.length > 0) {
				this.selectValueList03 = await res.map(item => {
					return {
						text: item.eqpmGrpNm,
						value: item.eqpmGrpId,
					};
				});

				this.selectValueList03.unshift({
					text: '전체',
					value: null,
				});

				if (
					this.routeData.eqpmGrpId != null &&
					this.routeData.eqpmGrpId != ''
				) {
					this.selectValue03 = this.routeData.eqpmGrpId;
				} else if (
					this.routeData.eqpmGrpId == null ||
					this.routeData.eqpmGrpId == ''
				) {
					this.selectValue03 = this.selectValueList03[0].value;
				}
			} else {
				this.selectValueList03 = [];
				this.selectValue03 = null;
			}

			this.setPageData({
				eqpmGrpId: this.selectValue03,
				eqpmGrpIdList: this.selectValueList03
			})
		},
		async getEqpm(data) { },
		async getGdIdx() {
			let res = await this.postApiReturn({
				apiKey: 'selectEqpmGdIdxCodeList',
				resKey: 'eqpmGdIdxCodeLists',
				sendParam: {
					eqpmGrpId: this.selectValue03,
				},
			});

			if (res.length > 0) {
				this.selectValueList05 = await res.map(item => {
					return {
						text: item.gdIdxNm,
						value: item.gdIdxId,
					};
				});
				this.selectValueList05.unshift({
					text: '전체',
					value: null,
				});
				this.selectValue05 = this.selectValueList05[0].value;
			} else {
				this.selectValueList05 = [];
				this.selectValue05 = null;
			}

			this.setPageData({
				gdIdxIdList: this.selectValueList05,
				gdIdxId: this.selectValue05,
			});
		},
		async getOkFg() {
			this.selectValueList06 = [
				{
					text: '전체',
					value: null,
				},
				{
					text: 'OK',
					value: 'OK',
				},
				{
					text: 'NG',
					value: 'NG',
				},
			];
			if (this.routeData.okFg == null || this.routeData.okFg == '') {
				this.selectValue06 = this.selectValueList06[0].value;
			} else if (this.routeData.okFg != null || this.routeData.okFg != '') {
				this.selectValue06 = this.routeData.okFg;
			}
			this.setPageData({
				okFg: this.selectValue06,
				okFgList: this.selectValueList06,
			});
		},
		async gridInit() {
			const gridHeight = this.$refs.gridParent.offsetHeight - 30;
			// const gridWidth = this.$refs.gridParent.offsetWidth / 2;

			const myOptions = {
				columnOptions: {
					resizable: true,
				},
				header: {
					height: 37,
				},
				rowHeight: 37,


				// pageOptions: {
				// 	useClient: true,
				// 	perPage: 10,
				// },
				// scrollX: false,
				// scrollY: false,

				// heightResizable: true,
				// rowHeight:'auto'
			};
			this.setGridOption({
				gridKey: this.gridName,
				value: Object.assign(Utility.defaultGridOption(gridHeight), myOptions),
			});

			const _this = this;
			const myColumns = [
				{ header: 'No.', name: 'rowSeq', align: 'right', width: 90 },
				{
					header: 'FAB',
					name: 'fabNm',
					align: 'left',
					width: 80,
				},
				{
					header: '설비그룹',
					name: 'eqpmGrpNm',
					width: 130,
				},
				{
					header: '설비id',
					name: 'eqpmId',
					hidden: true,
				},
				{
					header: '설비명',
					name: 'eqpmNm',
					width: 130,
				},
				{
					header: '가이드명',
					name: 'gdIdxId',
					hidden: true,
					// formatter({ value }) {
					// 	let retVal = '';
					// 	const newValue = _this.pageData.gdIdxIdList.filter(
					// 		item => item.value == value,
					// 	);
					// 	if (newValue.length > 0) {
					// 		retVal = newValue[0].text;
					// 	}
					// 	//빈값일때 전체방지
					// 	if (value == '' || value == null) {
					// 		retVal = '';
					// 	}
					// 	return retVal;
					// },
				},
				{
					header: '가이드명',
					name: 'gdIdxNm',
					width: 200,
				},
				{
					header: '주의',
					name: 'careStndVal',
					align: 'right',
					width: 80,
				},
				{
					header: '경고',
					name: 'warnStndVal',
					align: 'right',
					width: 80,
				},
				{
					header: '가이드값',
					name: 'totVal',
					align: 'right',
					width: 120,
				},
				{
					header: '평가',
					name: 'okFg',
					align: 'left',
					width: 80,
				},
				{
					header: '절감가이드 방법',
					name: 'gdMeth',
				},
				{
					header: 'NG발생건수',
					name: 'ngCnt',
					align: 'right',
					width: 80,
				},
			];

			this.setGridColumn({
				gridKey: this.gridName,
				value: myColumns,
			});

			// this.loadGrid = true;

			this.getRowGridData();
		},
		async getRowGridData() {
			this.loadGrid = false;

			var res = await this.postApiReturn({
				apiKey: 'selectEnrgEffcEqpmDetlMntr',
				resKey: 'eqpmDetlData',
				sendParam: {
					eqpmId: this.selectValue04.eqpmId,
					eqpmGrpId: this.selectValue03,
					fromDt: this.pageData.fromDt,
					toDt: this.pageData.toDt,
					gdIdxId: this.selectValue05,
					fabId: this.selectValue02,
					eqpmKindId: this.selectValue01,
					okFg: this.selectValue06,
				},
			});

			this.setGridData({
				gridKey: this.gridName,
				value: res,
			});

			this.loadGrid = true;
		},
		columnClick(e) {
			var data = this.$refs[this.gridName].gridInstance.invoke(
				'getRow',
				e.rowKey,
			);

			if (e.columnName == 'ngCnt' && e.targetType == 'cell') {
				// if(e.columnName == 'ngCnt' && data.ngCnt > 0){
				this.$refs['EnrgEffcEqpmDetailPop'].popCheck = true;
				this.setPageData({
					rowGridSelectKey: e.rowKey,
					rowGridSelectData: {
						...data,
						fromDt: this.pageData.fromDt,
						toDt: this.pageData.toDt,
					},
				});
			}

			if (e.columnName == 'gdIdxNm' && e.targetType == 'cell') {
				// if(e.columnName == 'ngCnt' && data.ngCnt > 0){
				this.$refs['EnrgEffcGdIdxDetPop'].popCheck = true;
				this.setPageData({
					rowGridSelectKey: e.rowKey,
					rowGridSelectData: {
						...data,
						fromDt: this.pageData.fromDt,
						toDt: this.pageData.toDt,
					},
				});
			}
		},
	},
};

const defaultData = {
	isFind: false,
	fabId: '',
	fabIdList: [],
	fabNm: '',
	eccId: '',
	eqpmInfo: null,
	eccIdList: [],
	eqpmGrpNm: '',
	eqpmGrpId: null,
	eqpmGrpList: [],
	eqpmId: [],
	eqpmNm: null,
	eqpmIdList: [],
	eqpmNm: '',
	eqpmKindId: '',
	gdIdxId: '',
	gdIdxIdList: [],
	checkedRows: [],
	cmCycle: 'CYC_DAY', // 주기
	defaultRange: {
		CYC_DAY: 30,
	},
	calcProc: null,
	calcDesc: null,
	fromDt: '',
	toDt: Utility.setFormatDate(new Date(), 'YYYYMMDD'),
	modalData: {},
	// 선택된 그룹코드 상세 데이터
	rowGridSelectKey: 0,
	rowGridSelectData: null,
	rowGrid: {
		data: [],
		column: [],
		option: {},
		defaultRow: {
			fabNm: '',
			eqpmGrpNm: '',
			eqpmId: null,
			gdIdxId: '',
			careStndVal: '',
			warnStndVal: '',
			totVal: '',
			okFg: '',
			gdMeth: '',
			ngCnt: '',
			rowStat: null,
		},
		buttonAuth: {
			add: false,
			remove: false,
			save: false,
			excel: false,
		},
	},
};
</script>
