<%@ page contentType="text/html; charset=utf-8" pageEncoding="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" %>
<script type="text/javascript" src="<c:url value="/validator.do"/>"></script>

<!-- Center -->
<div id="admin_cont">
  <div class="inner">
    <div class="title">
      <h3>전체 발송 리스트 </h3>
    </div>
		<form id="searchForm" name="searchForm" method="post">
		<input type="hidden" id="pageIndex" name="pageIndex" />
		<input type="hidden" id="noti_group_id" name="noti_group_id" />
		<input type="hidden" id="msg_status" name="msg_status" />
		<input type="hidden" id="firstSet" name="firstSet" value="${srvmnghVO}"/>
		<input type="hidden" id="mber" name="mber" value="${srvmnghVO.search_mber_no}"/>
		<input type="hidden" id="org_id" name="org_id" value="${srvmnghVO.search_org_id}"/>
    	<div class="mt20">
                <div class="board_added_module subtit">
                    <span class="sum_info">기본검색</span>
                </div>

                <table class="list">
                    <colgroup>
                        <col style="width:200px">
                        <col />
                    </colgroup>
                    <tbody>
						<tr>
						<th><label for="mber_search">회원사</label></th>
							<td>
                              	<ul class="inner-td">
                              		<select id="search_mber_no" class="form" name="search_mber_no" onchange="fnSearchMber();">
                              		<option value="all">전체</option>
	                              		<c:forEach items="${rmap}" var="list">
	                              			<option value="${list.mber_no}" ${list.mber_no eq srvmnghVO.search_mber_no ? "selected" : ""}>${list.cmpnm}</option>
	                              		</c:forEach>
                              		</select>
                              	</ul>
                            </td>
						</tr>
						<tr id="admin_tr" style="display:none">
						<th><label for="admin_search">관리자 </label></th>
							<td>
                              	<ul class="inner-td">
                              		<select id="search_org_id" class="form" name="search_org_id" onchange="fnSearchMber();">
                              		<option value="all">전체</option>
                              		<c:forEach items="${amap}" var="alist">
                              			<option value="${alist.org_sid}" ${alist.org_sid eq srvmnghVO.search_org_id ? "selected" : ""}>${alist.usrnm}</option>
                              		</c:forEach>
                              		</select>
                              	</ul>
                            </td>
						</tr>
                        <%-- <tr>
                            <th><label for="search">검색어</label></th>
                            <td>
                                <ul class="inner-td">
                                    <li>
                                        <select id="search_type" class="form" name="search_type">
                                            <option value="sender_id" <c:if test="${srvmnghVO.search_type eq 'sender_id'}">selected="selected"</c:if>>발송ID</option>
                                            <option value="sender_name" <c:if test="${srvmnghVO.search_type eq 'sender_name'}">selected="selected"</c:if>>발송자명</option>
                                            <option value="sender_num" <c:if test="${srvmnghVO.search_type eq 'sender_num'}">selected="selected"</c:if>>발신번호</option>
                                            <option value="080" <c:if test="${srvmnghVO.search_type eq '080'}">selected="selected"</c:if>>080번호</option>
                                            <option value="subject" <c:if test="${srvmnghVO.search_type eq 'subject'}">selected="selected"</c:if>>제목</option>
                                            <option value="content" <c:if test="${srvmnghVO.search_type eq 'content'}">selected="selected"</c:if>>내용</option>
                                        </select>
                                    </li>
                                    <li><input type="text" id="searchText" name="searchText" value="${srvmnghVO.searchText}"/></li>
                                </ul>
                            </td>
                        </tr> --%>
                        <tr>
                            <th>발송구분</th>
                            <td>
                                <label class="mr10" for="SMS"><input type="checkbox" name="type" value="0" id="SMS" <c:if test="${srvmnghVO.type eq '0'}">checked="checked"</c:if>>SMS</label>
                                <label class="mr10" for="LMS"><input type="checkbox" name="type" value="3" id="LMS" <c:if test="${srvmnghVO.type eq '3'}">checked="checked"</c:if>>LMS</label>
                                <label class="mr10" for="MMS"><input type="checkbox" name="type" value="2" id="MMS" <c:if test="${srvmnghVO.type eq '2'}">checked="checked"</c:if>>MMS</label>
                                <label class="mr10" for="alarm"><input type="checkbox" name="type" value="1008" id="alarm" <c:if test="${srvmnghVO.type eq '1008'}">checked="checked"</c:if>>알림톡</label>
                                <label class="mr10" for="text"><input type="checkbox" name="type" value="1009" id="friend" <c:if test="${srvmnghVO.type eq '1009'}">checked="checked"</c:if>>친구톡</label>
                            </td>
                        </tr>
                        <tr>
                            <th><label for="date">발송일자별</label></th>
                            <td>
                                <ul class="inner-td">                                    
                                    <li class="width-150"><div class="formbox"><input type="text" id="searchSdate" name="searchSdate" value="${srvmnghVO.searchSdate}" class="dateis" placeholder="날짜선택" onkeyUp="yyyymmddDate(this);" maxlength="10" onblur="yyyymmddDateBlur(this);" readonly="readonly"/></div></li>
                                    <li>  ~ </li>
                                    <li class="width-150"><div class="formbox"><input type="text" id="searchEdate" name="searchEdate" value="${srvmnghVO.searchEdate}" class="dateis" placeholder="날짜선택" onkeyUp="yyyymmddDate(this);" maxlength="10" onblur="yyyymmddDateBlur(this);" readonly="readonly"/></div></li>
                                    <li>
                                       <button type="button" class="button d s" onclick="javascript:btnToday();">오늘</button>
		                               <button type="button" class="button d s" onclick="javascript:btnYest();">어제</button>
		                               <button type="button" class="button d s" onclick="javascript:btnOneWeek();">일주일</button>
		                               <button type="button" class="button d s" onclick="javascript:btnLastMonth();">1개월</button>
		                               <button type="button" class="button d s" onclick="javascript:btnLast3Month();">3개월</button>
		                               <button type="button" class="button d s" onclick="javascript:btnAll(365);">전체</button>
                                    </li>
                                </ul>
                            </td>
                        </tr>
                    </tbody>
                </table>

                <div class="btn_wrap">
                    <button class="button b" onclick="fnSearch();return false;">검색</button>
                    <button class="button" onclick="settingreset();return false;">초기화</button>
                </div>
            </div>
		
            <div class="board">
                <div class="board_added_module mt30 subtit">
                    <span class="sum_info">전체 : <strong id="search_cnt">${srvmnghCnt}</strong> 건 조회</span>
                </div>
                <ul class="btn_wrap between">
                    <li><!-- <button class="button b b-inverse" onclick="javascript:fnExcelDown1()">Excel다운로드</button> --></li>
                    <li>
                        <ul class="d-flex">
                            <li class="mr10">목록 :</li>
                            <li>
                                <select id="pageUnit" class="form" name="pageUnit" onchange="fnSearch();">
                                	<option value="5" <c:if test="${srvmnghVO.pageUnit eq '5'}">selected="selected"</c:if>>5개</option>
                                    <option value="10" <c:if test="${srvmnghVO.pageUnit eq '10'}">selected="selected"</c:if>>10개</option>
                                    <option value="20" <c:if test="${srvmnghVO.pageUnit eq '20'}">selected="selected"</c:if>>20개</option>
                                </select>
                            </li>
                        </ul>
                    </li>
                </ul>
                <table class="list fix tb-border tb-center">
                    <caption>총 회원수</caption>
                    <colgroup>
                        <col style="width:50px">
                        <col />
                    </colgroup>
                    <thead>
                        <tr>
                            <th scope="col"></th>
                            <th style="width:15%;" scope="col">발송ID</th>
                            <th scope="col">구분</th>
                            <th style="width:15%;" scope="col">제목</th>
                            <th scope="col">전체 등록</th>
                            <!-- <th scope="col">수신 거부</th> -->
                            <th scope="col" class="text-blue">결과 정상</th>
                            <th scope="col" class="text-red">결과 실패</th>
                            <th scope="col">발송자</th>
                            <th scope="col">발신 번호<br>080수신거부</th>
                            <th>상태</th>
                            <th>발송일시</th>
                        </tr>
                    </thead>
                    <tbody class="tb">
                    	<c:if test="${fn:length(srvmnghList) == 0}">
							<tr>
								<td colspan="11" class="tc"> <spring:message	code="common.nodata.msg" /></td>
							</tr>
						</c:if>
                    	<c:forEach items="${srvmnghList}" var="list">
	                        <tr>
	                            <td><input type="checkbox" name="cancelBtn" value="${list.noti_group_id},${list.msg_status}"></td>
	                            <td>${list.noti_group_id eq null ? "":list.noti_group_id}</td>
	                            <c:choose>
	                            	<c:when test="${list.service_type eq 0}">
	                            		<td>SMS</td>
	                            	</c:when>
	                            	<c:when test="${list.service_type eq 2}">
	                            		<td>MMS</td>
	                            	</c:when>
	                            	<c:when test="${list.service_type eq 3}">
	                            		<td>LMS</td>
	                            	</c:when>
	                            	<c:when test="${list.msg_type eq 1008}">
	                            		<td>알림톡</td>
	                            	</c:when>
	                            	<c:when test="${list.msg_type eq 1009}">
	                            		<td>친구톡</td>
	                            	</c:when>
	                            	<c:otherwise>
	                            		<td>${list.msg_type}</td>
	                            	</c:otherwise>
	                            </c:choose>
	                            <td>${list.subject eq null ? "":list.subject}</td>
	                            <td>${list.all_cnt eq null ? "0":list.all_cnt}</td>
	                            <%-- <td>${list.block_sender_cnt eq null ? "0":list.all_cnt}</td> --%>
	                            <td class="text-blue">${list.success_cnt}</td>
	                            <td class="text-red">${list.fail_cnt eq null ? "0":list.all_cnt - list.success_cnt}</td>
	                            <td>${list.cmpnm eq null ? "":list.cmpnm}<br>${list.charger eq null ? "":list.charger}</td>
	                            <td>${list.callback eq null ? "":list.callback}<br>${list.ars_080_num eq null ? "":list.ars_080_num}</td>
	                            <c:choose>
	                            	<c:when test="${list.msg_status eq 'Y'}">
	                            		<td class="text-blue">발송대기</td>
	                            	</c:when>
	                            	<c:when test="${list.msg_status eq 'N'}">
	                            		<td class="text-blue">발송취소</td>
	                            	</c:when>
	                            	<c:when test="${list.msg_status eq 'S'}">
	                            		<td class="text-blue">발송완료</td>
	                            	</c:when>
	                            	<c:otherwise>
	                            		<td class="text-blue">발송대기</td>
	                            	</c:otherwise>
	                            </c:choose>
	                            <td><c:out value='${fn:substring(list.send_de, 0, 10) }' escapeXml="false" /><br>
                               		<c:out value='${fn:substring(list.send_de, 11, 16) }' escapeXml="false" /><br>
                                </td>
	                        </tr>
                        </c:forEach>
                    </tbody>
                </table>
            </div>
		    <ul class="btn_wrap between">
                <li><button class="button c" onclick="cancelSend();return false;">발송취소</button></li>
            </ul>
        <!-- //table -->
		</form>
	        <!-- 페이징 -->
	        <div class="board-paging">
	            <ui:pagination paginationInfo="${paginationInfo}" type="image" jsFunction="fnSearchNavi" />
	        </div>
	</div><!-- // admin inner -->
