<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorator="mob/common/layout/MypageLayoutMob">
<!--
 *******************************************************************************
 * @source  : MypageExchangeFormMob.html
 * @desc    : 마이페이지 > 교환신청 Page
 *============================================================================
 * STYLE24
 * Copyright(C) 2021 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2021.05.04   jsh77b     최초 작성
 *******************************************************************************
 -->
<body>

<th:block layout:fragment="content">

<main class="container my">
	<section class="content my_exchange my_return">
		<form id="exchangeForm" class="form_wrap">
			<input type="hidden" name="chgReason"/>
			<input type="hidden" name="shipCompCd"/>
			
			<div class="inner wide bg_beige">
				<div class="order_number">
					<dl class="clear">
						<dt>주문번호</dt>
						<dd th:text="${oneData.ordNo}"></dd>
					</dl>
				</div>
			</div>
			<div class="inner">
				<div class="part_goods">
					<div class="goods_top">
						<div th:unless="${oneData.giftPackYn == 'Y'}" class="goods_date">주문일 <span class="date" th:text="${oneData.ordDt}"></span></div>
						<div th:if="${oneData.giftPackYn == 'Y'}" class="goods_date">선물일 <span class="date" th:text="${oneData.ordDt}"></span></div>
					</div>
					
					<th:block th:if="${exchangeList.returnList}" th:each="exchange, status : ${exchangeList.returnList}" th:with="imageUrl=${@environment.getProperty('upload.goods.view')}">
						<div class="goods_section">
							<div class="goods_detail">
								<a href="javascript:void(0)" th:attr="goodsCd=${exchange.goodsCd}, optCd1=${exchange.goodsType == 'G056_N' ? exchange.optCd1 : ''}, ithrCd='IN21_03'" onclick="fnGoToGoodsDetail(this)">
									<div class="thumb_box">
										<img th:src="${imageUrl + '/' + exchange.sysImgNm + '?RS=90'}" width="100%" alt="">
									</div>
									<div class="info_box">
										<div class="od_name">
											<div class="brand">
												<span th:text="${exchange.brandNm}"></span>
												<div class="badge_wrap">
													<em class="order_badge order_bullet_badge"	th:if="${exchange.shotDelvYn == 'Y'}">총알배송</em>
													<em class="order_badge"						th:if="${exchange.shotDelvYn == 'N' and exchange.selfGoodsYn == 'Y'}">STYLE24 일반배송</em>
													<em class="order_badge"						th:if="${exchange.selfGoodsYn == 'N'}">업체직배송</em>
												</div>
											</div>
											<div class="name" th:text="${exchange.goodsNm}"></div>
										</div>
										<div class="od_opt">
											<!-- 세트상풍일때 -->
											<th:block th:if="${exchange.goodsType == 'G056_S'}" th:each="option, status : ${exchange.colorNmArr}">
												<div class="option">
													<em th:text="${exchange.itemNmArr[status.index]}"></em>
													<em th:text="${option}"></em>
													<em th:text="${exchange.optCd2Arr[status.index]}"></em>
												</div>
											</th:block>
											<!-- //세트상풍일때 -->
											<!-- 일반상풍일때 -->
											<th:block th:unless="${exchange.goodsType == 'G056_S'}">
												<div class="option">
													<em th:text="${exchange.colorNm}"></em>
													<em th:text="${exchange.optCd2}"></em>
												</div>
											</th:block>
											<!-- //일반상풍일때 -->
										</div>
										<div class="od_exchange_opt exchangeOption" style="display:none" th:attr="ordDtlNo=${exchange.ordDtlNo}">
											<th:block th:if="${exchange.goodsType == 'G056_S'}" th:each="option, status : ${exchange.colorNmArr}">
												<span>교환옵션</span>
												<div class="option">
													<em th:text="${exchange.itemNmArr[status.index]}"></em>
													<em th:text="${option}"></em>
													<em th:text="${exchange.optCd2Arr[status.index]}"></em>
												</div>
											</th:block>
											<th:block th:unless="${exchange.goodsType == 'G056_S'}">
												<span>교환옵션</span>
												<div class="option">
													<em th:text="${exchange.colorNm}"></em>
													<em th:text="${exchange.optCd2}"></em>
												</div>
											</th:block>
										</div>
										<div class="od_calc">
											<th:block th:if="${(exchange.ordAmt - exchange.cnclRtnAmt) > (exchange.realOrdAmt + exchange.pntDcAmt + exchange.gfcdUseAmt)}">
												<p class="sale_price">
													<del><em th:text="${#numbers.formatInteger(exchange.ordAmt - exchange.cnclRtnAmt, 1, 'COMMA')}"></em>원</del>
												</p>
											</th:block>
											<p class="price">
												<span class="selling_price">
													<em th:text="${#numbers.formatInteger(exchange.realOrdAmt + exchange.pntDcAmt + exchange.gfcdUseAmt, 1, 'COMMA')}"></em>원
												</span>
											</p>
										</div>
									</div>
								</a>
							</div>
							<div class="goods_select">
								<div class="select_custom select_count">
									<div class="combo">
										<input type="hidden" name="chgQty" value="0" th:attr="ordDtlNo=${exchange.ordDtlNo}, ordCanChgQty=${exchange.ordCanChgQty}"/>
										<input type="hidden" name="chgOptionList" th:attr="ordDtlNo=${exchange.ordDtlNo}"/>
										<div class="select">수량 0개</div>
									</div>
								</div>
							</div>
							<div class="goods_btn_wrap btn_group_flex">
								<div>
									<button type="button" class="btn btn_dark btn_opion_change" th:attr="ordNo=${exchange.ordNo}, ordDtlNo=${exchange.ordDtlNo}" onclick="fnChangeOption(this)">
										<span>교환 옵션 변경</span>
									</button>
								</div>
							</div>
							<th:block th:if="${#lists.size(exchangeList.returnList) == status.count}">
								<div class="goods_txt">
									<p class="cf_txt cf_desc">교환하실 상품의 수량 및 옵션을 선택하신 후<br>교환 신청을 하실 수 있습니다.</p>
								</div>
							</th:block>
						</div>
					</th:block>
				</div>
			</div>
			
			<!-- 교환사유 -->
			<div class="inner">
				<div class="tbl_wrap">
					<div class="tbl_tit"> 
						<h3>교환 사유</h3>
					</div>
					<div class="tbl tbl_tarea">
						<table>
							<colgroup>
								<col width="*">
							</colgroup>   
							<tr>
								<th class="sr-only">교환 접수 일자</th>
								<td>
									<div class="select_custom select_exchange">
										<div class="combo">
											<div class="select">교환 사유를 선택하세요.</div>
										</div>
									</div>
								</td>
							</tr>
							<tr>
								<th class="sr-only">교환 완료 일자</th>
								<td>
									<div class="textarea_wrap">
										<textarea class="doc_return" name="chgMemo" id="chgMemo" maxlength="200" placeholder="자세한 사유를 입력해주세요."></textarea>
										<p class="txt_cnt"><span id="return_cnt" class="c_primary">0</span>/200</p>
									</div>
								</td>
							</tr>
						</table>
					</div>
				</div>
			</div>
			<!-- //교환사유 -->
			
			<!-- 반품방식 -->
			<div class="inner wdGb">
				<div class="tbl_wrap">
					<div class="tbl_tit"> 
						<h3>반품방식 선택</h3>
					</div>
					<div class="retrieve_method">
						<div class="tbl tbl_radio">
							<table>
								<colgroup>
									<col width="*">
								</colgroup>
								<tbody>
									<tr>
										<th class="sr-only">방문 회수</th>
										<td>
											<div class="form_field">
												<input type="radio" name="wdGb" id="withdraw"  checked="checked" value="W">
												<label for="withdraw"><span>방문 회수 <b class="c_primary">택배사에서 고객에게 방문하여 회수</b></span></label>
											</div>
										</td>
									</tr>
									<tr>
										<th class="sr-only">직접 배송</th>
										<td>
											<div class="form_field">
												<input type="radio" name="wdGb" id="direct" value="D">
												<label for="direct"><span>직접 배송 <b class="c_primary">고객이 반품지로 직접 반송</b></span></label>
											</div>
										</td>
									</tr>
								</tbody>
								
							</table>
						</div>
					</div>
				</div>
			</div>
			<!-- //반품방식 -->
			
			<div class="inner">
				<div class="retrieve_box visit" style="display:block;" id="chgerLocation">
					<div class="tbl_wrap">
						<div class="tbl_tit"> 
							<h3>반품 회수지 주소</h3>
							<button type="button" id="btn_adrsChange_pop" class="btn_popup" onclick="fnChangeAddr('chger');"><span>회수지 변경</span></button>
						</div>
						<div class="tbl">
							<input type="hidden" name="chgerNm" 		th:value="${deliveryAddrInfo.recipNm}">
							<input type="hidden" name="chgerPhnno" 		th:value="${deliveryAddrInfo.recipPhnno}">
							<input type="hidden" name="chgerZipcode" 	th:value="${deliveryAddrInfo.recipZipcode}">
							<input type="hidden" name="chgerBaseAddr" 	th:value="${deliveryAddrInfo.recipBaseAddr}">
							<input type="hidden" name="chgerDtlAddr" 	th:value="${deliveryAddrInfo.recipDtlAddr}">
							<input type="hidden" name="chgerRtnMemo" 	th:value="${deliveryAddrInfo.delvMemo}">
							<div class="ship_info">
								<dl>
									<div class="name">
										<dt><span class="sr-only">배송지명</span></dt>
										<dd id="chgerNm" th:text="${deliveryAddrInfo.recipNm}"></dd>
									</div>
									<div class="addr">
										<dt><span class="sr-only">배송주소</span></dt>
										<dd id="chgerAddr" th:text="|${deliveryAddrInfo.recipBaseAddr}    ${deliveryAddrInfo.recipDtlAddr}|"></dd>
									</div>
									<div class="phone">
										<dt><span class="sr-only">휴대폰 번호</span></dt>
										<dd id="chgerPhnno" th:text="${deliveryAddrInfo.recipPhnno}"></dd>
									</div>
