<!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="btnAddAf">제휴채널 추가</button>
						<button type="button" class="btn btnRight btn-success btn-lg" id="btnDeleteAf">선택삭제</button>
						<br/>
						<div id="afChannelList" style="width:100%; height:200px;" class="ag-theme-balham"></div>
					</div>
				</td>
			</tr>
			</tbody>
		</table>
	</div>
</div>

<!-- //제휴채널 설정 -->
<script th:inline="javascript">
let channelName 	= gagajf.convertToArray([[${channelList}]]);
let afChannelList 	= [[${afChannelList}]];

let columnAfDefs = [
	{width: 40, minWidth: 40, cellClass: 'text-right', headerCheckboxSelection: true, checkboxSelection: true, filter: false},
	{headerName: "제휴채널코드", field: "afLinkCd", width: 100, cellClass: 'text-center'},
	{
		headerName: "제휴채널명", field: "afLinkNm", width: 180,
		cellRenderer: function(params) {
			return '<a href="javascript:void(0);">' + params.value + '</a>';
		}
	},
	{headerName: "상위제휴채널", field: "afChannel", width: 120, cellClass: 'text-center',
		cellEditor: 'agRichSelectCellEditor',
		cellEditorParams: { values: gagaAgGrid.extractValues(channelName) },
		valueFormatter: function (params) { return gagaAgGrid.lookupValue(channelName, params.value); },
		valueParser: function (params) { return gagaAgGrid.lookupKey(channelName, params.newValue); }
	}
];

// 제휴채널 그리드 설정
var afLinkOptions = gagaAgGrid.getGridOptions(columnAfDefs);

afLinkOptions.rowSelection = "multiple";
afLinkOptions.suppressRowClickSelection = true;

var afChannelInfoInit = function() {
	gagaAgGrid.createGrid('afChannelList'		, afLinkOptions );
	
	if (afChannelList != null) {
		afLinkOptions.api.setRowData(afChannelList);
	}
	
	// 제휴채널 추가
	$('#btnAddAf').on('click' , function () {
		cfnOpenAfChannelPopup("callBackAf");
	});
	
	// 제휴채널 선택삭제 버튼 클릭시
	$('#btnDeleteAf').on('click', function() {
		afLinkOptions.api.updateRowData({remove:gagaAgGrid.selectedRowData(afLinkOptions)});
	});
	
	// 비활성화처리 대기상태가아닐때 동작
	if (cpnDetail.cpnStat != 'G232_10' || issueCnt > 0) {
		$('#btnAddAf').hide();
		$('#btnDeleteAf').hide();
	}
}

//제휴채널 콜백
var callBackAf = function(result) {
	// 기존 리스트 데이터 for
	for(let i = 0 ; i < result.length ; i++) {
		let addChk = true, gridListValue = gagaAgGrid.getAllRowData(afLinkOptions);		// 받아온 모든 데이터

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

		// 중복되지 않은 데이터 리스트에 추가
		if(addChk) {
			gagaAgGrid.addRowData(afLinkOptions, {"afLinkCd":result[i].afLinkCd , "afLinkNm":result[i].afLinkNm, "afChannel":result[i].afChannel});
		}
	}
}

</script>
</html>