</div><!-- //admin cont -->

<script type="text/javascript">

	$(document).ready(function(){

		event();
	});

	function event(){
		$('input[type="checkbox"][name="type"]').click(function(){
			if($(this).prop('checked')){

				$('input[type="checkbox"][name="type"]').prop('checked',false);
				$(this).prop('checked',true);	
				
			}
		});

		$('input[type="checkbox"][name="cancelBtn"]').click(function(){
			if($(this).prop('checked')){

				$('input[type="checkbox"][name="cancelBtn"]').prop('checked',false);
				$(this).prop('checked',true);	
				var click = $(this).val();
				var subStr = click.split(',');
				$('#noti_group_id').val(subStr[0]);
				$('#msg_status').val(subStr[1]);
			}
		});

		buttonSet();
	}

	function buttonSet(){
		var mber_no = $('#mber').val();
		var dispaly = $('#admin_tr').css('display');
		
		if("all" == mber_no || null == mber_no || "" == mber_no){
			if("none" != dispaly){
				$('#admin_tr').css("display","none");
			}
		}else {
			$('#admin_tr').css("display","block");
			$('#admin_tr').attr("style", "");	
		}
	}

	function settingreset(){		
 		$("#searchForm")[0].reset();
		$("#admin_tr").val("all");
		$("#admin_tr").hide();
		$("#search_mber_no").val("all");
/* 		$("input[name=type]").prop("checked", false);
		$("#searchSdate").val("");
		$("#searchEdate").val(""); */
	}

	function fnSearch() {
		var searchForm = document.getElementById("searchForm");
		
		var pageIndex = document.getElementById("pageIndex");
		if(pageIndex.value == "") {
			pageIndex.value = "1";
		}	

		var mber_no = $('#search_mber_no').val();
		
		if("all" == mber_no){
			$('#search_mber_no').val("");
			$('#search_org_id').val("");
			$('#search_mber_no option').remove();
			$('#search_org_id option').remove();
		}
		
		searchForm.target = "_self";
		searchForm.action = "<c:url value='/admin/srvmngh/send/selectSendList.do'/>";
		searchForm.submit();
	}

	/**
	 * navigation 검색
	 */
	function fnSearchNavi(pageNo) {
		var pageIndex = document.getElementById("pageIndex");
		pageIndex.value = pageNo;
		
		fnSearch();	
	}

	function fnExcelDown1() {
		var searchForm = document.getElementById("searchForm");

		var pageIndex = document.getElementById("pageIndex");
		if(pageIndex.value == "") {
			pageIndex.value = "1";
		}	
		
		searchForm.target = "_self";
		searchForm.action = "<c:url value='/admin/srvmngh/send/selectSendListExcel.do'/>";
		searchForm.submit();
	
	}

	function cancelSend(){
		var noti_group_id = $('#noti_group_id').val();
		var msg_status = $('#msg_status').val();

		var pageIndex = document.getElementById("pageIndex");
		if(pageIndex.value == "") {
			pageIndex.value = "1";
		}
		
		if($('input:checkbox[name="cancelBtn"]').is(":checked") != true){
			alert("발송취소할 목록을 선택해주세요");
			return;
		}
		if('Y' != msg_status){
			alert("발송대기 목록만을 선택해주세요");
			return;
		}

		var searchForm = document.getElementById("searchForm");

		searchForm.target = "_self";
		searchForm.action = "<c:url value='/admin/srvmngh/send/cancelSend.do'/>";
		searchForm.submit();
	}

	function fnSearchMber(){
		var mber_no = $('#search_mber_no').val();

		if("all" == mber_no){
			settingreset();
		}else{
			fnSearch();
		}
		
	}

</script>