<!--									<div class="desc_txt">택배사에서 반품 상품을 직접 회수할 장소 선택</div>-->
								</dl>
							</div>
						</div>
					</div>
				</div>
				<div class="retrieve_box direct" style="display:none;" id="returnLocation">
					<div class="tbl_wrap">
						<div class="tbl_tit"> 
							<h3>반송 정보</h3>
						</div>
						<div class="tbl">
							<div class="ship_info">
								<dl>
									<div class="addr">
										<dt><span>반송 주소</span></dt>
										<dd th:text="|${deliveryAddrInfo.rtnLocBaseAddr} ${deliveryAddrInfo.rtnLocDtlAddr}|"></dd>
									</div>
									<div class="">
										<dt><span>택배사 선택</span></dt> <!-- 210409_수정 : class sr-only 삭제 -->
										<dd>
											<div class="select_custom select_courier">
												<div class="combo">
													<div class="select">택배사를 선택하세요.</div>
												</div>
											</div>
										</dd>
									</div>
									<div class="phone">
										<dt><span>반송 송장번호</span></dt>
										<dd><input type="tel" class="form_control" name="wdInvoiceNo" data-valid-type="numeric" value="" oninput='handleOnInput(this, 20)' placeholder="송장번호를 입력해주세요."></dd>
