<!DOCTYPE html>
<html lang="ko"
	xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : SupplyCompanyForm.html
 * @desc    : 공급업체관리 Page
 *============================================================================
 * STYLE24
 * Copyright(C) 2020 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2020.10.14   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/supply/company/list'}" onsubmit="$('#btnSearch').trigger('click'); return false;">
				<table class="frmStyle" aria-describedby="검색조건">
					<colgroup>
						<col style="width:10%;"/>
						<col style="width:30%;"/>
						<col style="width:10%;"/>
						<col style="width:20%;"/>
						<col style="width:10%;"/>
						<col/>
					</colgroup>
					<tr>
						<th>검색어</th>
						<td>
							<select name="searchGb">
								<option value="NAME">업체명</option>
								<option value="OWNER">대표자명</option>
							</select>
							<input type="text" name="searchTxt" class="w300" maxlength="20"/>
						</td>
						<th>입점상태</th>
						<td>
							<select name="supplyStat">
								<option value="">[전체]</option>
								<option th:if="${supplyStatList}" th:each="oneData, status : ${supplyStatList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
							</select>
						</td>
						<th>전자계약여부</th>
						<td>
							<select name="econtractYn">
								<option value="">[전체]</option>
								<option value="Y">[Y] Yes</option>
								<option value="N">[N] No</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">
			<!-- 버튼 배치 영역 -->
			<ul class="panelBar">
				<li class="right">
					<button type="button" class="btn btn-default btn-lg" id="btnExcel">엑셀다운로드</button>
				</li>
			</ul>
			<!-- //버튼 배치 영역 -->
			
			<div id="gridList" style="width: 100%; height: 470px" class="ag-theme-balham"></div>
		</div>
		<!-- //리스트 영역 -->
		
		<!-- 등록/수정 -->
		<div class="panelStyle">
			<form id="detailForm" name="detailForm" action="#" th:action="@{'/business/supply/company/save'}">
				<h4>기본정보</h4>
				<table class="frmStyle" aria-describedby="등록/수정 폼">
					<colgroup>
						<col style="width:10%;"/>
						<col style="width:15%;"/>
						<col style="width:10%;"/>
						<col style="width:15%;"/>
						<col style="width:10%;"/>
						<col style="width:15%;"/>
						<col style="width:10%;"/>
						<col style="width:15%;"/>
					</colgroup>
					<tr>
						<th>업체코드</th>
						<td>
							<input type="text" class="w150" name="supplyCompCd" placeholder="자동생성" maxlength="5" data-valid-name="업체코드" readonly="readonly"/>
						</td>
						<th>업체명<i class="required" title="필수"></i></th>
						<td>
							<input type="text" name="supplyCompNm" maxlength="100" required="required" data-valid-name="업체명"/>
						</td>
						<th>사업자구분<i class="required" title="필수"></i></th>
						<td>
							<label class="rdoBtn"><input type="radio" name="bizGb" value="C" checked="checked"/>법인<span></span></label>
							<label class="rdoBtn"><input type="radio" name="bizGb" value="P"/>개인<span></span></label>
						</td>
						<th>사업자등록번호<i class="required" title="필수"></i></th>
						<td>
							<input type="text" class="w150" name="bizNo" maxlength="20" required="required" data-valid-name="사업자등록번호"/>
							<button type="button" class="btn btn-info btn-sm" onclick="fnCertifyBizNo();">인증</button>
						</td>
					</tr>
					<tr>
						<th>업종<i class="required" title="필수"></i></th>
						<td>
							<input type="text" name="bizKind" maxlength="100" required="required" data-valid-name="업종"/>
						</td>
						<th>업태<i class="required" title="필수"></i></th>
						<td>
							<input type="text" name="bizType" maxlength="100" required="required" data-valid-name="업태"/>
						</td>
						<th>입점상태<i class="required" title="필수"></i></th>
						<td>
							<select name="supplyStat" id="supplyStat" required="required" data-valid-name="입점상태">
								<option value="">[선택]</option>
								<option th:if="${supplyStatList}" th:each="oneData, status : ${supplyStatList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
							</select>
						</td>
						<th>대표자명<i class="required" title="필수"></i></th>
						<td>
							<input type="text" name="ownerNm" maxlength="50" required="required" data-valid-name="대표자명"/>
						</td>
					</tr>
					<tr>
						<th rowspan="2">사업장주소<i class="required" title="필수"></i></th>
						<td colspan="3" rowspan="2">
							<input type="text" name="bizZipcode"  class="w100" data-valid-name="우편번호" readonly="readonly"/>
							<button type="button" class="btn btn-info btn-sm" onclick="fnOpenDaumAddr();">우편번호찾기</button>
							<input type="text" name="bizBaseAddr" class="w300" maxlength="200" required="required" data-valid-name="사업장주소" readonly="readonly"/><br>
							<input type="text" name="bizDtlAddr" class="w300" maxlength="200"/>
						</td>
						<th>홈페이지URL</th>
						<td colspan="3">
							<input type="text" name="homepageUrl" data-valid-name="홈페이지URL"/>
						</td>
					</tr>
					<tr>
						<th>대표전화<i class="required" title="필수"></i></th>
						<td>
							<input type="text" name="mainTelno" placeholder="02-0000-0000" maxlength="20" required="required" data-valid-name="대표전화"/>
						</td>
						<th>대표팩스번호</th>
						<td>
							<input type="text" name="mainFaxno" placeholder="02-0000-0000" maxlength="20" data-valid-name="대표팩스번호"/>
						</td>
					</tr>
					<tr>
						<th>유통구분<i class="required" title="필수"></i></th>
						<td>
							<select name="distributionGb" required="required" data-valid-name="유통구분">
								<option value="">[선택]</option>
								<option th:if="${distributionGbList}" th:each="oneData, status : ${distributionGbList}" th:value="${oneData.cd}" th:text="${oneData.cdNm}"></option>
							</select>
						</td>
						<th>판매수수료율<i class="required" title="필수"></i></th>
						<td>
							<input type="text" name="sellFeeRate" class="w100 aR" placeholder="0.0" maxlength="5" required="required" data-valid-type="real" data-valid-name="판매수수료율"/>%
						</td>
						<th>사용여부<i class="required" title="필수"></i></th>
						<td>
							<label class="rdoBtn"><input type="radio" name="useYn" value="Y" checked="checked"/>Yes<span></span></label>
							<label class="rdoBtn"><input type="radio" name="useYn" value="N"/>No<span></span></label>
						</td>
						<th>총알배송여부<i class="required" title="필수"></i></th>
						<td>
							<label class="rdoBtn"><input type="radio" name="shotDelvYn" value="Y"/>Yes<span></span></label>
							<label class="rdoBtn"><input type="radio" name="shotDelvYn" value="N" checked="checked"/>No<span></span></label>
						</td>
					</tr>
					<tr>
						<th>CS담당자</th>
						<td>
							<input type="text" name="csChargeNm" class="w100 aR" maxlength="50"/>
						</td>
						<th>CS담당자 연락처</th>
						<td>
							<input type="text" name="csChargeTelno" placeholder="02-0000-0000" maxlength="20"/>
						</td>
						<th>정산담당자</th>
						<td>
							<input type="text" name="settleChargeNm" class="w100 aR" maxlength="50"/>
						</td>
						<th>정산담당자 연락처</th>
						<td>
							<input type="text" name="settleChargeTelno" placeholder="02-0000-0000" maxlength="20"/>
						</td>
					</tr>
					<tr>
						<th>정산담당자 이메일</th>
						<td>
							<input type="text" name="settleChargeEmail" maxlength="100" data-valid-type="email"/>
						</td>
						<th>계산서 이메일</th>
						<td>
							<input type="text" name="billEmail" maxlength="100" data-valid-type="email"/>
						</td>
						<th>전자계약여부<i class="required" title="필수"></i></th>
						<td>
							<label class="rdoBtn"><input type="radio" name="econtractYn" value="Y" checked="checked"/>Yes<span></span></label>
							<label class="rdoBtn"><input type="radio" name="econtractYn" value="N"/>No<span></span></label>
						</td>
						<th>정산주기<i class="required" title="필수"></i></th>
						<td>
							<select name="settleDay" required="required" data-valid-name="정산주기">
								<option value="">[선택]</option>
								<option th:if="${settleDayList}" th:each="oneData, status : ${settleDayList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
							</select>
						</td>
					</tr>
					<tr>
						<th>거래은행</th>
						<td>
							<select name="bankCd">
								<option value="">[선택]</option>
								<option th:if="${bankList}" th:each="oneData, status : ${bankList}" th:value="${oneData.cd}" th:text="${oneData.cdNm}"></option>
							</select>
						</td>
						<th>계좌번호</th>
						<td>
							<input type="text" name="accountNo" maxlength="20"/>
						</td>
						<th>예금주명</th>
						<td colspan="3">
							<input type="text" class="w100" name="depositorNm" maxlength="50"/>
						</td>
					</tr>
					<tr>
						<th>비고</th>
						<td colspan="7">
							<textarea name="remarks" class="textareaR2"  data-valid-name="비고" ></textarea>
						</td>
					</tr>
				</table>
			</form>
			
			<!-- 버튼 배치 영역 -->
			<ul class="panelBar">
				<li class="right">
					<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="delvFeePolicyForm" name="delvFeePolicyForm" action="#" th:action="@{'/business/delivery/fee/policy/list/save'}">
				<input type="hidden" name="delvFeeCd"/>
				<input type="hidden" name="supplyCompCd"/>
				<input type="hidden" name="delvFeeCrite"/>
				<input type="hidden" name="minOrdAmt"/>
				<input type="hidden" name="delvFee"/>
				<input type="hidden" name="rtnDelvFee"/>
				<input type="hidden" name="useYn"/>
				
				<h4 class="delvFeePolicy" style="display: none;">배송비 정책<button type="button" class="btn btn-info btn-ssm marL10" id="btnAddDelvFeePolicy"><i class="fa fa-plus" aria-hidden="true"></i></button></h4>
				<table class="frmStyle delvFeePolicy" style="display: none;" aria-describedby="배송비 정책 등록/수정 폼">
					<colgroup>
						<col style="width:10%;"/>
						<col style="width:25%;"/>
						<col style="width:25%;"/>
						<col style="width:15%;"/>
						<col style="width:15%;"/>
						<col style="width:10%;"/>
					</colgroup>
					<thead>
						<tr>
							<th>정책코드</th>
							<th>배송비부과기준</th>
							<th>무료배송비기준금액</th>
							<th>배송비</th>
							<th>반품배송비</th>
							<th>사용여부</th>
						</tr>
					</thead>
					<tbody id="tbodyDelvFeePolicy">
					</tbody>
				</table>
			</form>
			
			<!-- 버튼 배치 영역 -->
			<ul class="panelBar delvFeePolicy" style="display: none;">
				<li class="right">
					<button type="button" class="btn btn-success btn-lg" id="btnSaveDelvFeePolicy">배송비 정책 저장</button>
				</li>
			</ul>
			<!-- //버튼 배치 영역 -->
		</div>
		<!-- 등록/수정 -->
	</div>

