<%
/**
 * <pre>
 * @Class Name : confirmRent.jsp
 * 
 * @Description : 대출신청 확인 화면 
 * 
 * 
 * @프로젝트명: 지방문화원 통합자료관리시스템 구축사업 (2021)
 *
 * </pre>
 *
 * @ ------------    --------    ---------------------------
 * @ 수정일                     수정자             수정내용
 * @ ------------    --------    ---------------------------
 * @ 2021. 10. 25.   KNKIM       최초 생성
 *
 * 
 * @author 이씨플라자 * DIGITALSHIP KNKIM
 * @since 2021. 10. 25.
 * @version 1.0
 *
 */
 %>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="ui" uri="http://egovframework.gov/ctl/ui" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="validator" uri="http://www.springmodules.org/tags/commons-validator" %>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>

<script>
var BTN_APP_PREFIX = "대출신청/예약"; 

$( document ).ready(function() {
    
    $("[id^=chk_item_]").prop("disabled", true);
    $("[id^=chk_all_]").prop("disabled", true);
    
    $("[id=selMngOrgCd]").change(function(obj) {
        var checked = $(this).prop("checked");
        var mngOrgCd = $(this).val();
        
        if(checked) {
            // uncheck others and disable
            // enable 
            //$("[id^=chk_item_" + mngOrgCd + "]").prop("checked", false);
            $("[id^=chk_item_" + mngOrgCd + "]").prop("disabled", false);
            $("[id^=chk_all_" + mngOrgCd + "]").prop("disabled", false);
            
            $("[id^=chk_item_]").not("[id^=chk_item_" + mngOrgCd + "]").prop("checked", false);
            $("[id^=chk_item_]").not("[id^=chk_item_" + mngOrgCd + "]").prop("disabled", true);
            
            $("[id^=chk_all_]").not("[id^=chk_all_" + mngOrgCd + "]").prop("checked", false);
            $("[id^=chk_all_]").not("[id^=chk_all_" + mngOrgCd + "]").prop("disabled", true);

        }
        $("#mngOrgCd").val(mngOrgCd);
        $("[id^=chk_item_" + mngOrgCd + "]").filter(":first").trigger("change");
        
    });

    $("[id^=chk_item_]").change(function(obj) {
        var mngOrgCd = $(this).attr("id").substr(9);
        
        var totNum = $("[id^=chk_item_" + mngOrgCd + "]").length;
        var selNum = $("[id^=chk_item_" + mngOrgCd + "]:checked").length;
        
        var checked = false;
        if(totNum <= selNum) checked = true;
        $("[id^=chk_all_" + mngOrgCd + "]").prop("checked", checked);

        $("#btnApp").val(BTN_APP_PREFIX + " (" + selNum + ")");
        $("#appCnt").text("(" + selNum + ")");
    });
    
    $("[id^=selMngOrgCd]").filter(":first").prop("checked", true);
    $("[id^=selMngOrgCd]").filter(":first").trigger("change",true);
    
}); // document ready


function fn_insertRentItems() {
    
    <sec:authorize access="not isAuthenticated()">
        alert("먼저 로그인하신 후, 이용해 주시기 바랍니다.");
    </sec:authorize>
    
    <sec:authorize access="isAuthenticated()">
        if($("#selItems").val() == "" || $("#selItems").val() == ",") {
            alert("대출할 대상정보가 없습니다.");
            return;
        }
        
        <c:if test='${fromCart == "Y" }'>
        $("#frm").attr("action","${pageContext.request.contextPath}/rent/insertRentItemsFromCart.do");
        </c:if>

        <c:if test='${fromCart != "Y" }'>
        $("#frm").attr("action","${pageContext.request.contextPath}/rent/insertRentItems.do");
        </c:if>

        $("#frm").submit();    
    </sec:authorize>
    
}

function fn_listCartItems() {
    
    <c:if test='${fromCart == "Y" }'>
    $("#frm").attr("action","${pageContext.request.contextPath}/cart/listCartItems.do");
    $("#frm").submit();     
    </c:if>
    
    <c:if test='${fromCart != "Y" }'>
    location.href = "/";
    </c:if>
}

$(document).ready(function() {
    
    /* 탭메뉴 */
    var selectedIdx = '> li:eq(' + (${cartTypeCd } -1) + ')';
    $('.cartTab ul.tabs').addClass('active').find(selectedIdx).addClass('current');
    
    $('.tab ul.tabs li a').click(function (g) { 
        var tab = $(this).closest('.tab'), 
            index = $(this).closest('li').index();
    
        tab.find('ul.tabs > li').removeClass('current');
        $(this).closest('li').addClass('current');
    
        tab.find('.tab_content').find('div.tabs_item').not('div.tabs_item:eq(' + index + ')').hide();
        tab.find('.tab_content').find('div.tabs_item:eq(' + index + ')').show();
        g.preventDefault();
    });
    
    if("${message}" != "") alert("${message}");
    

});

