<?php
require_once(dirname(__DIR__) . '/init.php');

// Güvenli session başlat
m_secure_session_start();

// Admin kontrolü
if (!m_admin_check()) {
    m_redirect(ADMIN_URL . '/login');
}

// Session güvenlik kontrolü
m_validate_session_security();

// Güvenlik dashboard verilerini al
$security_data = m_get_security_dashboard();
$security_score = $security_data['system_security_score'] ?? 100;
$active_alerts = $security_data['active_alerts'] ?? 0;
$total_events = $security_data['total_security_events_today'] ?? 0;
$failed_logins = $security_data['failed_login_attempts_today'] ?? 0;
$blocked_ips = $security_data['blocked_ips_today'] ?? 0;

// Test fonksiyonları
if (isset($_GET['action'])) {
    $action = $_GET['action'];
    
    switch ($action) {
        case 'security_scan':
            $scan_results = m_perform_security_scan();
            $scan_message = "Güvenlik taraması tamamlandı!";
            break;
            
        case 'create_alert':
            $result = m_create_security_alert('test_alert', 'medium', [
                'test' => 'data',
                'timestamp' => time()
            ]);
            $alert_message = $result ? "Test alarmı oluşturuldu!" : "Alarm oluşturulamadı!";
            break;
            
        case 'generate_report':
            $report = m_generate_security_report('daily');
            $report_message = "Günlük rapor oluşturuldu!";
            break;
            
        case 'update_stats':
            m_update_security_stats('test_event');
            $stats_message = "İstatistikler güncellendi!";
            break;
            
        case 'mark_alert_read':
            try {
                $db = new DB();
                $last_alert = $db->table('security_alerts')
                    ->where('1', '=', '1')
                    ->get();
                
                if (!empty($last_alert['data'])) {
                    usort($last_alert['data'], function($a, $b) {
                        return $b['id'] - $a['id'];
                    });
                    $last_alert['data'] = array_slice($last_alert['data'], 0, 1);
                    
                    if (!empty($last_alert['data'])) {
                        $result = m_mark_alert_as_read($last_alert['data'][0]['id']);
                        $read_message = $result ? "Alarm okundu olarak işaretlendi!" : "İşlem başarısız!";
                    }
                }
            } catch (Exception $e) {
                $read_message = "Hata: " . $e->getMessage();
            }
            break;
    }
}
?>

<?php require_once('template/header.php'); ?>

<!-- Font Awesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">

