{{-- resources/views/admin/qna/index.blade.php --}}

@extends('admin.layouts.app')

@section('title', '1:1 문의')
@section('page_title', '1:1 문의')
@section('page_desc', '접수/분배/처리/보류/완료 상태를 관리합니다.')
@section('content_class', 'a-content--full')

@push('head')
    <style>
        .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 .w120{width:120px;}
        .filters .w160{width:160px;}
        .filters .w220{width:220px;}

        .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--ghost{background:transparent;}
        .lbtn--sm{padding:7px 10px;font-size:12px;border-radius:11px;}
        .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);}

        .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;}
        .td-muted{opacity:.85;font-size:12px;}
        .clip{max-width:520px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;display:inline-block;vertical-align:bottom;}
        .row-mine{background:rgba(59,130,246,.06);}
        .table td{vertical-align:top;}

    </style>
@endpush

@section('content')
    @php
        $filters = $filters ?? [];
        $curYear = (int)date('Y');
        $selectedYear = (int)($filters['year'] ?? $year ?? $curYear);
        if ($selectedYear < 2018 || $selectedYear > $curYear) $selectedYear = $curYear;

        $qType = (string)($filters['q_type'] ?? 'title');
        $q = (string)($filters['q'] ?? '');
        $enquiryCode = (string)($filters['enquiry_code'] ?? '');
        $state = (string)($filters['state'] ?? '');
        $dateFrom = (string)($filters['date_from'] ?? '');
        $dateTo = (string)($filters['date_to'] ?? '');
        $myWork = !empty($filters['my_work']);

        $meAdminId = (int) auth('admin')->id();

        $total = (is_object($rows ?? null) && method_exists($rows, 'total')) ? (int)$rows->total() : (int)($rows?->count() ?? 0);
        $perPage = (int)($rows?->perPage() ?? 20);
        $curPage = (int)($rows?->currentPage() ?? 1);
        $no = $total > 0 ? ($total - (($curPage - 1) * $perPage)) : 0;
    @endphp

    <div class="a-card" style="padding:16px; margin-bottom:16px;">
        <div class="bar">
            <div class="bar__left">
                <div class="t">1:1 문의</div>
                <div class="a-muted d">접수/분배/처리/보류/완료 상태를 관리합니다.</div>
            </div>

            <div class="bar__right">
                <form method="GET" action="{{ route('admin.qna.index') }}" class="filters">
                    <select class="a-input w120" name="year">
                        @for($y = $curYear; $y >= 2018; $y--)
                            <option value="{{ $y }}" {{ $selectedYear === $y ? 'selected' : '' }}>{{ $y }}</option>
                        @endfor
                    </select>

                    <select class="a-input w160" name="enquiry_code">
                        <option value="">접수분류(전체)</option>
                        @foreach($categories as $c)
                            <option value="{{ $c['num'] }}" @selected((string)$enquiryCode === (string)$c['num'])>
                                {{ $c['label'] }}
                            </option>
                        @endforeach
                    </select>

                    <select class="a-input w160" name="state">
                        <option value="">처리상태(전체)</option>
                        @foreach($stateLabels as $k=>$v)
                            <option value="{{ $k }}" @selected((string)$state === (string)$k)>{{ $v[0] }}</option>
                        @endforeach
                    </select>

                    <select class="a-input w160" name="q_type">
                        <option value="title" @selected($qType==='title')>제목</option>
                        <option value="member_no" @selected($qType==='member_no')>회원번호</option>
                        <option value="member_email" @selected($qType==='member_email')>회원이메일</option>
                        <option value="admin_email" @selected($qType==='admin_email')>관리자이메일</option>
                    </select>

                    <input class="a-input w220" name="q" value="{{ $q }}" placeholder="검색어">

                    <input class="a-input w160" type="date" name="date_from" value="{{ $dateFrom }}">
                    <input class="a-input w160" type="date" name="date_to" value="{{ $dateTo }}">

                    <label class="a-check" style="margin:0;">
                        <input type="checkbox" name="my_work" value="1" {{ $myWork ? 'checked':'' }}>
                        <span>내업무</span>
                    </label>

                    <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.qna.index') }}">초기화</a>
                    </div>
                </form>
            </div>
        </div>
    </div>

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

        <div style="overflow:auto;">
            <table class="a-table table" style="width:100%; min-width:1300px;">
                <thead>
                <tr>
                    <th style="width:80px;">NO</th>
                    <th style="width:110px;">SEQ</th>
                    <th style="width:140px;">상태</th>
                    <th style="width:160px;">분류</th>
                    <th>제목</th>
                    <th style="width:120px;">회신</th>
                    <th style="width:120px;">회원</th>
                    <th style="width:180px;">등록일시</th>
                    <th style="width:130px;">배정자</th>
                    <th style="width:90px;">관리</th>
                </tr>
                </thead>
                <tbody>
                @forelse($rows as $r)
                    @php
                        $st = (string)($r->state ?? '');
                        $stLabel = $stateLabels[$st][0] ?? ($st ?: '-');
                        $stCls = $stateLabels[$st][1] ?? 'pill--muted';

                        $rt = (string)($r->return_type ?? '');
                        $rtLabel = in_array($rt, ['phone','sms'], true) ? 'SMS' : ($rt === 'email' ? 'MAIL' : '-');
                        $rtCls = $rtLabel === 'SMS' ? 'pill--warn' : ($rtLabel === 'MAIL' ? 'pill--ok' : 'pill--muted');

                        $catLabel = $categoriesMap[(int)($r->enquiry_code ?? 0)]['label'] ?? ((string)($r->enquiry_code ?? '-') ?: '-');

                        $assignedAdmin = (int)($r->answer_admin_num ?? 0);
                        $isMine = ($assignedAdmin > 0 && $assignedAdmin === $meAdminId);

                        $showUrl = route('admin.qna.show', ['seq'=>(int)$r->seq, 'year'=>$selectedYear] + request()->query());
                    @endphp

                    <tr class="{{ $isMine ? 'row-mine' : '' }}">
                        <td class="a-muted">{{ $no > 0 ? $no : '-' }}</td>
                        <td><span class="mono">#{{ (int)$r->seq }}</span></td>
                        <td><span class="pill {{ $stCls }}">● {{ $stLabel }}</span></td>
                        <td class="td-muted">{{ $catLabel }}</td>
                        <td>
                            <a href="{{ $showUrl }}" style="text-decoration:none; color:inherit;">
                                <div style="display:flex; gap:8px; align-items:center;">
                                    <span class="clip" style="font-weight:900;">{{ $r->enquiry_title ?? '-' }}</span>
                                </div>
                            </a>
                        </td>
                        <td><span class="pill {{ $rtCls }}">{{ $rtLabel }}</span></td>
                        <td class="td-muted"><span class="mono">{{ (int)($r->member_num ?? 0) }}</span></td>
                        <td class="td-muted">{{ $r->regdate ?? '-' }}</td>
                        <td class="td-muted">
                            @if($assignedAdmin > 0)
                                <span class="mono">{{ $assignedAdmin }}</span>
                                @if($isMine)
                                    <span class="pill pill--ok" style="margin-left:6px;">ME</span>
                                @endif
                            @else
                                -
                            @endif
                        </td>
                        <td style="text-align:right;">
                            <a class="lbtn lbtn--ghost lbtn--sm" href="{{ $showUrl }}">보기</a>
                        </td>
                    </tr>

                    @php if ($no > 0) $no--; @endphp
                @empty
                    {{-- ✅ 컬럼 수(10) 맞춰서 UI 깨짐 방지 --}}
                    <tr><td colspan="10" class="a-muted" style="padding:16px;">데이터가 없습니다.</td></tr>
                @endforelse
                </tbody>
            </table>
        </div>

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