<template>
	<v-row class="search-box" align="center" no-gutters>
		<!-- <v-col v-if="label" cols="2"> -->

		<v-col :cols="label ? textCols : ''">
			<!-- <v-col :cols="label ? 9 : ''"> -->
			<!-- :value="textfield" -->
			<v-btn @click="dialog = !dialog" class="v-input__custom">
				단가보기
			</v-btn>
		</v-col>

		<!-- <v-row justify="center"> -->
		<v-dialog
			ref="popModal"
			v-model="dialog"
			width="850"
			overlay-color="#000"
			overlay-opacity="0.8"
		>
			<v-card style="height: 100%">
				<v-card-title class="pa-5 d-flex align-center justify-space-between">
				</v-card-title>

				<!-- <div :style="'height: calc(50vh)'"> -->
				<!-- <div :style="{ height: 'calc(100% - 213px)' }"> -->
				<div ref="gridParent" style="height: 150px">
					<component
						:ref="gridName + parentPrgmId"
						:is="loadGrid && dialog ? 'Grid' : null"
						:gridName="gridName"
						:dataPath="searchParam.modalData.ErcChrgInfoMngPop"
						:parentPrgmId="parentPrgmId"
					/>
				</div>
				<!-- </div> -->
				<v-card-actions class="pa-5 d-flex align-center justify-center">
					<v-btn :ripple="false" @click="dialogOpenCloseEvent(dialog)"
						>닫기</v-btn
					>
				</v-card-actions>
			</v-card>
			<!-- <template v-slot:activator="{ on, attrs }">
        <v-btn color="primary" dark v-bind="attrs" v-on="on">공정/설비</v-btn>
      </template> -->
		</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,
			default: '비교대상 최대(20개)',
		},
		label: {
			type: String,
			require: true,
			// default: "공정/설비"
		},
		labelCols: {
			type: Number,
			require: false,
			default: 4,
		},
		textCols: {
			type: Number,
			require: false,
			default: 8,
		},
		height: {
			type: Number,
			default: 713,
		},
		eqpmYn: {
			type: Number,
			require: false,
			default: 1,
		},
		popTitle: {
			type: String,
			require: false,
			default: '',
		},
	},
	components: {
		Grid,
		Utility,
		// Tree
	},
	data() {
		return {
			labelPrepend: true,

			myModalKey: 'ErcChrgInfoMngPop',
			gridName: 'modalGrid',
			loadGrid: false,

			dialog: false,
			modalDataKey: 'modalData',
		};
	},
	computed: {
		...mapState({
			// searchParam: state => state.pageData,
			searchParam(state) {
				return state.pageData[this.parentPrgmId];
			},
			isDarkMode: 'isDarkMode',
		}),
		chkDialog() {
			return this.dialog;
		},
		selectValue() {},
	},
	watch: {
		async chkDialog(val) {
			if (val) {
				await this.getGridData();
			}
		},
	},

	beforeCreate() {
		this.$store.commit('setPageData', {
			modalData: { ErcChrgInfoMngPop },
		});
	},
	created() {
		this.init();
	},
	mounted() {
		// console.log("###",this.$refs.gridParent)
		// console.log("###",this.$refs.popModal)
	},
	methods: {
		...mapMutations({
			setPageData: 'setPageData',
			setModalGridData: 'setModalGridData',
			setModalGridColumn: 'setModalGridColumn',
			setModalGridOption: 'setModalGridOption',
		}),
		...mapActions({
			postApiReturn: 'modules/list/postApiReturn',
		}),
		init() {
			this.activeRowData = {};
			this.gridInit();
		},
		// 그리드 세팅
		gridInit() {
			// const gridHeight = this.$refs.gridParent.offsetHeight - 36;

			const myOptions = {
				columOptions: {
					resizeable: true,
				},
			};

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

			this.setModalGridColumn({
				modalKey: this.myModalKey,
				gridKey: this.gridName,
				modalDataKey: this.modalDataKey,
				value: myColumns,
			});
		},
		// 그리드 데이터 설정
		async getGridData() {
			this.loadGrid = false;
			const res = await this.postApiReturn({
				apiKey: 'selectElecPowChrgInfo',
				resKey: 'elecPowChrgInfoData',
				sendParam: {
					elecContId: this.searchParam.elecContId,
				},
			});

			function setRowSpanAttribute(
				res,
				targetAttributeName,
				targetAttributeList,
			) {
				if (!(res.length && res.length >= 1)) {
					return;
				}

				if (targetAttributeList == undefined) {
					var currentCnt = 1;
					var currentIdx = 0;
					var currentValue = res[0][targetAttributeName];
					for (var i = 1; i < res.length; i++) {
						if (res[i][targetAttributeName] != currentValue) {
							if (res[currentIdx]['_attributes'] == undefined) {
								res[currentIdx]['_attributes'] = { rowSpan: new Object() };
							}
							res[currentIdx]['_attributes']['rowSpan'][
								targetAttributeName
							] = currentCnt;

							currentValue = res[i][targetAttributeName];
							currentCnt = 1;
							currentIdx = i;
						} else if (
							res[i][targetAttributeName] == currentValue &&
							i == res.length - 1
						) {
							currentCnt = currentCnt + 1;
							if (res[currentIdx]['_attributes'] == undefined) {
								res[currentIdx]['_attributes'] = { rowSpan: new Object() };
							}
							res[currentIdx]['_attributes']['rowSpan'][
								targetAttributeName
							] = currentCnt;
						} else {
							currentCnt = currentCnt + 1;
						}
					}
				} else {
					var currentCnt = 1;
					var currentIdx = 0;
					for (var i = 1; i < res.length; i++) {
						if (!compareValue(res, targetAttributeList, currentIdx, i)) {
							if (res[currentIdx]['_attributes'] == undefined) {
								res[currentIdx]['_attributes'] = { rowSpan: new Object() };
							}
							if (currentCnt != 1) {
								res[currentIdx]['_attributes']['rowSpan'][
									targetAttributeName
								] = currentCnt;
							}
							currentCnt = 1;
							currentIdx = i;
						} else if (
							compareValue(res, targetAttributeList, currentIdx, i) &&
							i == res.length - 1
						) {
							currentCnt = currentCnt + 1;
							if (res[currentIdx]['_attributes'] == undefined) {
								res[currentIdx]['_attributes'] = { rowSpan: new Object() };
							}
							if (currentCnt != 1) {
								res[currentIdx]['_attributes']['rowSpan'][
									targetAttributeName
								] = currentCnt;
							}
						} else {
							currentCnt = currentCnt + 1;
						}
					}
				}
				function compareValue(res, targetAttributeList, currentIdx, targetIdx) {
					for (var i = 0; i < targetAttributeList.length; i++) {
						if (
							res[currentIdx][targetAttributeList[i]] !=
							res[targetIdx][targetAttributeList[i]]
						) {
							return false;
						}
					}
					return true;
				}
			}
			setRowSpanAttribute(res, 'elecContKindNm');
			setRowSpanAttribute(res, 'voltKindNm');
			setRowSpanAttribute(res, 'optKindNm');
			setRowSpanAttribute(res, 'baseChrg');

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

			this.loadGrid = true;
		},
		// row 클릭이벤트
		getRowData(data) {},

		search() {
			this.getGridData();
		},

		dialogOpenCloseEvent(val) {
			this.dialog = !val;
		},
	},
};