<!--										<dd><input type="number" pattern="\d*" class="form_control" name="wdInvoiceNo" data-valid-type="numeric" value="" oninput='handleOnInput(this, 20)' placeholder="송장번호를 입력해주세요."></dd>-->
									</div>
								</dl>
							</div>
						</div>
					</div>
				</div>
			</div>
			<div class="inner" id="recipLocation">
				<div class="tbl_wrap">
					<div class="tbl_tit"> 
						<h3>교환 상품 배송지</h3>
						<button type="button" class="btn_popup" onclick="fnChangeAddr('recip')"><span>배송지 변경</span></button>
					</div>
					<div class="tbl">
						<input type="hidden" name="recipNm" 		th:value="${deliveryAddrInfo.recipNm}">
						<input type="hidden" name="recipPhnno" 		th:value="${deliveryAddrInfo.recipPhnno}">
						<input type="hidden" name="recipZipcode" 	th:value="${deliveryAddrInfo.recipZipcode}">
						<input type="hidden" name="recipBaseAddr" 	th:value="${deliveryAddrInfo.recipBaseAddr}">
						<input type="hidden" name="recipDtlAddr"	th:value="${deliveryAddrInfo.recipDtlAddr}">
						<input type="hidden" name="delvMemo" 		th:value="${deliveryAddrInfo.delvMemo}">
						<div class="ship_info">
							<dl>
								<div class="name">
									<dt><span class="sr-only">배송지명</span></dt>
									<dd id="recipNm" th:text="${deliveryAddrInfo.recipNm}"></dd>
								</div>
								<div class="addr">
									<dt><span class="sr-only">배송주소</span></dt>
									<dd id="recipAddr" th:text="|${deliveryAddrInfo.recipBaseAddr}    ${deliveryAddrInfo.recipDtlAddr}|"></dd>
								</div>
								<div class="phone">
									<dt><span class="sr-only">휴대폰 번호</span></dt>
									<dd id="recipPhnno" th:text="${deliveryAddrInfo.recipPhnno}"></dd>
								</div>
							</dl>
						</div>
					</div>
				</div>
				<!-- 교환 배송비결제 필요할 경우 -->
				<div class="required_group" id="addDeliveryFee" style="display:none">
					<p>배송비 <span class="c_primary" id="addPayCost">0원</span> 추가 결제가 필요합니다.</p>
					<input type="hidden" name="addPayCost"/>
				</div>
				<!-- //교환 배송비결제 필요할 경우 -->
				<div class="btn_group btn_group_flex">
					<div><button type="button" class="btn btn_default" onclick="cfnGoToPage(_PAGE_MYPAGE_ORDER_LIST)"><span>취소</span></button></div>
					<div><button type="button" id="btn_exchange_confirm" class="btn btn_dark" onclick="fnExchange()"><span>교환 신청</span></button></div>
				</div>
			</div>
		</form>
	</section>
