<?php
/**
 * Enterprise HRM ERP System - DFC Quick Fix for Column Mismatch
 * Run this to quickly fix the column count issue
 */

require_once __DIR__ . '/../../includes/init.php';

if (!hasAdminAccess()) {
    die('Access denied. Admin privileges required.');
}

$db = getDB();
$db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);

$dbname = $db->query("SELECT DATABASE()")->fetchColumn();
$errors = [];
$success = [];

try {
    // Remove credit_sale column if it exists
    $stmt = $db->prepare("SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = ? AND table_name = 'dfc_entries' AND column_name = 'credit_sale'");
    $stmt->execute([$dbname]);
    if ($stmt->fetchColumn() > 0) {
        $db->exec("ALTER TABLE dfc_entries DROP COLUMN credit_sale");
        $success[] = "✓ Removed credit_sale column";
    }
    
    // Ensure odc_cost_2 exists
    $stmt = $db->prepare("SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = ? AND table_name = 'dfc_entries' AND column_name = 'odc_cost_2'");
    $stmt->execute([$dbname]);
    if ($stmt->fetchColumn() == 0) {
        $db->exec("ALTER TABLE dfc_entries ADD COLUMN odc_cost_2 decimal(15,2) DEFAULT 0.00 AFTER odc_cost");
        $success[] = "✓ Added odc_cost_2 column";
    }
    
    // Get final column count
    $stmt = $db->prepare("
        SELECT COUNT(*) 
        FROM INFORMATION_SCHEMA.COLUMNS 
        WHERE TABLE_SCHEMA = ? AND TABLE_NAME = 'dfc_entries'
        AND COLUMN_NAME NOT IN ('entry_id', 'created_at', 'updated_at')
    ");
    $stmt->execute([$dbname]);
    $columnCount = $stmt->fetchColumn();
    
    if ($columnCount == 32) {
        $success[] = "✓ Table structure is correct (32 columns match INSERT statement)";
    } else {
        $errors[] = "⚠ Column count is {$columnCount}, expected 32. Please check table structure.";
    }
    
} catch (Exception $e) {
    $errors[] = "Error: " . $e->getMessage();
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>DFC Quick Fix</title>
    <style>
        body { font-family: Arial; padding: 40px; background: #f5f5f5; }
        .container { background: white; padding: 30px; border-radius: 8px; max-width: 800px; margin: 0 auto; }
        .success { color: #28a745; padding: 15px; background: #d4edda; border: 1px solid #c3e6cb; border-radius: 4px; margin: 10px 0; }
        .error { color: #dc3545; padding: 15px; background: #f8d7da; border: 1px solid #f5c6cb; border-radius: 4px; margin: 10px 0; }
        .btn { display: inline-block; padding: 12px 24px; background: #006699; color: white; text-decoration: none; border-radius: 4px; margin-top: 20px; font-weight: bold; }
        .btn:hover { background: #004d73; }
    </style>
</head>
<body>
    <div class="container">
        <h1>DFC Quick Fix - Column Mismatch</h1>
        
        <?php if (!empty($success)): ?>
        <div class="success">
            <strong>✓ Fixed!</strong>
            <ul>
                <?php foreach ($success as $msg): ?>
                <li><?php echo htmlspecialchars($msg); ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
        <?php endif; ?>
        
        <?php if (!empty($errors)): ?>
        <div class="error">
            <strong>⚠ Errors:</strong>
            <ul>
                <?php foreach ($errors as $error): ?>
                <li><?php echo htmlspecialchars($error); ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
        <?php endif; ?>
        
        <div style="margin-top: 30px;">
            <a href="<?php echo BASE_URL; ?>/modules/dfc/list.php" class="btn">→ Go to DFC Module</a>
        </div>
    </div>
</body>
</html>
