<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : WithdrawChangePopForm.html
 * @desc    : 회수 사유변경 팝업 화면
 *============================================================================
 * STYLE24
 * Copyright(C) 2021 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2021.05.20   moon       최초 작성
 *******************************************************************************
 -->
<div class="modalPopup" data-width="1400" data-height="520">
	<div class="panelStyle">
		<div class="panelTitle">
			<h2>사유변경</h2>
			<button type="button" class="close" onclick="uifnPopupClose('popupWithdrawChangeForm');"><i class="fa fa-times"></i></button>
		</div>
			
		<div class="panelContent" style="height:90%; overflow-y:auto; padding:0px 20px !important; ">
			<form id="cancelRequestFrm" name="cancelRequestFrm" action="/orderChange/return" method="post" target="hdFrameForOrderCancel">
				<input type="hidden" name="ordNo" 			th:value="${ordNo}"/>
				<input type="hidden" name="ordChgSq" 		th:value="${ordChgSq}"/>
				<input type="hidden" name="chgReason" 		value=""/>
				
				<th:block th:if="${chgGb} == 'G680_30'">
					<h4>반품정보</h4>
				</th:block>
				<th:block th:if="${chgGb} == 'G680_40'">
					<h4>교환정보</h4>
				</th:block>
				<div id="gridOrderWithdrawList" style="width:100%; height: 200px;" class="ag-theme-balham"></div>
				<div style="text-align:right; padding-bottom:5px; padding-top:5px;">
					<button type="button" class="btn btn-success" id="changeSaveBtn">저장</button>
				</div>
				
				<table class="frmStyle">
					<colgroup>
						<col style="width:160px;" />
						<col style="width:200px;" />
						<col style="width:160px;" />
						<col style="width:150px;" />
						<col style="width:160px;" />
						<col style="width:150px;" />
						<col style="width:160px;" />
						<col />
					</colgroup>
					<tbody>
						<tr>
							<!-- 반품회수 -->
							<th:block th:if="${chgGb} == 'G680_30'">
								<th>반품사유 <i class="star"></i></th>
								<td>
									<select name="selectChgReason" onchange="fnChangeChgReason($(this).val());">
										<option value="">[선택하세요]</option>
										<th:block th:if="${chgReasonList}" th:each="oneData, status : ${chgReasonList}">
											<th:block th:if="${oneData.cd} == ${chgReason}">
												<option th:value="|${oneData.cd}|" th:text="|${oneData.cdNm}|" selected></option>
											</th:block>
											<th:block th:unless="${oneData.cd} == ${chgReason}">
												<option th:value="|${oneData.cd}|" th:text="|${oneData.cdNm}|"></option>
											</th:block>
										</th:block>
									</select>
								</td>
							</th:block>
							<!-- //반품회수 -->
							<!-- 교환회수 -->
							<th:block th:if="${chgGb} == 'G680_40'">
								<th>교환사유 <i class="star"></i></th>
								<td>
									<select name="selectChgReason" onchange="fnChangeChgReason($(this).val());">
										<option value="">[선택하세요]</option>
										<th:block th:if="${chgReasonList}" th:each="oneData, status : ${chgReasonList}">
											<th:block th:if="${oneData.cd} == ${chgReason}">
												<option th:value="|${oneData.cd}|" th:text="|${oneData.cdNm}|" selected></option>
											</th:block>
											<th:block th:unless="${oneData.cd} == ${chgReason}">
												<option th:value="|${oneData.cd}|" th:text="|${oneData.cdNm}|"></option>
											</th:block>
										</th:block>
									</select>
								</td>
							</th:block>
							<!-- //교환회수 -->
							<th>회수방법</th>
							<td>
								<th:blcok th:if="${wdGb} == 'W'">회수요청</th:blcok>
								<th:blcok th:if="${wdGb} == 'D'">직접발송</th:blcok>
							</td>
							<th>귀책사유</th>
							<td><span id="imputeReason"></span></td>
							<th>추가비용</th>
							<td><input type="text" name="addPayAmt" data-valid-type="integer"  readonly class="w100 aR"/>원</td>
						</tr>
						<tr>
							<th>요청메모</th>
							<td colspan="7">
								<textarea id="chgMemo" name="chgMemo" style="height:80px;" placeholder="취소사유를 300자내외로 작성해 주세요" th:text="${chgMemo}"></textarea>
							</td>
						</tr>
					</tbody>
				</table>
			</form>
		</div>
	</div>
</div>

<!-- data -->
<script th:inline="javascript">
/*<![CDATA[*/
var orderWithdrawList 	= [[${orderWithdrawList}]];		// 회수대상목록
var chgReason			= [[${chgReason}]];				// 사유코드
var orgChgReason		= [[${chgReason}]];				// ORG 사유코드
var chgGb				= [[${chgGb}]];					// 요청구분
var spanRefundAmt       = [[${spanRefundAmt}]];			// 환불비용
var delvFee				= [[${delvFee}]];				// 배송비
var rtnDelvFee 			= [[${rtnDelvFee}]];			// 반품비
var wdGb 				= [[${wdGb}]];					// 회수구분(W:회수요청, D:직접배송)
var ordChgSq			= [[${ordChgSq}]];				// 주문변경번호