</main>

<!-- 210408_ 추가 : 교환 수량 선택 팝업 -->
<th:block th:if="${exchangeList.returnList}" th:each="exchange, status : ${exchangeList.returnList}">
	<div class="popup_box exchangeCountPop">
		<div class="lap">
			<div class="popup_close">카테고리닫기</div>
			<div class="popup_head sr-only">
				<h2 class="">주문상태 선택 팝업</h2>
			</div>
			<div class="popup_con">
				<div class="button_list clear">
					<button type="button" class="on" qty=0 onclick="fnChangeExchangeQty(this);">
						<span>수량 0개</span>
					</button>
					<th:block th:each="num : ${#numbers.sequence(1, exchange.ordCanChgQty)}">
						<button type="button" th:qty="${num}" onclick="fnChangeExchangeQty(this);">
							<span th:text="|수량 ${num}개|"></span>
						</button>
					</th:block>
					<input type="hidden" name="ordDtlNo" th:value="${exchange.ordDtlNo}">
				</div> 
			</div>
		</div>
	</div>
</th:block>
<!-- //210408_ 추가 : 수량 선택 팝업 -->

<!-- 210408_ 추가 : 사유 선택 팝업 -->
<div id="exchangeReasonPop" class="popup_box exchangeReasonPop">
	<div class="lap">
		<div class="popup_close">카테고리닫기</div>
		<div class="popup_head sr-only">
			<h2 class="">주문상태 선택 팝업</h2>
		</div>
		<div class="popup_con">
			<div class="button_list">
				<th:block th:if="${exchangeReason}" th:each="oneData, status : ${exchangeReason}">
					<button type="button" th:attr="chgReason=${oneData.cd}" onclick="fnChangeExchangeReason(this);">
						<span th:text="${oneData.cdNm}"></span>
					</button>
				</th:block>
			</div> 
		</div>
	</div>
</div>
<!-- //210408_ 추가 : 사유 선택 팝업 -->

<!-- 210819_ 추가 : 택배사 선택 팝업 -->
<div id="exchangeCourierPop" class="popup_box exchangeCourierPop" style="display: none;">
	<div class="lap">
		<div class="popup_close">카테고리닫기</div>
		<div class="popup_head sr-only">
			<h2 class="">택배사 선택 팝업</h2>
		</div>
		<div class="popup_con">
			<div class="button_list">
				<th:block th:if="${shipCompanyList}" th:each="oneData, status : ${shipCompanyList}">
					<button type="button" th:attr="shipCompCd=${oneData.cd}" onclick="fnSelectShipComp(this);"><span th:text="${oneData.cdNm}"></span></button>
				</th:block>
			</div>
		</div>
	</div>
</div>
<!-- //210819_ 추가 : 택배사 선택 팝업 -->

<script src="/ux/plugins/gaga/gaga.paging.js"></script>
<script th:inline="javascript">
var isLogin 		= [[${isLogin}]];
let oneData 		= [[${oneData}]];
let isCustomer 		= false;
let addrGb 			= '';
let jsonObj 		= {};
let ordNo 			= [[${oneData.ordNo}]];

