<!DOCTYPE html>
<html lang="ko"
	xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : DeliveryLocForm.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/delvloc/list'}" onsubmit="$('#btnSearch').trigger('click'); return false;">
				<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/>
					</colgroup>
					<tr>
						<th>공급업체</th>
						<td>
<!-- 							<select name="supplyCompCd"> -->
<!-- 								<option value="">[전체]</option> -->
<!-- 								<option th:if="${supplyCompList}" th:each="oneData, status : ${supplyCompList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option> -->
<!-- 							</select> -->
							<input type="text" class="w100" name="supplyCompSearchTxt" id="supplyCompSearchTxt" maxlength="20"/>
							<button type="button" class="btn icn" onclick="cfnOpenCompanyListPopup('fnSetSupplyCompInfo', 'M');"><i class="fa fa-search"></i></button>
							<span id="supplyCompTxt"></span>
							<input type="hidden" name="supplyCompList"/>
						</td>
						<th>출고처유형</th>
						<td>
							<select name="delvLocClsf">
								<option value="">[전체]</option>
								<option th:if="${delvLocClsfList}" th:each="oneData, status : ${delvLocClsfList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
							</select>
						</td>
						<th>출고처명</th>
						<td>
							<input type="text" name="delvLocNm" maxlength="50"/>
						</td>
						<th>사용여부</th>
						<td>
							<select name="useYn">
								<option value="Y">Yes</option>
								<option value="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/delvloc/save'}">
				<input type="hidden" name="mode" value="N"/>
				
				<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>출고처코드<i class="required" title="필수"></i></th>
						<td colspan="7">
							<input type="text" class="w100" name="delvLocCd" maxlength="20" placeholder="자동생성" required="required" data-valid-type="alphaNumeric" data-valid-name="출고처코드" readonly="readonly"/>
							<span class="infoTxt cBlue">
								<i class="fa fa-info-circle" aria-hidden="true"></i>물류창고 : ST0001, <i class="fa fa-info-circle" aria-hidden="true"></i>일반매장/직송매장 : ERP매장코드, <i class="fa fa-info-circle" aria-hidden="true"></i>입점업체 : DL+일련번호4자리로 자동생성
							</span>
						</td>
					</tr>
					<tr>
						<th>출고처명<i class="required" title="필수"></i></th>
						<td>
							<input type="text" class="w200" name="delvLocNm" maxlength="100" placeholder="출고처명" required="required" data-valid-name="출고처명"/>
						</td>
						<th>출고처유형<i class="required" title="필수"></i></th>
						<td>
							<select name="delvLocClsf" required="required" data-valid-name="출고처유형">
								<option value="">[선택]</option>
								<option th:if="${delvLocClsfList}" th:each="oneData, status : ${delvLocClsfList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
							</select>
						</td>
						<th>공급업체<i class="required" title="필수"></i></th>
						<td colspan="3">
							<select name="supplyCompCd" required="required" data-valid-name="공급업체">
								<option value="">[선택]</option>
								<option th:if="${supplyCompList}" th:each="oneData, status : ${supplyCompList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
							</select>
						</td>
					</tr>
					<tr>
						<th>재고적용율</th>
						<td colspan="3">
							<input type="text" class="w100 aR" name="stockApplRate" maxlength="5" data-valid-type="real"/>%
							<span class="cBlue">* WMS부터 수신된 재고의 판매가용재고 적용율(%). 물류창고와 직송매장에 한함</span>
						</td>
						<th>출고수수료율</th>
						<td colspan="3">
							<input type="text" class="w100 aR" name="delvFeeRate" maxlength="5" data-valid-type="real"/>%
							<span class="cBlue">* 직송매장 출고 시 적용할 출고수수료율(%)</span>
						</td>
					</tr>
					<tr>
						<th>출고지정순서</th>
						<td colspan="3">
							<input type="text" class="w100 aR" name="delvAssignOrd" maxlength="3" data-valid-type="integer"/>
							<span class="cBlue">* 물류창고와 직송매장에 대해 출고지정되는 순서</span>
						</td>
