<!DOCTYPE html>
<html lang="ko"
	xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : AflinkForm.html
 * @desc    : 제휴채널관리 Page
 *============================================================================
 * STYLE24
 * Copyright(C) 2020 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2020.10.20   gagamel     최초 작성
 *******************************************************************************
 -->
	<div id="main">
		<!-- 메인타이틀 영역 -->
		<div class="main-title">
		</div>
		<!-- //메인타이틀 영역 -->
		
		<!-- 메뉴 설명 -->
		<div class="infoBox menu-desc">
		</div>
		<!-- //메뉴 설명 -->
		
		<!-- 검색조건 영역 -->
		<div class="panelStyle">
			<form id="searchForm" name="searchForm" action="#" th:action="@{'/business/aflink/list'}" onsubmit="$('#btnSearch').trigger('click'); return false;">
				<table class="frmStyle" aria-describedby="검색조건">
					<colgroup>
						<col style="width:10%;"/>
						<col/>
					</colgroup>
					<tr>
						<th>상위제휴채널</th>
						<td>
							<select name="afChannel">
								<option value="">[전체]</option>
								<option th:if="${afChannelList}" th:each="oneData, status : ${afChannelList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
							</select>
						</td>
					</tr>
				</table>
				
				<ul class="panelBar">
					<li class="center">
						<button type="button" class="btn btn-base btn-lg" id="btnSearch">조회</button>
						<button type="button" class="btn btn-gray btn-lg" onclick="$('#searchForm')[0].reset();">초기화</button>
					</li>
				</ul>
			</form>
		</div>
		<!-- 검색조건 영역 -->

		<!-- 리스트 영역 -->
		<div class="panelStyle">
			<!-- 다중 TABLE 배치 -->
			<ul class="division">
				<li style="width: 60%">
					<!-- 버튼 배치 영역 -->
					<ul class="panelBar">
						<li class="left">
							<button type="button" class="btn btn-danger btn-lg" id="btnDelete">사용안함</button>
						</li>
						<li class="right">
							<button type="button" class="btn btn-default btn-lg" id="btnExcel">엑셀다운로드</button>
						</li>
					</ul>
					<!-- //버튼 배치 영역 -->
					
					<div id="gridList" style="width: 100%; height: 570px;" class="ag-theme-balham"></div>
				</li>
				<li>
					<!-- 버튼 배치 영역 -->
					<ul class="panelBar">
						<li>
							<button type="button" class="btn btn-info btn-lg" id="btnNew">신규</button>
							<button type="button" class="btn btn-success btn-lg" id="btnSave">저장</button>
						</li>
					</ul>
					<!-- //버튼 배치 영역 -->
					
					<form id="detailForm" name="detailForm" action="#" th:action="@{'/business/aflink/save'}">
						<table class="frmStyle">
							<colgroup>
								<col style="width:30%"/>
								<col/>
							</colgroup>
							<tr>
								<th>제휴코드<i class="star"></i></th>
								<td>
									<input type="text" class="w100" name="afLinkCd" placeholder="" maxlength="5" required="required" data-valid-type="alphaNumeric" data-valid-name="제휴코드" onkeyup="$(this).val($(this).val().toUpperCase());"/>
								</td>
							</tr>
							<tr>
								<th>제휴명<i class="star"></i></th>
								<td>
									<input type="text" class="w300" name="afLinkNm" placeholder="" maxlength="100" required="required" data-valid-name="제휴명"/>
								</td>
							</tr>
							<tr>
								<th>상위제휴채널<i class="star"></i></th>
								<td>
									<select name="afChannel" required="required" data-valid-name="상위제휴채널">
										<option value="">[선택]</option>
										<option th:if="${afChannelList}" th:each="oneData, status : ${afChannelList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
									</select>
								</td>
							</tr>
							<tr>
								<th>마진율<i class="star"></i></th>
								<td>
									<input type="text" class="w100 text-right" name="feeRate" placeholder="" maxlength="3" required="required" data-valid-type="real" data-valid-name="마진율" />%
								</td>
							</tr>
							<tr>
								<th>표시순서<i class="star"></i></th>
								<td>
									<input type="text" class="w100 text-right" name="dispOrd" placeholder="" maxlength="5" required="required" data-valid-type="numeric" data-valid-name="표시순서" />
								</td>
							</tr>
							<tr>
								<th>사용여부<i class="star"></i></th>
								<td>
									<input type="hidden" name="useYn"/>
									<label class="chkBox"><input type="checkbox" name="chkUseYn" value="Y" checked="checked" disabled="disabled"/>사용</label>
								</td>
							</tr>
						</table>
					</form>
				</li>
			</ul>
			<!--//다중 TABLE 배치 -->
		</div>
		<!-- //리스트 영역 -->
	</div>