$(document).ready(function() {
	// 타이틀명
	$('#htopTitle').text('교환 신청');

	// 입점업체의 경우 회수방식 직접배송 처리
	if (oneData.selfGoodsYn != 'Y') {
		$('#withdraw').click();
		$('.wdGb').css('display','none');
	}
});

// 교환수량버튼기능
$(".select_custom.select_count").each(function(idx){
	$(this).on("click", function(){
		// 전체비활성화
		$(".exchangeCountPop").hide();
		
		// 수량팝업활성화
		$(".exchangeCountPop").eq(idx).show().addClass("active");
		
		$("body").css({"overflow":"hidden"});
	});
});

// 반품 방식 변경 이벤트
$('#exchangeForm input[name=wdGb]').on('change', function() {
	let wdGb = $('#exchangeForm input[name=wdGb]:radio:checked').val();

	if (wdGb == 'W') {
		$('#chgerLocation').css('display', '');
		$('#returnLocation').css('display', 'none');
	} else {
		$('#chgerLocation').css('display', 'none');
		$('#returnLocation').css('display', '');
	}

	// 추가 배송비 계산
	fnAddCost();
});

// 교환 처리
var fnExchange = function() {
	let url 			= '/mypage/exchange';
	let delvFeeCd 		= oneData.delvFeeCd;
	let ordDtlNo 		= oneData.ordDtlNo;
	let supplyCompCd 	= oneData.supplyCompCd;
	let excDelvFee 		= oneData.excDelvFee;
	let rtnDelvFee 		= oneData.rtnDelvFee;
	let chgReason 		= $('#exchangeForm input[name=chgReason]').val();
	let addPayCost 		= $('#exchangeForm input[name=addPayCost]').val();
	
	// 교환 사유 체크
	if (gagajf.isNull(chgReason)) {
		mcxDialog.alert('교환 사유를 선택해주세요.');
		return false;
	}

	// 교환 상품 설정
	let chgOrdDtlNoArr = [];
	$.each($('#exchangeForm input[name=chgQty]'), function(idx, item) {
		if (Number($(item).val()) > 0) {
			chgOrdDtlNoArr.push($(item).attr('ordDtlNo'));
		}
	});

	// 교환옵션 설정
	let cancelReqList = [];
	let optionCnt = 0;
	$.each($('#exchangeForm input[name=chgOptionList]'), function(idx, item) {
		if (chgOrdDtlNoArr.includes($(item).attr('ordDtlNo')) && !gagajf.isNull($(item).val())) {
			let json 		= $(item).val();
			let jsonData 	= JSON.parse(json);
			$.each(jsonData, function (index, chgOpt) {
				let cancelReq 	= {};
				cancelReq.ordDtlNo 			= chgOpt.ordDtlNo;
				cancelReq.ordDtlItemSq 		= chgOpt.ordDtlItemSq;
				cancelReq.delvFeeCd 		= delvFeeCd;
				cancelReq.supplyCompCd 		= supplyCompCd;
				cancelReq.excDelvFee 		= excDelvFee;
				cancelReq.rtnDelvFee 		= rtnDelvFee;
				// 2021.07.26 교환옵션 optCd2 -> optCd로 변경 처리
				//cancelReq.ordChgOpt 		= chgOpt.chgOptCd2;
				cancelReq.ordChgOpt 		= chgOpt.chgOptCd;
				cancelReq.chgQty 			= chgOpt.chgQty;
				cancelReqList.push(cancelReq);
			});
			
			optionCnt++;
		}
	});

	// 교환 수량 및 변경 옵션 체크
	if (chgOrdDtlNoArr.length > optionCnt) {
		mcxDialog.alert('교환하실 상품의 변경 옵션을 선택해주세요.');
		return false;
	} else if (chgOrdDtlNoArr.length < optionCnt || chgOrdDtlNoArr.length == 0) {
		mcxDialog.alert('교환하실 상품의 수량을 선택해주세요.');
		return false;
	}

	// 교환 필요 데이터 설정
	let wdGb			= $('#exchangeForm input[name=wdGb]:radio:checked').val();
	let data 			= {};
	data.ordNo 			= oneData.ordNo;
	data.isCustomer 	= isCustomer;
	data.wdGb 			= wdGb;
	data.wdInvoiceNo 	= $('#exchangeForm input[name=wdInvoiceNo]').val();
	data.recipNm 		= $('#exchangeForm input[name=recipNm]').val();
	data.recipPhnno 	= $('#exchangeForm input[name=recipPhnno]').val();
	data.recipTelno 	= $('#exchangeForm input[name=recipTelno]').val();
	data.recipZipcode 	= $('#exchangeForm input[name=recipZipcode]').val();
	data.recipBaseAddr 	= $('#exchangeForm input[name=recipBaseAddr]').val();
	data.recipDtlAddr 	= $('#exchangeForm input[name=recipDtlAddr]').val();
	data.delvMemo 		= $('#exchangeForm input[name=delvMemo]').val();
	data.chgReason 		= $('#exchangeForm input[name=chgReason]').val();
	data.chgMemo 		= $('#exchangeForm textarea[name=chgMemo]').val();
	data.chgerNm 		= wdGb == 'W' ? $('#exchangeForm input[name=chgerNm]').val() : '';
	data.chgerPhnno 	= wdGb == 'W' ? $('#exchangeForm input[name=chgerPhnno]').val() : '';
	data.chgerTelno 	= wdGb == 'W' ? $('#exchangeForm input[name=chgerTelno]').val() : '';
	data.chgerEmail 	= wdGb == 'W' ? $('#exchangeForm input[name=chgerEmail]').val() : '';
	data.chgerZipcode 	= wdGb == 'W' ? $('#exchangeForm input[name=chgerZipcode]').val() : '';
	data.chgerBaseAddr 	= wdGb == 'W' ? $('#exchangeForm input[name=chgerBaseAddr]').val() : '';
	data.chgerDtlAddr 	= wdGb == 'W' ? $('#exchangeForm input[name=chgerDtlAddr]').val() : '';
	data.chgerRtnMemo 	= wdGb == 'W' ? $('#exchangeForm input[name=chgerRtnMemo]').val() : '';
	data.addPayCost 	= addPayCost;
	data.addPayAmt 		= 0;
	data.cancelReqList 	= cancelReqList;
	data.shipCompCd		= $('#exchangeForm input[name=shipCompCd]').val();

	// 2021.11.09 card007 휴대폰번호 유효성검증 추가
	if (wdGb == 'W' && !gagajf.checkPhnno(data.chgerPhnno)) {
		mcxDialog.alert("회수지 휴대폰 번호가 유효하지 않습니다.");

		let offset = $('#chgerLocation').offset();
		$('html, body').animate({scrollTop : offset.top - 50}, 0);

		return false;
	}
	
	if (!gagajf.checkPhnno(data.recipPhnno)) {
		mcxDialog.alert("교환배송지 휴대폰 번호가 유효하지 않습니다.");

		let offset = $('#recipLocation').offset();
		$('html, body').animate({scrollTop : offset.top - 50}, 0);

		return false;
	}

	let jsonData = JSON.stringify(data);

	// 교환 처리
	gagajf.ajaxJsonSubmit(url, jsonData, function(result) {
		if (addPayCost > 0) {
			let ordChgSq = result.ordChgSq;
			let ordNo = result.ordNo;

			$('#addPaymentForm input[name=ordChgSq]').val(ordChgSq);
			$('#addPaymentForm input[name=ordNo]').val(ordNo);

			document.addPaymentForm.submit();
		} else {
			cfnGoToPage(_PAGE_MYPAGE_CRE_LIST);
		}
	}, true);
}

