<%
/**
 * <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" %>

<c:set var="pageTitle">카트</c:set>

<!DOCTYPE html>
<html lang="ko">
<head>
<title>${pageTitle}</title>

<!-- GRID -->
<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/js/jsgrid/nlib-jsgrid.css" />
<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/js/jsgrid/nlib-jsgrid-theme.css" />
<script src="${pageContext.request.contextPath}/js/jsgrid/nlib-jsgrid.js"></script>

<script type="text/javaScript" language="javascript">
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 + ")");
    });
    
    $("[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_searchArticle(pageSize, pageIndex) {
    $("#jsGrid").jsGrid("search"
            , {'pageSize'  : (pageSize  ? pageSize  : fn_getIntValue("pageSize", ${pageSize}) ), 
               'pageIndex' : (pageIndex ? pageIndex : fn_getIntValue("pageIndex", 1))
              }
    );
}

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();
}

</script>
</head>

<body>

<h1>${pageTitle }</h1>

메시지 : ${message} 
&nbsp;<br/>
<form name="frm" id="frm"  
      action="${pageContext.request.contextPath}/cart/listCartItems.do" 
      method="post"> 
      
      cartTypeCd = <input type="text" id="cartTypeCd" name="cartTypeCd" value="${cartTypeCd }" />
      mngOrgCd = <input type="text" id="mngOrgCd" name="mngOrgCd" value="" />
      selItems = <input type="text" id="selItems" name="selItems" value="" />
      <br>
      
      <c:if test='${cartTypeCd == "1"}'>
        <a href="javascript:void(0);" onclick="fn_search('1')"><span style="color:red">대출</span></a>
      </c:if>
      <c:if test='${cartTypeCd != "1"}'>
        <a href="javascript:void(0);" onclick="fn_search('1')"><span style="color:blue">대출</span></a>
      </c:if>
      
      &nbsp;&nbsp;|&nbsp;&nbsp;
      
      <c:if test='${cartTypeCd == "2"}'>
        <a href="javascript:void(0);" onclick="fn_search('2')"><span style="color:red">열람</span></a>
      </c:if>
      <c:if test='${cartTypeCd != "2"}'>
        <a href="javascript:void(0);" onclick="fn_search('2')"><span style="color:blue">열람</span></a>
      </c:if>
      
      
      <br>
      ----------------------------------
      <br>
      1. 카트 > 2.신청/예약확인 > 3.신청/예약완료
      <br> 
      ----------------------------------
      <br>
    
	      <c:set var="preMngOrgCd" value="" />
	      <c:set var="mngOrgCdCnt" value="0" />
          <c:forEach var="cart" items="${list}" varStatus="status">
          
            <c:if test="${preMngOrgCd != cart.mngOrgCd}">
	            <c:if test="${status.count > 1 }">
	                </table>
	                &nbsp;<br>
	                &nbsp;<br>
	                <script>
	                $("#_${preMngOrgCd}_cnt").text("${mngOrgCdCnt }");
	                </script>
	            </c:if>
	                     
              <c:set var="mngOrgCdCnt" value="0" />
		      <table style="border: 1px solid black;">
		          <tr>
		            <td colspan="8">
		              selMngOrgCd
		              <input type="radio" id="selMngOrgCd" name="selMngOrgCd" value="${cart.mngOrgCd}">
		              ${cart.mngOrgNm } (<span id="_${cart.mngOrgCd}_cnt"></span>)
		            </td>
		          </tr>
		          <tr>
		            <td>
		            chk_all_${cart.mngOrgCd }
		            <input type="checkbox" id="chk_all_${cart.mngOrgCd }" name="chk_all_${cart.mngOrgCd }" onclick="fn_selectAll('${cart.mngOrgCd}')">선택</a></td>
		            <td>ID</td>
		            <td>썸네일/관심</td>
		            <td>제목</td>
		            <td>이용상태</td>
		            <td>반납예정일</td>
		            <td>대출</td>
		            <td>방문열람</td>
		          </tr>
		       <c:set var="preMngOrgCd" value="${cart.mngOrgCd}" />   
            </c:if>
            
            <c:set var="mngOrgCdCnt" value="${mngOrgCdCnt+1}" />
            
			<tr>
			<td>
			chk_item_${cart.mngOrgCd }
			<input type="checkbox" name="chk_item_${cart.mngOrgCd }" id="chk_item_${cart.mngOrgCd }" value="${cart.masterId }" /></td>
			<td>${cart.mbCartId }</td>
			<td><img src="${cart.rprsThumbUrl }" width="30" height="40"> : ${cart.interestYn }</td>
			<td>${cart.title }<br>
			${cart.masterId }
			${cart.mngOrgCd } 
			
			</td>
			<td>${cart.useStatus }<br> ${cart.useStatusNm }</td>
			<td>${cart.rtnExpctDd }</td>
			<td>${cart.operOutRangeCd } ${cart.operOutRangeNm }</td>
			<td>${cart.operReadRangeCd } ${cart.operReadRangeNm }</td>
			</tr>
          </c:forEach>
      

                <c:if test="${fn:length(list) > 0 }">
                    </table>
                    &nbsp;<br>
                    &nbsp;<br>
                    <script>
                    $("#_${preMngOrgCd}_cnt").text("${mngOrgCdCnt }");
                    </script>
                </c:if>   
                      
      <input type="button" name="btnDelete" id="btnDelete" value="선택삭제" onclick="fn_deleteCartItems()" />
      
      <c:if test='${cartTypeCd == "1"}'>
      <input type="button" name="btnMove" id="btnMove" value="방문열람으로이동" onclick="fn_changeCartItems('2', '방문열람')" />
      <input type="button" name="btnApp" id="btnApp" value="대출신청/예약 (0)" onclick="fn_reserveCartItems()" />
      <script>BTN_APP_PREFIX = "대출신청/예약"; </script>
      </c:if>
      
      <c:if test='${cartTypeCd == "2"}'>
      <input type="button" name="btnMove" id="btnMove" value="대출로이동" onclick="fn_changeCartItems('1', '대출')" />
      <input type="button" name="btnApp" id="btnApp" value="열람신청 (0)" onclick="fn_reserveCartItems()" />
      <script>BTN_APP_PREFIX = "열람신청"; </script>
      </c:if>

</form>

</body>
</html>

