<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="java.io.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ include file="/jsp/common/include/localemessage.jsp" %>
<%
	response.setHeader("Pragma", "No-cache");
	response.setHeader("Cache-Control", "no-cache");
	response.setHeader("Expires", "0");
%>
<html>
<head>
	<title></title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<jsp:include page="/jsp/common/include/css.jsp"/>
	<jsp:include page="/jsp/common/include/script.jsp"/>
	<script language="javascript" >
		var url      = '<c:url value="/onl/apim/portalinquiry/portalInquiryMan.json" />';
		var url_view = '<c:url value="/onl/apim/portalinquiry/portalInquiryMan.view" />';
		var select_orgId = "";

		function formatInquiryStatus(cellvalue, options, rowObject) {
			if (cellvalue == 'PENDING') {
				return '<span style="color: red;">문의중</span>';
			} else if (cellvalue == 'REVIEWING') {
				return '<span >문의검토중</span>'
			} else if (cellvalue == 'RESPONDED') {
				return '<span >답변완료</span>'
			} else if (cellvalue == 'CLOSED') {
				return '<span >답변종료</span>'
			} else {
				return cellvalue;	
			}
		}

		function formatVisibility(cellvalue, options, rowObject) {
			if (cellvalue == 'PRIVATE') {
				return '<span style="color:#c0392b; font-weight:bold;">비공개</span>';
			} else if (cellvalue == 'ALL') {
				return '전체공개';
			} else if (cellvalue == 'ORG') {
				return '법인공개';
			}
			return cellvalue;
		}

		function formatFile(cellvalue, options, rowObject) {
			var iconPath = '${pageContext.request.contextPath}/images/icon_file.png';
			var icon = rowObject.hasAttachment ? '<img src="' + iconPath + '" alt="File Attached" style="vertical-align: middle; margin-left: 5px; width: 16px; height: 16px;">' : '';
			var commentCount = rowObject.commentCount > 0 ? ' <span style="color: #337ab7;">[' + rowObject.commentCount + ']</span>' : '';
			return cellvalue + icon + commentCount;
		}

		function deleteSelectedRows() {
			var selectedIds = $("#grid").jqGrid('getGridParam', 'selarrrow');

			if (selectedIds.length === 0) {
				alert('항목을 선택하세요.');
				return;
			}

			if (!confirm('선택된 ' + selectedIds.length + '개 항목을 삭제하시겠습니까?')) {
				return;
			}

			$.ajax({
				type: "POST",
				url: url,
				dataType: "json",
				data: {
					cmd: 'DELETE_BULK',
					ids: selectedIds.join(',')
				},
				success: function(data) {
					alert('삭제되었습니다.');
					$("#grid").jqGrid('clearGridData', true);
					$("#grid").trigger("reloadGrid");
				},
				error: function(e) {
					alert('삭제 중 오류가 발생했습니다.');
				}
			});
		}

		function init(){
			$.ajax({
				type : "POST",
				url:url,
				dataType:"json",
				data:{cmd: 'LIST_INIT_COMBO'},
				success:function(json){

					new makeOptions("CODE","NAME")
							.setObj($("select[name=searchOrgId]"))
							.setNoValueInclude(true)
							.setNoValue("", "<%=localeMessage.getString("combo.all")%>")
							.setData(json.orgRows)
							.setFormat(nameOptionFormat).rendering();

					putSelectFromParam();
					select_orgId = ": ;" + getGridSelectText("CODE", "NAME", json.orgRows);
				},
				error:function(e){
					alert(e.responseText);
				}
			});
		}

		$(document).ready(function() {
			$('#grid').jqGrid({
				datatype:"json",
				mtype: 'POST',
				url: url,
				postData : { cmd : 'LIST' },
				colNames:['No.', 'id', '제목', '법인명', '상태', '공개범위', '작성자', '등록일', '답변자','답변일'],
				colModel:[
					{ name : 'rowNum'           , align:'center', width:50, sortable:false },
					{ name : 'id'               , align:'center', key:true, hidden:true},
					{ name : 'inquirySubject'   , align:'left'  , width:200 , formatter: formatFile},
					{ name : 'orgName'   		, align:'center'  , width:100 , },
					{ name : 'inquiryStatus'    , align:'center', width:70, formatter: formatInquiryStatus },
					{ name : 'visibility'       , align:'center', width:70, formatter: formatVisibility },
					{ name : 'inquirer.userName', align:'center', width:70 },
					{ name : 'createdDate'      , align:'center', width:100, formatter: timeStampFormat },
					{ name : 'responderName'    , align:'center', width:70 },
					{ name : 'responseDate'     , align:'center', width:100, formatter: timeStampFormat }
				],
				jsonReader: {
					repeatitems:false
				},
				pager : $('#pager'),
				page : '${param.page}',
				rowNum : '${rmsDefaultRowNum}',
				autoheight: true,
				height: $("#container").height(),
				autowidth: true,
				viewrecords: true,
				rowList : eval('[${rmsDefaultRowList}]'),
				multiselect: true,
				multiboxonly: true,
				loadComplete: function() {
					var records = $(this).getGridParam('records');
					var page = $(this).getGridParam('page');
					var rowNum = $(this).getGridParam('rowNum');
					var rows = $(this).getDataIDs();

					for (var i = 0; i < rows.length; i++) {
						var number = ((page - 1) * rowNum + i) + 1;
						$(this).setCell(rows[i], 'rowNum', number);
					}
				},
				ondblClickRow: function(rowId) {
					var rowData = $(this).getRowData(rowId);
					var id = rowData['id'];
					var url2 = url_view;
					url2 += '?cmd=DETAIL';
					url2 += '&page='+$(this).getGridParam("page");
					url2 += '&returnUrl='+getReturnUrl();
					url2 += '&menuId='+'${param.menuId}';
					url2 += '&id='+id;
					goNav(url2);
				},
				loadError: function (jqXHR, textStatus, errorThrown) {
					var location ='<%=request.getContextPath()%>/';
					comloadError(jqXHR, textStatus, errorThrown ,location);
				}
			});

			init();

			resizeJqGridWidth('grid','content_middle','1000');

			$("#btn_search").click(function(){
				var postData = getSearchForJqgrid("cmd","LIST");
				$("#grid").setGridParam({ url:url,postData: postData ,page:1 }).trigger("reloadGrid");
			});

			$("#btn_new").click(function(){
				var url2 = url_view;
				url2 += '?cmd=DETAIL';
				url2 += '&page='+$("#grid").getGridParam("page");
				url2 += '&returnUrl='+getReturnUrl();
				url2 += '&menuId='+'${param.menuId}';
				goNav(url2);
			});

			$("#btn_delete_selected").click(function(){
				deleteSelectedRows();
			});

			$("input[name^=search]").keydown(function(key){
				if (key.keyCode == 13){
					$("#btn_search").click();
				}
			});

			buttonControl();
		});
	</script>
