<?php
/**
 * Update Salary Register Table - Add per_day_wages column
 * Access via: http://localhost/hrm2/database/update_salary_register.php
 */

// Simple database connection
$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);
    
    $output = [];
    
    // Check if column already exists
    $checkStmt = $db->query("SHOW COLUMNS FROM salary_register LIKE 'per_day_wages'");
    
    if ($checkStmt->rowCount() > 0) {
        $output[] = "✓ Column 'per_day_wages' already exists";
        $success = true;
    } else {
        // Add column
        $db->exec("ALTER TABLE salary_register ADD COLUMN per_day_wages DECIMAL(10,2) DEFAULT 0 AFTER food_allowance");
        $output[] = "✓ Column 'per_day_wages' added successfully!";
        
        // Verify
        $verifyStmt = $db->query("SHOW COLUMNS FROM salary_register LIKE 'per_day_wages'");
        if ($verifyStmt->rowCount() > 0) {
            $output[] = "✓ Column verified";
            $success = true;
        } else {
            throw new Exception("Column verification failed");
        }
    }
    
    $output[] = "<br><strong style='color: green; font-size: 18px;'>✅ UPDATE COMPLETED!</strong>";
    $output[] = "<br>Salary Register table is ready with per_day_wages column!";
    
} catch (Exception $e) {
    $success = false;
    $output[] = "<strong style='color: red;'>❌ ERROR:</strong> " . $e->getMessage();
}

?>
<!DOCTYPE html>
<html>
<head>
    <title>Update Salary Register Table</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 50px auto;
            padding: 20px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.3);
        }
        h1 {
            color: #333;
            border-bottom: 3px solid #667eea;
            padding-bottom: 10px;
        }
        .output {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 5px;
            margin: 20px 0;
            line-height: 1.8;
            border-left: 4px solid <?php echo $success ?? false ? '#28a745' : '#dc3545'; ?>;
        }
        .btn {
            display: inline-block;
            padding: 12px 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            text-decoration: none;
            border-radius: 6px;
            margin-top: 15px;
        }
        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(0,0,0,0.2);
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>💰 Update Salary Register Table</h1>
        
        <div class="output">
            <?php foreach ($output as $line): ?>
                <div><?php echo $line; ?></div>
            <?php endforeach; ?>
        </div>
        
        <?php if ($success ?? false): ?>
            <a href="../modules/payroll/salary_register.php" class="btn">
                🚀 Go to Salary Register
            </a>
        <?php else: ?>
            <a href="javascript:location.reload()" class="btn">
                🔄 Try Again
            </a>
        <?php endif; ?>
        
        <div style="margin-top: 20px; padding: 15px; background: #e8f4fd; border-radius: 5px; font-size: 13px;">
            <strong>ℹ️ What this does:</strong><br>
            Adds the <code>per_day_wages</code> column to the salary_register table if it doesn't exist.
        </div>
    </div>
</body>
</html>
