<!DOCTYPE html>
<html lang="ko"
	xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : GoodsCouponFormMob.html
 * @desc    : 상품 쿠폰 팝업
 *============================================================================
 * STYLE24
 * Copyright(C) 2020 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2021.03.11   eskim        최초 작성
 *******************************************************************************
 -->
<div class="modal-dialog" role="document" th:if="${goodsCouponList != null and !goodsCouponList.empty}">
	<div class="modal-content">
		<div class="modal-header">
			<h5 class="modal-title" id="modalScrollLabel">할인쿠폰 받기</h5>
		</div>
		<div class="modal-body">
		<form name="goodsCouponForm" id="goodsCouponForm" method="post">
		<input type="hidden" name="goodsCd" th:value="${params.goodsCd}"/>
			<div class="pop_cont">
				<ul class="coupon_list">
					<li th:each="goodsCoupon, status : ${goodsCouponList}">
						<div class="coupon" >
							<div>
								<p class="cp_name" th:text="${goodsCoupon.cpnNm}">
									TBJ 시즌오프 20% 할인쿠폰
								</p>
								<p class="cp_cont">
									<span th:if="${goodsCoupon.dcWay == 'G240_10'}"><em th:text="${#numbers.formatInteger(goodsCoupon.dcVal, 0,'COMMA')}">12,399,900</em>원</span>
									<span th:unless="${goodsCoupon.dcWay == 'G240_10'}"><em th:text="|${goodsCoupon.dcVal}%|">40%</em></span>
								</p>
								<p class="cp_condition">
									<th:block th:text="${#numbers.formatInteger(goodsCoupon.buyLimitAmt, 0,'COMMA')}"></th:block>원 이상 구매 시 최대 <th:block th:text="${#numbers.formatInteger(goodsCoupon.maxDcAmt, 0,'COMMA')}"></th:block>원 할인
									<span>1인 최대 <th:block th:text="${(goodsCoupon.custPubLimitQty > 0) ? #numbers.formatInteger(goodsCoupon.custPubLimitQty, 0,'COMMA') +'장' : '무제한'}"></span>
								</p>
							</div>
							<div class="btn_group_flex">
								<div>
									<button type="button" class="btn btn_dark" 
											th:disabled="${goodsCoupon.custPubLimitQty > 0 and goodsCoupon.custPubLimitQty <= goodsCoupon.custCouponCnt}"
											th:attr="cpnId=${goodsCoupon.cpnId}" th:id="${'coupon_'+goodsCoupon.cpnId}" onclick="fnGoodsCouponDown(this);">
										<span>쿠폰받기</span>
										<span>받기완료</span>
									</button>
								</div>
							</div>
							<!-- <th:block th:if="${goodsCoupon.custPubLimitQty > 0 and goodsCoupon.custPubLimitQty <= goodsCoupon.custCouponCnt}">
							<button type="button" class="btn btn_dark btn_block btn_coupon_down"   disabled="disabled"><span>받기완료</span></button>
							</th:block>
							<th:block th:unless="${goodsCoupon.custPubLimitQty > 0 and goodsCoupon.custPubLimitQty <= goodsCoupon.custCouponCnt}">
							<button type="button" class="btn btn_dark btn_block btn_coupon_down" th:attr="cpnId=${goodsCoupon.cpnId}" th:id="${'coupon_'+goodsCoupon.cpnId}" onclick="fnGoodsCouponDown(this);"><span>쿠폰받기</span></button>
							</th:block> -->
						</div>
					</li>
				</ul>
			</div>
		</form>	
		</div>
		<div class="modal-footer">
			<button type="button" id="btn_all_coupon" class="btn btn_primary" onclick="fnGoodsCouponDownAll();">
			<span>쿠폰 모두 받기</span><span>쿠폰 모두 받기 완료</span></button>
		</div>
	</div>