// 교환 옵션 변경 팝업
var fnChangeOption = function(param) {
	let ordNo 		= $(param).attr('ordNo');
	let ordDtlNo 	= $(param).attr('ordDtlNo');

	// 옵션변경 수량 체크
	let chgQtyArr 		= [];
	let chgOrdDtlNoArr 	= [];

	$.each($('#exchangeForm input[name=chgQty]'), function(idx, item) {
		chgQtyArr.push($(item).val());
		chgOrdDtlNoArr.push($(item).attr('ordDtlNo'));
	});

	let index 	= chgOrdDtlNoArr.indexOf(ordDtlNo);
	let chgQty 	= chgQtyArr[index];
	
	if (chgQty == null || chgQty <= 0) {
		mcxDialog.alert('교환 옵션 변경 수량을 선택해주세요.');
		return false;
	}

	var jsonObj 		= {};
	jsonObj.ordNo 		= ordNo;
	jsonObj.ordDtlNo 	= ordDtlNo;
	jsonObj.chgQty 		= chgQty;

	$.ajax({
		type		: "POST",
		url 		: "/mypage/change/option/popup/form",
		data		: jsonObj,
		dataType 	: "html",
		success 	: function(result) {
			if (result != null) {
				$("#exchangePop .modal-content").html(result);
				$("#exchangePop").modal("show");
			}
		}
	});
}

