<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org"
	xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
	layout:decorator="web/common/layout/MypageLayoutWeb">
<!--
 *******************************************************************************
 * @source  : MypageCustDeliveryAddrFormWeb.html
 * @desc    : 마이페이지 > 배송지 관리 Page
 *============================================================================
 * STYLE24
 * Copyright(C) 2021 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2021.04.02   jsshin     최초 작성
 *******************************************************************************
 -->

<body>
<th:block layout:fragment="content">
<div class="content myAddressList"> <!-- 페이지특정 클래스 = myAddressList -->
	<div class="cont_body">
		<!-- CONT-BODY -->
		<div class="lnb">
			<div class="lnb_tit">
				<h2>마이페이지</h2>
			</div>
			<div class="lnb_list">
				<ul id="mypageLnbList"></ul>
			</div>
		</div>
		<div class="cont">
			<div class="sec_head">
				<h3 class="subH1">배송지 관리</h3>
			</div>
			<div class="sec_body">
				<h4 class="subH3">배송지 목록</h4>
				<!-- 데이터 있을 시 -->
				<div id="data" class="tbl type1">
					<table>
						<colgroup>
							<col width="*">
							<col width="170">
						</colgroup>
						<tbody id="custDeliveryList">

						</tbody>
					</table>
				</div>
				<!-- //데이터 있을 시 -->
				<!-- 데이터 없을 시 -->
				<div id="nodata" class="nodata">
					<div class="txt_box">
						<p>
							등록된 배송지가 없습니다.<br/>
						</p>
					</div>
				</div>
				<!-- //데이터 없을 시 -->
			</div>
			<div class="btn_footer_area">
				<button type="button" id="btnAdrsaddPop" class="btn btn_dark btn_md"><span>배송지 추가</span></button>
			</div>
		</div>
		<!-- // CONT-BODY -->
	</div>