</div>
<a href="javascript:void(0);" rel="modal:close" onclick="cfCloseLayer('layer_goods_coupon')" class="close-modal">Close</a>
<script th:inline="javascript">
/*<![CDATA[*/

	// 쿠폰 다운로드
	var fnGoodsCouponDown = function(obj){
	
		if (!cfCheckLogin()) {
			cfnGoToPage(_PAGE_LOGIN);
			return false;
		}
		
		let $obj = $(obj);
		let cpnId = $obj.attr('cpnId');
		let goodsCd = $('#goodsCouponForm input[name=goodsCd]').val();
		
		gagajf.ajaxJsonSubmit(_PAGE_GOODS_CPN_DOWNLOAD, JSON.stringify({goodsCd: goodsCd, cpnId: cpnId}), fnGoodsCouponCallBack);
		
	//쿠폰이 발급되었습니다
	//죄송합니다. 쿠폰이 모두 소진되었습니다
	//죄송합니다. 해당 쿠폰은 다운로드가 불가합니다.
	}
	
	// 쿠폰다운로드 콜백
	var fnGoodsCouponCallBack = function(result){
		if (result.status == "200"){
			let cpnId = result.params.cpnId;
			if (result.goodsCouponList == null) return;
			let list = result.goodsCouponList;

			list.forEach(function(coupon){
				if (coupon.cpnId == cpnId){
					if (Number(coupon.custPubLimitQty) > 0){
						if (Number(coupon.custPubLimitQty) <= Number(coupon.custCouponCnt)){
							//$('#coupon_'+ coupon.cpnId).html('<span>받기완료</span>');
							$('#coupon_'+ coupon.cpnId).attr('disabled', true);
							$('#coupon_'+ coupon.cpnId).find('span::after').attr('content','');
						}
					}
					if (Number(coupon.TotPubLimitQty) > 0){
						if (Number(coupon.TotPubLimitQty) <= Number(coupon.CustCouponCnt)){
							//$('#coupon_'+ coupon.cpnId).html('<span>받기완료</span>');
							$('#coupon_'+ coupon.cpnId).attr('disabled', true);
							$('#coupon_'+ coupon.cpnId).find('span::after').attr('content','');
						}
					}
				}
			});
		}
		jfCouponCheck();
	}
	
	
	// 쿠폰 전체 다운로드
	var fnGoodsCouponDownAll = function(){
		//00개 쿠폰이 발급되었습니다.
		let couponDownAbleYn = "N";
		let arrCpnId = [];
		$('.btn_group_flex .btn_dark').each(function(){
			if (!$(this).attr('disabled')){
				couponDownAbleYn = "Y";
				arrCpnId.push($(this).attr('cpnId'));
			}
		});	
		
		if ("N" == couponDownAbleYn){
			mcxDialog.alert("다운 받을 쿠폰이 없습니다.");
			return false;
		}
		let goodsCd = $('#goodsCouponForm input[name=goodsCd]').val();
		gagajf.ajaxJsonSubmit(_PAGE_GOODS_CPN_DOWNLOAD, JSON.stringify({goodsCd: goodsCd, arrCpnId: arrCpnId}), fnGoodsCouponDownAllBack);
		
	}
	
	// 쿠폰 전체 다운로드 콜백
	var fnGoodsCouponDownAllBack = function(result){
		if (result.status == "200"){
			if (result.goodsCouponList == null) return;
			let list = result.goodsCouponList;

			list.forEach(function(coupon){
				$('.btn_group_flex .btn_dark').each(function(){
					if (!$(this).attr('disabled')){
						let cpnId = $(this).attr('cpnId');
						if (coupon.cpnId == cpnId){
							if (Number(coupon.custPubLimitQty) > 0){
								if (Number(coupon.custPubLimitQty) <= Number(coupon.custCouponCnt)){
								//	$('#coupon_'+ coupon.cpnId).html('<span>받기완료</span>');
									$('#coupon_'+ coupon.cpnId).attr('disabled', true);
								}
							}
							if (Number(coupon.TotPubLimitQty) > 0){
								if (Number(coupon.TotPubLimitQty) <= Number(coupon.CustCouponCnt)){
								//	$('#coupon_'+ coupon.cpnId).html('<span>받기완료</span>');
									$('#coupon_'+ coupon.cpnId).attr('disabled', true);
								}
							}
						}
					}
				});	
			});
		}
		jfCouponCheck();
	}
	
	
	var jfCouponCheck = function(){
		let couponDownAbleYn = "N";
		$('.btn_group_flex .btn_dark').each(function(){
			if (!$(this).attr('disabled')){
				couponDownAbleYn = "Y";
			}
		});	
		
		if(couponDownAbleYn == "N"){
			$('#btn_all_coupon').attr('disabled', true);
		}
	}	
	
	$(document).ready(function() {
		
		jfCouponCheck();
	});
	
	
/*]]>*/
</script>
</html>