// 교환 옵션 변경 팝업 콜백 처리
var fnChangeOptionCallback = function(param) {
	let ordDtlNo;
	let html = '';
	
	html += '<span>교환옵션</span>';

	// 교환옵션 HTML 처리
	$.each(param, function(idx, item) {
		ordDtlNo 		= item.ordDtlNo;
		let colorNm 	= item.colorNm;
		let itemNm 		= item.itemNm;
		let goodsType 	= item.goodsType;
		let optCd2 		= item.chgOptCd2;
		let qty 		= item.chgQty;

		if (goodsType == 'G056_S') {
			html += '<div class="option">';
			html += '	<em>'+itemNm+'</em>';
			html += '	<em>'+colorNm+'</em>';
			html += '	<em>'+optCd2+'</em>';
			html += '</div>';
		} else {
			html += '<div class="option">';
			html += '	<em>'+colorNm+'</em>';
			html += '	<em>'+optCd2+'</em>';
			html += '</div>';
		}
	});

	$.each($('#exchangeForm .exchangeOption'), function(idx, item) {
		if (ordDtlNo == $(item).attr('ordDtlNo')) {
			$(item).css('display', '');
			$(item).html(html);
		}
	});

	// 교환옵션 값 JSON 형태로 설정
	$.each($('#exchangeForm input[name=chgOptionList]'), function(idx, item) {
		if (ordDtlNo == $(item).attr('ordDtlNo')) {
			$(item).val(JSON.stringify(param));
		}
	});
}

// 교환 수량 변경 이벤트
var fnChangeExchangeQty = function(param) {
	var ordDtlNo = "";
	
	// 교환수량설정
	if (param != null) {
		$("#exchangeForm input[name=chgQty]").each(function(idx){
			ordDtlNo = $(this).attr("ordDtlNo");
			if (ordDtlNo == $(param).parent().find("input[name=ordDtlNo]").val()) {
				$(this).val($(param).attr("qty"));
				$(this).parent().find(".select").text($(param).find("span").text());
			}
		});
	}
	
	// 교환옵션 표기 초기화
	$.each($('#exchangeForm .exchangeOption'), function(idx, item) {
		if (ordDtlNo == $(item).attr('ordDtlNo')) {
			$(item).css('display', 'none');
			$(item).html('');
		}
	});

	// 교환옵션 값 초기화
	$.each($('#exchangeForm input[name=chgOptionList]'), function(idx, item) {
		if (ordDtlNo == $(item).attr('ordDtlNo')) {
			$(item).val('');
		}
	});
	
	// 추가 배송비 계산
	fnAddCost();
}

// 교환 사유 변경 이벤트
var fnChangeExchangeReason = function(param) {
	var customerReasonArr 	= ['G689_10', 'G689_30'];
	let chgReason 			= $(param).attr('chgReason');

	// 귀책사유체크(취소,반품,교환)
	isCustomer = customerReasonArr.includes(chgReason);
	
	// 반품 사유 설정
	$('#exchangeForm input[name=chgReason]').val(chgReason);
	$("#exchangeForm .select_custom.select_exchange .select").text($(param).find("span").text());

	// 추가 배송비 계산
	fnAddCost();
}

// 추가 배송비 계산
var fnAddCost = function() {
	let addPayCost 		= 0;
	let delvFee 		= oneData.rtnDelvFee;
	let rtnDelvFee 		= oneData.rtnDelvFee;

	// 교환 수량 체크
	let qty = 0;
	$.each($('#exchangeForm input[name=chgQty]'), function (idx, item) {
		qty += Number($(item).val());
	});

	// 교환 수량이 있고 고객 귀책 사유 시 추가 배송비 계산
	if (isCustomer && qty > 0) {
		addPayCost = delvFee;

		// 회수 요청 시 반품배송비 추가
		if ($('#exchangeForm input[name=wdGb]:radio:checked').val() == 'W') {
			addPayCost += rtnDelvFee;
		}

		// 추가배송비 영역 설정
		$('#addDeliveryFee').css('display', '');
		$('#addPayCost').text(addPayCost.addComma() + '원');
		$('#exchangeForm input[name=addPayCost]').val(addPayCost);
	} else {
		$('#addDeliveryFee').css('display', 'none');
		$('#exchangeForm input[name=addPayCost]').val(0);
	}

	// 팝업 닫기
	$(".popup_close").trigger("click");
}

