<!DOCTYPE html>
<html lang="ko"
	xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : CardPromotionPopupForm.html
 * @desc    : 카드프로모션 팝업 화면
 *============================================================================
 * STYLE24
 * Copyright(C) 2019 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2021.02.03   eskim       최초 작성
 *******************************************************************************
 -->	
	<div class="modalPopup" data-width="1000"> <!-- data-width="850"  -->
		<div class="panelStyle">
			<div class="panelTitle">
				<h2>프로모션<th:block th:text="${cardPrmtSq > 0 }?'상세':'등록'"></th:block> </h2>
				<button type="button" class="close" onclick="uifnPopupClose('popupCardPromotion')"><i class="fa fa-times"></i></button>
			</div>
			<form id="cardPromotionForm" name="cardPromotionForm" >
			<input type="hidden" name="prmtGb"  value="A"/> <!-- 할인 -->
			<input type="hidden" name="delCardPrmtCdtSq" /> <!-- 삭제할 프로모션조건 -->
			
			<div class="panelContent">
				<table class="frmStyle">
					<colgroup>
						<col style="width:10%;"/>
						<col style="width:50%;"/>
						<col style="width:10%;"/>
						<col/>
					</colgroup>
					<tr>
						<th>프로모션명<em class="required" title="필수"></em></th>
						<td><input type="text" class="w500" id="prmtNm" name="prmtNm"  maxlength="30" required="required" data-valid-name="프로모션명"  th:value="${cardPrmt.prmtNm}"/></td>
						<th>프로모션ID</th>
						<td><input type="text" class="w100 formControl" id="cardPrmtSq" name="cardPrmtSq"  readonly th:value="${cardPrmt.cardPrmtSq}"/></td>
					</tr>
					<tr>
						<th>행사기간<em class="required" title="필수"></em></th>
						<td id="sellTermsP">
							<input name="prmtStd" id="prmtStd" type="text" class="w80 schDate" maxlength="10" required="required" data-valid-name="행사시작일" th:value="${cardPrmt.prmtStd}"/>
							~
							<input name="prmtEdd" id="prmtEdd" type="text" class="w80 schDate" maxlength="10" required="required" data-valid-name="행사종료일" th:value="${cardPrmt.prmtEdd}"/>
						</td>
						<th>노출여부<em class="required" title="필수"></em></th>
						<td >
							<label class="rdoBtn"><input type="radio" name="dispYn" value="Y" th:checked="${cardPrmt.dispYn =='Y' or #strings.isEmpty(cardPrmt.dispYn)}"/>Y</label>
							<label class="rdoBtn"><input type="radio" name="dispYn" value="N" th:checked="${cardPrmt.dispYn =='N'}"/>N</label>
							<input type="hidden" id="dispYnOrg" name="dispYnOrg" th:value="${cardPrmt.dispYn}"/>
						</td>
					</tr>
					<tr>
						<th>행사구분<em class="required" title="필수"></em></th>
						<td colspan="3" >
							<label class="rdoBtn"><input type="radio" name="dcGb" value="1" th:checked="${cardPrmt.dcGb =='1' or #strings.isEmpty(cardPrmt.dcGb)}" th:disabled="${cardPrmtSq > 0}"/>즉시할인</label>
							<label class="rdoBtn"><input type="radio" name="dcGb" value="2" th:checked="${cardPrmt.dcGb =='2'}" th:disabled="${cardPrmtSq > 0}"/>청구할인</label>
							<label class="rdoBtn"><input type="radio" name="dcGb" value="3" th:checked="${cardPrmt.dcGb =='3'}" th:disabled="${cardPrmtSq > 0}"/>제휴카드</label>
						</td>
					</tr>
					<tr>
						<th>행사조건<em class="required" title="필수"></em></th>
						<td colspan="3" >
							<th:block th:if="${#lists.isEmpty(cardPrmt.cardPrmtCdtList)}"  >
								<span id="contionArea">
								<label class="rdoBtn"><input type="radio" name="dcWay" value="G240_10" checked/> 정액할인</label>
								<label class="rdoBtn"><input type="radio" name="dcWay" value="G240_11" />정율할인</label>
								<p>구매금액기준 <input type="text" class="w100 aR"  name="minPayAmt" data-valid-type="numeric"  data-valid-name="구매금액기준"  maxlength="10"/> 원 이상 구매시
								<input type="text" class="w100 aR"  name="dcVal" data-valid-type="numeric"  data-valid-name="할인"  maxlength="10"/> 할인(율/액)</p>
								<p>최대할인금액 <input type="text" class="w100 aR"  name="maxDcAmt" data-valid-type="numeric"  data-valid-name="최대할인금액"  maxlength="10"/> 원</p>
								</span>
								<p>대상카드 <button type="button" class="btn btn-base btn-sm" onClick="fnCardAdd(1);">선택</button><br/>
								<span id="arrCardNm1" class="cRed"></span></p>
								<input type="hidden" name="arrCardCd"/>
								<input type="hidden" name="cardPrmtCdtSq" />
								<input type="hidden" name="cardIdx" id="cardIdx"/>
							</th:block>
							<th:block th:if="${cardPrmt.cardPrmtCdtList}" th:each="cardPrmtCdt, sizeStatus : ${cardPrmt.cardPrmtCdtList}">
								<th:block  th:if="${sizeStatus.first}">
									<span id="contionArea" th:if="${cardPrmt.dcGb !='3'}">
									<label class="rdoBtn"><input type="radio" name="dcWay" value="G240_10" th:checked="${cardPrmtCdt.dcWay =='G240_10' or #strings.isEmpty(cardPrmtCdt.dcWay)}"/>정액할인</label>
									<label class="rdoBtn"><input type="radio" name="dcWay" value="G240_11" th:checked="${cardPrmtCdt.dcWay =='G240_11'}"/>정율할인</label>
									<p>구매금액기준 <input type="text" class="w100 aR"  name="minPayAmt" data-valid-type="numeric"  data-valid-name="구매금액기준" th:value="${cardPrmtCdt.minPayAmt}" maxlength="10"/> 원 이상 구매시
									<input type="text" class="w100 aR" name="dcVal" data-valid-type="numeric"  data-valid-name="할인" th:value="${cardPrmtCdt.dcVal}" maxlength="10"/> 할인(율/액)</p>
									<p>최대할인금액 <input type="text" class="w100 aR"  name="maxDcAmt" data-valid-type="numeric"  data-valid-name="최대할인금액" th:value="${cardPrmtCdt.maxDcAmt}" maxlength="10"/> 원</p>
									</span>
									<p>대상카드 <button type="button" class="btn btn-base btn-sm" onClick="fnCardAdd([[${sizeStatus.count}]]);">선택</button><br/>
									<span th:id="${'arrCardNm'+ sizeStatus.count}" class="cRed"  th:text="${cardPrmtCdt.arrCardNm}"></span>
									<input type="hidden" name="arrCardCd" th:value="${cardPrmtCdt.arrCardCd}"/>
									<input type="hidden" name="cardPrmtCdtSq" th:value="${cardPrmtCdt.cardPrmtCdtSq}"/>
									<input type="hidden" name="cardIdx" id="cardIdx"/>
								</th:block>
							</th:block>

						</td>
					</tr>
					<tr>
						<th>연결 URL <div class="iconTooltip">
									<i class="fa fa-info" aria-hidden="true"></i>
									<span class="left" style="width:320px;">URL 입력 시 Front에서 바로가기 버튼이 노출됩니다.</span>
								</div>
						</th>
						<td><input type="text" class="w500" id="linkUrl" name="linkUrl"  maxlength="150" data-valid-name="연결URL"  th:value="${cardPrmt.linkUrl}"/></td>
					</tr>
					<tr id="dcGb3Note" th:class="${(cardPrmtSq > 0 and cardPrmt.dcGb =='3')? 'on': 'off'}">
						<th>유의사항</th>
						<td  colspan="3">
						<textarea class="textareaR2" name="note" id="note" th:field="${cardPrmt.note}"></textarea>
						</td>
					</tr>
				</table>
			</div>
			<ul class="panelBar">
				<li class="left">
					<button type="button" class="btn btn-danger btn-lg" id="btnCardInterestDelete" th:if="${cardPrmtSq}">삭제</button>
				</li>	
				<li class="right">
					<button type="button" class="btnRight btn btn-success btn-lg" id="btnCardPromotiontSave">적용</button>
				</li>
			</ul>
			</form>	
		</div>
	</div>
