<?php
/**
 * Check Salary Register Table Status
 */

$host = 'localhost';
$dbname = 'hrm2';
$username = 'root';
$password = '';

try {
    $db = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // Check if table exists
    $tables = $db->query("SHOW TABLES LIKE 'salary_register'")->fetchAll();
    
    if (empty($tables)) {
        $status = "❌ Table NOT EXISTS";
        $message = "Table 'salary_register' does not exist. Please run migration.";
        $color = "red";
        $columns = [];
    } else {
        $status = "✅ Table EXISTS";
        $message = "Table 'salary_register' found in database.";
        $color = "green";
        
        // Get columns
        $columns = $db->query("DESCRIBE salary_register")->fetchAll(PDO::FETCH_ASSOC);
    }
    
} catch (Exception $e) {
    $status = "❌ ERROR";
    $message = $e->getMessage();
    $color = "red";
    $columns = [];
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Check Salary Register Table</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 900px;
            margin: 50px auto;
            padding: 20px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
        }
        .container {
            background: white;
            padding: 40px;
            border-radius: 12px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.3);
        }
        h1 {
            color: #333;
            border-bottom: 3px solid #667eea;
            padding-bottom: 15px;
        }
        .status {
            padding: 20px;
            border-radius: 8px;
            margin: 20px 0;
            font-size: 18px;
            font-weight: bold;
            background: <?php echo $color === 'green' ? '#d4edda' : '#f8d7da'; ?>;
            color: <?php echo $color === 'green' ? '#155724' : '#721c24'; ?>;
            border: 2px solid <?php echo $color === 'green' ? '#28a745' : '#dc3545'; ?>;
        }
        .message {
            padding: 15px;
            background: #f8f9fa;
            border-radius: 6px;
            margin: 20px 0;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
        }
        th, td {
            padding: 12px;
            text-align: left;
            border: 1px solid #ddd;
        }
        th {
            background: #667eea;
            color: white;
        }
        tr:nth-child(even) {
            background: #f8f9fa;
        }
        .btn {
            display: inline-block;
            padding: 12px 24px;
            background: #667eea;
            color: white;
            text-decoration: none;
            border-radius: 6px;
            margin: 10px 5px;
        }
        .btn:hover {
            background: #764ba2;
        }
        .btn-success {
            background: #28a745;
        }
        .btn-success:hover {
            background: #218838;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🔍 Salary Register Table Check</h1>
        
        <div class="status">
            <?php echo $status; ?>
        </div>
        
        <div class="message">
            <?php echo $message; ?>
        </div>
        
        <?php if (!empty($columns)): ?>
            <h2>📊 Table Structure (<?php echo count($columns); ?> columns)</h2>
            <table>
                <thead>
                    <tr>
                        <th>Column Name</th>
                        <th>Type</th>
                        <th>Null</th>
                        <th>Key</th>
                        <th>Default</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($columns as $col): ?>
                    <tr>
                        <td><strong><?php echo $col['Field']; ?></strong></td>
                        <td><?php echo $col['Type']; ?></td>
                        <td><?php echo $col['Null']; ?></td>
                        <td><?php echo $col['Key']; ?></td>
                        <td><?php echo $col['Default'] ?? 'NULL'; ?></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
            
            <div style="text-align: center; margin-top: 30px;">
                <a href="../modules/payroll/salary_register.php" class="btn btn-success">
                    ✅ Go to Salary Register
                </a>
            </div>
        <?php else: ?>
            <div style="text-align: center; margin-top: 30px;">
                <h2>⚠️ Action Required:</h2>
                <p style="font-size: 16px; color: #666;">
                    Table does not exist. Please run the migration script first.
                </p>
                <a href="run_salary_register_migration.php" class="btn btn-success">
                    🚀 Run Migration Now
                </a>
            </div>
        <?php endif; ?>
        
        <div style="text-align: center; margin-top: 20px;">
            <a href="javascript:location.reload()" class="btn">🔄 Refresh Check</a>
            <a href="../" class="btn">🏠 Dashboard</a>
        </div>
    </div>
</body>
</html>
