<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : CouponPopupForm.html
 * @desc    : 쿠폰 등록 팝업 화면
 *============================================================================
 * ISTYLE24
 * Copyright(C) 2020 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2020.12.23   xyzp1539       최초 작성
 *******************************************************************************
 -->
<div class="inner-panelContent">
	<div class="panelContent">
		<div class="panelBar">
			<h4>업체 분담율</h4>
		</div>
		<table class="frmStyle">
			<colgroup>
				<col style="width:15%;">
				<col style="width:85%;">
			</colgroup>
			<tbody>
			<tr>
				<th>업체 분담율</th>
				<td>
					<div class="padding10 inner-tb-solid">
						<button type="button" class="btn btnRight btn-success btn-lg" id="btnAddApplyBurden">업체추가</button>
						<button type="button" class="btn btnRight btn-success btn-lg" id="btnDelApplyBurden">선택삭제</button>
						<br/>
						<div id="inComGridList" style="width:100%; height:200px;" class="ag-theme-balham"></div>
					</div>
				</td>
			</tr>
			</tbody>
		</table>
	</div>
</div> 

<script th:inline="javascript">

var cpnDtlBurdenList = [[${cpnDtlBurdenList}]];

//ag-grid 입점업체분담율 컬럼
var columnDefs = [
	{width: 40, minWidth: 40, cellClass: 'text-center', headerCheckboxSelection: true, checkboxSelection: true, filter: false},
	{ headerName: "입점업체코드" , field:"supplyCompCd" , width:150, cellClass:"text-center"
	} ,
	{ headerName: "입점업체명"  , field: "supplyCompNm" , width: 120 , cellClass: 'text-center'},
	{ headerName: "쿠폰분담율(%)", field:"burdenRate" , width:150, cellClass: "text-center" , required: true
		,valueFormatter: function(params) {
			if(params.value && params.value > 0) {
				if(params.value > 100){
					mcxDialog.alert('할인율은 100이하로 설정해주세요.');
					params.data.burdenRate = '0';
					return '0';
				}
				return params.value.addComma();
			} else {
				return '0';
			}
		},
		cellEditorParams: { maxlength: 4, validType: 'numeric'}
	} ,
	{ headerName: "쿠폰번호"  , field: "cpnId" , width: 120 , cellClass: 'text-center' , hide:true},
	{ headerName: "삭제여부"  , field: "delYn" , width: 120 , cellClass: 'text-center' , hide:true}
];

var inComGridOptions = gagaAgGrid.getGridOptions(columnDefs);
inComGridOptions.defaultColDef.editable = true;
inComGridOptions.rowSelection = "multiple";
inComGridOptions.stopEditingWhenGridLosesFocus = true;

var burdenInfoInit = function() {
	gagaAgGrid.createGrid('inComGridList'		, inComGridOptions);

	//업체분담율 추가 버튼 클릭시
	$('#CouponForm #btnAddApplyBurden').on('click', function() {
		cfnOpenCompanyListPopup("fnSetPopupApplyBurdenInfo" ,"M");
	});
	
	// 업체분담율 선택삭제 버튼 클릭시
	$('#CouponForm #btnDelApplyBurden').on('click', function() {
		inComGridOptions.api.updateRowData({remove:gagaAgGrid.selectedRowData(inComGridOptions)});
	});
	
	if (cpnDtlBurdenList != null) {
		inComGridOptions.api.setRowData(cpnDtlBurdenList);
	}
	
	// 비활성화처리 대기상태가아닐때 동작
	if (cpnDetail.cpnStat != 'G232_10' || issueCnt > 0) {
		$('#CouponForm #btnAddApplyBurden').hide();
		$('#CouponForm #btnDelApplyBurden').hide();
		inComGridOptions.defaultColDef.editable = false;
	}
}

//분담업체율 설정 / 업체 추가 콜백함수
var fnSetPopupApplyBurdenInfo = function(result) {
	// 기존 리스트 데이터 for
	for(let i = 0 ; i < result.length ; i++) {
		let addChk = true, gridListValue = gagaAgGrid.getAllRowData(inComGridOptions);		// 받아온 모든 데이터

		// 받아온 data for
		for(let j = 0 ; j < gridListValue.length ; j++) {
			// 동일한 data는 추가하지 않음
			if(gridListValue[j].supplyCompCd == result[i].supplyCompCd) {	addChk = false;	}		// 중복체크
		}

		// 중복되지 않은 데이터 리스트에 추가
		if(addChk) {
			gagaAgGrid.addRowData(inComGridOptions, {"supplyCompCd" : result[i].supplyCompCd, "supplyCompNm" : result[i].supplyCompNm, "delYn" : result[i].delYn});
		}
	}
};
</script>
</html>