<script th:inline="javascript">
/*<![CDATA[*/
	let afChannelList = gagajf.convertToArray([[${afChannelList}]]);
	let useYnList = { "Y":"Yes", "N":"No" };

	let columnDefs = [
		{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',
			valueGetter: function (params) { return gagaAgGrid.lookupValue(afChannelList, params.data.afChannel); }
		},
		{
			headerName: "마진율", field: "feeRate", width: 100, cellClass: 'text-center',
			cellRenderer: function(params) { return params.value + '%'; }
		},
		{headerName: "표시순서", field: "dispOrd", width: 100, cellClass: 'text-center'},
		{
			headerName: "사용여부", field: "useYn", width: 100, cellClass: 'text-center',
			valueGetter: function (params) { return gagaAgGrid.lookupValue(useYnList, params.data.useYn); }
		}
	];

	let gridOptions = gagaAgGrid.getGridOptions(columnDefs);

	// 다중 선택
	gridOptions.rowSelection = 'multiple';
	
	// Cell click
	gridOptions.onCellClicked = function(event) {
		if (event.colDef.field != 'afLinkNm')
			return;
		
		$('#detailForm input[name=afLinkCd]').val(event.data.afLinkCd);
		$('#detailForm input[name=afLinkCd]').attr('readonly', true);
		$('#detailForm input[name=afLinkNm]').val(event.data.afLinkNm);
		$('#detailForm select[name=afChannel]').val(event.data.afChannel);
		$('#detailForm input[name=feeRate]').val(event.data.feeRate);
		$('#detailForm input[name=dispOrd]').val(event.data.dispOrd);
		
		$("#detailForm input[name=useYn]").val(event.data.useYn);
		if (event.data.useYn == 'Y') {
			$("#detailForm input:checkbox[name=chkUseYn]").attr('checked', true);
			$("#detailForm input:checkbox[name=chkUseYn]").parent().addClass('checked');
		} else {
			$("#detailForm input:checkbox[name=chkUseYn]").attr('checked', false);
			$("#detailForm input:checkbox[name=chkUseYn]").parent().removeClass('checked');
		}

		$('#detailForm input[name=afLinkNm]').focus();
	}
	
	// 검색
	$('#btnSearch').on('click', function() {
		var actionUrl = $('#searchForm').prop('action') + '?' + $('#searchForm').serialize();

		// Fetch data
		gagaAgGrid.fetch(actionUrl, gridOptions, '#searchForm');
	});

	// 신규
	$('#btnNew').on('click', function() {
		$('#detailForm')[0].reset();
		$('#detailForm input[name=afLinkCd]').attr('readonly', false);
		$('#detailForm input[name=afLinkCd]').removeClass('formControl');
		$('#detailForm input:checkbox[name=chkUseYn]').attr('disabled', true);
		$('#detailForm input:checkbox[name=chkUseYn]').addClass('formControl');
		$('#detailForm input[name=afLinkCd]').focus();
	});
	
	// 사용안함 버튼 클릭 시
	$('#btnDelete').on('click', function() {
		var removedData = gagaAgGrid.removeRowData(gridOptions);

		if (removedData.length == 0) {
			mcxDialog.alert('선택된 행이 없습니다.');
			return;
		}

		mcxDialog.confirm('사용안함으로 처리하시겠습니까?', {
			cancelBtnText: "취소",
			sureBtnText: "확인",
			sureBtnClick: function(){
				// delete 대신 update 처리해야 하므로 다음과 같이 useYn 값을 변환
				var updatedData = [];

				$.each(removedData, function(idx, item) {
					item.useYn = 'N';
					updatedData.push(item);
				});

				var jsonData = JSON.stringify(updatedData);
				gagajf.ajaxJsonSubmit('/business/aflink/list/delete'
						, jsonData
						, function() {
							$('#btnSearch').trigger('click');
						});
			}
		});
	});
	
	// 저장 처리
	$('#btnSave').on('click', function() {
		// 입력 값 체크
		if (!gagajf.validation('#detailForm'))
			return false;

		$('#detailForm input[name=useYn]').val($('#detailForm input:checkbox[name=chkUseYn]').is(":checked") ? 'Y' : 'N');

		mcxDialog.confirm('저장하시겠습니까?', {
			cancelBtnText: "취소",
			sureBtnText: "확인",
			sureBtnClick: function(){
				gagajf.ajaxFormSubmit($('#detailForm').prop('action')
						, $('#detailForm')
						, function() {
							$('#btnNew').trigger('click');
							$('#btnSearch').trigger('click');
						}
				);
			}
		});
	});
	
	// 엑셀다운로드
	$('#btnExcel').on('click', function() {
		gagaAgGrid.exportToExcel('제휴채널 목록', gridOptions);
	});
	
	$(document).ready(function() {
		// Create a agGrid
		gagaAgGrid.createGrid('gridList', gridOptions);
	});
/*]]>*/
</script>

</html>