var columnWidthdrawList = [

	{headerName: "변경번호",			field:"ordChgSq",			width:80, cellClass: 'text-center'},
	{headerName: "주문번호",			field:"ordNo",				width:80, cellClass: 'text-center'},
	{headerName: "상세번호",			field:"ordDtlNo",			width:80, cellClass: 'text-center'},
	{headerName: "클레임상태",			field:"chgStatNm", 			width:100, cellClass: 'text-center'},
	{headerName: "요청일시",			field:"regDt", 				width:150, cellClass: 'text-center'},
	{headerName: "요청자명",			field:"chgerNm",			width:100, cellClass: 'text-center'},
	{headerName: "휴대전화",			field:"chgerPhnno",			width:120, cellClass: 'text-center'},
	{headerName: "상품명",			field:"goodsNm",			width:100, cellClass: 'text-center'},
	{headerName: "상품코드",			field:"goodsCd",			width:100, cellClass: 'text-center'},
	{headerName: "옵션",				field:"optCd",				width:100, cellClass: 'text-center'},
	{headerName: "요청수량",			field:"chgQty",				width:100, cellClass: 'text-center'},
	{headerName: "물류메모",			field:"adminMemo",			width:200, cellClass: 'text-center'}

];

var gridOptionsWidthdrawList = gagaAgGrid.getGridOptions(columnWidthdrawList);
</script>

<script th:inline="javascript">

// 사유코드체크
var fnChangeChgReason = function(reasonCd) {

	var customerReasonArr = ['G688_10', 'G689_10'];	
	isCustomer = false;
	
	// 귀책사유체크(취소,반품,교환)
	for (i=0 ; i<customerReasonArr.length ; i++) {
		if (customerReasonArr[i] == reasonCd) {
			isCustomer = true;
		}
	}
	
	// 귀책사유체크(회사,고객)
	if (isCustomer) {
		$("#imputeReason").text("고객");
	} else {
		$("#imputeReason").text("회사");
	}
	
	$('#cancelRequestFrm input[name=chgReason]').val(reasonCd);

	
	var changeYn = "N";
	var addPayCost = 0 ;
	var rsn = $('#cancelRequestFrm input[name=chgReason]').val(); // 사유코드 
	
	if(orgChgReason != rsn){
		if(chgGb == "G680_30"){   // 반품
			if(rsn == "G688_10"){ // 고객귀책 
				if(wdGb == "W" && spanRefundAmt < rtnDelvFee ){  // 회수요청 
					changeYn = "Y";
					addPayCost = rtnDelvFee;
				}
			}
			
		} else { // 교환
			if(rsn == "G689_10"){ // 고객귀책 
				if(wdGb == "W"){  // 회수요청 
					changeYn = "Y";
					addPayCost = rtnDelvFee + delvFee;
				} else {          // 직접배송
					changeYn = "Y";
					addPayCost = rtnDelvFee;
				}
			}
		}
		
		if(changeYn == "Y"){
			$('#cancelRequestFrm input[name=addPayAmt]').val(addPayCost);
		} else {
			$('#cancelRequestFrm input[name=addPayAmt]').val(0);
		}
	} else {
		$('#cancelRequestFrm input[name=addPayAmt]').val(0);
	}

}

$(document).ready(function() {

	// 1. 그리드생성
	gagaAgGrid.createGrid('gridOrderWithdrawList', gridOptionsWidthdrawList);
	gridOptionsWidthdrawList.api.setRowData(orderWithdrawList);

	// 2. TOTAL ROWS 없애기
	gagaAgGrid.hideStatusBar('gridOrderWithdrawList');

	// 사유체크실행
	fnChangeChgReason(chgReason);
	
});


// 저장
$("#changeSaveBtn").on("click", function(){
	
	// 1. 취소사유 체크
	var chgReason = $("#cancelRequestFrm select[name=selectChgReason]").val();
	if (gagajf.isNull(chgReason)) {
		mcxDialog.alert("취소사유를 입력하세요."); 
		return ;
	}
	
	
	var changeYn = "N";
	var addPayCost = 0 ;
	var rsn = $('#cancelRequestFrm input[name=chgReason]').val(); // 사유코드 
	
	if(orgChgReason != rsn){
		if(chgGb == "G680_30"){   // 반품
			if(rsn == "G688_10"){ // 고객귀책 
				if(wdGb == "W" && spanRefundAmt < rtnDelvFee){  // 회수요청 
					changeYn = "Y";
					addPayCost = rtnDelvFee;
				}
			} else if(orgChgReason == "G688_10" &&  rsn != "G688_10"){
				changeYn = "S";
			}
			
		} else { // 교환
			if(rsn == "G689_10"){ // 고객귀책 
				if(wdGb == "W"){  // 회수요청 
					changeYn = "Y";
					addPayCost = rtnDelvFee + delvFee;
				} else {          // 직접배송
					changeYn = "Y";
					addPayCost = rtnDelvFee;
				}
			}else if(orgChgReason == "G689_10" &&  rsn != "G689_10"){
				changeYn = "S";
			}
		}
	}
	
	
	var ordDtlNoList = "";
	for(var i=0; i<orderWithdrawList.length; i++) {
		if(i == 0){
			ordDtlNoList = orderWithdrawList[i].ordDtlNo;
		} else {
			ordDtlNoList = ordDtlNoList +","+ orderWithdrawList[i].ordDtlNo;
		}
		
	}

	mcxDialog.confirm('변경 하시겠습니까?', {
		cancelBtnText: "취소",
		sureBtnText: "확인",
		sureBtnClick: function () {
			var jsonData = {
				"ordChgSq": ordChgSq,
				"changeYn": changeYn,
				"addPayCost": addPayCost,
				"chgGb": chgGb,
				"chgReason" : chgReason,
				"chgMemo": $("#cancelRequestFrm textarea[name=chgMemo]").val(),
				"ordDtlNoList": ordDtlNoList
			};
			var tranData = JSON.stringify(jsonData);
			gagajf.ajaxJsonSubmit('/withdraw/change/save', tranData, function() {
				fnSearchList();
				uifnPopupClose('popupWithdrawChangeForm');});
		}
	});
});
</script>
</html>