const ErcChrgInfoMngPop = {
	modalGrid: {
		data: [],
		column: [], // myColumns,
		option: {}, // myOptions
	},
};

const myColumns = [
	{ header: '전력계약 ID', name: 'eleclContId', hidden: true },
	{ header: '전력계약 명', name: 'eleclContNm', hidden: true },
	{ header: '전력계약구분CD', name: 'elecContKind', hidden: true },
	{ header: '전력계약구분', name: 'elecContKindNm', align: 'center' },
	{ header: '전압구분CD', name: 'voltKind', hidden: true },
	{ header: '전압구분', name: 'voltKindNm', align: 'center' },
	{ header: '옵션CD', name: 'optKind', hidden: true },
	{ header: '옵션', name: 'optKindNm', align: 'center' },
	{
		header: '기본요금(원/kW)',
		name: 'baseChrg',
		align: 'right',
		formatter({ value }) {
			return Utility.setFormatInt(value);
		},
	},
	{ header: '시간대CD', name: 'peakKind', hidden: true },
	{ header: '시간대', name: 'peakKindNm', align: 'center' },
	{
		header: '여름철(6~8월)',
		name: 'unitPrcSum',
		align: 'right',
		formatter({ value }) {
			return Utility.setFormatIntDecimal(value, 1);
		},
	},
	{
		header: '봄·가을철(3~5,9~10월)',
		name: 'unitPrcSprfal',
		align: 'right',
		formatter({ value }) {
			return Utility.setFormatIntDecimal(value, 1);
		},
	},
	{
		header: '겨울철(11~2월)',
		name: 'unitPrcWin',
		align: 'right',
		formatter({ value }) {
			return Utility.setFormatIntDecimal(value, 1);
		},
	},
];
</script>
