<?php
/**
 * Verify Pay Mode and Bank Mode Columns
 * Check if columns exist in employees table
 */

require_once __DIR__ . '/../includes/init.php';
requireLogin();

header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE html>
<html>
<head>
    <title>Verify Pay Mode Columns</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; background: #f5f5f5; }
        .container { background: white; padding: 20px; border-radius: 5px; max-width: 800px; margin: 0 auto; }
        .success { color: #28a745; font-weight: bold; }
        .error { color: #dc3545; font-weight: bold; }
        .info { color: #17a2b8; }
        pre { background: #f8f9fa; padding: 15px; border-radius: 3px; overflow-x: auto; }
        .btn { display: inline-block; padding: 10px 20px; background: #007bff; color: white; text-decoration: none; border-radius: 3px; margin-top: 10px; margin-right: 10px; }
        .btn:hover { background: #0056b3; }
        table { width: 100%; border-collapse: collapse; margin-top: 20px; }
        th, td { padding: 8px; text-align: left; border: 1px solid #ddd; }
        th { background: #f8f9fa; }
    </style>
</head>
<body>
    <div class="container">
        <h2>Verify Pay Mode and Bank Mode Columns</h2>
        <pre>
<?php

try {
    $db = getDB();
    
    echo "Checking employees table structure...\n\n";
    
    // Get all columns
    $stmt = $db->query("SHOW COLUMNS FROM employees");
    $columns = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    $payModeExists = false;
    $bankModeExists = false;
    
    foreach ($columns as $col) {
        if ($col['Field'] === 'pay_mode') {
            $payModeExists = true;
            echo "<span class='success'>✓ pay_mode column exists</span>\n";
            echo "  Type: {$col['Type']}\n";
            echo "  Null: {$col['Null']}\n";
            echo "  Default: " . ($col['Default'] ?? 'NULL') . "\n";
        }
        if ($col['Field'] === 'bank_mode') {
            $bankModeExists = true;
            echo "<span class='success'>✓ bank_mode column exists</span>\n";
            echo "  Type: {$col['Type']}\n";
            echo "  Null: {$col['Null']}\n";
            echo "  Default: " . ($col['Default'] ?? 'NULL') . "\n";
        }
    }
    
    echo "\n";
    
    if (!$payModeExists) {
        echo "<span class='error'>✗ pay_mode column does NOT exist</span>\n";
        echo "  Run migration: database/migrations/add_pay_mode_bank_mode_employees.sql\n";
    }
    
    if (!$bankModeExists) {
        echo "<span class='error'>✗ bank_mode column does NOT exist</span>\n";
        echo "  Run migration: database/migrations/add_pay_mode_bank_mode_employees.sql\n";
    }
    
    // Check sample employee data
    echo "\n--- Sample Employee Data ---\n";
    $stmt = $db->query("SELECT employee_id, employee_code, first_name, pay_mode, bank_mode FROM employees LIMIT 5");
    $employees = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    if (count($employees) > 0) {
        echo "<table>\n";
        echo "<tr><th>ID</th><th>Code</th><th>Name</th><th>Pay Mode</th><th>Bank Mode</th></tr>\n";
        foreach ($employees as $emp) {
            echo "<tr>";
            echo "<td>{$emp['employee_id']}</td>";
            echo "<td>" . htmlspecialchars($emp['employee_code'] ?? '') . "</td>";
            echo "<td>" . htmlspecialchars(($emp['first_name'] ?? '') . ' ' . ($emp['last_name'] ?? '')) . "</td>";
            echo "<td>" . htmlspecialchars($emp['pay_mode'] ?? 'NULL') . "</td>";
            echo "<td>" . htmlspecialchars($emp['bank_mode'] ?? 'NULL') . "</td>";
            echo "</tr>\n";
        }
        echo "</table>\n";
    } else {
        echo "No employees found.\n";
    }
    
    echo "\n--- Summary ---\n";
    if ($payModeExists && $bankModeExists) {
        echo "<span class='success'>✓ All columns exist. Pay Mode should work correctly.</span>\n";
    } else {
        echo "<span class='error'>✗ Some columns are missing. Please run the migration first.</span>\n";
    }
    
} catch (Exception $e) {
    echo "<span class='error'>❌ Error: " . htmlspecialchars($e->getMessage()) . "</span>\n";
}

?>
        </pre>
        <p>
            <a href="<?php echo BASE_URL; ?>/database/run_pay_mode_migration.php" class="btn">Run Migration</a>
            <a href="<?php echo BASE_URL; ?>/modules/employee/list.php" class="btn">Go to Employee List</a>
        </p>
    </div>
</body>
</html>