</script>
    
<form name="frm" id="frm" action="${pageContext.request.contextPath}/cart/listCartItems.do" method="post"> 
      <input type="hidden" id="cartTypeCd" name="cartTypeCd" value="${rentVO.cartTypeCd }" />
      <input type="hidden" id="mngOrgCd" name="mngOrgCd" value="${rentVO.mngOrgCd }" />
      <input type="hidden" id="mngOrgNm" name="mngOrgNm" value="${rentVO.mngOrgNm }" />
      
      
    <!-- 카트 섹션 -->
    <section class="location cart cart-confirm">
        <h2 class="blind">카트 섹션영역</h2>
        <div class="inner">
            <div class="row-top">
                <div class="location-box">
                    <ul class="loc">
                        <li>HOME</li>
                        <c:if test='${fromCart == "Y" }'>
                        <li>카트</li>
                        </c:if>
                        <li class="on">대출신청/예약</li>
                    </ul>
                    <ul class="tit">
                        <li><span>대출</span>신청/예약</li>
                    </ul>
                </div>
                <div class="arr"><img src="/images/icon/icon-join-arr2.png" alt="화살표 아이콘"></div>
            </div>
            <div class="contents row-bottom">
                <div class="cart-box">
                    <div class="tab">
                        <div class="tab_content loan-confirm">
                            <div class="tabs_item">
                                <div class="inner">
                                    <div class="list-cart-wrap confirm">

      
          <c:set var="totCnt" value="0" />
          <c:set var="rentType1Cnt" value="0" />
          <c:set var="rentType2Cnt" value="0" />
          <c:set var="selItems" value="" />
          
                            <h3><label>대출신청/예약확인 : ${rentVO.mngOrgNm }</label></h3>
                                <div class="list-cart">
                                    <div class="list t-tit-line">
                                        <div class="t-data t-mo">자료정보</div>
                                        <div class="t-state t-mo">이용상태</div>
                                        <div class="t-date t-mo">반납예정일</div>
                                        <div class="t-reser t-mo">신청/예약정보</div>
                                        <div class="t-visit t-mo">비고</div>
                                    </div>
          
                                <c:forEach var="rent" items="${list}" varStatus="status">
                                
                                    <%
                                    //-----------------------------------
                                    // 대출신청인 경우 
                                    //-----------------------------------
                                    // 대출가능코드(operOutRangeCd)가 01:대출가능이면서
                                    // 이용상태코드 (useStatus)가 01:이용가능, 또는 05 : 이용가능, 또는 04 : 열람대기(대출가능) 인 경우
                                    %>
                                    <c:if test="${rent.operOutRangeCd == '01' and (rent.useStatus == '01' or rent.useStatus == '04' or rent.useStatus == '05')  }">
                                    <c:set var="rentType1Cnt" value="${rentType1Cnt+1}" />
                                    <c:set var="selItems" value='${selItems += "," += rent.masterId }' />
                                    </c:if>

                                    <%
                                    //-----------------------------------
                                    // 대출예약신청인 경우 
                                    //-----------------------------------
                                    // 대출가능코드(operOutRangeCd)가  01 :대출가능이면서 
                                    // 이용상태코드 (useStatus)가 02 : 대출중 (대기자 0명) 인 경우
                                    %>
                                    <c:if test="${rent.operOutRangeCd == '01' and rent.useStatus == '02'  }">
                                    <c:set var="rentType2Cnt" value="${rentType2Cnt+1}" />
                                    <c:set var="selItems" value='${selItems += "," += rent.masterId }' />
                                    </c:if>
                                    
                                    <div class="list list-content">
                                        <div class="t-data">
                                            <div class="img"><a href="javascript:void(0)"><img src="${rent.rprsThumbUrlWithDefaultSmall }" alt="썸네일" width="60" height="91" title="상세보기" onclick="gfn_getDetail('${rent.masterId }')"></a>
                                                  <c:if test='${rent.MUseYn == "Y"}'>
                                                      <span id="favorites_${rent.masterId}" class="favorites" onclick="fn_changeInterest('${rent.masterId }', 'off')" title="관심자료에서 삭제합니다.">
                                                      <img src="/images/icon/icon-cart-Favorites-on.png" class="off" alt="즐겨찾기 아이콘">
                                                      <img src="/images/icon/icon-cart-Favorites.png" class="on" alt="즐겨찾기 아이콘">
                                                      </span>
                                                  </c:if>
                                                  <c:if test='${rent.MUseYn != "Y"}'>
                                                      <span id="favorites_${rent.masterId}" class="favorites" onclick="fn_changeInterest('${rent.masterId }', 'on')" title="관심자료로 등록합니다.">
                                                      <img src="/images/icon/icon-cart-Favorites.png" class="off" alt="즐겨찾기 아이콘">
                                                      <img src="/images/icon/icon-cart-Favorites-on.png" class="on" alt="즐겨찾기 아이콘">
                                                      </span>
                                                  </c:if>                                            
                                            </div>
                                            <div class="type">
                                                <c:if test="${rent.ableOnline }">
                                                <span class="online">온라인열람</span>
                                                </c:if>
                                                
                                                <c:if test="${rent.ableRent }">
                                                <span class="loan">대출</span>
                                                </c:if>
                                                
                                                <c:if test="${rent.ableRead }">
                                                <span class="visit">방문열람</span>
                                                </c:if>
                                            </div>
                                            <div class="title"><a href="javascript:void(0)" onclick="gfn_getDetail('${rent.masterId }')" title="상세보기">${rent.title }</a></div>
                                        </div>
                                        <div class="t-state <c:if test="${rent.useStatus != '03' and rent.useStatus != '06' }">poss</c:if> <c:if test="${rent.useStatus == '03' or rent.useStatus == '06' }">imposs</c:if>"><p>${rent.useStatusNm }</p></div>
                                        <div class="t-date">${rent.rtnExpctDd }</div>
                                        <div class="t-reser <c:if test='${rent.ableRentNow }'>poss</c:if> <c:if test='${!rent.ableRentNow}'>imposs</c:if> ">
                                                <c:if test='${!rent.ableRentNow}'>신청 불가</c:if>
                                                <c:if test='${rent.ableRentAppNow}'>대출신청</c:if>
                                                <c:if test='${rent.ableRentResNow}'>대출예약</c:if>
                                        </div>
                                        <div class="t-visit">
                                                <c:if test='${!rent.ableRentNow }'>이미 대출 대기자가 있거나 대출가능 자료가 아닙니다.</c:if>
                                                <c:if test='${rent.ableRentAppNow}'>대출신청 자료는 신청 다음날까지 방문대출이 가능합니다.</c:if>
                                                <c:if test='${rent.ableRentResNow}'>대출예약 자료는 반납 즉시 문자(SMS)나 이메일로 안내 드립니다.</c:if>
                                        </div>
                                    </div>
                                    
                                </c:forEach>   
                                
                                    </div> <!-- 목록 테이블  list-cart : 종료 -->
                                </div>
                                
                                <div class="total-box">
                                    <div class="row-top">
                                        <div class="t-subsc">대출신청</div>
                                        <div class="t-reser">대출예약</div>
                                        <div class="t-total">총 자료 건수</div>
                                    </div>
                                    <div class="row-bottom">
                                        <div class="t-subsc"><strong>${rentType1Cnt}</strong>건</div>
                                        <div class="plus"><strong>+</strong></div>
                                        <div class="t-reser"><strong>${rentType2Cnt}</strong>건</div>
                                        <div class="result"><strong>=</strong></div>
                                        <div class="t-total"><strong>${rentType1Cnt + rentType2Cnt}</strong>건</div>
                                    </div>
                                </div>
                                
                                <p class="text">- 대출예약 자료는 반납 즉시 문자(SMS)나 이메일을 통해 안내 드리며, 3일간 별도 보관됩니다. (3일이 지나면 예약이 취소됩니다.)</p>
                                <p class="text">- 대출신청 자료의 경우 대출을 신청한 후 다음날까지 방문대출이 가능합니다.</p>
                                
                                <div class="btn-wrap">  
                                    <button type="button" class="btn btn-gray" onclick="fn_listCartItems()">취소</button>
                                    
                                    <c:if test="${(rentType1Cnt + rentType2Cnt) > 0}">
                                    <button type="button" class="btn btn-color" onclick="fn_insertRentItems()">신청/예약하기<span id="totCnt" class="count">(${rentType1Cnt + rentType2Cnt})</span></button>
                                    </c:if>
                                    <c:if test="${(rentType1Cnt + rentType2Cnt) < 1}">
                                    <button type="button" class="btn btn-color" onclick="alert('대출신청할 자료가 없습니다.');">신청/예약하기<span id="totCnt" class="count">(${rentType1Cnt + rentType2Cnt})</span></button>
                                    </c:if>
                                </div>                                
                                
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
    
    <input type="hidden" id="selItemsOld" name="selItemsOld" value="${rentVO.selItems }" />
    <input type="hidden" id="selItems" name="selItems" value="${selItems }" />
    
</form>