<?php
/**
 * Add Status Column to Salary Register
 * Run via: http://localhost/hrm2/database/add_status_column.php
 */

error_reporting(E_ALL);
ini_set('display_errors', 1);

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

$output = [];
$success = false;

try {
    $db = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $output[] = "✓ Database connected";
    
    // Check if column exists
    $checkCol = $db->query("SHOW COLUMNS FROM salary_register LIKE 'status'");
    if ($checkCol->rowCount() > 0) {
        $output[] = "ℹ️ Status column already exists";
        $success = true;
    } else {
        // Add status column
        $sql = "ALTER TABLE salary_register ADD COLUMN status VARCHAR(50) DEFAULT 'Active' AFTER category";
        $db->exec($sql);
        $output[] = "✓ Status column added!";
        
        // Verify
        $verify = $db->query("SHOW COLUMNS FROM salary_register LIKE 'status'");
        if ($verify->rowCount() > 0) {
            $output[] = "✓ Column verified successfully";
        }
        
        $success = true;
    }
    
    $output[] = "<br><strong style='color: green; font-size: 20px;'>✅ SUCCESS!</strong>";
    $output[] = "<br><a href='../modules/payroll/salary_register.php' style='display: inline-block; padding: 15px 30px; background: #28a745; color: white; text-decoration: none; border-radius: 8px; margin-top: 20px; font-size: 16px;'>🚀 Go to Salary Register</a>";
    
} catch (Exception $e) {
    $success = false;
    $output[] = "<strong style='color: red;'>❌ ERROR:</strong> " . $e->getMessage();
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Add Status Column</title>
    <meta charset="UTF-8">
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }
        .container {
            background: white;
            padding: 40px;
            border-radius: 16px;
            box-shadow: 0 20px 60px rgba(0,0,0,0.3);
            max-width: 700px;
            width: 100%;
        }
        h1 {
            color: #333;
            margin-bottom: 10px;
            font-size: 28px;
        }
        .subtitle {
            color: #666;
            margin-bottom: 30px;
            font-size: 14px;
        }
        .output {
            background: #f8f9fa;
            padding: 25px;
            border-radius: 8px;
            line-height: 2;
            border-left: 5px solid <?php echo $success ? '#28a745' : '#dc3545'; ?>;
            margin: 20px 0;
        }
        .output div {
            margin: 5px 0;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>📋 Add Status Column</h1>
        <div class="subtitle">Adding status dropdown to Salary Register</div>
        
        <div class="output">
            <?php foreach ($output as $line): ?>
                <div><?php echo $line; ?></div>
            <?php endforeach; ?>
        </div>
        
        <div style="margin-top: 30px; padding-top: 20px; border-top: 2px solid #eee; color: #666; font-size: 13px;">
            <strong>ℹ️ What was added:</strong><br>
            • Status column in salary_register table<br>
            • Default value: 'Active'<br>
            • Options: Active, Resigned, New Joining, Abscondee, Transferred
        </div>
    </div>
</body>
</html>
