<%@ 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/portalorg/portalOrgMan.json" />';
var url_view = '<c:url value="/onl/apim/portalorg/portalOrgMan.view" />';
var select_approvalStatus = "";
var select_status = "";
/* var select_orgType = "";  */

function getGridSelectText(key, value, data) {
	if (!Array.isArray(data) || data.length === 0) {
        return "";
    }

    return data
        .filter(item => item[key] !== undefined && item[value] !== undefined)
        .map(item => String(item[key]) + ":" + String(item[value]))
        .join(';');
}

function formatCompanyRegNo(text) {
    return text.replace(/(\d{3})(\d{2})(\d{5})/, "$1-$2-$3");
}

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

			const comboConfigs = [
				{name: 'searchOrgStatus', data: json.orgStatusRows },
				{name: 'searchApprovalStatus', data: json.approvalStatusRows },
			];

			comboConfigs.forEach(function(config)  {
				new makeOptions("CODE","NAME")
						.setObj($("select[name=" + config.name + "]"))
						.setNoValueInclude(true)
						.setNoValue("", "<%=localeMessage.getString("combo.all")%>")
						.setData(config.data)
						.setFormat(nameOptionFormat).rendering();
			});

			putSelectFromParam();

			select_approvalStatus  = ": ;" + getGridSelectText("CODE", "NAME",	json.approvalStatusRows);
			select_status = ": ;" + getGridSelectText("CODE", "NAME",	json.orgStatusRows);

			initializeGrid();
		},
		error:function(e){
			alert(e.responseText);
		}
	});
}

function initializeGrid() {
	var gridPostData = getSearchForJqgrid("cmd","LIST"); //jqgrid에서는 object 로

	$('#grid').jqGrid({
		datatype:"json",
		mtype: 'POST',
		url: url,
		postData : gridPostData,
		colNames:['No.',
                  "id",
                  '<%= localeMessage.getString("portalOrg.orgName") %>',
                  '<%= localeMessage.getString("portalOrg.orgCode") %>',
                  '<%= localeMessage.getString("portalOrg.compRegNo") %>',
                  '<%= localeMessage.getString("portalUser.createOn") %>',
                  '<%= localeMessage.getString("portalOrg.orgStatus") %>',
                  '<%= localeMessage.getString("portalUser.approvalStatus") %>'
                  ],
		colModel:[
				{ name : 'rowNum'   	 , align:'center'	, width:'30', sortable:false  },
				{ name : 'id'  			 , align:'center'	, key:true, hidden:true},
				{ name : 'orgName'		 , align:'center'	, width:"80"},
				{ name : 'orgCode'		 , align:'center'	, width:"80"},
				{ name : 'compRegNo' 	 , align:'center' 	, formatter:formatCompanyRegNo },
				{ name : 'createdDate'	 , align:'center'	, width:"90", formatter: timeStampFormat },
				{ name : 'orgStatus'   	 , align:'center'	, width:'60',editoptions: { value: select_status }, formatter: "select"},
				{ name : 'approvalStatus' , align:'center'	, width:"60", edittype: 'select' , editoptions: { value: select_approvalStatus }, formatter: "select"}
			],
        jsonReader: {
             repeatitems:false
        },
		pager : $('#pager'),
		page : '${param.page}',
		rowNum : '${rmsDefaultRowNum}',
	    autoheight: true,
	    height: $("#container").height(),
		autowidth: true,
		viewrecords: true,
		rowList : eval('[${rmsDefaultRowList}]'),
		loadComplete:function (d){
			var records = $(this).getGridParam('records');
			var page = $(this).getGridParam('page');
			var rowNum = $(this).getGridParam('rowNum');
			var rows = $(this).getDataIDs();
			var colModel = $(this).getGridParam("colModel");

			for (var i = 0; i < rows.length; i++) {
				var number = ((page - 1) * rowNum + i) + 1;
				// var reverseNumber = records - ((page - 1) * rowNum + i);
				$(this).setCell(rows[i], 'rowNum', number);
			}
			for(var i = 0 ; i< colModel.length; i++){
				$(this).setColProp(colModel[i].name, {sortable : false});
			}
		},
		ondblClickRow: function(rowId) {
			var rowData = $(this).getRowData(rowId);
		    var key = rowData['id'];

			var args = {};
			var url2 = url_view;
			url2 += '?cmd=DETAIL';
			url2 += '&page='+$(this).getGridParam("page");
			url2 += '&returnUrl='+getReturnUrl();
			url2 += '&menuId='+'${param.menuId}';
		    url2 += '&id=' + key;
			goNav(url2);
        },loadError: function (jqXHR, textStatus, errorThrown) {
        		if(jqXHR.status == 401) {
	        		var location ='<%=request.getContextPath()%>/';
		        	comloadError(jqXHR, textStatus, errorThrown ,location);
		    	}
    	}
	});

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


$(document).ready(function() {
	$("input[name=searchStartYYYYMMDD],input[name=searchEndYYYYMMDD]").inputmask("yyyy-mm-dd",{'autoUnmask':true});

	init();

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

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

	$("#startDatepicker").datepicker();
	$("#endDatepicker").datepicker();

	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_new" level="W"><i class="material-icons">add</i> <%= localeMessage.getString("button.new") %></button>
			<button type="button" class="cssbtn" id="btn_search" level="R"><i class="material-icons">search</i> <%= localeMessage.getString("button.search") %></button>
		</div>
		  <%--법인정보관리--%>
		<div class="title"><%= localeMessage.getString("portalOrg.title") %></div>

		<table class="search_condition" cellspacing=0;>
			<tbody>
				<tr>
					<th style="width:10%;"><%= localeMessage.getString("portalOrg.orgName") %></th>
					<td style="width:15%;">
						<input type="text" name="searchOrgName" value="${param.searchOrgName}">
					</td>

					<th style="width:10%;"><%= localeMessage.getString("portalOrg.compRegNo") %></th>
					<td style="width:15%;">
						<input type="text" name="searchOrgCompRegNo" value="${param.searchOrgCompRegNo}">
					</td>

					<th  style="width:10%;"><%= localeMessage.getString("portalOrg.orgStatus") %></th>
					<td style="width:15%;">
						<div class="select-style">
							<select type="text" name="searchOrgStatus" value="${param.searchOrgStatus}"></select>
						</div>
					</td>

					<th style="width:10%;"><%= localeMessage.getString("portalUser.approvalStatus") %></th>
					<td style="width:15%;">
						<div class="select-style">
							<select type="text" name="searchApprovalStatus" value="${param.searchApprovalStatus}"></select>
						</div>
					</td>
				</tr>
			</tbody>
		</table>

		<table id="grid" ></table>
		<div id="pager"></div>
	  </div><!-- end content_middle -->
	</div><!-- end right_box -->
  </body>
</html>