<style>
    .security-card {
        transition: all 0.3s ease;
        border: none;
        box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075);
    }
    .security-card:hover {
        transform: translateY(-2px);
        box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);
    }
    .security-score {
        font-size: 3rem;
        font-weight: bold;
    }
    .security-metric {
        text-align: center;
        padding: 1.5rem;
    }
    .security-metric .icon {
        font-size: 2.5rem;
        margin-bottom: 1rem;
    }
    .event-item {
        border-left: 4px solid #dee2e6;
        padding: 0.75rem;
        margin-bottom: 0.5rem;
        background: #f8f9fa;
        border-radius: 0.25rem;
    }
    .event-item.success { border-left-color: #28a745; }
    .event-item.warning { border-left-color: #ffc107; }
    .event-item.danger { border-left-color: #dc3545; }
    .event-item.info { border-left-color: #17a2b8; }
    .alert-item {
        border: 1px solid #dee2e6;
        border-radius: 0.5rem;
        padding: 1rem;
        margin-bottom: 0.75rem;
        background: white;
    }
    .alert-item.high { border-left: 4px solid #dc3545; }
    .alert-item.medium { border-left: 4px solid #ffc107; }
    .alert-item.low { border-left: 4px solid #28a745; }
</style>

                    <!-- start page title -->
                    <div class="row">
                        <div class="col-12">
                            <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                                <h4 class="mb-sm-0 font-size-18">
                                    <i class="fas fa-shield-alt text-success me-2"></i>
                                    <?= lang('security_dashboard', [], 'Güvenlik Paneli') ?>
                                </h4>
                                <div class="page-title-right">
                                    <ol class="breadcrumb m-0">
                                        <li class="breadcrumb-item"><a href="<?= ADMIN_URL ?>"><?= lang('dashboard', [], 'Dashboard') ?></a></li>
                                        <li class="breadcrumb-item active"><?= lang('security_dashboard', [], 'Güvenlik Paneli') ?></li>
                                    </ol>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!-- end page title -->

                    <!-- Mesajlar -->
                    <?php if (isset($scan_message)): ?>
                        <div class="alert alert-success alert-dismissible fade show" role="alert">
                            <i class="fas fa-check-circle me-2"></i><?= $scan_message ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    <?php endif; ?>
                    
                    <?php if (isset($alert_message)): ?>
                        <div class="alert alert-info alert-dismissible fade show" role="alert">
                            <i class="fas fa-bell me-2"></i><?= $alert_message ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    <?php endif; ?>
                    
                    <?php if (isset($report_message)): ?>
                        <div class="alert alert-success alert-dismissible fade show" role="alert">
                            <i class="fas fa-file-alt me-2"></i><?= $report_message ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    <?php endif; ?>
                    
                    <?php if (isset($stats_message)): ?>
                        <div class="alert alert-info alert-dismissible fade show" role="alert">
                            <i class="fas fa-chart-bar me-2"></i><?= $stats_message ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    <?php endif; ?>
                    
                    <?php if (isset($read_message)): ?>
                        <div class="alert alert-warning alert-dismissible fade show" role="alert">
                            <i class="fas fa-eye me-2"></i><?= $read_message ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    <?php endif; ?>

                    <!-- Güvenlik Skoru ve Genel Durum -->
                    <div class="row">
                        <div class="col-xl-4">
                            <div class="card security-card">
                                <div class="card-body">
                                    <div class="text-center">
                                        <div class="security-score text-<?= $security_score >= 80 ? 'success' : ($security_score >= 60 ? 'warning' : 'danger') ?>">
                                            <?= $security_score ?>
                                        </div>
                                        <h5 class="mb-3"><?= lang('security_score', [], 'Güvenlik Skoru') ?></h5>
                                        <div class="progress mb-3" style="height: 8px;">
                                            <div class="progress-bar bg-<?= $security_score >= 80 ? 'success' : ($security_score >= 60 ? 'warning' : 'danger') ?>" 
                                                 role="progressbar" style="width: <?= $security_score ?>%"></div>
                                        </div>
                                        <p class="text-muted mb-0">
                                            <?= $security_score >= 80 ? 'Mükemmel güvenlik durumu' : ($security_score >= 60 ? 'İyi güvenlik durumu' : 'Dikkat gerektiren durum') ?>
                                        </p>
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <div class="col-xl-8">
                            <div class="row">
                                <div class="col-md-4">
                                    <div class="card security-card">
                                        <div class="card-body security-metric">
                                            <div class="icon text-info">
                                                <i class="fas fa-calendar-day"></i>
                                            </div>
                                            <h3 class="mb-1"><?= $total_events ?></h3>
                                            <p class="text-muted mb-0"><?= lang('security_events_today', [], 'Bugünkü Olaylar') ?></p>
                                        </div>
                                    </div>
                                </div>
                                
                                <div class="col-md-4">
                                    <div class="card security-card">
                                        <div class="card-body security-metric">
                                            <div class="icon text-warning">
                                                <i class="fas fa-times-circle"></i>
                                            </div>
                                            <h3 class="mb-1"><?= $failed_logins ?></h3>
                                            <p class="text-muted mb-0"><?= lang('failed_login_attempts', [], 'Başarısız Girişler') ?></p>
                                        </div>
                                    </div>
                                </div>
                                
                                <div class="col-md-4">
                                    <div class="card security-card">
                                        <div class="card-body security-metric">
                                            <div class="icon text-danger">
                                                <i class="fas fa-ban"></i>
                                            </div>
                                            <h3 class="mb-1"><?= $blocked_ips ?></h3>
                                            <p class="text-muted mb-0"><?= lang('blocked_ips', [], 'Bloklu IP\'ler') ?></p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Hızlı İşlemler -->
                    <div class="row">
                        <div class="col-12">
                            <div class="card security-card">
                                <div class="card-header">
                                    <h4 class="card-title mb-0">
                                        <i class="fas fa-tools me-2"></i>
                                        Hızlı İşlemler
                                    </h4>
                                </div>
                                <div class="card-body">
                                    <div class="row">
                                        <div class="col-md-2 col-sm-4 mb-3">
                                            <a href="?action=security_scan" class="btn btn-outline-primary w-100">
                                                <i class="fas fa-search me-2"></i>Güvenlik Taraması
                                            </a>
                                        </div>
                                        <div class="col-md-2 col-sm-4 mb-3">
                                            <a href="?action=create_alert" class="btn btn-outline-info w-100">
                                                <i class="fas fa-bell me-2"></i>Test Alarmı
                                            </a>
                                        </div>
                                        <div class="col-md-2 col-sm-4 mb-3">
                                            <a href="?action=generate_report" class="btn btn-outline-success w-100">
                                                <i class="fas fa-file-alt me-2"></i>Rapor Oluştur
                                            </a>
                                        </div>
                                        <div class="col-md-2 col-sm-4 mb-3">
                                            <a href="?action=update_stats" class="btn btn-outline-warning w-100">
                                                <i class="fas fa-chart-bar me-2"></i>İstatistik Güncelle
                                            </a>
                                        </div>
                                        <div class="col-md-2 col-sm-4 mb-3">
                                            <a href="?action=mark_alert_read" class="btn btn-outline-secondary w-100">
                                                <i class="fas fa-eye me-2"></i>Alarm Oku
                                            </a>
                                        </div>
                                        <div class="col-md-2 col-sm-4 mb-3">
                                            <a href="<?= ADMIN_URL ?>" class="btn btn-outline-dark w-100">
                                                <i class="fas fa-home me-2"></i>Ana Sayfa
                                            </a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Son Güvenlik Olayları ve Alarmlar -->
                    <div class="row">
                        <div class="col-xl-8">
                            <div class="card security-card">
                                <div class="card-header">
                                    <h4 class="card-title mb-0">
                                        <i class="fas fa-history me-2"></i>
                                        <?= lang('recent_security_events', [], 'Son Güvenlik Olayları') ?>
                                    </h4>
                                </div>
                                <div class="card-body">
                                    <?php if (!empty($security_data['recent_events'])): ?>
                                        <?php foreach (array_slice($security_data['recent_events'], 0, 10) as $event): ?>
                                            <div class="event-item <?= $event['event_type'] == 'successful_login' ? 'success' : ($event['event_type'] == 'failed_login' ? 'warning' : 'info') ?>">
                                                <div class="d-flex justify-content-between align-items-center">
                                                    <div>
                                                        <strong><?= ucfirst(str_replace('_', ' ', $event['event_type'])) ?></strong>
                                                        <br>
                                                        <small class="text-muted">
                                                            <i class="fas fa-globe me-1"></i><?= $event['ip_address'] ?>
                                                            <i class="fas fa-clock ms-2 me-1"></i><?= date('d.m.Y H:i:s', strtotime($event['created_at'])) ?>
                                                        </small>
                                                    </div>
                                                    <div class="text-end">
                                                        <?php if ($event['event_type'] == 'successful_login'): ?>
                                                            <span class="badge bg-success">Başarılı</span>
                                                        <?php elseif ($event['event_type'] == 'failed_login'): ?>
                                                            <span class="badge bg-warning">Başarısız</span>
                                                        <?php else: ?>
                                                            <span class="badge bg-info">Bilgi</span>
                                                        <?php endif; ?>
                                                    </div>
                                                </div>
                                            </div>
                                        <?php endforeach; ?>
                                    <?php else: ?>
                                        <div class="text-center py-4">
                                            <i class="fas fa-shield-alt text-muted" style="font-size: 3rem;"></i>
                                            <p class="text-muted mt-2">Henüz güvenlik olayı kaydedilmemiş.</p>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                        
                        <div class="col-xl-4">
                            <div class="card security-card">
                                <div class="card-header">
                                    <h4 class="card-title mb-0">
                                        <i class="fas fa-bell me-2"></i>
                                        <?= lang('active_alerts', [], 'Aktif Alarmlar') ?>
                                        <span class="badge bg-danger ms-2"><?= $active_alerts ?></span>
                                    </h4>
                                </div>
                                <div class="card-body">
                                    <?php if (!empty($security_data['unread_alerts'])): ?>
                                        <?php foreach (array_slice($security_data['unread_alerts'], 0, 5) as $alert): ?>
                                            <div class="alert-item <?= $alert['severity'] ?>">
                                                <div class="d-flex justify-content-between align-items-start">
                                                    <div class="flex-grow-1">
                                                        <h6 class="mb-1"><?= ucfirst(str_replace('_', ' ', $alert['alert_type'])) ?></h6>
                                                        <p class="mb-1 small"><?= $alert['message'] ?></p>
                                                        <small class="text-muted">
                                                            <i class="fas fa-clock me-1"></i><?= date('d.m.Y H:i', strtotime($alert['created_at'])) ?>
                                                        </small>
                                                    </div>
                                                    <div class="ms-2">
                                                        <?php if ($alert['severity'] == 'high'): ?>
                                                            <span class="badge bg-danger">Yüksek</span>
                                                        <?php elseif ($alert['severity'] == 'medium'): ?>
                                                            <span class="badge bg-warning">Orta</span>
                                                        <?php else: ?>
                                                            <span class="badge bg-success">Düşük</span>
                                                        <?php endif; ?>
                                                    </div>
                                                </div>
                                            </div>
                                        <?php endforeach; ?>
                                    <?php else: ?>
                                        <div class="text-center py-4">
                                            <i class="fas fa-bell-slash text-muted" style="font-size: 3rem;"></i>
                                            <p class="text-muted mt-2">Aktif alarm bulunmuyor.</p>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>

                                 </div> <!-- container-fluid -->
             </div>
             <!-- End Page-content -->

         </div>
         <!-- end main content-->

     </div>
     <!-- END layout-wrapper -->

<?php require_once('template/footer.php'); ?> 