@extends('layouts.app')

@section('title', ($board->tenant?->company_name ? $board->tenant->company_name . ' ' : '') . $board->name)

@section('content')
<!-- 페이지 헤더 -->
<div class="flex justify-between items-center mb-6">
    <div>
        <h1 class="text-2xl font-bold text-gray-800">
            @if($board->tenant?->company_name)
                <span class="text-blue-600">{{ $board->tenant->company_name }}</span>
            @endif
            {{ $board->name }}
        </h1>
        @if($board->description)
            <p class="text-sm text-gray-500 mt-1">{{ $board->description }}</p>
        @endif
    </div>
    <div class="flex items-center gap-3">
        <a href="{{ route('boards.posts.create', ['boardCode' => $board->board_code, 't' => $board->tenant_id]) }}"
           class="px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-lg text-sm font-medium transition flex items-center gap-2">
            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/>
            </svg>
            글쓰기
        </a>
        <a href="{{ route('boards.index') }}"
           class="px-4 py-2 bg-gray-100 hover:bg-gray-200 text-gray-700 rounded-lg text-sm font-medium transition flex items-center gap-2">
            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 10h16M4 14h16M4 18h16"/>
            </svg>
            게시판 목록
        </a>
    </div>
</div>

<!-- 통계 카드 -->
<div class="grid grid-cols-2 md:grid-cols-4 {{ isset($stats['trashed']) ? 'lg:grid-cols-5' : '' }} gap-4 mb-6">
    <div class="bg-white rounded-lg shadow-sm p-4">
        <div class="text-sm text-gray-500">전체 게시글</div>
        <div class="text-2xl font-bold text-gray-900">{{ number_format($stats['total']) }}</div>
    </div>
    <div class="bg-white rounded-lg shadow-sm p-4">
        <div class="text-sm text-gray-500">공지사항</div>
        <div class="text-2xl font-bold text-blue-600">{{ number_format($stats['notices']) }}</div>
    </div>
    <div class="bg-white rounded-lg shadow-sm p-4">
        <div class="text-sm text-gray-500">오늘 작성</div>
        <div class="text-2xl font-bold text-green-600">{{ number_format($stats['today']) }}</div>
    </div>
    <div class="bg-white rounded-lg shadow-sm p-4">
        <div class="text-sm text-gray-500">게시 중</div>
        <div class="text-2xl font-bold text-gray-600">{{ number_format($stats['published']) }}</div>
    </div>
    @if(isset($stats['trashed']))
    <div class="bg-white rounded-lg shadow-sm p-4 border-l-4 border-red-500">
        <div class="text-sm text-gray-500">삭제됨</div>
        <div class="text-2xl font-bold text-red-600">{{ number_format($stats['trashed']) }}</div>
    </div>
    @endif
</div>

<!-- 검색 -->
<div class="bg-white rounded-lg shadow-sm p-4 mb-6">
    <form action="{{ route('boards.posts.index', ['boardCode' => $board->board_code, 't' => $board->tenant_id]) }}" method="GET" class="flex items-center gap-4">
        <input type="hidden" name="t" value="{{ $board->tenant_id }}">
        <div class="flex-1">
            <input type="text" name="search" value="{{ $filters['search'] ?? '' }}"
                   placeholder="제목, 내용 검색..."
                   class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
        </div>
        <button type="submit" class="px-4 py-2 bg-gray-100 hover:bg-gray-200 text-gray-700 rounded-lg transition">
            검색
        </button>
        @if(!empty($filters['search']))
            <a href="{{ route('boards.posts.index', ['boardCode' => $board->board_code, 't' => $board->tenant_id]) }}" class="px-4 py-2 text-gray-500 hover:text-gray-700">
                초기화
            </a>
        @endif
    </form>
</div>