</head>
<body>
<div class="right_box">
	<div class="content_top">
		<ul class="path">
			<li><a href="#">${rmsMenuPath}</a></li>
		</ul>
	</div><!-- end content_top -->
	<div class="content_middle" id="content_middle">
		<div class="search_wrap">
			<button type="button" class="cssbtn" id="btn_search" level="R"><i class="material-icons">search</i> <%= localeMessage.getString("button.search") %></button>
			<button type="button" class="cssbtn" id="btn_delete_selected" level="W" style="background-color: #dc3545; border-color: #dc3545; color: white;"><i class="material-icons">delete_forever</i> 선택 삭제</button>
		</div>
		<div class="title">Q&A 목록<span class="tooltip">Q&A를 관리합니다.</span></div>
		<form id="ajaxForm"  onsubmit="return false;">
			<table class="search_condition" cellspacing=0;>
				<tbody>
				<tr>
					<th style="width:150px;">답변여부</th>
					<td style="width:150px;">
						<div class="select-style">
							<select name="searchInquiryStatus">
								<option value="">전체</option>
								<option value="PENDING">문의중</option>
								<option value="REVIEWING">문의검토중</option>
								<option value="RESPONDED">답변완료</option>
								<option value="CLOSED">답변종료</option>
							</select>
						</div>
					</td>
					<th style="width:150px;">법인명</th>
					<td style="width:250px;">
						<div class="select-style">
							<select name="searchOrgId"></select>
						</div>
					</td>
					<th style="width:150px;">작성자</th>
					<td style="width:150px;"><input type="text" name="searchInquiryUserName"></td>
				</tr>
				<tr>
					<th>제목/내용</th>
					<td colspan="5"><input type="text" name="searchSubjectDetail"></td>
				</tr>
				</tbody>
			</table>
		</form>
		<table id="grid" ></table>
		<div id="pager"></div>

	</div><!-- end content_middle -->
</div><!-- end right_box -->
</body>
</html>
