@extends('layouts.app')

@section('title', '트리거 관리')

@section('content')
<!-- 페이지 헤더 -->
<div class="flex justify-between items-center mb-6">
    <h1 class="text-2xl font-bold text-gray-800">트리거 관리</h1>
</div>

@include('trigger-audit.partials.sub-nav')

<!-- 플래시 메시지 -->
@if(session('success'))
<div class="mb-4 p-4 bg-green-50 border border-green-200 text-green-700 rounded-lg">{{ session('success') }}</div>
@endif
@if(session('error'))
<div class="mb-4 p-4 bg-red-50 border border-red-200 text-red-700 rounded-lg">{{ session('error') }}</div>
@endif

<!-- 통계 카드 -->
<div class="grid grid-cols-1 md:grid-cols-4 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-800">{{ $data['total_tables'] }}</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">{{ $data['tracked_tables'] }}</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-400">{{ $data['excluded_tables'] }}</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">{{ $data['active_triggers'] }}</div>
    </div>
</div>

<!-- 벌크 액션 -->
<div class="bg-white rounded-lg shadow-sm p-4 mb-6">
    <div class="flex items-center justify-between">
        <div>
            <h3 class="text-sm font-semibold text-gray-700">전체 트리거 관리</h3>
            <p class="text-xs text-gray-500 mt-1">모든 추적 대상 테이블의 트리거를 일괄 처리합니다.</p>
        </div>
        <div class="flex gap-2">
            <form method="POST" action="{{ route('trigger-audit.triggers.regenerate') }}"
                  onsubmit="return confirm('전체 테이블의 트리거를 재생성합니다. 계속하시겠습니까?')">
                @csrf
                <button type="submit" class="bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded-lg text-sm">
                    전체 재생성
                </button>
            </form>
            <form method="POST" action="{{ route('trigger-audit.triggers.drop') }}"
                  onsubmit="return confirm('⚠️ 전체 테이블의 트리거를 삭제합니다. 삭제 후 감사 로그가 기록되지 않습니다. 계속하시겠습니까?')">
                @csrf
                <button type="submit" class="bg-red-600 hover:bg-red-700 text-white px-4 py-2 rounded-lg text-sm">
                    전체 삭제
                </button>
            </form>
        </div>
    </div>
</div>

<!-- 테이블 목록 -->
<div class="bg-white rounded-lg shadow-sm overflow-hidden mb-6">
    <table class="w-full text-sm">
        <thead class="bg-gray-50 text-gray-600">
            <tr>
                <th class="px-4 py-3 text-left">테이블명</th>
                <th class="px-4 py-3 text-center">PK</th>
                <th class="px-4 py-3 text-center">컬럼 수</th>
                <th class="px-4 py-3 text-center">AI</th>
                <th class="px-4 py-3 text-center">AU</th>
                <th class="px-4 py-3 text-center">AD</th>
                <th class="px-4 py-3 text-center">액션</th>
            </tr>
        </thead>
        <tbody class="divide-y divide-gray-100">
            @foreach($data['table_status'] as $table)
            <tr class="hover:bg-gray-50">
                <td class="px-4 py-3 font-mono text-xs">{{ $table['table_name'] }}</td>
                <td class="px-4 py-3 text-center text-xs text-gray-500">{{ $table['pk_column'] }}</td>
                <td class="px-4 py-3 text-center text-xs text-gray-500">{{ $table['column_count'] }}</td>
                <td class="px-4 py-3 text-center">
                    <span class="inline-block w-3 h-3 rounded-full {{ $table['has_insert'] ? 'bg-green-500' : 'bg-gray-300' }}"
                          title="AFTER INSERT {{ $table['has_insert'] ? '활성' : '비활성' }}"></span>
                </td>
                <td class="px-4 py-3 text-center">
                    <span class="inline-block w-3 h-3 rounded-full {{ $table['has_update'] ? 'bg-green-500' : 'bg-gray-300' }}"
                          title="AFTER UPDATE {{ $table['has_update'] ? '활성' : '비활성' }}"></span>
                </td>
                <td class="px-4 py-3 text-center">
                    <span class="inline-block w-3 h-3 rounded-full {{ $table['has_delete'] ? 'bg-green-500' : 'bg-gray-300' }}"
                          title="AFTER DELETE {{ $table['has_delete'] ? '활성' : '비활성' }}"></span>
                </td>
                <td class="px-4 py-3 text-center">
                    <div class="flex gap-1 justify-center">
                        <form method="POST" action="{{ route('trigger-audit.triggers.regenerate') }}" class="inline"
                              onsubmit="return confirm('{{ $table['table_name'] }} 테이블의 트리거를 재생성합니다.')">
                            @csrf
                            <input type="hidden" name="table_name" value="{{ $table['table_name'] }}">
                            <button type="submit" class="text-blue-600 hover:text-blue-800 text-xs px-2 py-1 rounded hover:bg-blue-50">재생성</button>
                        </form>
                        <form method="POST" action="{{ route('trigger-audit.triggers.drop') }}" class="inline"
                              onsubmit="return confirm('{{ $table['table_name'] }} 테이블의 트리거를 삭제합니다.')">
                            @csrf
                            <input type="hidden" name="table_name" value="{{ $table['table_name'] }}">
                            <button type="submit" class="text-red-600 hover:text-red-800 text-xs px-2 py-1 rounded hover:bg-red-50">삭제</button>
                        </form>
                    </div>
                </td>
            </tr>
            @endforeach
        </tbody>
    </table>
</div>

<!-- 제외 테이블 정보 -->
<div class="bg-gray-50 rounded-lg p-4">
    <h3 class="text-sm font-semibold text-gray-600 mb-2">제외 테이블 (트리거 생성 안 함)</h3>
    <div class="flex flex-wrap gap-2">
        @foreach($data['exclude_tables'] as $table)
        <span class="inline-flex items-center px-2 py-1 rounded text-xs bg-gray-200 text-gray-600 font-mono">{{ $table }}</span>
        @endforeach
    </div>
</div>
@endsection