<!-- 						<th>출고지정등급</th> -->
<!-- 						<td> -->
<!-- 							<select name="delvAssignGrade"> -->
<!-- 								<option value="">[선택]</option> -->
<!-- 								<option value="A">[A] A등급</option> -->
<!-- 								<option value="B">[B] B등급</option> -->
<!-- 								<option value="C">[C] C등급</option> -->
<!-- 								<option value="Z">[Z] 출고지정제외</option> -->
<!-- 							</select> -->
<!-- 						</td> -->
						<th>사용여부<i class="required" title="필수"></i></th>
						<td colspan="3">
							<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>
					</tr>
					<tr>
						<th>출고처주소<i class="required" title="필수"></i></th>
						<td colspan="7">
							<input type="text" class="w100" name="delvLocZipcode" data-valid-name="출고처 우편번호" readonly="readonly"/>
							<button type="button" class="btn btn-info btn-sm" onclick="fnOpenDaumAddr('delvLoc');">우편번호찾기</button>
							<input type="text" class="w300" name="delvLocBaseAddr" required="required" data-valid-name="출고처 기본주소" readonly="readonly"/>
							-
							<input type="text" class="w300" name="delvLocDtlAddr" maxlength="100" required="required" data-valid-name="출고처 상세주소"/>
						</td>
					</tr>
					<tr>
						<th>반품처명</th>
						<td>
							<input type="text" class="w200" name="rtnLocNm" maxlength="100" placeholder="반품처명"/>
						</td>
						<th>반품처주소<i class="required" title="필수"></i></th>
						<td colspan="5">
							<input type="text" class="w100" name="rtnLocZipcode" data-valid-name="반품처 우편번호" readonly="readonly"/>
							<button type="button" class="btn btn-info btn-sm" onclick="fnOpenDaumAddr('rtnLoc');">우편번호찾기</button>
							<input type="text" class="w300" name="rtnLocBaseAddr" required="required" data-valid-name="반품처 기본주소" readonly="readonly"/>
							-
							<input type="text" class="w300" name="rtnLocDtlAddr" maxlength="100" required="required" data-valid-name="반품처 상세주소"/>
						</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>
			<!-- //버튼 배치 영역 -->
		</div>
		<!-- 등록/수정 -->
	</div>

