@extends('admin.layouts.app')

@section('title', '공지사항')
@section('page_title', '공지사항')
@section('page_desc', '공지사항(상단공지/숨김/첨부파일)을 관리합니다.')
@section('content_class', 'a-content--full')

@push('head')
    <style>
        /* notice index only - match admins */
        .bar{display:flex;justify-content:space-between;align-items:flex-end;gap:12px;flex-wrap:wrap;}
        .bar__left .t{font-weight:900;font-size:16px;}
        .bar__left .d{font-size:12px;margin-top:4px;}
        .bar__right{display:flex;gap:10px;flex-wrap:wrap;align-items:flex-end;}

        .filters{display:flex;gap:8px;flex-wrap:wrap;align-items:flex-end;}
        .filters .q{width:230px;}
        .filters .st{width:150px;}

        .lbtn{padding:8px 12px;font-size:13px;border-radius:12px;line-height:1.1;text-decoration:none;display:inline-flex;align-items:center;justify-content:center;gap:6px;
            border:1px solid rgba(255,255,255,.10);background:rgba(255,255,255,.06);color:inherit;cursor:pointer;}
        .lbtn:hover{background:rgba(255,255,255,.10);text-decoration:none;}
        .lbtn--primary{background:rgba(59,130,246,.88);border-color:rgba(59,130,246,.95);color:#fff;}
        .lbtn--primary:hover{background:rgba(59,130,246,.98);}
        .lbtn--ghost{background:transparent;}
        .lbtn--sm{padding:7px 10px;font-size:12px;border-radius:11px;}
        .lbtn--wide{padding:10px 14px;font-weight:800;}

        .pill{display:inline-flex;align-items:center;gap:6px;padding:6px 10px;border-radius:999px;font-size:12px;
            border:1px solid rgba(255,255,255,.10);background:rgba(255,255,255,.06);}
        .pill--ok{border-color:rgba(34,197,94,.35);background:rgba(34,197,94,.12);}
        .pill--bad{border-color:rgba(244,63,94,.35);background:rgba(244,63,94,.10);}
        .pill--warn{border-color:rgba(245,158,11,.35);background:rgba(245,158,11,.12);}
        .pill--muted{opacity:.9;}

        .mono{padding:4px 8px;border-radius:10px;background:rgba(255,255,255,.06);border:1px solid rgba(255,255,255,.10);
            font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;font-size:12px;}
        .row-disabled{opacity:.70;}
        .table td{vertical-align:top;}
        .clip{max-width:520px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;display:inline-block;vertical-align:bottom;}
    </style>
@endpush

@section('content')
    @php
        // 컨트롤러에서 templates 로 내려오든, page 로 내려오든 호환
        $page = $page ?? ($templates ?? null);
        $filters = $filters ?? [];

        $field = (string)($filters['field'] ?? 'subject');
        $q = (string)($filters['q'] ?? '');
    @endphp

    <div class="a-card" style="padding:16px; margin-bottom:16px;">
        <div class="bar">
            <div class="bar__left">
                <div class="t">공지사항</div>
                <div class="a-muted d">공지사항(상단공지/숨김/첨부파일)을 관리합니다.</div>
            </div>

            <div class="bar__right">
                <a class="lbtn lbtn--primary lbtn--wide"
                   href="{{ route('admin.notice.create', request()->query()) }}">
                    + 공지 등록
                </a>

                <form method="GET" action="{{ route('admin.notice.index') }}" class="filters">
                    <div>
                        <select class="a-input st" name="field">
                            <option value="subject" {{ $field==='subject' ? 'selected' : '' }}>제목</option>
                            <option value="content" {{ $field==='content' ? 'selected' : '' }}>내용</option>
                        </select>
                    </div>

                    <div>
                        <input class="a-input q" name="q" value="{{ $q }}" placeholder="검색어 입력">
                    </div>

                    <div style="display:flex; gap:8px; align-items:flex-end;">
                        <button class="lbtn lbtn--ghost" type="submit">검색</button>
                        <a class="lbtn lbtn--ghost" href="{{ route('admin.notice.index') }}">초기화</a>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <div class="a-card" style="padding:16px;">
        <div class="a-muted" style="margin-bottom:10px;">총 <b>{{ $page?->total() ?? 0 }}</b>건</div>

        <div style="overflow:auto;">
            <table class="a-table table" style="width:100%; min-width:1100px;">
                <thead>
                <tr>
                    <th style="width:80px;">NO</th>
                    <th>제목</th>
                    <th style="width:140px;">상태</th>
                    <th style="width:140px;">공지</th>
                    <th style="width:170px;">등록일시</th>
                    <th style="width:80px;">hit</th>
                    <th style="width:90px;">관리</th>
                </tr>
                </thead>
                <tbody>
                @php
                    $total = $page?->total() ?? 0;
                    $perPage = $page?->perPage() ?? 15;
                    $curPage = $page?->currentPage() ?? 1;
                    $no = $total - (($curPage - 1) * $perPage);
                @endphp

                @forelse($page as $it)
                    @php
                        $isHidden = (($it->hiding ?? 'N') === 'Y');     // Y=숨김
                        $isPinned = ((int)($it->first_sign ?? 0) > 0);  // >0=공지
                    @endphp
                    <tr class="{{ $isHidden ? 'row-disabled' : '' }}">
                        <td class="a-muted">{{ $no }}</td>

                        <td>
                            <div style="display:flex; gap:8px; align-items:center;">
                                @if($isPinned)
                                    <span class="pill pill--ok">● 공지</span>
                                @endif
                                @if($isHidden)
                                    <span class="pill pill--bad">● 숨김</span>
                                @endif
                                <span class="clip">{{ $it->subject ?? '-' }}</span>
                            </div>
                        </td>

                        <td>
                            @if($isHidden)
                                <span class="pill pill--bad">● 숨김</span>
                            @else
                                <span class="pill pill--ok">● 노출</span>
                            @endif
                        </td>

                        <td>
                            @if($isPinned)
                                <span class="pill pill--ok">On</span>
                            @else
                                <span class="pill pill--muted">Off</span>
                            @endif
                        </td>

                        <td class="a-muted">{{ $it->regdate ?? '-' }}</td>
                        <td class="a-muted" style="text-align:center;">{{ $it->hit ?? 0 }}</td>

                        <td style="text-align:right;">
                            <a class="lbtn lbtn--ghost lbtn--sm"
                               href="{{ route('admin.notice.edit', ['id'=>(int)$it->seq] + request()->query()) }}">
                                보기
                            </a>
                        </td>
                    </tr>
                    @php $no--; @endphp
                @empty
                    <tr><td colspan="7" class="a-muted" style="padding:18px;">데이터가 없습니다.</td></tr>
                @endforelse
                </tbody>
            </table>
        </div>

        <div style="margin-top:12px;">
            {{ $templates->onEachSide(1)->links('vendor.pagination.admin') }}
        </div>
    </div>
@endsection
