<?php
/**
 * Run Pay Mode and Bank Mode Migration
 * Adds pay_mode and bank_mode columns to employees table
 */

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

// Only allow Super Admin or HR Admin
if (!isSuperAdmin() && !isHRAdmin()) {
    die('Access denied. Only Super Admin and HR Admin can run migrations.');
}

header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE html>
<html>
<head>
    <title>Pay Mode Migration</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; }
        .btn:hover { background: #0056b3; }
    </style>
</head>
<body>
    <div class="container">
        <h2>Pay Mode and Bank Mode Migration</h2>
        <pre>
<?php

try {
    $db = getDB();
    
    echo "Starting migration...\n\n";
    
    // Get existing columns
    $stmt = $db->query("SHOW COLUMNS FROM employees");
    $existingColumns = $stmt->fetchAll(PDO::FETCH_COLUMN);
    
    echo "Existing columns: " . count($existingColumns) . " found\n";
    echo "Checking for pay_mode and bank_mode columns...\n\n";
    
    $columnsAdded = 0;
    $errors = [];
    
    // Add pay_mode column
    if (!in_array('pay_mode', $existingColumns)) {
        try {
            $db->exec("ALTER TABLE `employees` ADD COLUMN `pay_mode` varchar(50) DEFAULT NULL AFTER `bank_ifsc`");
            echo "✓ Added column: pay_mode\n";
            $columnsAdded++;
        } catch (PDOException $e) {
            $errorMsg = "Error adding pay_mode: " . $e->getMessage();
            echo "✗ " . $errorMsg . "\n";
            $errors[] = $errorMsg;
        }
    } else {
        echo "ℹ Column pay_mode already exists\n";
    }
    
    // Add bank_mode column
    if (!in_array('bank_mode', $existingColumns)) {
        try {
            $db->exec("ALTER TABLE `employees` ADD COLUMN `bank_mode` varchar(50) DEFAULT 'NEFT' AFTER `pay_mode`");
            echo "✓ Added column: bank_mode\n";
            $columnsAdded++;
        } catch (PDOException $e) {
            $errorMsg = "Error adding bank_mode: " . $e->getMessage();
            echo "✗ " . $errorMsg . "\n";
            $errors[] = $errorMsg;
        }
    } else {
        echo "ℹ Column bank_mode already exists\n";
    }
    
    // Verify columns
    echo "\n--- Verification ---\n";
    $stmt = $db->query("SHOW COLUMNS FROM employees WHERE Field IN ('pay_mode', 'bank_mode')");
    $columns = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    if (count($columns) > 0) {
        foreach ($columns as $col) {
            echo "Column: {$col['Field']} | Type: {$col['Type']} | Default: {$col['Default']}\n";
        }
    } else {
        echo "⚠ Warning: Columns not found after migration\n";
    }
    
    echo "\n--- Migration Summary ---\n";
    if ($columnsAdded > 0) {
        echo "<span class='success'>✓ Migration completed successfully!</span>\n";
        echo "Columns added: {$columnsAdded}\n";
    } else {
        echo "<span class='info'>ℹ All columns already exist. No changes needed.</span>\n";
    }
    
    if (!empty($errors)) {
        echo "\n<span class='error'>Errors encountered:</span>\n";
        foreach ($errors as $error) {
            echo "  - {$error}\n";
        }
    }
    
} catch (Exception $e) {
    echo "<span class='error'>❌ Migration failed: " . htmlspecialchars($e->getMessage()) . "</span>\n";
    echo "Stack trace: " . htmlspecialchars($e->getTraceAsString()) . "\n";
}

?>
        </pre>
        <p>
            <a href="<?php echo BASE_URL; ?>/modules/employee/list.php" class="btn">Go to Employee List</a>
            <a href="<?php echo BASE_URL; ?>/modules/employee/edit.php?id=1" class="btn">Test Edit Employee</a>
        </p>
    </div>
</body>
</html>

