@extends('admin.layouts.app')

@section('title', '메일 이력 상세')
@section('page_title', '메일 이력 상세')
@section('page_desc', '배치 및 수신자별 상세')

@push('head')
    <style>
        .hgrid{display:flex; gap:16px; flex-wrap:wrap;}
        .hbox{min-width:160px;}
        .hbox .k{font-size:12px; opacity:.8;}
        .hbox .v{font-weight:800; margin-top:4px;}
        .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;}
        .btn{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;}
        .btn:hover{background:rgba(255,255,255,.10);}
        .btn--primary{background:rgba(59,130,246,.88);border-color:rgba(59,130,246,.95);color:#fff;}
        .btn--danger{background:rgba(244,63,94,.88);border-color:rgba(244,63,94,.95);color:#fff;}
    </style>
@endpush

@section('content')
    @php
        $statusLabel = $labels ?? [];
        $modeLabel   = $modeLabels ?? [];

        $sent = (int)($batch->sent_count ?? 0);
        $total = (int)($batch->valid_count ?? $batch->total_count ?? 0);

    @endphp

    <div class="a-card" style="padding:16px; margin-bottom:16px;">
        <div class="hgrid">
            <div class="hbox">
                <div class="k a-muted">Batch ID</div>
                <div class="v">#{{ $batch->id }}</div>
            </div>
            <div class="hbox">
                <div class="k a-muted">상태</div>
                <div class="v">{{ $statusLabel[(string)$batch->status] ?? $batch->status }}</div>
            </div>
            <div class="hbox">
                <div class="k a-muted">모드</div>
                <div class="v">{{ $modeLabel[(string)$batch->send_mode] ?? $batch->send_mode }}</div>
            </div>
            <div class="hbox">
                <div class="k a-muted">예약</div>
                <div class="v">{{ $batch->scheduled_at ?? '-' }}</div>
            </div>
            <div class="hbox">
                <div class="k a-muted">진행률</div>
                <div class="v"><b>{{ $sent }}</b>/{{ $total }}</div>
            </div>
        </div>

        <hr style="margin:14px 0; opacity:.15;">

        <div class="a-muted" style="margin-bottom:6px;">제목</div>
        <div class="mono" style="display:inline-block;">{{ $batch->subject_raw ?? '-' }}</div>

        <div style="height:10px;"></div>

        <div class="a-muted" style="margin-bottom:6px;">내용</div>
        <div style="white-space:pre-wrap; background:rgba(255,255,255,.03); padding:12px; border-radius:12px;">
            {{ $batch->body_raw ?? $batch->body ?? '' }}
        </div>

        <div style="margin-top:12px; display:flex; gap:8px; flex-wrap:wrap;">
            <a class="btn" href="{{ route('admin.mail.logs') }}">목록</a>
            <a class="btn" href="{{ route('admin.mail.send') }}">발송</a>

            @if(in_array((string)$batch->status, ['queued','submitting','scheduled'], true))
                <form method="POST" action="{{ route('admin.mail.logs.cancel', ['batchId'=>$batch->id]) }}" data-confirm="이 배치를 취소할까요?" style="display:inline;">
                    @csrf
                    <button class="btn btn--danger" type="submit">취소/중지</button>
                </form>
            @endif

            @if(in_array((string)$batch->status, ['failed','partial'], true))
                <form method="POST" action="{{ route('admin.mail.logs.retry_failed', ['batchId'=>$batch->id]) }}" data-confirm="실패 건만 재시도할까요?" style="display:inline;">
                    @csrf
                    <button class="btn btn--primary" type="submit">실패 재시도</button>
                </form>
            @endif
        </div>
    </div>

    <div class="a-card" style="padding:16px;">
        <form method="GET" action="{{ route('admin.mail.logs.show', ['batchId'=>$batch->id]) }}"
              style="display:flex; gap:10px; flex-wrap:wrap; align-items:end; margin-bottom:12px;">
            <div style="min-width:140px;">
                <div class="a-muted" style="margin-bottom:6px;">상태</div>
                <select class="a-input" name="status" style="width:140px;">
                    <option value="">전체</option>
                    @foreach(['queued'=>'대기','submitted'=>'완료','failed'=>'실패'] as $k=>$v)
                        <option value="{{ $k }}" @selected(request('status')===$k)>{{ $v }}</option>
                    @endforeach
                </select>
            </div>
            <div style="min-width:240px;">
                <div class="a-muted" style="margin-bottom:6px;">수신자</div>
                <input class="a-input" name="to" value="{{ request('to') }}" placeholder="user@example.com" style="width:240px;">
            </div>
            <div style="flex:1; min-width:240px;">
                <div class="a-muted" style="margin-bottom:6px;">검색(제목/내용)</div>
                <input class="a-input" name="q" value="{{ request('q') }}" placeholder="치환된 내용 검색" style="max-width:360px;">
            </div>
            <button class="btn btn--primary" type="submit">필터</button>
        </form>

        <div style="overflow:auto;">
            <table class="a-table" style="width:100%; min-width:1100px;">
                <thead>
                <tr>
                    <th style="width:80px;">Seq</th>
                    <th style="width:260px;">수신자</th>
                    <th style="width:120px;">상태</th>
                    <th style="width:170px;">제출시간</th>
                    <th>제목</th>
                    <th>요약</th>
                </tr>
                </thead>
                <tbody>
                @forelse($items as $it)
                    <tr>
                        <td class="a-muted">{{ $it->seq }}</td>
                        <td><span class="mono">{{ $it->to_email }}</span></td>
                        <td>{{ $statusLabel[(string)$it->status] ?? $it->status }}</td>
                        <td class="a-muted">{{ $it->sent_at ?? '-' }}</td>
                        <td style="max-width:420px; white-space:nowrap; overflow:hidden; text-overflow:ellipsis;">{{ $it->subject_final ?? $it->subject ?? '-' }}</td>
                        <td style="max-width:520px; white-space:nowrap; overflow:hidden; text-overflow:ellipsis;">{{ $it->body_final ?? '-' }}</td>
                    </tr>
                @empty
                    <tr><td colspan="6" class="a-muted">상세 데이터가 없습니다.</td></tr>
                @endforelse
                </tbody>
            </table>
        </div>
        <div style="margin-top:12px;">
            {{ $items->onEachSide(1)->links('vendor.pagination.admin') }}
        </div>
    </div>
@endsection