<script th:inline="javascript">
/*<![CDATA[*/
	
	//카드조회팝업
	var fnCardAdd = function(idx){
		$('#cardPromotionForm').find('#cardIdx').val(idx);
		var arrCardCd = $('#cardPromotionForm').find("input[name=arrCardCd]").eq(idx-1).val();
		cfnOpenCardListPopup('fnCardAddCallBakup', arrCardCd);	// /로 구분
	}
	
	// 카드추가/삭제
	var fnCardAddCallBakup = function(arrCardCd, arrCardNm) {
		var index = $('#cardPromotionForm').find('#cardIdx').val();
		$('#cardPromotionForm').find("#arrCardNm" + (index)).html(arrCardNm);
		$('#cardPromotionForm input[name=arrCardCd]').eq(index-1).val(arrCardCd);
	}
	
	// 카드프로모션 조건 삭제항목 설정
	var fnDeleteCardPrmtCdt = function(cardPrmtCdtSq){
		var delCardPrmtCdtSq = $('#cardPromotionForm input[name=delCardPrmtCdtSq]').val();
		delCardPrmtCdtSq += "/" + cardPrmtCdtSq; 
		$('#cardPromotionForm input[name=delCardPrmtCdtSq]').val(delCardPrmtCdtSq);
	}
	
	//카드 프로모션 삭제
	$('#btnCardInterestDelete').click(function(e) {
		var arrCardPrmtSq = [];
		
		var toDateStr = new Date().format("YYYYMMDD");
		var prmtEdd = $('#cardPromotionForm input[name=prmtEdd]').val();
		if (toDateStr > prmtEdd.replace(/-/gi,'') ){
			chkFlag = true;
			mcxDialog.alertC("종료된 무이자할부정보는 삭제할 수 없습니다.", {
				sureBtnText: "확인",
				sureBtnClick: function() {
					$('#cardPromotionForm input[name=prmtEdd]').focus();
				}
			});
			return false;
		}
	
		arrCardPrmtSq.push($('#cardPromotionForm input[name=cardPrmtSq]').val());
		
		mcxDialog.confirm('삭제하시겠습니까? <br/> 삭제한 프로모션은 복구할수 없습니다.',  {
			cancelBtnText: "취소",
			sureBtnText: "확인",
			sureBtnClick: function(){
				var data = {arrCardPrmtSq : arrCardPrmtSq};
				var jsonData = JSON.stringify(data);
				gagajf.ajaxJsonSubmit('/marketing/card/promotion/delete', jsonData, fnSaveCardInterestCallBack);
			}
		});
	});
	
	// 행사구분 변경시
	$("#cardPromotionForm input[name=dcGb]").bind('click change', function () {
		var cardPrmtSq = [[${cardPrmtSq}]];
		// 수정시에는 미 적용
		if (cardPrmtSq > 0){
			return;
		}
		var radioValue = $(this).val();
		// 제휴카드일 경우
		if (radioValue == "3"){ 
			$("#cardPromotionForm input[name=minPayAmt]").val('');
			$("#cardPromotionForm input[name=dcVal]").val('');
			$("#cardPromotionForm input[name=maxDcAmt]").val('');
			$("#cardPromotionForm").find('#contionArea').removeClass('on').addClass('off');
			$("#cardPromotionForm").find('#dcGb3Note').removeClass('off').addClass('oon');
		}else{
			$("#cardPromotionForm").find('#dcGb3Note').removeClass('on').addClass('off');
			$("#cardPromotionForm").find('#contionArea').removeClass('off').addClass('on');
		}
	});
	
	// 저장 클릭 시
	$('#btnCardPromotiontSave').on('click', function() {
		
		var optCheck = false;
		// 입력 값 체크
		if (!gagajf.validation('#cardPromotionForm'))
			return false;
			
		if (gagajf.isNull($("#cardPromotionForm input[name=prmtStd]").val()) || gagajf.isNull($("#cardPromotionForm input[name=prmtEdd]").val())){
			mcxDialog.alertC("행사기간의 시작일자와 종료일자를 바르게 입력하세요.", {
				sureBtnText: "확인",
				sureBtnClick: function() {
					$('#cardPromotionForm input[name=prmtStd]').focus();
				}
			});
			return false;
		}
		
	
		var fromDate = $("#cardPromotionForm input[name=prmtStd]").val().replaceAll('-', '');
		var toDate = $("#cardPromotionForm input[name=prmtEdd]").val().replaceAll('-', '');

		if (fromDate > toDate) {
			mcxDialog.alertC("전시기간의 시작일시는 종료일시 보다 클 수 없습니다.", {
				sureBtnText: "확인",
				sureBtnClick: function() {
					$('#cardPromotionForm input[name=prmtStd]').focus();
				}
			});
			return false;
		}
		
		var idx = 0;
		$('#cardPromotionForm').find('#cardContionList tr').each(function() {
			var dcGb = $("#cardPromotionForm input[name=dcGb]").val();
			var dcWay = $(this).find("input[name=dcWay]").val();
			var minPayAmt = $(this).find("input[name=minPayAmt]").val();
			var dcVal = $(this).find("input[name=dcVal]").val();
			var maxDcAmt = $(this).find("input[name=maxDcAmt]").val();
			var arrCardCd = $(this).find("input[name=arrCardCd]").val();
			
			if ("3" != dcGb){
				
				if (gagajf.isNull(minPayAmt) || Number(minPayAmt) < 10000){
					optCheck = true;
					mcxDialog.alertC("구매기준금액을 확인해주세요", {
						sureBtnText: "확인",
						sureBtnClick: function() {
							$('#cardPromotionForm').find('#cardContionList tr').find("input[name=minPayAmt]").eq(idx).focus();
						}
					});
					return false;
				}
				
				if (gagajf.isNull(dcVal)){
					optCheck = true;
					mcxDialog.alertC("할인을 확인해주세요", {
						sureBtnText: "확인",
						sureBtnClick: function() {
							$('#cardPromotionForm').find('#cardContionList tr').find("input[name=dcVal]").eq(idx).focus();
						}
					});
					return false;
				}
				
				
				if (dcWay == 'G240_11' && (Number(dcVal) > 99)){
					optCheck = true;
					mcxDialog.alertC("할인율이 99보다 큽니다.확인해주세요", {
						sureBtnText: "확인",
						sureBtnClick: function() {
							$('#cardPromotionForm').find('#cardContionList tr').find("input[name=dcVal]").eq(idx).focus();
						}
					});
					return false;
				}
				
				if (gagajf.isNull(maxDcAmt)){
					optCheck = true;
					mcxDialog.alertC("최대할인금액을 확인해주세요", {
						sureBtnText: "확인",
						sureBtnClick: function() {
							$('#cardPromotionForm').find('#cardContionList tr').find("input[name=maxDcAmt]").eq(idx).focus();
						}
					});
					return false;
				}
				
			}
			
			if (gagajf.isNull(arrCardCd)){
				optCheck = true;
				mcxDialog.alert((idx+1)+"번째 행사조건의 대상카드를 선택해주세요")
				return false;
			}
			
			idx++;
		});
		
		if (optCheck){
			return false;
		}
		
		mcxDialog.confirm('저장하시겠습니까?',  {
			cancelBtnText: "취소",
			sureBtnText: "확인",
			sureBtnClick: function(){

				gagajf.ajaxFormSubmit("/marketing/card/interest/save", "#cardPromotionForm", fnSaveCardInterestCallBack); 

			}
		});
	});

	
	//창종료
	var fnSaveCardInterestCallBack = function(){
		fnCardPromotionListSearch(); 	//본창 호출
		uifnPopupClose('popupCardPromotion');
		
	}

	$(document).ready(function() {
		//gagajf.setDate('#sellTermsP','prmtStd', 'prmtEdd', 't');
		var date = new Date();
		$('#cardPromotionForm  input[name=prmtStd]').val(date.format("YYYY-MM-DD"));
		$('#cardPromotionForm  input[name=prmtEdd]').val(date.format("YYYY-MM-DD"));
	});

/*]]>*/
</script>
</html>