<?php $CURRENT_SECTION = 'permission';
include '../inc/header.php'; ?>

    <div class="container" style="max-width:1280px; margin-top:24px;">
        <div class="row g-3">
            <div class="col-md-4">
                <div class="card shadow-sm">
                    <div class="card-header">메뉴 트리</div>
                    <div class="card-body" style="max-height:60vh; overflow:auto;">
                        <div class="mb-2 d-flex gap-2">
                            <input type="text" class="form-control" id="menuSearch" placeholder="메뉴명/코드 검색">
                            <button class="btn btn-outline-secondary" id="btnMenuSearch">검색</button>
                        </div>
                        <ul id="menuTree" class="list-group small">
                            <!-- 샘플 -->
                            <li class="list-group-item">
                                <a href="#" class="menu-node" data-id="1" data-code="dashboard">대시보드</a>
                            </li>
                            <li class="list-group-item">
                                <a href="#" class="menu-node" data-id="2" data-code="order">수주</a>
                                <ul class="mt-1">
                                    <li><a href="#" class="menu-node" data-id="21" data-code="order.manage">수주관리</a></li>
                                    <li><a href="#" class="menu-node" data-id="22" data-code="order.edit">수주 등록/수정</a></li>
                                </ul>
                            </li>
                            <!-- ... -->
                        </ul>
                    </div>
                    <div class="card-footer d-flex gap-2">
                        <button class="btn btn-sm btn-outline-primary" id="btnAddChild">하위메뉴 추가</button>
                        <button class="btn btn-sm btn-outline-danger" id="btnDeleteMenu">삭제</button>
                    </div>
                </div>
            </div>

            <div class="col-md-8">
                <div class="card shadow-sm">
                    <div class="card-header">메뉴 상세/권한 항목</div>
                    <div class="card-body">
                        <form id="menuForm">
                            <div class="row g-3">
                                <div class="col-md-6">
                                    <label class="form-label">메뉴명</label>
                                    <input type="text" class="form-control" name="title" required>
                                </div>
                                <div class="col-md-6">
                                    <label class="form-label">코드</label>
                                    <input type="text" class="form-control" name="code" required>
                                </div>
                                <div class="col-md-8">
                                    <label class="form-label">경로(Path)</label>
                                    <input type="text" class="form-control" name="path" placeholder="/tenant/...">
                                </div>
                                <div class="col-md-4">
                                    <label class="form-label">정렬</label>
                                    <input type="number" class="form-control" name="sort_order" value="10">
                                </div>
                                <div class="col-md-4">
                                    <div class="form-check mt-4">
                                        <input class="form-check-input" type="checkbox" name="is_active" id="is_active" checked>
                                        <label class="form-check-label" for="is_active">활성화</label>
                                    </div>
                                </div>
                            </div>

                            <hr>
                            <div>
                                <label class="form-label">권한 항목</label>
                                <div class="d-flex flex-wrap gap-3">
                                    <div class="form-check"><input class="form-check-input" type="checkbox" name="perm_read" id="perm_read"><label class="form-check-label" for="perm_read">읽기</label></div>
                                    <div class="form-check"><input class="form-check-input" type="checkbox" name="perm_create" id="perm_create"><label class="form-check-label" for="perm_create">쓰기</label></div>
                                    <div class="form-check"><input class="form-check-input" type="checkbox" name="perm_update" id="perm_update"><label class="form-check-label" for="perm_update">수정</label></div>
                                    <div class="form-check"><input class="form-check-input" type="checkbox" name="perm_delete" id="perm_delete"><label class="form-check-label" for="perm_delete">삭제</label></div>
                                    <div class="form-check"><input class="form-check-input" type="checkbox" name="perm_approve" id="perm_approve"><label class="form-check-label" for="perm_approve">결재</label></div>
                                </div>
                            </div>

                            <div class="mt-3 d-flex gap-2">
                                <button class="btn btn-primary" type="submit">저장</button>
                                <button class="btn btn-outline-secondary" type="button" id="btnReset">초기화</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        $(function(){
            // 트리에서 노드 선택 → 상세 로드(샘플)
            $(document).on('click','.menu-node',function(e){
                e.preventDefault();
                $('.menu-node').removeClass('fw-bold');
                $(this).addClass('fw-bold');
                const code = $(this).data('code');
                // 샘플 바인딩
                $('#menuForm [name=title]').val($(this).text().trim());
                $('#menuForm [name=code]').val(code);
                $('#menuForm [name=path]').val('/tenant/'+code.replaceAll('.','/')+'.php');
            });

            $('#menuForm').on('submit', function(e){
                e.preventDefault();
                alert('저장(샘플) — 실제는 /api/permission/structure_save.php 로 POST');
            });

            $('#btnAddChild').on('click', ()=>alert('하위메뉴 추가(샘플)'));
            $('#btnDeleteMenu').on('click', ()=>confirm('삭제하시겠습니까?') && alert('삭제(샘플)'));
            $('#btnReset').on('click', ()=>$('#menuForm')[0].reset());
        });
    </script>

<?php include '../inc/footer.php'; ?>
