@extends('layouts.app')

@section('title', '삭제된 데이터 상세')

@section('content')
    <!-- 페이지 헤더 -->
    <div class="flex justify-between items-center mb-6">
        <div class="flex items-center gap-4">
            <a href="{{ route('archived-records.index') }}"
               class="text-gray-500 hover:text-gray-700 transition">
                <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7" />
                </svg>
            </a>
            <h1 class="text-2xl font-bold text-gray-800">삭제된 데이터 상세</h1>
        </div>
        @if(auth()->user()?->is_super_admin)
        <a href="{{ route('archived-records.restore-check', $batchInfo['batch_id']) }}"
           class="inline-flex items-center px-4 py-2 bg-green-600 hover:bg-green-700 text-white font-medium text-sm rounded-lg transition">
            <svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" />
            </svg>
            데이터 복원
        </a>
        @endif
    </div>

    <!-- 알림 메시지 -->
    @if(session('success'))
    <div class="mb-6 p-4 bg-green-50 border border-green-200 rounded-lg">
        <div class="flex items-center">
            <svg class="w-5 h-5 text-green-600 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
            </svg>
            <span class="text-green-800">{{ session('success') }}</span>
        </div>
    </div>
    @endif

    @if(session('error'))
    <div class="mb-6 p-4 bg-red-50 border border-red-200 rounded-lg">
        <div class="flex items-center">
            <svg class="w-5 h-5 text-red-600 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
            </svg>
            <span class="text-red-800">{{ session('error') }}</span>
        </div>
    </div>
    @endif

    <!-- 작업 요약 정보 -->
    <div class="bg-white rounded-lg shadow-sm mb-6">
        <div class="px-6 py-4 border-b border-gray-200">
            <h2 class="text-lg font-semibold text-gray-800">작업 요약</h2>
        </div>
        <div class="p-6">
            <dl class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
                <div>
                    <dt class="text-sm font-medium text-gray-500">작업 설명</dt>
                    <dd class="mt-1 text-sm text-gray-900 font-medium">{{ $batchInfo['batch_description'] ?? '삭제 작업' }}</dd>
                </div>
                <div>
                    <dt class="text-sm font-medium text-gray-500">삭제된 레코드 수</dt>
                    <dd class="mt-1">
                        <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-purple-100 text-purple-800">
                            {{ $batchInfo['record_count'] }}건
                        </span>
                    </dd>
                </div>
                <div>
                    <dt class="text-sm font-medium text-gray-500">삭제자</dt>
                    <dd class="mt-1 text-sm text-gray-900">{{ $batchInfo['deleted_by'] }}</dd>
                </div>
                <div>
                    <dt class="text-sm font-medium text-gray-500">삭제일시</dt>
                    <dd class="mt-1 text-sm text-gray-900">{{ $batchInfo['deleted_at']?->format('Y-m-d H:i:s') ?? '-' }}</dd>
                </div>
            </dl>
        </div>
    </div>

    <!-- 삭제된 레코드 목록 -->
    @foreach($records as $index => $record)
    <div class="bg-white rounded-lg shadow-sm mb-6">
        <div class="px-6 py-4 border-b border-gray-200 flex items-center justify-between">
            <div class="flex items-center gap-3">
                <span class="inline-flex items-center justify-center w-8 h-8 rounded-full bg-gray-100 text-gray-600 text-sm font-medium">
                    {{ $index + 1 }}
                </span>
                <h2 class="text-lg font-semibold text-gray-800">{{ $record->record_type_label }}</h2>
                <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium {{ $record->record_type === 'tenant' ? 'bg-blue-100 text-blue-800' : 'bg-green-100 text-green-800' }}">
                    {{ $record->record_type }}
                </span>
            </div>
            <span class="text-sm text-gray-500">원본 ID: {{ $record->original_id }}</span>
        </div>

        <!-- 메인 데이터 -->
        <div class="p-6 border-b border-gray-200">
            <h3 class="text-sm font-semibold text-gray-700 mb-3">메인 데이터</h3>
            @if($record->main_data)
                @php
                    $mainData = is_array($record->main_data) ? $record->main_data : json_decode($record->main_data, true);
                @endphp
                <div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4 mb-4">
                    @foreach(array_slice($mainData ?? [], 0, 8) as $key => $value)
                        <div class="bg-gray-50 p-3 rounded">
                            <dt class="text-xs font-medium text-gray-500">{{ $key }}</dt>
                            <dd class="mt-1 text-sm text-gray-900 truncate" title="{{ is_array($value) ? json_encode($value) : $value }}">
                                @if(is_array($value))
                                    <span class="text-gray-400">[배열]</span>
                                @elseif(is_null($value))
                                    <span class="text-gray-400">null</span>
                                @else
                                    {{ Str::limit((string)$value, 50) }}
                                @endif
                            </dd>
                        </div>
                    @endforeach
                </div>
                <details class="mt-2">
                    <summary class="text-sm text-blue-600 cursor-pointer hover:text-blue-800">전체 데이터 보기</summary>
                    <pre class="mt-2 bg-gray-900 text-green-400 p-4 rounded-lg overflow-x-auto text-sm font-mono max-h-64">{{ json_encode($mainData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) }}</pre>
                </details>
            @else
                <p class="text-gray-500">메인 데이터가 없습니다.</p>
            @endif
        </div>

        <!-- 관련 테이블 데이터 -->
        @if($record->relations->isNotEmpty())
        <div class="p-6">
            <h3 class="text-sm font-semibold text-gray-700 mb-3">관련 테이블 데이터 ({{ $record->relations->count() }}개)</h3>
            <div class="space-y-4">
                @foreach($record->relations as $relation)
                <details class="border border-gray-200 rounded-lg">
                    <summary class="px-4 py-3 bg-gray-50 cursor-pointer hover:bg-gray-100 flex items-center justify-between">
                        <div class="flex items-center gap-2">
                            <span class="font-medium text-gray-900">{{ $relation->table_name_label }}</span>
                            <span class="text-xs text-gray-500">({{ $relation->table_name }})</span>
                        </div>
                        <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-gray-200 text-gray-800">
                            {{ $relation->record_count }}건
                        </span>
                    </summary>
                    <div class="p-4 border-t border-gray-200">
                        @if($relation->data)
                            <pre class="bg-gray-900 text-green-400 p-4 rounded-lg overflow-x-auto text-sm font-mono max-h-64">{{ json_encode(is_array($relation->data) ? $relation->data : json_decode($relation->data, true), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) }}</pre>
                        @else
                            <p class="text-gray-500">데이터가 없습니다.</p>
                        @endif
                    </div>
                </details>
                @endforeach
            </div>
        </div>
        @endif

        <!-- 노트 -->
        @if($record->notes)
        <div class="px-6 py-4 bg-yellow-50 border-t border-yellow-200">
            <div class="flex items-start gap-2">
                <svg class="w-5 h-5 text-yellow-600 mt-0.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 8h10M7 12h4m1 8l-4-4H5a2 2 0 01-2-2V6a2 2 0 012-2h14a2 2 0 012 2v8a2 2 0 01-2 2h-3l-4 4z" />
                </svg>
                <div>
                    <span class="text-sm font-medium text-yellow-800">노트</span>
                    <p class="text-sm text-yellow-700 mt-1">{{ $record->notes }}</p>
                </div>
            </div>
        </div>
        @endif
    </div>
    @endforeach
@endsection
