<?php
/**
 * Salary Register Table Migration Runner
 * Access via: http://localhost/hrm2/database/run_salary_register_migration.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 = [];
    
    // Read SQL file
    $sqlFile = __DIR__ . '/migrations/create_salary_register_table.sql';
    
    if (!file_exists($sqlFile)) {
        throw new Exception("SQL file not found: $sqlFile");
    }
    
    $sql = file_get_contents($sqlFile);
    $output[] = "✓ SQL file loaded";
    
    // Execute SQL
    $db->exec($sql);
    $output[] = "✓ salary_register table created!";
    
    // Add employee fields if table already exists
    $alterFile = __DIR__ . '/migrations/add_employee_fields_to_salary_register.sql';
    if (file_exists($alterFile)) {
        try {
            $alterSql = file_get_contents($alterFile);
            $db->exec($alterSql);
            $output[] = "✓ Employee fields added/updated";
        } catch (Exception $e) {
            // Columns might already exist, that's ok
            $output[] = "ℹ️ Employee fields already exist";
        }
    }
    
    // Verify
    $verifyStmt = $db->query("SHOW TABLES LIKE 'salary_register'");
    if ($verifyStmt->rowCount() > 0) {
        $output[] = "✓ Table verified successfully";
        
        // Show table structure
        $structureStmt = $db->query("DESCRIBE salary_register");
        $columns = $structureStmt->fetchAll(PDO::FETCH_COLUMN);
        $output[] = "✓ Table has " . count($columns) . " columns";
    }
    
    $success = true;
    $output[] = "<br><strong style='color: green; font-size: 18px;'>✅ MIGRATION COMPLETED!</strong>";
    
} catch (Exception $e) {
    $success = false;
    $output[] = "<strong style='color: red;'>❌ ERROR:</strong> " . $e->getMessage();
}

?>
<!DOCTYPE html>
<html>
<head>
    <title>Salary Register Migration</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: 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;
        }
        .output {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 6px;
            margin: 20px 0;
            line-height: 1.8;
            border-left: 4px solid <?php echo $success ? '#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: 20px;
            transition: transform 0.2s;
        }
        .btn:hover {
            transform: translateY(-2px);
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>💰 Salary Register Table Migration</h1>
        
        <div class="output">
            <?php foreach ($output as $line): ?>
                <div><?php echo $line; ?></div>
            <?php endforeach; ?>
        </div>
        
        <?php if ($success): ?>
            <div style="text-align: center;">
                <a href="../modules/payroll/salary_register.php" class="btn">
                    🚀 Go to Salary Register
                </a>
            </div>
        <?php else: ?>
            <div style="text-align: center;">
                <a href="javascript:location.reload()" class="btn">
                    🔄 Try Again
                </a>
            </div>
        <?php endif; ?>
        
        <div style="margin-top: 30px; padding-top: 20px; border-top: 2px solid #eee; color: #666; font-size: 13px;">
            <strong>ℹ️ What this migration does:</strong><br>
            1. Creates <code>salary_register</code> table<br>
            2. Sets up columns for salary calculations<br>
            3. Enables Excel-like salary management
        </div>
    </div>
</body>
</html>
