<template>
	<div>
		<div class="d-flex justify-space-between">
			<v-card-title class="px-0 pt-0 custom-title-4">가상 검침 정보</v-card-title>
			<Buttons :parentPrgmId="parentPrgmId" :bindingData="gridName" :detailList="detailReadPlcImgList"
				:btnActionsFnc="btnActions" />
		</div>

		<div ref="gridParent" style="height: calc((100vh - 520px) /2);">
			<Grid :ref="gridName" :is="loadGrid ? 'Grid' : null" :gridName="gridName" :parentPrgmId="parentPrgmId" @getRowsData="getRowData"
				:innerTabGridInfo="innerTabGridInfo"  />
		</div>
		<div ref="formParent" style="height: calc((100vh - 400px) /2);">
			<component :is="'Form'" :parentPrgmId="parentPrgmId" :bindingData="gridName"
				:detailList="detailReadPlcImgList" @gridEditingFinish="gridEditingFinish" />
		</div>
	</div>
</template>
<script>
import { mapState, mapMutations, mapActions } from 'vuex';
import Buttons from '~/components/common/button/Buttons';
import Grid from '~/components/common/Grid';
import InputText from '~/components/common/input/InputText';
import Form from '~/components/common/form/Form';
import mixinGlobal from '@/mixin/global.js';
import Utility from '~/plugins/utility';
export default {
	mixins: [mixinGlobal],
	components: {
		Buttons,
		Grid,
		InputText,
		Form,
	},
	props: {
		parentPrgmId: {
			type: String,
			require: true,
		},
		innerTabGridInfo: {
			type: Object,
			default: null,
		},
	},
	data() {
		return {
			loadGrid: false,
			gridName: 'rowReadPlcImgGrid',
			detailReadPlcImgList: myReadPlcImgDetail,
		};
	},
	computed: {
		...mapState({
			pageData(state) {
				return state.pageData[this.parentPrgmId];
			},
		}),
		selectedCommCd() {
			return this.pageData[this.gridName].data;
		},
		myUseFgList() {
			return this.pageData.useFgList;
		},
		selectedComId() {
			return this.pageData.rowGridSelectData.comId;
		},
		selectedBlocId() {
			return this.pageData.rowGridSelectData.blocId;
		},
		selectedReadPlcId() {
			return this.pageData.rowGridSelectData.readPlcId;
		},
	},
	mounted() {
		this.init();
	},
	methods: {
		...mapMutations({
			setPageData: 'setPageData',
			setGridData: 'setGridData',
			setGridColumn: 'setGridColumn',
			setGridOption: 'setGridOption',
		}),
		...mapActions({
			postApi: 'modules/list/postApi',
			postUpdateApi: 'modules/list/postUpdateApi',
			postApiReturn: 'modules/list/postApiReturn',
			setTree: 'modules/list/setTree',
			chkOpenTabList: 'chkOpenTabList',
		}),
		init() {
			this.gridInit();
		},
		gridInit() {
			const gridHeight = this.$refs['gridParent'].offsetHeight - 36;

			const myOptions = {
				columnOptions: {
					resizable: true,
				},
				// bodyHeight: gridHeight,
				// minBodyHeight: gridHeight,
				header: {
					height: 37,
				},
				rowHeight: 37,
				// minRowHeight: 29,
				selectionUnit: 'row',
				editingEvent: 'click',
				rowHeight: 'auto',
			};
			this.setGridOption({
				gridKey: this.gridName,
				// value: myOptions
				value: Object.assign(Utility.defaultGridOption(gridHeight), myOptions),
			});

			const _this = this;
			let ddctKindList = [];
			this.pageData.ddctKindList.forEach(item => {
				const it = { text: item.commCdNm, value: item.commCd };
				ddctKindList.push(it);
			});
			const myColumns = [
				{ header: '회사 ID', name: 'comId', hidden: true },
				{ header: '검침개소 번호', name: 'readPlcId', hidden: true },
				{ header: '대상 검침개소 번호', name: 'objReadPlcId'},
				{ header: '대상 검침개소명', name: 'objReadPlcNm', align: 'left' },
				{ header: '분배율(1~100)', name: 'distRt', align: 'right' },
				{
					header: '가감유형',
					name: 'ddctKind',
					formatter({ value }) {
						let retVal = '';
						const newValue = ddctKindList.filter(item => item.value == value);
						if (newValue.length > 0) {
							retVal = newValue[0].text;
						}
						return retVal;
					},
				},
				{ header: '등록 사용자', name: 'regUserNo', hidden: true },
				{ header: '등록 일자', name: 'regDttm', hidden: true },
				{ header: '수정 사용자', name: 'procUserNo', hidden: true },
				{ header: '수정 일자', name: 'procDttm', hidden: true },
			];

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

			this.loadGrid = true;
		},
		async getRowData(data, gridName) {
			this.setGridSelectData({
				gridKey: gridName,
				gridSelect: true,
				rowGridSelectKey: data.rowKey,
				rowGridSelectData: Object.assign({}, data),
			});
		},
		async btnActions(action) {
			let dataArr = [];
			switch (action) {
				case 'add':
					this.$refs[this.gridName].addRow();
					break;

				case 'remove':
					this.$refs[this.gridName].removeRow();
					break;

				case 'save':
					this.loadGrid = false;
					dataArr = this.$refs[this.gridName].save();
					this.setPageData({ isFind: true });
					if (dataArr.length > 0) {
						const sendParam = {
							datas: {
								dsReadPlcImgInfo: dataArr.map(item => ({
									...item,
									comId: this.selectedComId,
									readPlcId: this.selectedReadPlcId,
								})),
							},
							params: {},
						};
						await this.postUpdateApi({
							apiKey: 'saveReadPlcImgInfo',
							sendParam: sendParam,
						});
						this.loadGrid = true;
						this.setPageData({ isFind: true });
					} else {
						this.loadGrid = true;
						alert('저장할 내용이 없습니다.');
					}
					break;

				default:
					break;
			}
		},
		gridEditingFinish(data, bindingData) {
			this.$refs[this.gridName].editingFinish(data);
		},
		search() { },
	},
};

const myReadPlcImgDetail = [
	{
		type: 'ReadPlcPop',
		label: '대상 검침개소',
		title: '검침개소명',
		valueNm: 'objReadPlcId',
		disabled: false,
		cols: 6,
		labelCols: 12,
		textCols: 12,
		class: 'py-3 pr-4 pb-2',
		required: true,
		iconShow:true
	},
	{
		type: 'InputNumber',
		label: '분배율(1~100)',
		valueNm: 'distRt',
		disabled: false,
		cols: 6,
		class: 'py-3 pl-4',
		labelCols: 12,
		textCols: 12,
		inputType: 'number',
		min: 1,
		max: 100,
		required: true,
		iconShow:true
	},
	{
		type: 'InputText',
		valueNm: 'objReadPlcNm',
		readonly: true,
		cols: 6,
		textCols: 12,
		class: 'pt-1 py-3 pr-4',
	},
	{
		showValue: false
	},
	{
		type: 'SelectBox',
		label: '가감유형',
		valueNm: 'ddctKind',
		disabled: false,
		cols: 6,
		labelCols: 12,
		textCols: 12,
		class: 'py-3 pr-4',
		list: 'ddctKindList',
		itemText: 'commCdNm',
		itemValue: 'commCd',
		required: true,
		iconShow: true
	},
];
</script>
