<?php
require 'config/config.php';

?>
<!DOCTYPE html>
<html>
<head>
    <title>Update Currency Symbol</title>
    <style>
        body { font-family: Arial, sans-serif; max-width: 600px; margin: 50px auto; padding: 20px; }
        .success { background: #d4edda; color: #155724; padding: 15px; border-radius: 5px; border: 1px solid #c3e6cb; }
        .error { background: #f8d7da; color: #721c24; padding: 15px; border-radius: 5px; border: 1px solid #f5c6cb; }
        .info { background: #d1ecf1; color: #0c5460; padding: 15px; border-radius: 5px; border: 1px solid #bee5eb; }
        h1 { color: #333; }
        button { background: #007bff; color: white; border: none; padding: 10px 20px; border-radius: 5px; cursor: pointer; font-size: 16px; }
        button:hover { background: #0056b3; }
    </style>
</head>
<body>
    <h1>Update Currency Symbol to GH₵</h1>

    <?php if ($_SERVER['REQUEST_METHOD'] === 'POST'): ?>
        <?php
        try {
            $db = db();
            
            // Update currency_symbol setting to GH₵ for all tenants
            $stmt = $db->prepare("UPDATE settings SET setting_value = 'GH₵' WHERE setting_key = 'currency_symbol'");
            $stmt->execute();
            
            $affectedRows = $stmt->rowCount();
            
            // Also update the tenant table currency if it exists
            $stmt = $db->prepare("UPDATE tenants SET currency = 'GHS', currency_symbol = 'GH₵'");
            $stmt->execute();
            $tenantAffected = $stmt->rowCount();
            
            ?>
            <div class="success">
                <strong>Success!</strong><br>
                Currency symbol updated to GH₵<br>
                Settings updated: <?= $affectedRows ?> rows<br>
                Tenants updated: <?= $tenantAffected ?> rows
            </div>
            <p><a href="<?= APP_URL ?>/sales">Go to Sales</a></p>
            <?php
            
        } catch (Exception $e) {
            ?>
            <div class="error">
                <strong>Error:</strong> <?= htmlspecialchars($e->getMessage()) ?>
            </div>
            <?php
        }
        ?>
    <?php else: ?>
        <div class="info">
            This will update the currency symbol from ₦ (Naira) to GH₵ (Ghana Cedis) throughout the system.
        </div>
        <form method="POST">
            <button type="submit">Update Currency Symbol to GH₵</button>
        </form>
        <p><a href="<?= APP_URL ?>/sales">Cancel and go to Sales</a></p>
    <?php endif; ?>

</body>
</html>
