<?php
/**
 * Script untuk Check Menu di Shared Hosting
 * 
 * Script ini membantu memverifikasi apakah semua file dan konfigurasi sudah benar
 * untuk menampilkan menu "Approval" dan "Perangkat User"
 * 
 * CARA PENGGUNAAN:
 * 1. Upload file ini ke folder public/ di hosting
 * 2. Akses via browser: https://yourdomain.com/check-menu.php?secret=check-menu-2024
 * 3. Setelah selesai, HAPUS file ini untuk keamanan!
 */

// Security: Hanya izinkan akses jika ada parameter secret
$secret = $_GET['secret'] ?? '';
$allowedSecret = 'check-menu-2024'; // GANTI dengan password yang kuat!

if ($secret !== $allowedSecret) {
    http_response_code(403);
    die('Access Denied. Gunakan parameter ?secret=your-secret');
}

// Get base path
$basePath = dirname(__DIR__);
chdir($basePath);

// Output HTML
header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Check Menu - Laravel Filament</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            max-width: 1000px;
            margin: 50px auto;
            padding: 20px;
            background: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            border-bottom: 3px solid #6366f1;
            padding-bottom: 10px;
        }
        h2 {
            color: #555;
            margin-top: 30px;
            border-bottom: 2px solid #e5e7eb;
            padding-bottom: 5px;
        }
        .check-item {
            background: #f8f9fa;
            padding: 15px;
            margin: 10px 0;
            border-left: 4px solid #6366f1;
            border-radius: 5px;
        }
        .success {
            color: #10b981;
            font-weight: bold;
        }
        .error {
            color: #ef4444;
            font-weight: bold;
        }
        .warning {
            color: #f59e0b;
            font-weight: bold;
        }
        .info {
            color: #3b82f6;
        }
        pre {
            background: #1e1e1e;
            color: #d4d4d4;
            padding: 15px;
            border-radius: 5px;
            overflow-x: auto;
            font-size: 12px;
            margin: 10px 0;
        }
        .alert {
            padding: 15px;
            border-radius: 5px;
            margin: 20px 0;
        }
        .alert-success {
            background: #d1fae5;
            border: 1px solid #10b981;
            color: #065f46;
        }
        .alert-error {
            background: #fee2e2;
            border: 1px solid #ef4444;
            color: #991b1b;
        }
        .alert-warning {
            background: #fef3c7;
            border: 1px solid #f59e0b;
            color: #92400e;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin: 15px 0;
        }
        th, td {
            padding: 10px;
            text-align: left;
            border-bottom: 1px solid #e5e7eb;
        }
        th {
            background: #f3f4f6;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🔍 Check Menu Configuration</h1>
        
        <div class="alert alert-warning">
            <strong>⚠️ PENTING:</strong> Setelah selesai, HAPUS file ini untuk keamanan!
        </div>

        <?php
        $checks = [];
        $errors = [];
        $warnings = [];

        // Check 1: File AdminPanelProvider.php
        $adminPanelPath = $basePath . '/app/Providers/Filament/AdminPanelProvider.php';
        $checks[] = [
            'name' => 'File AdminPanelProvider.php',
            'path' => $adminPanelPath,
            'exists' => file_exists($adminPanelPath),
            'readable' => file_exists($adminPanelPath) ? is_readable($adminPanelPath) : false,
        ];

        // Check 2: File UserDeviceResource.php
        $userDevicePath = $basePath . '/app/Filament/Resources/UserDeviceResource.php';
        $checks[] = [
            'name' => 'File UserDeviceResource.php',
            'path' => $userDevicePath,
            'exists' => file_exists($userDevicePath),
            'readable' => file_exists($userDevicePath) ? is_readable($userDevicePath) : false,
        ];

        // Check 3: Check if Approval group exists in AdminPanelProvider
        if (file_exists($adminPanelPath)) {
            $content = file_get_contents($adminPanelPath);
            $hasApprovalGroup = strpos($content, "'Approval'") !== false || strpos($content, '"Approval"') !== false;
            $checks[] = [
                'name' => 'Navigation Group "Approval" di AdminPanelProvider',
                'path' => $adminPanelPath,
                'exists' => true,
                'readable' => true,
                'hasApproval' => $hasApprovalGroup,
            ];
        }

        // Check 4: Check if UserDeviceResource uses Approval group
        if (file_exists($userDevicePath)) {
            $content = file_get_contents($userDevicePath);
            $usesApprovalGroup = strpos($content, "navigationGroup = 'Approval'") !== false || 
                                 strpos($content, 'navigationGroup = "Approval"') !== false;
            $checks[] = [
                'name' => 'UserDeviceResource menggunakan group "Approval"',
                'path' => $userDevicePath,
                'exists' => true,
                'readable' => true,
                'usesApproval' => $usesApprovalGroup,
            ];
        }

        // Check 5: Check cache files
        $cacheFiles = [
            'bootstrap/cache/config.php' => 'Config Cache',
            'bootstrap/cache/routes-v7.php' => 'Route Cache',
            'bootstrap/cache/packages.php' => 'Packages Cache',
            'bootstrap/cache/services.php' => 'Services Cache',
        ];

        foreach ($cacheFiles as $cacheFile => $label) {
            $cachePath = $basePath . '/' . $cacheFile;
            $checks[] = [
                'name' => $label,
                'path' => $cachePath,
                'exists' => file_exists($cachePath),
                'readable' => file_exists($cachePath) ? is_readable($cachePath) : false,
                'isCache' => true,
            ];
        }

        // Check 6: Check if UserDevice model exists
        $userDeviceModelPath = $basePath . '/app/Models/UserDevice.php';
        $checks[] = [
            'name' => 'Model UserDevice.php',
            'path' => $userDeviceModelPath,
            'exists' => file_exists($userDeviceModelPath),
            'readable' => file_exists($userDeviceModelPath) ? is_readable($userDeviceModelPath) : false,
        ];

        // Check 7: Check database table
        try {
            require $basePath . '/vendor/autoload.php';
            $app = require_once $basePath . '/bootstrap/app.php';
            $app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
            
            $dbCheck = \DB::select("SHOW TABLES LIKE 'user_devices'");
            $tableExists = count($dbCheck) > 0;
            
            $checks[] = [
                'name' => 'Database Table user_devices',
                'path' => 'Database',
                'exists' => $tableExists,
                'readable' => true,
                'isDatabase' => true,
            ];
        } catch (Exception $e) {
            $checks[] = [
                'name' => 'Database Table user_devices',
                'path' => 'Database',
                'exists' => false,
                'readable' => false,
                'isDatabase' => true,
                'error' => $e->getMessage(),
            ];
        }

        // Display results
        echo '<h2>📋 Hasil Pengecekan</h2>';
        echo '<table>';
        echo '<tr><th>Item</th><th>Status</th><th>Path</th></tr>';
        
        foreach ($checks as $check) {
            $status = '';
            $statusClass = '';
            
            if (isset($check['error'])) {
                $status = '<span class="error">❌ Error: ' . htmlspecialchars($check['error']) . '</span>';
                $errors[] = $check;
            } elseif (isset($check['hasApproval'])) {
                if ($check['hasApproval']) {
                    $status = '<span class="success">✅ Ada</span>';
                } else {
                    $status = '<span class="error">❌ Tidak ditemukan</span>';
                    $errors[] = $check;
                }
            } elseif (isset($check['usesApproval'])) {
                if ($check['usesApproval']) {
                    $status = '<span class="success">✅ Menggunakan</span>';
                } else {
                    $status = '<span class="error">❌ Tidak menggunakan</span>';
                    $errors[] = $check;
                }
            } elseif ($check['exists']) {
                if (isset($check['isCache'])) {
                    $status = '<span class="warning">⚠️ Cache ditemukan (perlu di-clear)</span>';
                    $warnings[] = $check;
                } else {
                    $status = '<span class="success">✅ Ada</span>';
                }
            } else {
                $status = '<span class="error">❌ Tidak ditemukan</span>';
                if (!isset($check['isDatabase'])) {
                    $errors[] = $check;
                }
            }
            
            $path = isset($check['isDatabase']) ? 'Database' : str_replace($basePath, '...', $check['path']);
            
            echo '<tr>';
            echo '<td><strong>' . htmlspecialchars($check['name']) . '</strong></td>';
            echo '<td>' . $status . '</td>';
            echo '<td><code style="font-size: 11px;">' . htmlspecialchars($path) . '</code></td>';
            echo '</tr>';
        }
        
        echo '</table>';

        // Summary
        echo '<h2>📊 Summary</h2>';
        
        if (count($errors) > 0) {
            echo '<div class="alert alert-error">';
            echo '<strong>❌ Ditemukan ' . count($errors) . ' masalah:</strong><ul>';
            foreach ($errors as $error) {
                echo '<li>' . htmlspecialchars($error['name']) . '</li>';
            }
            echo '</ul></div>';
        }
        
        if (count($warnings) > 0) {
            echo '<div class="alert alert-warning">';
            echo '<strong>⚠️ ' . count($warnings) . ' peringatan:</strong><ul>';
            foreach ($warnings as $warning) {
                echo '<li>' . htmlspecialchars($warning['name']) . ' - Perlu di-clear</li>';
            }
            echo '</ul></div>';
        }
        
        if (count($errors) === 0 && count($warnings) === 0) {
            echo '<div class="alert alert-success">';
            echo '<strong>✅ Semua check berhasil! Menu seharusnya sudah muncul.</strong>';
            echo '</div>';
        }

        // Recommendations
        echo '<h2>💡 Rekomendasi</h2>';
        echo '<div class="check-item">';
        echo '<ol>';
        
        if (count($errors) > 0) {
            echo '<li><strong>Upload file yang hilang:</strong>';
            echo '<ul>';
            foreach ($errors as $error) {
                if (!isset($error['isDatabase']) && !isset($error['hasApproval']) && !isset($error['usesApproval'])) {
                    echo '<li>' . htmlspecialchars($error['name']) . '</li>';
                }
            }
            echo '</ul></li>';
        }
        
        if (count($warnings) > 0) {
            echo '<li><strong>Clear cache:</strong>';
            echo '<ul>';
            echo '<li>Gunakan script <code>clear-cache.php</code> atau jalankan: <code>php artisan optimize:clear</code></li>';
            echo '</ul></li>';
        }
        
        if (count($errors) === 0 && count($warnings) > 0) {
            echo '<li><strong>Clear cache dan refresh browser:</strong>';
            echo '<ul>';
            echo '<li>Clear cache menggunakan script <code>clear-cache.php</code></li>';
            echo '<li>Hard refresh browser (Ctrl+Shift+R atau Cmd+Shift+R)</li>';
            echo '<li>Clear browser cache jika perlu</li>';
            echo '</ul></li>';
        }
        
        echo '<li><strong>Verifikasi:</strong>';
        echo '<ul>';
        echo '<li>Login ke admin panel</li>';
        echo '<li>Cek apakah menu "Approval" muncul di sidebar</li>';
        echo '<li>Cek apakah "Perangkat User" muncul di dalam group "Approval"</li>';
        echo '</ul></li>';
        
        echo '</ol>';
        echo '</div>';

        // Instructions
        echo '<div class="alert alert-warning" style="margin-top: 30px;">';
        echo '<h3>📝 Instruksi:</h3>';
        echo '<ul>';
        echo '<li>Akses script ini dengan: <code>?secret=' . $allowedSecret . '</code></li>';
        echo '<li><strong>HAPUS file ini setelah selesai!</strong></li>';
        echo '</ul>';
        echo '</div>';
        ?>
    </div>
</body>
</html>