// 배송지 정보 설정
var delvAddrSet = function(delvObj) {
	let recipNm 		= delvObj.recipNm;
	let recipPhnno 		= delvObj.recipPhnno;
	let recipZipcode 	= delvObj.recipZipcode;
	let recipBaseAddr 	= delvObj.recipBaseAddr;
	let recipDtlAddr 	= delvObj.recipDtlAddr;
	let delvMemo 		= delvObj.delvMemo;

	if (addrGb == 'recip') {
		$('#exchangeForm input[name=recipNm]').val(recipNm);
		$('#exchangeForm input[name=recipPhnno]').val(recipPhnno);
		$('#exchangeForm input[name=recipZipcode]').val(recipZipcode);
		$('#exchangeForm input[name=recipBaseAddr]').val(recipBaseAddr);
		$('#exchangeForm input[name=recipDtlAddr]').val(recipDtlAddr);
		$('#exchangeForm input[name=delvMemo]').val(delvMemo);
		
		$('#recipNm').text(recipNm);
		$('#recipPhnno').text(recipPhnno);
		$('#recipAddr').text(recipBaseAddr + ' ' + recipDtlAddr);
		$('#delvMemo').text(delvMemo);
	} else if (addrGb == 'chger') {
		let chgerAddr = recipBaseAddr + ' ' + recipDtlAddr;
		$('#exchangeForm input[name=chgerNm]').val(recipNm);
		$('#exchangeForm input[name=chgerPhnno]').val(recipPhnno);
		$('#exchangeForm input[name=chgerZipcode]').val(recipZipcode);
		$('#exchangeForm input[name=chgerBaseAddr]').val(recipBaseAddr);
		$('#exchangeForm input[name=chgerDtlAddr]').val(recipDtlAddr);
		$('#exchangeForm input[name=chgerRtnMemo]').val(delvMemo);

		$('#chgerNm').text(recipNm);
		$('#chgerPhnno').text(recipPhnno);
		$('#chgerAddr').text(chgerAddr);
	}
}

// 배송메모 설정
var delvMemoSet = function(delvMemoObj) {
	let delvMemo = delvMemoObj.delvMemo;
	$('input[name=delvMemo]').val(delvMemo);
	$("#delvMemo").text(delvMemo);
}

// 배송지 및 교환지 변경 처리
var fnChangeAddr = function(param) {
	addrGb = param;
	fnChangeDeliveryAddr();
}

// 택배사 버튼 클릭 이벤트
var fnSelectShipComp = function(param) {
	let shipCompCd = $(param).attr('shipCompCd');
	$('#exchangeForm input[name=shipCompCd]').val(shipCompCd);
	$("#exchangeForm .select_custom.select_courier .select").text($(param).find("span").text());
	
	// 팝업 닫기
	$("#exchangeCourierPop .popup_close").trigger("click");
}
</script>

<script>
// 세부 사유 글자 수 제한
$('.doc_return').keyup(function(e){
	var content = $(this).val();
	$('#return_cnt').html(content.length);
	//if (content.length > 200){
	//	mcxDialog.alert("최대 200자까지 입력 가능합니다."); //210420_수정 : 시스템 alert -> dialog 변경.
	//	$(this).val(content.substring(0, 200));
	//	$('#return_cnt').html("200");
	//}
});

// 반품 사유 선택 팝업
$(document).on("click", ".select_custom.select_exchange",function(e){
	$('#exchangeReasonPop').show().addClass("active");
	$("body").css({"overflow":"hidden"});
});

// 팝업_닫기
$('.popup_close').on("click",function(){
	$('.popup_box').hide().removeClass('active');
	$("body").css({"overflow":"visible"});
});

// 버튼 색
$(document).on('click', '.popup_box .button_list button',function(){
	//$('.popup_box .button_list button').removeClass('on');
	$.each($(this).parent().find('button'), function(idx, item) {
		$(item).removeClass('on');
	});
	$(this).addClass('on');
});

function handleOnInput(el, maxlength) {
	if(el.value.length > maxlength)  {
		el.value = el.value.substr(0, maxlength);
	}
}

// 210819_ 추가 : 택배사 선택 팝업
$(document).on("click",".select_custom.select_courier",function(e){
	$('#exchangeCourierPop').show().addClass("active");
	$("body").css({"overflow":"hidden"});
});
</script>


</th:block>

</body>
</html>
