<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="java.io.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ 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>API 그룹 선택</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <jsp:include page="/jsp/common/include/css_custom.jsp"/>
    <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/apigroup/apiGroupMan.json" />';

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

        $(document).ready(function() {
            var urlParams = new URLSearchParams(window.location.search);
            var selectedGroupIds = urlParams.get("selectedGroupIds");

            if (selectedGroupIds) {
                selectedGroupIds = selectedGroupIds.split(','); // 콤마로 구분된 ID들을 배열로 변환
            } else {
                selectedGroupIds = [];
            }

            $('#grid').jqGrid({
                datatype: "json",
                mtype: 'POST',
                url: url,
                postData: getSearchForJqgrid("cmd", "LIST"),
                colNames: ['API 그룹 ID', 'API 그룹 명', '설명'],
                colModel: [
                    {name: 'id', hidden: true},
                    {name: 'groupName', align: 'center', width: "180"},
                    {name: 'groupDesc', align: 'left', width: "200"}
                ],
                jsonReader: {
                    repeatitems: false
                },
                pager: $('#pager'),
                page: '${param.page}',
                rowNum: '${rmsDefaultRowNum}',
                autoheight: true,
                height: 'auto',
                width: 410,
                autowidth: false,
                viewrecords: true,
                shrinkToFit: false,
                multiselect: true,
                multiboxonly: false,
                loadComplete: function(data) {
                    var $grid = $(this);
                    $.each($grid.getDataIDs(), function(_, id) {
                        var rowData = $grid.getRowData(id);
                        if ($.inArray(rowData.id, selectedGroupIds) !== -1) {
                            $grid.jqGrid('setSelection', id, true);
                        }
                    });
                }
            });

            $("#btn_search").click(search);

            $("#btn_save").click(function() {
                var selectedRows = $('#grid').getGridParam('selarrrow');

                var selectedGroups = [];
                if (selectedRows.length > 0) {
                    selectedGroups = selectedRows.map(function(rowid) {
                        var rowData = $('#grid').getRowData(rowid);
                        return {
                            id: rowData.id,
                            groupName: rowData.groupName
                        };
                    });
                }
                // 부모 창의 selectApiGroup 함수 호출
                if (window.parent && typeof window.parent.selectApiGroup === "function") {
                    window.parent.selectApiGroup(selectedGroups);
                    window.close();
                }
            });

            $("#btn_close").click(function () {
                window.close();
            });

            // 검색어 입력 후 엔터 키 처리
            $("input[name=searchGroupName]").keydown(function(key) {
                if (key.keyCode == 13) {
                    search();
                }
            });
        });
    </script>
</head>
<body>
<div class="popup_box">
    <div class="search_wrap">
        <button type="button" class="cssbtn" id="btn_save" level="W">
            <i class="material-icons">save</i> <%= localeMessage.getString("button.save") %>
        </button>
        <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_close" level="R" status="DETAIL"><i
                class="material-icons">close</i> <%= localeMessage.getString("button.close") %>
        </button>
    </div>
    <div class="title"></div>
    <table class="search_condition" cellspacing="0">
        <tr>
            <th style="width:80px;">API 그룹 명</th>
            <td>
                <input type="text" name="searchGroupName" value="${param.searchGroupName}">
            </td>
        </tr>
    </table>
    <table id="grid"></table>
    <div id="pager"></div>
</div>
</body>
</html>