<!-- 게시글 목록 -->
<div class="bg-white rounded-lg shadow-sm overflow-hidden">
    <table class="min-w-full divide-y divide-gray-200">
        <thead class="bg-gray-50">
            <tr>
                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider w-16">번호</th>
                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">제목</th>
                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider w-32">작성자</th>
                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider w-32">작성일</th>
                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider w-20">조회</th>
                @if($isSuperAdmin ?? false)
                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider w-32">관리</th>
                @endif
            </tr>
        </thead>
        <tbody class="bg-white divide-y divide-gray-200">
            @forelse($posts as $post)
                <tr class="hover:bg-gray-50 {{ $post->is_notice ? 'bg-blue-50' : '' }} {{ $post->trashed() ? 'bg-red-50 opacity-75' : '' }}">
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                        @if($post->trashed())
                            <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-red-100 text-red-800">
                                삭제
                            </span>
                        @elseif($post->is_notice)
                            <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-blue-100 text-blue-800">
                                공지
                            </span>
                        @else
                            {{ $post->id }}
                        @endif
                    </td>
                    <td class="px-6 py-4">
                        @if($post->trashed())
                            <span class="text-gray-400 line-through">{{ $post->title }}</span>
                            <span class="text-xs text-red-500 ml-2">({{ $post->deleted_at->format('Y-m-d') }} 삭제)</span>
                        @else
                            <a href="{{ route('boards.posts.show', ['boardCode' => $board->board_code, 'post' => $post, 't' => $board->tenant_id]) }}"
                               class="text-gray-900 hover:text-blue-600 font-medium">
                                @if($post->is_secret)
                                    <svg class="w-4 h-4 inline-block text-gray-400 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z"/>
                                    </svg>
                                @endif
                                {{ $post->title }}
                            </a>
                            @if($post->files_count > 0)
                                <span class="inline-flex items-center ml-2 text-gray-400" title="첨부파일 {{ $post->files_count }}개">
                                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15.172 7l-6.586 6.586a2 2 0 102.828 2.828l6.414-6.586a4 4 0 00-5.656-5.656l-6.415 6.585a6 6 0 108.486 8.486L20.5 13"/>
                                    </svg>
                                    <span class="text-xs ml-0.5">{{ $post->files_count }}</span>
                                </span>
                            @endif
                            @if($post->comments_count > 0)
                                <span class="inline-flex items-center ml-2 text-blue-500" title="댓글 {{ $post->comments_count }}개">
                                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z"/>
                                    </svg>
                                    <span class="text-xs ml-0.5">{{ $post->comments_count }}</span>
                                </span>
                            @endif
                        @endif
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                        {{ $post->author?->name ?? '알 수 없음' }}
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                        {{ $post->created_at->format('Y-m-d') }}
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                        {{ number_format($post->views) }}
                    </td>
                    @if($isSuperAdmin ?? false)
                    <td class="px-6 py-4 whitespace-nowrap text-sm">
                        @if($post->trashed())
                            <div class="flex items-center gap-2">
                                <form action="{{ route('boards.posts.restore', ['boardCode' => $board->board_code, 'post' => $post->id, 't' => $board->tenant_id]) }}" method="POST" class="inline">
                                    @csrf
                                    <button type="submit" class="text-green-600 hover:text-green-800 text-xs font-medium">
                                        복원
                                    </button>
                                </form>
                                <form action="{{ route('boards.posts.forceDestroy', ['boardCode' => $board->board_code, 'post' => $post->id, 't' => $board->tenant_id]) }}" method="POST" class="inline" onsubmit="return confirm('정말 영구 삭제하시겠습니까? 이 작업은 되돌릴 수 없습니다.')">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="text-red-600 hover:text-red-800 text-xs font-medium">
                                        영구삭제
                                    </button>
                                </form>
                            </div>
                        @else
                            <span class="text-gray-400 text-xs">-</span>
                        @endif
                    </td>
                    @endif
                </tr>
            @empty
                <tr>
                    <td colspan="5" class="px-6 py-12 text-center text-gray-500">
                        <svg class="w-12 h-12 mx-auto text-gray-300 mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
                        </svg>
                        <p>게시글이 없습니다.</p>
                        <a href="{{ route('boards.posts.create', ['boardCode' => $board->board_code, 't' => $board->tenant_id]) }}" class="mt-2 inline-block text-blue-600 hover:text-blue-800">
                            첫 게시글 작성하기
                        </a>
                    </td>
                </tr>
            @endforelse
        </tbody>
    </table>

    <!-- 페이지네이션 -->
    @if($posts->hasPages())
        <div class="px-6 py-4 border-t border-gray-200">
            {{ $posts->withQueryString()->links() }}
        </div>
    @endif
</div>

<!-- 알림 메시지 -->
@if(session('success'))
    <div class="fixed bottom-4 right-4 bg-green-500 text-white px-6 py-3 rounded-lg shadow-lg" id="toast">
        {{ session('success') }}
    </div>
    <script>setTimeout(() => document.getElementById('toast')?.remove(), 3000);</script>
@endif

@if(session('error'))
    <div class="fixed bottom-4 right-4 bg-red-500 text-white px-6 py-3 rounded-lg shadow-lg" id="toast-error">
        {{ session('error') }}
    </div>
    <script>setTimeout(() => document.getElementById('toast-error')?.remove(), 3000);</script>
@endif
@endsection