<!DOCTYPE html>
<html lang="ko"
	xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : SaveInvoiceForm.html
 * @desc    : 송장등록/수정 화면
 *============================================================================
 * ISTYLE24
 * Copyright(C) 2021 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2021.03.06   moon       최초 작성
 *******************************************************************************
 -->


<div class="modalPopup" data-width="900" >
	<div class="panelStyle">
		<div class="panelTitle">
			<h2>배송정보</h2>
			<button type="button" class="close" onclick="uifnPopupClose('popupChangeInvoice')"><i class="fa fa-times"></i></button>
		</div>
		<div class="panelContent" style="overflow-y:auto;">
		<form id="saveInvoiceForm">
			<table class="frmStyle">
				<colgroup>
					<col style="width:160px;" />
					<col />
					<col />
					<col />
					<col />
					<col />
					<col />
				</colgroup>
				<tbody>
				<tr>
					<th>배송업체</th>
					<th:block th:if="${shipCompanyList}">
						<td>
							<select name="shipCompany">
								<option value="">[선택하세요]</option>
								<option th:if="${shipCompanyList}" th:each="oneData, status : ${shipCompanyList}" th:value="${oneData.cd}" th:text="${oneData.cdNm}" th:selected="${oneData.cd == ordDelivery.shipCompCd}"></option>
							</select>
						</td>
						<th>송장번호</th>
						<td><input type="text" name="invoiceNo" th:value="${#strings.defaultString(ordDelivery.invoiceNo,'')}" data-valid-type="numeric" ></td>
					</th:block>
					<td>
						<button type="button" class="btn btn-success" id="saveAccountBtn" onclick="fnSaveAccount();">저장</button>
					</td>
				</tbody>
			</table>
		</form>
		</div>
	</div>
</div>


<script th:inline="javascript">
/*<![CDATA[*/
/* 이벤트 바인딩 관련 */
	var ordDtlNo = [[${ordDelivery.ordDtlNo}]];
	var ordDtlStat = [[${ordDelivery.ordDtlStat}]];
	var orgInvoiceNo  = [[${ordDelivery.invoiceNo}]];
	var orgShipCompCd = [[${ordDelivery.shipCompCd}]];  
	/* 환불계좌번호 저장 */
	var fnSaveAccount = function(){
		var shipCompCd = $("#saveInvoiceForm select[name=shipCompany]").val();
		var invoiceNo = $("#saveInvoiceForm input[name=invoiceNo]").val();
		
		if(orgInvoiceNo == invoiceNo && orgShipCompCd == shipCompCd){
			mcxDialog.alert('변경된 내역이 없습니다.');
			return;
		}

		if(gagajf.isNull(shipCompCd)) {
			mcxDialog.alert('택배사 선택을 해주세요.');
			return;
		}

		if(gagajf.isNull(invoiceNo)) {
			mcxDialog.alert('송장번호를 입력해 주세요.');
			return;
		}

		// CJ택배만 체크  (다른택배 송장체크로직 없음.)
		if(shipCompCd == "D1029"){

			//송장번호 자리수 체크 12자리
			if(invoiceNo.length != 12){
				mcxDialog.alert('['+invoiceNo+']은 잘못된 송장번호 형식입니다1.');
				chk = "N";
				return false;
			}
			// 송장번호 유효성 체크 [ 앞2자리 제외하고 3번째자리부터 9개숫자를 7로나눈 나머지가 12번째 숫자임.]
			var invoNolast = invoiceNo.substring(11,12);
			var chkInvono = invoiceNo.substring(2,11); 
			var invonoMod = chkInvono % 7 ;
	
			if(invoNolast != invonoMod ){
				mcxDialog.alert('['+invoiceNo+']은 잘못된 송장번호 형식입니다2.');
				chk = "N";
				return false;
			}
		}  
 		
		mcxDialog.confirm('송장등록/수정된 내용은 물류시스템에 전송되지 않습니다. <br> 저장 하시겠습니까?', {
			cancelBtnText: "취소",
			sureBtnText: "확인",
			sureBtnClick: function () {
					var param = {};
					param.shipCompCd = shipCompCd;
					param.invoiceNo  = invoiceNo;
					param.ordDtlNo   = ordDtlNo;
					param.orgInvoiceNo = orgInvoiceNo;

				var jsonData = JSON.stringify([param]);
				gagajf.ajaxJsonSubmit('/delivery/orderDetail/save', jsonData, function (result) {
					$('#btnSearch').trigger('click');
					uifnPopupClose('popupChangeInvoice');
				});
			}
		});
	};

	$(document).ready(function() {


	});

/*]]>*/
</script>

</html>
