<?php
/**
 * Salary Periods Table Migration
 * Access via: http://localhost/hrm2/database/run_salary_periods_migration.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";
    
    // Create salary_periods table
    $sql = "
    CREATE TABLE IF NOT EXISTS salary_periods (
        period_id INT AUTO_INCREMENT PRIMARY KEY,
        site_id INT NOT NULL,
        salary_month VARCHAR(10) NOT NULL COMMENT 'Format: Jan-25, Feb-25, etc.',
        period_display VARCHAR(50) NOT NULL COMMENT 'Format: January 2025, February 2025',
        employee_count INT DEFAULT 0,
        is_finalized TINYINT(1) DEFAULT 0,
        created_by INT DEFAULT NULL,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        finalized_at TIMESTAMP NULL DEFAULT NULL,
        
        UNIQUE KEY unique_site_period (site_id, salary_month),
        KEY idx_site_month (site_id, salary_month),
        KEY idx_created_at (created_at)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    ";
    
    $db->exec($sql);
    $output[] = "✓ salary_periods table created!";
    
    // Verify
    $check = $db->query("SHOW TABLES LIKE 'salary_periods'");
    if ($check->rowCount() > 0) {
        $output[] = "✓ Table verified successfully";
        
        $structure = $db->query("DESCRIBE salary_periods");
        $columns = $structure->fetchAll(PDO::FETCH_COLUMN);
        $output[] = "✓ Table has " . count($columns) . " columns";
    }
    
    $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>Salary Periods Migration</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>💰 Salary Periods Table Creation</h1>
        <div class="subtitle">Period tracking for 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 created:</strong><br>
            • salary_periods table for tracking periods<br>
            • Month/year based period management<br>
            • Similar to attendance_periods system<br>
            • Ready to create salary periods!
        </div>
    </div>
</body>
</html>