<script th:inline="javascript">
/*<![CDATA[*/
	let supplyStatList = gagajf.convertToArray([[${supplyStatList}]]);
	let distributionGbList = gagajf.convertToArray([[${distributionGbList}]]);
	let bankList = gagajf.convertToArray([[${bankList}]]);

	let columnDefs = [
		{ headerName: "업체코드", field: "supplyCompCd", width: 100, cellClass: 'text-center' },
		{
			headerName: "업체명", field: "supplyCompNm", width: 150, cellClass: 'text-center',
			cellRenderer: function(params) {
				return '<a href="javascript:void(0);">' + params.value + '</a>';
			}
		},
		{
			headerName: "사업자구분", field: "bizGb", width: 100, cellClass: 'text-center',
			valueGetter: function (params) { return params.data.bizGb == 'C' ? '법인' : '개인'; }
		},
		{ headerName: "사업자등록번호", field: "bizNo", width: 120, cellClass: 'text-center' },
		{
			headerName: "전자계약여부", field: "econtractYn", width: 100, cellClass: 'text-center',
			valueGetter: function (params) { return params.data.econtractYn == 'Y' ? 'Yes' : 'No'; }
		},
		{ headerName: "업종", field: "bizKind", width: 150, cellClass: 'text-center' },
		{ headerName: "업태", field: "bizType", width: 150, cellClass: 'text-center' },
		{
			headerName: "총알배송여부", field: "shotDelvYn", width: 100, cellClass: 'text-center',
			valueGetter: function (params) { return params.data.shotDelvYn == 'Y' ? 'Yes' : 'No'; }
		},
		{
			headerName: "입점상태", field: "supplyStat", width: 100, cellClass: 'text-center',
			valueGetter: function (params) {
				return gagaAgGrid.lookupValue(supplyStatList, params.data.supplyStat);
			}
		},
		{ headerName: "대표자명", field: "ownerNm", width: 100, cellClass: 'text-center' },
		{ headerName: "우편번호", field: "bizZipcode", width: 80, cellClass: 'text-center' },
		{ headerName: "기본주소", field: "bizBaseAddr", width: 250 },
		{ headerName: "상세주소", field: "bizDtlAddr", width: 150 },
		{ headerName: "대표전화번호", field: "mainTelno", width: 120, cellClass: 'text-center' },
		{ headerName: "대표팩스번호", field: "mainFaxno", width: 120, cellClass: 'text-center' },
		{ headerName: "홈페이지URL", field: "homepageUrl", width: 200 },
		{
			headerName: "유통구분", field: "distributionGb", width: 100, cellClass: 'text-center',
			valueGetter: function (params) {
				return gagaAgGrid.lookupValue(distributionGbList, params.data.distributionGb);
			}
		},
// 		{
// 			headerName: "무료배송비최소주문금액", field: "minOrdAmt", width: 150, cellClass: 'text-center',
// 			valueGetter: function(params) {
// 				return Number(params.data.minOrdAmt).addComma();
// 			}
// 		},
// 		{
// 			headerName: "배송비", field: "delvFee", width: 100, cellClass: 'text-center',
// 			valueGetter: function(params) {
// 				return Number(params.data.delvFee).addComma();
// 			}
// 		},
		{
			headerName: "판매수수료율", field: "sellFeeRate", width: 120, cellClass: 'text-center',
			valueGetter: function(params) {
				return params.data.sellFeeRate + '%';
			}
		},
		{ headerName: "CS담당자", field: "csChargeNm", width: 100, cellClass: 'text-center', hide: true },
		{ headerName: "CS담당자연락처", field: "csChargeTelno", width: 100, cellClass: 'text-center', hide: true },
		{ headerName: "정산담당자", field: "settleChargeNm", width: 100, cellClass: 'text-center', hide: true },
		{ headerName: "정산담당자연락처", field: "settleChargeTelno", width: 100, cellClass: 'text-center', hide: true },
		{ headerName: "정산담당자이메일", field: "settleChargeEmail", width: 100, cellClass: 'text-center', hide: true },
		{ headerName: "계산서이메일", field: "billEmail", width: 100, cellClass: 'text-center', hide: true },
		{ headerName: "정산주기", field: "settleDay", width: 100, cellClass: 'text-center', hide: true },
		{
			headerName: "은행명", field: "bankCd", width: 150, cellClass: 'text-center', hide: true,
			valueGetter: function (params) {
				return gagaAgGrid.lookupValue(bankList, params.data.bankCd);
			}
		},
		{ headerName: "계좌번호", field: "accountNo", width: 150, cellClass: 'text-center', hide: true },
		{ headerName: "예금주명", field: "depositorNm", width: 100, cellClass: 'text-center', hide: true },
		{
			headerName: "사용여부", field: "useYn", width: 100, cellClass: 'text-center',
			valueGetter: function (params) { return params.data.useYn == 'Y' ? 'Yes' : 'No'; }
		},
		{ headerName: "비고", field: "remarks", width: 150, hide: true }
	];

	let gridOptions = gagaAgGrid.getGridOptions(columnDefs);

	// Cell click
	gridOptions.onCellClicked = function(event) {
		if (event.colDef.field != 'supplyCompNm')
			return;
		
		$('#detailForm input[name=supplyCompCd]').val(event.data.supplyCompCd);
		$('#detailForm input[name=supplyCompNm]').val(event.data.supplyCompNm);
		$('#detailForm select[name=supplyStat]').val(event.data.supplyStat);
		$('#detailForm input[name=ownerNm]').val(event.data.ownerNm);
		
		if (event.data.bizGb == 'C') {
			$('#detailForm input:radio[name=bizGb]').eq(0).trigger('click');
		} else {
			$('#detailForm input:radio[name=bizGb]').eq(1).trigger('click');
		}
		
		$('#detailForm input[name=bizNo] ').val(event.data.bizNo);
		$('#detailForm input[name=bizKind]').val(event.data.bizKind);
		$('#detailForm input[name=bizType]').val(event.data.bizType);
		$('#detailForm input[name=bizZipcode]').val(event.data.bizZipcode);
		$('#detailForm input[name=bizBaseAddr]').val(event.data.bizBaseAddr);
		$('#detailForm input[name=bizDtlAddr]').val(event.data.bizDtlAddr);
		$('#detailForm input[name=homepageUrl]').val(event.data.homepageUrl);
		$('#detailForm select[name=distributionGb]').val(event.data.distributionGb);
		$('#detailForm input[name=mainTelno]').val(event.data.mainTelno);
		$('#detailForm input[name=mainFaxno]').val(event.data.mainFaxno);
		$('#detailForm input[name=sellFeeRate]').val(event.data.sellFeeRate);
		$('#detailForm input[name=csChargeNm]').val(event.data.csChargeNm);
		$('#detailForm input[name=csChargeTelno]').val(event.data.csChargeTelno);
		$('#detailForm input[name=settleChargeNm]').val(event.data.settleChargeNm);
		$('#detailForm input[name=settleChargeTelno]').val(event.data.settleChargeTelno);
		$('#detailForm input[name=settleChargeEmail]').val(event.data.settleChargeEmail);
		$('#detailForm input[name=billEmail]').val(event.data.billEmail);
		
		if (event.data.econtractYn == 'Y') {
			$('#detailForm input:radio[name=econtractYn]').eq(0).trigger('click');
		} else {
			$('#detailForm input:radio[name=econtractYn]').eq(1).trigger('click');
		}
		
		$('#detailForm select[name=settleDay]').val(event.data.settleDay);
// 		$('#detailForm input[name=minOrdAmt]').val(Number(event.data.minOrdAmt).addComma());
// 		$('#detailForm input[name=delvFee]').val(Number(event.data.delvFee).addComma());
		$('#detailForm select[name=bankCd]').val(event.data.bankCd);
		$('#detailForm input[name=accountNo]').val(event.data.accountNo);
		$('#detailForm input[name=depositorNm]').val(event.data.depositorNm);
		$('#detailForm textarea[name=remarks]').val(event.data.remarks);
		
		if (event.data.useYn == 'Y') {
			$('#detailForm input:radio[name=useYn]').eq(0).trigger('click');
		} else {
			$('#detailForm input:radio[name=useYn]').eq(1).trigger('click');
		}
		
		if (event.data.shotDelvYn == 'Y') {
			$('#detailForm input:radio[name=shotDelvYn]').eq(0).trigger('click');
		} else {
			$('#detailForm input:radio[name=shotDelvYn]').eq(1).trigger('click');
		}
		
		// 배송비 정책 조회
		fnGetDeliveryFeePolicy(event.data.supplyCompCd);
	}

	// 검색
	$('#btnSearch').on('click', function() {
		gagaAgGrid.fetch($('#searchForm').prop('action'), gridOptions, '#searchForm');
	});
	
	// 신규버튼
	$('#btnNew').on('click', function() {
		$("#detailForm")[0].reset();
		
		// 배송비 정책 초기화
		$('#tbodyDelvFeePolicy').html('');
		$('.delvFeePolicy').hide();
	});

	// 기본정보 저장
	$("#btnSave").on("click", function() {
		// 필수값 체크
		if (!gagajf.validation('#detailForm'))
			return false;

		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);
	});

	// 배송비 정책 추가
	$('#btnAddDelvFeePolicy').on('click', function() {
		let idx = $('#delvFeePolicyForm input[name=delvFeeCd]').length;
		
		let tag = '<tr>\n';
		tag += '	<td align="center">\n';
		tag += '		<input type="text" name="delvFeeCd" class="w100 aC" placeholder="자동생성" maxlength="20" readonly="readonly"/>\n';
		tag += '		<input type="hidden" name="supplyCompCd" value="' + $('#detailForm input[name=supplyCompCd]').val() + '"/>\n';
		tag += '	</td>\n';
		tag += '	<td align="center">\n';
		tag += '		<select name="delvFeeCrite" onclick="fnChangeDeliveryFeeCriterion(' + idx + ', this);">\n';
		tag += '			<option value="G078_10">[G078_10] 조건부 무료</option>\n';
		tag += '			<option value="G078_20">[G078_20] 무료</option>\n';
		tag += '			<option value="G078_30">[G078_30] 유료</option>\n';
		tag += '		</select>\n';
		tag += '	</td>\n';
		tag += '	<td align="center">\n';
		tag += '		<input type="text" name="minOrdAmt" class="w100 aR" placeholder="" maxlength="7" required="required" data-valid-type="integer" data-valid-name="무료배송비기준금액"/>원 <span id="txtCriterion' + idx + '">이상</span>\n';
		tag += '	</td>\n';
		tag += '	<td align="center">\n';
		tag += '		<input type="text" name="delvFee" class="w100 aR" placeholder="" maxlength="7" required="required" data-valid-type="integer" data-valid-name="배송비"/>원\n';
		tag += '	</td>\n';
		tag += '	<td align="center">\n';
		tag += '		<input type="text" name="rtnDelvFee" class="w100 aR" placeholder="" maxlength="7" required="required" data-valid-type="integer" data-valid-name="반품배송비"/>원\n';
		tag += '	</td>\n';
		tag += '	<td align="center">\n';
		tag += '		<select name="useYn">\n';
		tag += '			<option value="Y">Yes</option>\n';
		tag += '			<option value="N">No</option>\n';
		tag += '		</select>\n';
		tag += '	</td>\n';
		tag += '</tr>\n';
		
		$('#tbodyDelvFeePolicy').append(tag);
	});
	
	// 배송비 정책 조회
	let fnGetDeliveryFeePolicy = function(supplyCompCd) {
		$('#tbodyDelvFeePolicy').html('');
		
		$.getJSON('/business/delivery/fee/policy/list/' + supplyCompCd
			, function(result, status) {
				if (result == null || result.length == 0) {
					$('.delvFeePolicy').show();
					return;
				}
				
				$.each(result, function(idx, item) {
					let tag = '<tr>\n';
					tag += '	<td align="center">\n';
					tag += '		<input type="text" name="delvFeeCd" class="w100 aC" placeholder="자동생성" value="' + item.delvFeeCd + '" maxlength="20" readonly="readonly"/>\n';
					tag += '		<input type="hidden" name="supplyCompCd" value="' + item.supplyCompCd + '"/>\n';
					tag += '	</td>\n';
					tag += '	<td align="center">\n';
					tag += '		<select name="delvFeeCrite" onclick="fnChangeDeliveryFeeCriterion(' + (idx + 1) + ', this);">\n';
					tag += '			<option value="G078_10"' + (item.delvFeeCrite == "G078_10" ? " selected" : "") + '>[G078_10] 조건부 무료</option>\n';
					tag += '			<option value="G078_20"' + (item.delvFeeCrite == "G078_20" ? " selected" : "") + '>[G078_20] 무료</option>\n';
					tag += '			<option value="G078_30"' + (item.delvFeeCrite == "G078_30" ? " selected" : "") + '>[G078_30] 유료</option>\n';
					tag += '		</select>\n';
					tag += '	</td>\n';
					tag += '	<td align="center">\n';
					tag += '		<input type="text" name="minOrdAmt" class="w100 aR" placeholder="" value="' + item.minOrdAmt.addComma() + '" maxlength="7" required="required" data-valid-type="integer" data-valid-name="무료배송비기준금액"/>원 <span id="txtCriterion' + (idx + 1) + '">이상</span>\n';
					tag += '	</td>\n';
					tag += '	<td align="center">\n';
					tag += '		<input type="text" name="delvFee" class="w100 aR" placeholder="" value="' + item.delvFee.addComma() + '" maxlength="7" required="required" data-valid-type="integer" data-valid-name="배송비"/>원\n';
					tag += '	</td>\n';
					tag += '	<td align="center">\n';
					tag += '		<input type="text" name="rtnDelvFee" class="w100 aR" placeholder="" value="' + item.rtnDelvFee.addComma() + '" maxlength="7" required="required" data-valid-type="integer" data-valid-name="반품배송비"/>원\n';
					tag += '	</td>\n';
					tag += '	<td align="center">\n';
					tag += '		<select name="useYn">\n';
					tag += '			<option value="Y" ' + (item.useYn == "Y" ? "selected" : "") + '>Yes</option>\n';
					tag += '			<option value="N" ' + (item.useYn == "N" ? "selected" : "") + '>No</option>\n';
					tag += '		</select>\n';
					tag += '	</td>\n';
					tag += '</tr>\n';
					
					$('#tbodyDelvFeePolicy').append(tag);
				});
				
				$('.delvFeePolicy').show();
			});
	}

	// 배송비부과기준
	var fnChangeDeliveryFeeCriterion = function(idx, obj) {
		if ($(obj).val() == 'G078_10') {
			$('#delvFeePolicyForm input[name=minOrdAmt]').eq(idx).attr('readonly', false);
			$('#txtCriterion' + idx).html('이상');
// 			$('#delvFeePolicyForm input[name=delvFee]').eq(idx).attr('readonly', false);
		} else if ($(obj).val() == 'G078_20') {
			$('#delvFeePolicyForm input[name=minOrdAmt]').eq(idx).val(0);
			$('#delvFeePolicyForm input[name=minOrdAmt]').eq(idx).attr('readonly', true);
			$('#txtCriterion' + idx).html('이상');
// 			$('#delvFeePolicyForm input[name=delvFee]').eq(idx).val(0);
// 			$('#delvFeePolicyForm input[name=delvFee]').eq(idx).attr('readonly', true);
		} else if ($(obj).val() == 'G078_30') {
			$('#delvFeePolicyForm input[name=minOrdAmt]').eq(idx).val(0);
			$('#delvFeePolicyForm input[name=minOrdAmt]').eq(idx).attr('readonly', true);
			$('#txtCriterion' + idx).html('금액에 상관 없음');
// 			$('#delvFeePolicyForm input[name=delvFee]').eq(idx).attr('readonly', false);
		}
	}
	
	// 배송비정책 저장
	$("#btnSaveDelvFeePolicy").on("click", function() {
		// 필수값 체크
		if (!gagajf.validation('#delvFeePolicyForm'))
			return false;

		mcxDialog.confirm("배송비정책을 저장하시겠습니까?", {
			cancelBtnText: "취소",
			sureBtnText: "확인",
			sureBtnClick: function() {
				// comma(,) 제거
				gagajf.removeCommaAtNumberFormattedInput('#delvFeePolicyForm');
				
				var arrData = [];
				$('#tbodyDelvFeePolicy tr').each(function(idx) {
					var params = new Object();
					params.delvFeeCd = $(this).find('input[name=delvFeeCd]').val();
					params.supplyCompCd = $(this).find('input[name=supplyCompCd]').val();
					params.delvFeeCrite = $(this).find('select[name=delvFeeCrite]').val();
					params.minOrdAmt = $(this).find('input[name=minOrdAmt]').val();
					params.delvFee = $(this).find('input[name=delvFee]').val();
					params.rtnDelvFee = $(this).find('select[name=rtnDelvFee]').val();
					params.useYn = $(this).find('select[name=useYn]').val();
					arrData.push(params);
				});
				
				let jsonData = JSON.stringify(arrData);
				gagajf.ajaxJsonSubmit($('#delvFeePolicyForm').prop('action'), jsonData, function() {
					// 배송비정책 조회
					fnGetDeliveryFeePolicy($('#detailForm input[name=supplyCompCd]').val());
				});
			}
		});
	});
	
	/**
	 * DAUM을 이용한 우편번호 팝업 레이어
	 */
	var fnOpenDaumAddr = function() {
		let daumZip = new daum.Postcode({
			oncomplete: function(data) {
				// 우편번호와 주소 정보를 해당 필드에 넣는다.
				$('#detailForm input[name=bizZipcode]').val(data.zonecode);
				$('#detailForm input[name=bizBaseAddr]').val(cfnGetDaumRoadAddr(data));
				$('#detailForm input[name=bizDtlAddr]').focus();
				
				cfnCloseDaumAddr();
			},
			width: '100%'
		});
		
		cfnOpenDaumAddr(daumZip);
	}
	
	$(document).ready(function() {
		// Create a agGrid
		gagaAgGrid.createGrid('gridList', gridOptions);
	});
/*]]>*/
</script>

</html>
