<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="parmeter" value=""></c:set>

<c:if test="${!empty searchType && !empty searchText}">
    <c:set var="parameter" value="?searchType=${searchType}&searchText=${searchText}"></c:set>
</c:if>
<div class="container">
    <div class="subHeader">
        <h1 class="logo">안동시 교통정보센터</h1>
        <div class="pageLocation">
            <%@ include file="/WEB-INF/jsp/egovframework/include/adminSubMenu.jsp" %>
        </div>
    </div>
    <div class="subContent">
        <%@ include file="/WEB-INF/jsp/egovframework/include/adminMenu.jsp" %>
        <div class="pageTitle">
            <h2>팝업창 관리</h2>
            <p>안동시 첨단교통센터 <span>관리자</span> 정보입니다.</p>
        </div>
        <div class="adminWrap">
            <div class="boardWrap">
                <form action="">
                    <div class="bod_head">
                        <p class="page_num">현재 페이지&nbsp;
                            <span class="bold red">
                                ${pageMaker.currentPage}
                            </span>
                            &nbsp;/
                            &nbsp;전체 페이지&nbsp;${pageMaker.totalPage}
                        </p>
                        <fieldset class="bod_search">
                            <select id="searchType">
                                <option value="title">제목</option>
                            </select>
                            <input id="searchText" name="searchText" type="text" value="${searchText}" placeholder="검색어를 입력하세요">
                            <input type="button" value="검색" name="searchBt" title="검색" class="button" onclick="searchPopup($('#searchText').val(), $('#searchType').val())">
                        </fieldset>
                    </div>
                    <table class="bod_list gosi">
                        <caption>전체 목록을 번호, 제목, 첨부파일, 기간, 작성일, 사용여부, 관리 항목을 순서대로 안내하는 표입니다.</caption>
                        <colgroup>
                            <col style="width: 5%;">
                            <col style="width: auto;">
                            <col style="width: 5%;">
                            <col style="width: 15%;">
                            <col style="width: 10%;">
                            <col style="width: 5%;">
                            <col style="width: 12%;">
                        </colgroup>
                        <thead>
                            <tr>
                                <th scope="col">번호</th>
                                <th scope="col">제목</th>
                                <th scope="col">첨부</th>
                                <th scope="col">기간</th>
                                <th scope="col">작성일</th>
                                <th scope="col">사용여부</th>
                                <th scope="col">관리</th>
                            </tr>
                        </thead>
                        <tbody>
                            <c:forEach var="item" items="${popupList}" varStatus="status">
                                <tr>
                                    <td class="list_num">${item.popupId}</td>
                                    <td class="list_tit">
                                        <a href="/admin/popupDetail/${item.popupId}.do">
                                            ${item.title}
                                        </a>
                                    </td>
                                    <td class="list_file"><img src="/images/contents/ico_file.png" alt="첨부파일"></td>
                                    <td class="list_writer">${item.post_from} ~ ${item.post_to }</td>
                                    <td class="list_date">${item.regDate}</td>
                                    <td class="list_view">
                                        <span id="use_yn_${item.popupId}" class="bullet <c:if test="${item.use_yn == 'Y'}">current</c:if>" onclick="useEditPopup('${item.popupId}')">
                                            <c:if test="${item.use_yn == 'N'}">미</c:if>사용
                                        </span>
                                    </td>
                                    <td class="list_btn">
                                        <div class="innerBtn">
                                            <button type="button" class="button" onclick="deletePopup(${item.popupId }, '${item.title}')">삭제</button>
                                            <button type="button" class="button" onclick="editPopup(${item.popupId })">수정</button>
                                        </div>
                                    </td>
                                </tr>
                            </c:forEach>
                        </tbody>
                    </table>
                </form>
                <div class="btnArea">
                    <button type="button" class="button" onclick="writePopup()">팝업등록</button>
                </div>
                <div class="bod_page">
                    <div class="paging">
                        <c:if test="${pageMaker.prev}">
                            <a href="/admin/popupList/1.do" class="pre_end"><img src="/images/contents/btn_pg_first.gif" alt="처음으로"></a>
                            <a href="/admin/popupList/${pageMaker.startPage - 1}.do" class="pre"><img src="/images/contents/btn_pg_prev.gif" alt="이전 5페이지"></a>
                        </c:if>
                        <c:forEach begin="${pageMaker.startPage}" end="${pageMaker.endPage}" var="idx">
                            <c:choose>
                                <c:when test="${pageMaker.currentPage eq idx}">
                                    <span>${idx}</span>
                                </c:when>
                                <c:otherwise>
                                    <a href="/admin/popupList/${idx}.do">${idx}</a>
                                </c:otherwise>
                            </c:choose>
                        </c:forEach>
                        <c:if test="${pageMaker.next}">
                            <a href="/admin/popupList/${pageMaker.endPage + 1}.do"><img src="/images/contents/btn_pg_next.gif" alt="다음 5페이지"></a>
                            <a href="/admin/popupList/${pageMaker.totalPage}.do" class="next_end"><img src="/images/contents/btn_pg_last.gif" alt="끝으로"></a>
                        </c:if>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!--//contents-->
<script type="text/javascript">

    /**
     * 수정 화면 이동
     * @param popId
     */
    function editPopup(popId) {
        location.href = "/admin/editPopup/" + popId + ".do";
    }

    /**
     * 팝업창 삭제
     * @param popId
     * @param popupTitle
     */
    function deletePopup(popId, popupTitle) {
        let delConfirm = confirm("제목 : " + popupTitle + "\n해당 게시물을 삭제 하시겠습니까?");
        if (delConfirm) {
            $.ajax({
                type : 'post',
                url : '/admin/deletePopup.do',
                data : {popupId : popId},
                success : function(data){
                    if (data && data > 0) {
                        alert('팝업 게시물이 삭제 되었습니다.');
                        location.reload();
                    }
                    else {
                        alert('팝업 게시물이 삭제되지 않았습니다.');
                    }
                },
                error : function(error) {
                    alert('팝업 게시물 삭제중 오류가 발생하였습니다. \n' + error);
                }
            });
        }
    }

    /**
     * 팝업창 작성 화면 이동
     */
    function writePopup() {
        location.href = '/admin/writePopup.do';
    }

    /**
     * 사용여부 변경
     * @param popupId
     */
    function useEditPopup(popupId) {
        const useYnElement = $('#use_yn_' + popupId);
        let word = '사용';
        let useYn = 'Y';
        if (useYnElement.text().length > 0 && useYnElement.text().trim() === '사용') {
            word = '미' + word;
            useYn = 'N';
        }
        const yes = confirm('해당 팝업창을 ' + word + '으로 설정하시겠습니까?');
        if (yes) {
            $.ajax({
                type : 'post',
                url : '/admin/updatePopupUseYN.do',
                data : {id : popupId, useYN : useYn},
                success : function(data){
                    if (data && data > 0) {
                        alert('팝업창 사용여부를 설정하였습니다.');
                        location.reload();
                    }
                    else {
                        alert('팝업창 사용여부가 설정되지 않았습니다.');
                    }
                },
                error : function(req, status, error) {
                    alert('팝업창 사용여부 설정 중 오류가 발생하였습니다.');
                }
            });
        }
    }

    function searchPopup(searchText, searchType) {
        location.href = '/admin/popupList.do?searchText=' +searchText + '&searchType='+searchType;
    }
</script>