<%
/**
 * <pre>
 * @Class Name : listNotices.jsp
 * 
 * @Description : 공지사항 목록을 조회한다.
 * 
 * 
 * @프로젝트명: 지방문화원 통합자료관리시스템 구축사업 (2021)
 *
 * </pre>
 *
 * @ ------------    --------    ---------------------------
 * @ 수정일                     수정자             수정내용
 * @ ------------    --------    ---------------------------
 * @ 2021. 6. 15.    KNKIM       최초 생성
 *
 * 
 * @author 이씨플라자 * DIGITALSHIP KNKIM
 * @since 2021. 6. 15.
 * @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);
    
    $("[name=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);
        $("#mngOrgNm").val($("#_" + mngOrgCd + "_nm").text());
        
        $("[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_deleteCartItems() {
    if($("[id^=chk_item_]:checked").length < 1) {
        alert("삭제할 대상을 선택해 주세요.");
        return;
    }
    
    if(!confirm(($("[id^=chk_item_]:checked").length) + "건을 삭제하시겠습니까?")) {
        return;
    }
    
    $("#selItems").val(fn_getCheckedCartItemsValues());
    $("#frm").attr("action","${pageContext.request.contextPath}/cart/deleteCartItems.do");
    $("#frm").submit();
}

function fn_getCheckedCartItemsValues() {
    var values = "";
    $("[id^=chk_item_]:checked").each(function() {
        if(values.length > 0) values += ",";
        values += $(this).val();
    });
    return values;
}

function fn_checkMngOrgGroup() {
    alert("hi");
    // $("[id^=chk_" + mngOrgCd + "]").prop("checked", checked);
}

// 선택한 게시글 상세 보기로 이동
function fn_viewDetail(articleNo) {
    $("#articleNo").val(articleNo);
    $("#articleForm").attr("action", "${pageContext.request.contextPath}/board/selectNotice.do");
    $("#articleForm").submit();
}

// 게시글 작성으로 이동
function fn_newArticle() {
    $("#articleNo").val("");
    $("#articleForm").attr("action", "${pageContext.request.contextPath}/board/insertNoticeForm.do");
    $("#articleForm").submit();
}

function fn_search(cartTypeCd) {
    $("#cartTypeCd").val(cartTypeCd);
    $("#frm").submit();
}

function fn_selectAll(mngOrgCd) {
    var allOjbName = "chk_all_" + mngOrgCd;
    var checked = $("#" + allOjbName).prop("checked");
    $("[id^=chk_item_" + mngOrgCd + "]").prop("checked", checked);

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

function fn_changeCartItems(cartTypeCd, cartTypeNm) {
    if($("[id^=chk_item_]:checked").length < 1) {
        alert("변경할 대상을 선택해 주세요.");
        return;
    }
    
    if(!confirm(($("[id^=chk_item_]:checked").length) + "건을 " + cartTypeNm + "(으)로 변경하시겠습니까?")) {
        return;
    }
    $("#cartTypeCd").val(cartTypeCd);
    $("#selItems").val(fn_getCheckedCartItemsValues());
    
    $("#frm").attr("action", "${pageContext.request.contextPath}/cart/changeCartType.do");
    $("#frm").submit();
}

// 대출신청으로 이동 
function fn_appRent() {
    
    <sec:authorize access="not isAuthenticated()">
        alert("먼저 로그인하신 후, 이용해 주시기 바랍니다.");
    </sec:authorize>
    
    <sec:authorize access="isAuthenticated()">
        if($("[id^=chk_item_]:checked").length < 1) {
            alert("대출할 대상을 선택해 주세요.");
            return;
        }
        
        
        $("#selItems").val(fn_getCheckedCartItemsValues());
        $("#frm").attr("action","${pageContext.request.contextPath}/rent/confirmRentFromCart.do");
        
        $("#frm").submit();    
    </sec:authorize>
    
}

// 열람신청으로 이동 
function fn_appVisit() {

    <sec:authorize access="not isAuthenticated()">
        alert("먼저 로그인하신 후, 이용해 주시기 바랍니다.");
    </sec:authorize>
    
    <sec:authorize access="isAuthenticated()">
        if($("[id^=chk_item_]:checked").length < 1) {
            alert("방문열람할 대상을 선택해 주세요.");
            return;
        }
        
        
        $("#selItems").val(fn_getCheckedCartItemsValues());
        $("#frm").attr("action","${pageContext.request.contextPath}/read/confirmReadFromCart.do");
        
        $("#frm").submit();    
    </sec:authorize>
    
}

$(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="${cartTypeCd }" />
      <input type="hidden" id="mngOrgNm" name="mngOrgNm" value="${mngOrgNm }" />
      <input type="hidden" id="mngOrgCd" name="mngOrgCd" value="" />
      <input type="hidden" id="selItems" name="selItems" value="" />
      
    <!-- 카트 섹션 -->
    <section class="location cart">
        <h2 class="blind">카트 섹션영역</h2>
        <div class="inner">
            <div class="row-top">
                <div class="location-box">
                    <ul class="loc">
                        <li>HOME</li>
                        <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="cartTab">
                        <ul class="tabs active">
                            <li><a href="javascript:void(0);" onclick="fn_search('1')" <c:if test='${cartTypeCd == "1"}'>class="current"</c:if> >대출 <span>(${cartTypeCd1Cnt })</span></a></li>
                            <li><a href="javascript:void(0);" onclick="fn_search('2')" <c:if test='${cartTypeCd == "2"}'>class="current"</c:if> >방문열람 <span>(${cartTypeCd2Cnt })</span></a></li>
                        </ul>
                        
                        <div class="tab_content">
                            <div class="tabs_item">
                                <div class="inner">
                                    <div class="list-cart-wrap">

      
          <c:set var="preMngOrgCd" value="" />
          <c:set var="mngOrgCdCnt" value="0" />
          <c:set var="totCnt" value="0" />
          
          <c:forEach var="cart" items="${list}" varStatus="status">
		          
		            <c:if test="${preMngOrgCd != cart.mngOrgCd}">
		            
		                <c:if test="${status.count > 1 }">
		                    </div>
		                    <script>
		                    $("#_${preMngOrgCd}_cnt").text("${mngOrgCdCnt }");
		                    </script>
		                </c:if>
		                         
		                <c:set var="mngOrgCdCnt" value="0" />
		                    <!-- 문화원 시작 -->
		                    <h3><label for="selMngOrgCd_${cart.mngOrgCd}"><input type="radio" id="selMngOrgCd_${cart.mngOrgCd}" name="selMngOrgCd" value="${cart.mngOrgCd}"><span id="_${cart.mngOrgCd}_nm">${cart.mngOrgNm }</span> (<span class="count" id="_${cart.mngOrgCd}_cnt"></span>)</label></h3>
		                        <div class="list-cart">
		                            <div class="list t-tit-line">
		                                <div class="t-chk"><input type="checkbox" id="chk_all_${cart.mngOrgCd }" name="chk_all_${cart.mngOrgCd }" onclick="fn_selectAll('${cart.mngOrgCd}')"><label for="chk_all_${cart.mngOrgCd }" style="display:none">전체자료선택</label></div>
		                                <div class="t-data">자료정보</div>
		                                <div class="t-state">이용상태</div>
		                                <div class="t-date">반납예정일</div>
		                                <div class="t-loan">대출</div>
		                                <div class="t-visit">방문열람</div>
		                            </div>              
		               <c:set var="preMngOrgCd" value="${cart.mngOrgCd}" />   
		            </c:if>
		            
		            <c:set var="mngOrgCdCnt" value="${mngOrgCdCnt+1}" />
		            <c:set var="totCnt" value="${totCnt+1}" />
		    
		            <!-- 행 : 시작  -->
		            <div class="list">
		                <div class="t-chk"><input type="checkbox" name="chk_item_${cart.mngOrgCd }" id="chk_item_${cart.mngOrgCd }" value="${cart.masterId }" ><label for="chk_item_${cart.mngOrgCd }" style="display:none">문화원선택</label></div>
		                <div class="t-data">
		                    <div class="img">${cart.interestYn }
		                      <a href="javascript:void(0)"><img src="${cart.rprsThumbUrlWithDefaultSmall }" alt="썸네일" width="60" height="91" title="상세보기" onclick="gfn_getDetail('${cart.masterId }')"></a>
		                      <sec:authorize access="isAuthenticated()">
		                      
		                      <c:if test='${cart.MUseYn == "Y"}'>
    		                      <span id="favorites_${cart.masterId }" class="favorites" onclick="fn_changeInterestBtn('${cart.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='${cart.MUseYn != "Y"}'>
                                  <span class="favorites" onclick="fn_changeInterestBtn('${cart.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>
		                      </sec:authorize>
		                    </div>
		                    <div class="type">
                                <c:if test="${cart.openDivCd != '3' }">
                                <span class="online">온라인열람</span>
                                </c:if>
                                
                                <c:if test="${cart.operOutRangeCd == '01' }">
                                <span class="loan">대출</span>
                                </c:if>
		                        
		                        <c:if test="${cart.operReadRangeCd != '03' }">
		                        <span class="visit">방문열람</span>
		                        </c:if>
		                        
		                    </div>
		                    <div class="title"><a href="javascript:void(0)" onclick="gfn_getDetail('${cart.masterId }')" title="상세보기">${cart.title }</a></div>
		                </div>
		                <div class="t-state <c:if test="${cart.useStatus != '03' and cart.useStatus != '06' }">poss</c:if> <c:if test="${cart.useStatus == '03' or cart.useStatus == '06' }">imposs</c:if>"><p>${cart.useStatusNm }</p></div>
		                <div class="t-date">${cart.rtnExpctDd }</div>
		                <div class="t-loan  <c:if test="${cart.operOutRangeCd == '01' }">poss</c:if> <c:if test="${cart.operOutRangeCd != '01' }">imposs</c:if>">${cart.operOutRangeNm }</div>
		                <div class="t-visit <c:if test="${cart.operReadRangeCd != '03' }">poss</c:if> <c:if test="${cart.operReadRangeCd == '03' }">imposs</c:if>">${cart.operReadRangeNm }</div>
		            </div>            
		            <!-- 행 : 종료 -->
        </c:forEach>           

        <c:if test="${fn:length(list) > 0 }">
            </div>
            <script>
            $("#_${preMngOrgCd}_cnt").text("${mngOrgCdCnt }");
            </script>
        </c:if>       

        <c:if test="${totCnt < 1 }">
                            <!-- 자료 없음  -->
                                <div class="list-cart">
                                    <div class="list t-tit-line">
                                        <div class="t-chk"><input name="mngNone" id="mngNone" type="checkbox"><label for="mngNone" style="display:none">검색어</label></div>
                                        <div class="t-data">자료정보</div>
                                        <div class="t-state">이용상태</div>
                                        <div class="t-date">반납예정일</div>
                                        <div class="t-loan">대출</div>
                                        <div class="t-visit">방문열람</div>
                                    </div>
                                    <div class="no-result">
                                         <span>자료가 없습니다.</span>
                                    </div>                                    
                                </div>              
        </c:if>         
        
        <c:if test="${totCnt > 0 }">
                                    <div class="btn-box">   
                                        <div class="left">
                                            <button type="button" class="btn btn-gray" onclick="fn_deleteCartItems()">선택삭제</button>
                                            
										      <c:if test='${cartTypeCd == "1"}'>
										          <button type="button" class="btn btn-black" onclick="fn_changeCartItems('2', '방문열람')">방문열람으로</button>
										      </c:if>
										      
										      <c:if test='${cartTypeCd == "2"}'>
										          <button type="button" class="btn btn-black" onclick="fn_changeCartItems('1', '대출')">대출로</button>
										      </c:if>                                    
                                        </div>
                                        <div class="right">
                                            <c:if test='${cartTypeCd == "1"}'>
	                                            <button type="button" name="btnApp" id="btnApp" class="btn btn-color" onclick="fn_appRent()">대출신청/예약 <span id="appCnt" class="count">(0)</span></button>
	                                            <script>BTN_APP_PREFIX = "대출신청/예약"; </script>
                                            </c:if>
                                            <c:if test='${cartTypeCd == "2"}'>
                                                <button type="button" name="btnApp" id="btnApp" class="btn btn-color" onclick="fn_appVisit()">열람신청 <span id="appCnt" class="count">(0)</span></button>
                                                <script>BTN_APP_PREFIX = "열람신청"; </script>
                                            </c:if>
                                        </div>
                                    </div>
        </c:if>         
                                    
                                    </div>
                                    
                                </div>
                            </div>
                            
                        </div>
                        
                        
                    </div>
                </div>
            </div>
        </div>
    </section>
    
</form>

      

                      