<script th:inline="javascript">
/*<![CDATA[*/
	let supplyCompList = gagajf.convertToArray([[${supplyCompList}]]);
	let delvLocClsfList = gagajf.convertToArray([[${delvLocClsfList}]]);
	let delvGradeList = { "A":"A등급", "B":"B등급", "C":"C등급", "Z":"출고지정제외" };
	let invoicePrintTypeList = gagajf.convertToArray([[${invoicePrintTypeList}]]);

	let columnDefs = [
		{ headerName: "출고처코드", field: "delvLocCd", width: 100, cellClass: 'text-center' },
		{
			headerName: "출고처명", field: "delvLocNm", width: 150, cellClass: 'text-center',
			cellRenderer: function(params) {
				return '<a href="javascript:void(0);">' + params.value + '</a>';
			}
		},
		{
			headerName: "출고처유형", field: "delvLocClsf", width: 150, cellClass: 'text-center',
			valueGetter: function (params) {
				return gagaAgGrid.lookupValue(delvLocClsfList, params.data.delvLocClsf);
			}
		},
// 		{
// 			headerName: "매장POS", field: "btnPos", width: 120, cellClass: 'text-center',
// 			cellRenderer: function(params) {
// 				var btn = '';
// 				if (params.data.delvLocClsf === 'G024_21') {
// 					btn ='<button type="button" class="btn btn-success btn-sm" onclick="cfnPopPos(\'' + params.data.delvLocCd + '\');\">매장[POS]</button>';
// 				}
// 				return btn;
// 			}
// 		},
		{
			headerName: "공급업체", field: "supplyCompCd", width: 150, cellClass: 'text-center',
			valueGetter: function (params) {
				return gagaAgGrid.lookupValue(supplyCompList, params.data.supplyCompCd);
			}
		},
		{ headerName: "재고적용율(%)", field: "stockApplRate", width: 120, cellClass: 'text-center' },
		{ headerName: "출고수수료율(%)", field: "delvFeeRate", width:120, cellClass: 'text-center' },
		{ headerName: "출고지정순서", field: "delvAssignOrd", width: 120, cellClass: 'text-center' },
// 		{
// 			headerName: "출고지정등급", field: "delvAssignGrade", width: 150, cellClass: 'text-center',
// 			cellRenderer: function (params) {
// 				return gagaAgGrid.lookupValue(delvGradeList, params.value);
// 			}
// 		},
// 		{
// 			headerName: "송장출력형태", field: "invoicePrintType", width:150 , cellClass: 'text-center',
// 			valueGetter: function (params) {
// 				return gagaAgGrid.lookupValue(invoicePrintTypeList, params.data.invoicePrintType);
// 			}
// 		},
		{ headerName: "출고처우편번호", field: "delvLocZipcode", width: 150, cellClass: 'text-center', hide: true },
		{ headerName: "출고처기본주소", field: "delvLocBaseAddr", width: 300, hide: true },
		{ headerName: "출고처상세주소", field: "delvLocDtlAddr", width: 300, hide: true },
		{ headerName: "반품처명", field: "rtnLocNm", width: 100, cellClass: 'text-center', hide: true },
		{ headerName: "반품처우편번호", field: "rtnLocZipcode", width: 150, cellClass: 'text-center', hide: true },
		{ headerName: "반품처기본주소", field: "rtnLocBaseAddr", width: 300, hide: true },
		{ headerName: "반품처상세주소", field: "rtnLocDtlAddr", width: 300, hide: true },
		{
			headerName: "사용여부", field: "useYn", width: 100, cellClass: 'text-center',
			valueGetter: function (params) { return params.data.useYn == 'Y' ? 'Yes' : 'No'; }
		}
	];

	let gridOptions = gagaAgGrid.getGridOptions(columnDefs);

	// Cell click
	gridOptions.onCellClicked = function(event) {
		if (event.colDef.field != 'delvLocNm')
			return;
		
		$('#detailForm input[name=mode]').val('U');
		$('#detailForm input[name=delvLocCd]').attr('readonly', true);
		$('#detailForm input[name=delvLocCd]').val(event.data.delvLocCd);
		$('#detailForm input[name=delvLocNm]').val(event.data.delvLocNm);
		$('#detailForm select[name=delvLocClsf]').val(event.data.delvLocClsf);
		$('#detailForm select[name=supplyCompCd]').val(event.data.supplyCompCd);
		$('#detailForm input[name=delvAssignOrd]').val(event.data.delvAssignOrd);
		$('#detailForm select[name=delvAssignGrade]').val(event.data.delvAssignGrade);
		$('#detailForm input[name=stockApplRate]').val(Number(event.data.stockApplRate).addComma());
		$('#detailForm input[name=delvFeeRate]').val(Number(event.data.delvFeeRate).addComma());
// 		$('#detailForm select[name=invoicePrintType]').val(event.data.invoicePrintType);
		$('#detailForm input[name=delvLocZipcode]').val(event.data.delvLocZipcode);
		$('#detailForm input[name=delvLocBaseAddr]').val(event.data.delvLocBaseAddr);
		$('#detailForm input[name=delvLocDtlAddr]').val(event.data.delvLocDtlAddr);
		$('#detailForm input[name=rtnLocZipcode]').val(event.data.rtnLocZipcode);
		$('#detailForm input[name=rtnLocBaseAddr]').val(event.data.rtnLocBaseAddr);
		$('#detailForm input[name=rtnLocDtlAddr]').val(event.data.rtnLocDtlAddr);
		$('#detailForm input[name=rtnLocNm]').val(event.data.rtnLocNm);
		
		if (event.data.useYn == 'Y') {
			$('#detailForm input:radio[name=useYn]').eq(0).attr('checked', true);
		} else {
			$('#detailForm input:radio[name=useYn]').eq(1).attr('checked', true);
		}
	}

	// 업체 조회 팝업에서 호출
	var fnSetSupplyCompInfo = function(result) {
		var arrSupplyComp = [];
		var supplyCompTxt = "";
		var sIndex = 0;
		$('#supplyCompTxt').html('');
		$('#searchForm input[name=supplyCompSearchTxt]').val('');
		
		result.forEach(function(supplyComp) {
			sIndex++; 
			arrSupplyComp.push(supplyComp.supplyCompCd);
		});

		// 조회 값이 하나일 경우 화면에 코드 노출. 그 외는 갯수 처리 
		if (sIndex == 1) {
			$('#searchForm input[name=supplyCompSearchTxt]').val(arrSupplyComp[0]);
		} else {
			supplyCompTxt = sIndex + " 개";
			$('#supplyCompTxt').html(supplyCompTxt);
		}
		
		var jsonData = JSON.stringify(arrSupplyComp);
		$("#searchForm input[name=supplyCompList]").val(jsonData);
	}
	
	// 출고처유형 셀렉트박스 변경 시 이벤트
	$('#detailForm select[name=delvLocClsf]').on('change', function() {
		let val = $(this).val();
		let mode = $('#detailForm input[name=mode]').val();
		
		if (val == 'G024_10') { // 물류창고
			if (mode == 'N') {
				$('#detailForm input[name=delvLocCd]').attr('readonly', true); // 직접입력 불가
				$('#detailForm input[name=delvLocCd]').val('ST0001');
			}
		
			$('#detailForm input[name=stockApplRate]').val(100);
		} else if (val == 'G024_20' || val == 'G024_21') { // 일반매장, 직송매장
			if (mode == 'N') {
				$('#detailForm input[name=delvLocCd]').attr('readonly', false); // 직접입력 가능
				$('#detailForm input[name=delvLocCd]').val('');
				$('#detailForm input[name=delvLocCd]').attr('placeholder', 'ERP매장코드');
			}
		
			$('#detailForm input[name=stockApplRate]').val(100);
		} else { // 입점업체
			if (mode == 'N') {
				$('#detailForm input[name=delvLocCd]').attr('readonly', true); // 직접입력 불가
				$('#detailForm input[name=delvLocCd]').val('');
				$('#detailForm input[name=delvLocCd]').attr('placeholder', '자동생성');
			}
		
			$('#detailForm input[name=stockApplRate]').val('');
		}
	});
	
	// 검색
	$('#btnSearch').on('click', function() {
		gagaAgGrid.fetch($('#searchForm').prop('action'), gridOptions, '#searchForm');
	});

	// 신규버튼
	$('#btnNew').on('click', function() {
		$("#detailForm")[0].reset();
		$('#detailForm input[name=delvLocCd]').attr('readonly', true);
		$('#detailForm input[name=delvLocNm]').focus();
	});
	
	// 저장
	$("#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);
	});
	
	/**
	 * DAUM을 이용한 우편번호 팝업 레이어
	 * @param callbackPosition - 우편번호 정보를 설정할 위치
	 */
	var fnOpenDaumAddr = function(callbackPosition) {
		let daumZip = new daum.Postcode({
			oncomplete: function(data) {
				// 우편번호와 주소 정보를 해당 필드에 넣는다.
				if (callbackPosition == 'delvLoc') {
					$('#detailForm input[name=delvLocZipcode]').val(data.zonecode);
					$('#detailForm input[name=delvLocBaseAddr]').val(cfnGetDaumRoadAddr(data));
					$('#detailForm input[name=delvLocDtlAddr]').focus();
				} else if (callbackPosition == 'rtnLoc') {
					$('#detailForm input[name=rtnLocZipcode]').val(data.zonecode);
					$('#detailForm input[name=rtnLocBaseAddr]').val(cfnGetDaumRoadAddr(data));
					$('#detailForm input[name=rtnLocDtlAddr]').focus();
				}
				
				cfnCloseDaumAddr();
			},
			width: '100%'
		});
		
		cfnOpenDaumAddr(daumZip);
	}
	
	$(document).ready(function() {
		// Create a agGrid
		gagaAgGrid.createGrid('gridList', gridOptions);
	});
/*]]>*/
</script>

</html>
