<template>
	<div>
		<div
			class="d-flex justify-space-between align-center"
			style="height: 80px;"
		>
			<span class="txt custom-title-4-new">공정 입출력 정보</span>
			<Buttons
				:parentPrgmId="parentPrgmId"
				:bindingData="gridName"
				:btnActionsFnc="btnActions"
			/>
		</div>
		<div ref="gridParent" style="height: calc(100vh - 760px);">
			<component
				:ref="gridName"
				:is="loadGrid ? 'Grid' : null"
				:gridName="gridName"
				:parentPrgmId="parentPrgmId"
				@getRowsData="getRowData"
				:innerTabGridInfo="innerTabGridInfo"
				:dataPath="dataPathMock"
			/>
		</div>
		<div>
			<component
				:is="'Form'"
				:bindingData="gridName"
				:parentPrgmId="parentPrgmId"
				:detailList="detailList"
				@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 Form from '~/components/common/form/Form';
import Utility from '~/plugins/utility';
import mixinGlobal from '@/mixin/global.js';
export default {
	mixins: [mixinGlobal],
	components: {
		Buttons,
		Grid,
		Form,
		Utility,
	},
	props: {
		parentPrgmId: {
			type: String,
			require: true,
		},
		innerTabGridInfo: {
			type: Object,
			default: null,
		},
	},
	data() {
		return {
			loadGrid: true,
			gridName: 'rowEccIaoGrid',
			inputList_emMapDiv: [],
			inputList_cmInOut: [],
			detailList: myDetail,
			dataPathMock: {
				"rowEccIaoGrid": {
				column: [
					{ header: '검침 대상 ID', name: 'readObjId', align: 'left' },
					{ header: '검침 대상 명', name: 'readObjNm', align: 'left' },
					{ header: '검침 대상 유형', name: 'readObjKind', align: 'left' },
					{ header: '그룹', name: 'grpCd', align: 'left' },
				],
				data: dataPathDataExample,
				defaultRow: dataPathDataExample,
				option: {}
				}
			},
		};
	},
	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;
		},
		// selectedObjId() {
		//   return this.pageData.rowGridSelectData.objId;
		// }
		selectedObjId() {
			if (this.pageData.rowEccIaoGrid.rowGridSelectData === null) {
				return '';
			} else {
				return this.pageData.rowEccIaoGrid.rowGridSelectData.objKind;
			}
		},
	},
	watch: {
		selectedObjId(val) {
			if (val === 'TAG') {
				this.detailList[2].class = 'py-2 d-none';
				this.detailList[3].class = 'py-2 d-none';
				this.detailList[4].class = 'py-2';
				this.detailList[5].class = 'py-2';
			} else {
				// }else if(val==="READ_PLC"){
				this.detailList[5].class = 'py-2 d-none';
				this.detailList[4].class = 'py-2 d-none';
				this.detailList[3].class = 'py-2';
				this.detailList[2].class = 'py-2';
			}
		},
	},
	created() {},
	mounted() {
		this.init();
	},
	methods: {
		...mapMutations({
			setPageData: 'setPageData',
			setGridData: 'setGridData',
			setGridColumn: 'setGridColumn',
			setGridOption: 'setGridOption',
			setGridSelectData: 'setGridSelectData',
		}),
		...mapActions({
			postApi: 'modules/list/postApi',
			postUpdateApi: 'modules/list/postUpdateApi',
			postApiReturn: 'modules/list/postApiReturn',
			setTree: 'modules/list/setTree',
			chkOpenTabList: 'chkOpenTabList',
			getCodeList: 'modules/search/getCodeList',
		}),
		init() {
			this.gridInit();
		},
		search() {},
		setEditorContent() {
			this.inputList_emMapDiv = this.pageData.emMapDivList.map(item => {
				return {
					text: item.commCdNm,
					value: item.commCd,
				};
			});

			this.inputList_cmInOut = this.pageData.cmInoutList.map(item => {
				return {
					text: item.commCdNm,
					value: item.commCd,
				};
			});
		},
		gridInit() {
			this.setEditorContent();
			const gridHeight = this.$refs.gridParent.offsetHeight - 30;

			this.loadGrid = false;

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

			const _this = this;
			let useFgSelectList = [];
			this.pageData.useFgList.forEach(item => {
				const it = { text: item.commCdNm, value: item.commCd };
				useFgSelectList.push(it);
			});
			const myColumns = [
				{ header: '회사 ID', name: 'comId', hidden: true },
				{ header: '공정 번호', name: 'eccId', hidden: true },
				{ header: '대상 ID', name: 'objId', align: 'center', hidden: true },
				{ header: '대상 명', name: 'objNm', width: 300, align: 'left' },
				{
					header: '에너지원 명',
					name: 'ercId',
					align: 'left',
					formatter({ value }) {
						var retVal = '';

						for (var i = 0; i < _this.pageData.ercNmList.length; i++) {
							if (_this.pageData.ercNmList[i].value === value) {
								retVal = _this.pageData.ercNmList[i].text;
							}
						}

						return retVal;
					},
				},
				{
					header: '대상 유형',
					name: 'objKind',
					align: 'center',
					formatter({ value }) {
						var retVal = '';

						for (var i = 0; i < _this.pageData.emMapDivList.length; i++) {
							if (_this.pageData.emMapDivList[i].commCd === value) {
								retVal = _this.pageData.emMapDivList[i].commCdNm;
							}
						}

						return retVal;
					},
				},
				{
					header: '투입 생산 유형',
					name: 'inProdKind',
					align: 'center',
					formatter({ value }) {
						var retVal = '';

						for (var i = 0; i < _this.pageData.cmInoutList.length; i++) {
							if (_this.pageData.cmInoutList[i].commCd === value) {
								retVal = _this.pageData.cmInoutList[i].commCdNm;
							}
						}

						return retVal;
					},
				},
				{
					header: '계산 여부',
					name: 'calcFg',
					align: 'center',
					formatter({ value }) {
						value = value === true ? '1' : '0';
						const newValue = _this.pageData.useFgList.filter(
							item => item.commCd == value,
						);
						return newValue[0].commCdNm;
						// return value === true ? "사용" : "사용안함";
					},
				},
				{ header: '분배율(1~100)', name: 'distRt', align: 'right' },
				{ header: '사업장', name: 'blocId', align: 'center', hidden: true },
				{ header: '등록 사용자', name: 'regUserNo', hidden: true },
				{ header: '등록 일자', name: 'regDttm', hidden: true },
				{ header: '수정 사용자', name: 'procUserNo', hidden: true },
				{ header: '수정 일자', name: 'procDttm', hidden: true },
				{ header: 'rowStat', name: 'rowStat', hidden: true },
			];

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

			this.loadGrid = true;
		},
		async getRowData(data, gridName) {
			if (data.rowStat === 'I') {
				this.detailList[0].disabled = false;
				this.detailList[2].disabled = false;
				this.detailList[3].disabled = false;
				this.detailList[4].disabled = false;
				this.detailList[5].disabled = false;
			} else {
				this.detailList[0].disabled = true;
				this.detailList[2].disabled = true;
				this.detailList[3].disabled = true;
				this.detailList[4].disabled = true;
				this.detailList[5].disabled = true;
			}
			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();
					this.detailList[0].disabled = false;
					this.detailList[2].disabled = false;
					this.detailList[3].disabled = false;
					this.detailList[4].disabled = false;
					this.detailList[5].disabled = false;
					break;

				case 'remove':
					this.$refs[this.gridName].removeRow();
					this.detailList[0].disabled = true;
					this.detailList[2].disabled = true;
					this.detailList[3].disabled = true;
					this.detailList[4].disabled = true;
					this.detailList[5].disabled = true;
					break;

				case 'save':
					this.loadGrid = false;
					dataArr = this.$refs[this.gridName].save();
					if (dataArr.length > 0) {
						var validCheck = true;
						// console.log('dataArr : ', dataArr);
						dataArr.forEach(item => {
							if (
								item.distRt == '' ||
								item.distRt == null ||
								item.objKind == '' ||
								item.objKind == null ||
								item.objId == '' ||
								item.objId == null ||
								item.objNm == '' ||
								item.objNm == null ||
								item.inProdKind == '' ||
								item.inProdKind == null ||
								item.ercId == '' ||
								item.ercId == null
							) {
								validCheck = false;
								alert('필수 입력값을 입력해주세요');
								// this.setPageData({ isFind: true });
								this.loadGrid = true;
							}
						});

						if (validCheck) {
							const sendParam = {
								datas: {
									dsEccIao: dataArr.map(item => ({
										...item,
										comId: this.selectedComId,
										calcFg: item.calcFg === true ? '1' : '0',
										blocId: this.userInfo.blocId,
										eccId: this.pageData.rowGrid.rowGridSelectData.eccId,
									})),
								},
								params: {},
							};
							await this.postUpdateApi({
								apiKey: 'saveEccIao',
								sendParam: sendParam,
							});
							this.setPageData({ isFind: true });
							this.loadGrid = true;
						}
					} else {
						alert('저장할 내용이 없습니다.');
					}
					break;

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

const myDetail = [
	{
		type: 'SelectBox',
		label: '대상 유형',
		disabled: false,
		cols: 6,
		class: 'py-2 pr-4',
		list: 'emMapDivList',
		valueNm: 'objKind',
		itemText: 'commCdNm',
		itemValue: 'commCd',
		required: true,
		disabled: true,
		labelCols: 12,
		textCols: 12,
		iconShow: true,
	},
	{
		type: 'ReadPlcPop',
		label: '대상 항목',
		valueNm: 'objId',
		disabled: false,
		cols: 6,
		class: 'pt-2 pb-0 mb-0 pl-4',
		required: true,
		bindNm: 'objNm',
		labelCols: 12,
		textCols: 12,
		iconShow: true,
		noText: true,
		iconShow: true,
	},
	{
		// 공백 처리
		type: 'Label',
		cols: 6,
		class: 'py-2 pr-4',
		disabled: false,
	},
	{
		type: 'InputText',
		valueNm: 'objNm',
		readonly: true,
		disabled: true,
		cols: 6,
		class: 'pl-4',
		labelCols: 12,
		textCols: 12,
		iconShow: true,
	},
	{
		type: 'EvtObjPop',
		labelContent: '대상 항목',
		modalTitle:'대상 항목',
		valueNm: 'objId',
		valueNm2: 'objNm',
		bindNm: 'objNm',
		disabled: true,
		required: true,
		cols: 6,
		class: 'd-none pr-4',
		disableContent: true,
		labelCols: 12,
		textCols: 12,
		iconShow: true,
	},
	{
		type: 'InputText',
		cols: 6,
		class: 'd-none pt-0 pl-4',
		valueNm: 'objNm',
		readonly: true,
		disabled: true,
		labelCols: 12,
		textCols: 12,
		iconShow: true,
	},
	{
		type: 'SelectBox',
		label: '에너지원',
		disabled: false,
		cols: 6,
		class: 'py-2 pr-4',
		list: 'ercNmList',
		valueNm: 'ercId',
		itemText: 'text',
		itemValue: 'value',
		required: true,
		labelCols: 12,
		textCols: 12,
		iconShow: true,
	},
	{
		type: 'SelectBox',
		label: '투입생산유형',
		disabled: false,
		cols: 6,
		class: 'py-2 pl-4',
		list: 'cmInoutList',
		valueNm: 'inProdKind',
		itemText: 'commCdNm',
		itemValue: 'commCd',
		required: true,
		labelCols: 12,
		textCols: 12,
		iconShow: true,
	},
	{
		type: 'CheckBox',
		cols: 6,
		class: 'py-2 pr-4',
		label: '계산 여부',
		valueNm: 'calcFg',
		disabled: false,
		value: { '1': true, '0': false },
		required: true,
		disabledFg: 'objKind',
		labelCols: 12,
		textCols: 12,
		iconShow: true,
	},
	{
		type: 'InputText',
		label: '분배율',
		valueNm: 'distRt',
		readonly: false,
		cols: 6,
		class: 'py-2 pl-4',
		required: true,
		labelCols: 12,
		textCols: 12,
		iconShow: true,
	},
];
const dataPathDataExample = [
	{
		readObjId: 'OBJ001',
		readObjNm: '서울 본사',
		readObjKind: 'TYPE01',
		grpCd: 'GRP01',
	},
]
</script>