</div>
<script th:inline="javascript">
	/*<![CDATA[*/
	
	// 2021.08.11 배송지갯수체크 20개이하
	var deliveryAddrCnt = [[${deliveryAddrCnt}]];

	var fnCustDeliveryAddList = function () {
		$.get('/mypage/customer/delivery/addr/list', fnCustDeliveryAddListCallback);
	}

	var fnCustDeliveryAddListCallback = function (result) {
		let html = '';
		if (result.length < 1) {
			$('#nodata').show();
		} else {
			$('#nodata').hide();
			$.each(result, function (idx, custDeliveryAddr){
				if (custDeliveryAddr.defaultYn  === 'Y') {
					html += '<tr class="addrs_default">\n';
				} else {
					html += '<tr>\n';
				}
				html +=	'    <td>\n';
				html +=	'        <div class="block_line">\n';
				html +=	'            <ul>\n';
				html +=	'                <li>\n';
				html +=	'                   <span>'+custDeliveryAddr.recipNm +'</span>\n';
				if (custDeliveryAddr.defaultYn  === 'Y') {
					html += '                   <span class="tag">기본 배송지</span>\n';
				}
				if (custDeliveryAddr.shotDelvUseYn  === 'Y') {
					html += '                   <span class="tag primary_line">총알배송</span>\n';
				}
				html +=	'                </li>\n';
				html +=	'                <li>\n';
				html +=	'                    <span class="phone_number">'+custDeliveryAddr.recipPhnno+'</span>\n';
				html +=	'                </li>\n';
				html +=	'                <li>\n';
				html +=	'                   <span>'+custDeliveryAddr.recipBaseAddr + custDeliveryAddr.recipDtlAddr +'</span>\n';
				html +=	'                </li>\n';
				html +=	'                <li>\n';
				// 2021.08.24 null 처리
				if (custDeliveryAddr.delvMemo == null || custDeliveryAddr.delvMemo == '') {
					html +=	'                   배송요청 사항&nbsp;:&nbsp;<span></span>\n';
				} else {
					html +=	'                   배송요청 사항&nbsp;:&nbsp;<span>'+ custDeliveryAddr.delvMemo +'</span>\n';
				}
				
				html +=	'                </li>\n';
				html +=	'            </ul>\n';
				html +=	'        </div>\n';
				html +=	'    </td>\n';
				html +=	'    <td>\n';
				html +=	'        <div class="button_box">\n';
				html +=	'            <p>';
				html +=	'                <button type="button" class="btn btn_default btn_sm" onclick="fnModiFyCustDelvAddr(\''+custDeliveryAddr.custDelvAddrSq+'\')">';
				html +=	'                    <span>수정</span>';
				html +=	'                </button>';
				html +=	'            </p>';
				html +=	'            <p>';
				html +=	'                <button type="button" class="btn btn_dark btn_sm" onclick="fnDeleteCustDelvAddr(\''+custDeliveryAddr.custDelvAddrSq+'\')">';
				html +=	'                   <span>삭제</span>';
				html +=	'                </button>';
				html +=	'            </p>';
				html +=	'        </div>\n';
				html +=	'    </td>\n';
				html += '</tr>\n';
			});
		}
		$('#custDeliveryList').html(html);
	}

	// 배송지 추가
	$('#btnAdrsaddPop').on('click', function () {
		
		// 2021.08.11 배송지갯수체크 20개이하
		$.ajax( {
			type		: "POST",
			url 		: '/common/addDelvAddrCnt',
			dataType 	: 'html',
			beforeSend : function(xhr, settings) {
				xhr.setRequestHeader("AJAX"			, "true");
				xhr.setRequestHeader('Accept'		, 'application/json');
				xhr.setRequestHeader('Content-Type'	, 'application/json');
			},
			success 	: function(result) {
				if (parseInt(result) >= 20) {
					mcxDialog.alert("배송지는 최대 20개까지 등록 가능합니다.");
					return false;
				} else {
					$.ajax( {
						type		: "POST",
						url 		: '/common/delvAddrAddPop',
						dataType 	: 'html',
						beforeSend : function(xhr, settings) {
							xhr.setRequestHeader("AJAX"			, "true");
							xhr.setRequestHeader('Accept'		, 'application/json');
							xhr.setRequestHeader('Content-Type'	, 'application/json');
						},
						success 	: function(result) {
							$("#adrsAddPop .modal-dialog .modal-content").html(result);
							$("#adrsAddPop").modal("show");
						}
					});
				}
			}
		});
	});

	var delvAddrSet = function (delvObj) {
		if(!gagajf.isNull(delvObj.custDelvAddrSq)) {
			fnCustDeliveryAddList();
		}
	};

	var fnModiFyCustDelvAddr = function (custDelvAddrSq) {
		let params = {}
		params.custDelvAddrSq = custDelvAddrSq;

		$.ajax( {
			type		: "POST",
			url 		: '/common/delvAddrModifyPop',
			data		: JSON.stringify(params),
			dataType 	: 'html',
			beforeSend : function(xhr, settings) {
				xhr.setRequestHeader("AJAX"			, "true");
				xhr.setRequestHeader('Accept'		, 'application/json');
				xhr.setRequestHeader('Content-Type'	, 'application/json');
			},
			success 	: function(result) {
				$("#adrsModifyPop .modal-dialog .modal-content").html(result);
				$("#adrsModifyPop").modal("show");
			}
		});
	};
	
	var fnDeleteCustDelvAddr = function (custDelvAddrSq) {

		mcxDialog.confirm("배송지를 삭제하시겠습니까?", {
		cancelBtnText : "취소",
		sureBtnText : "확인",
		sureBtnClick : function() {
				let params = {}
				params.custDelvAddrSq = custDelvAddrSq;
				let jsonData = JSON.stringify(params);
				gagajf.ajaxJsonSubmit("/mypage/customer/delivery/addr/delete", jsonData, function (result) {
					if (result.isSuccess) {
						fnCustDeliveryAddList();
					} else {
						mcxDialog.alert("배송지 삭제를 실패 하였습니다.");
						return;
					}
				});
			}
		});
	};


	$(document).ready(function() {

		// 마이페이지 LNB 설정
		fnSetMypageLnbList(8);

		// 마이페이지 location 설정
		fnSetMypageLocation('배송지 관리');

		// 배송목록 조회
		fnCustDeliveryAddList();


	});




	/*]]>*/
</script>
</th:block>
</body>
</html>


