<?php
$pageTitle = 'Settings';
require_once __DIR__ . '/../../app/includes/header.php'; require_admin();
require_role_access('*');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    verify_csrf();
    try {
        $oldLogo = get_setting('quotation_logo_path', '');
        $newLogo = save_quotation_logo_upload($_FILES['quotation_logo'] ?? []);

        foreach (['company_name', 'company_phone', 'company_email', 'company_address', 'default_vat_percent', 'default_terms', 'visitor_branding'] as $key) {
            update_setting($key, trim((string)post($key)));
        }

        if ($newLogo !== null) {
            update_setting('quotation_logo_path', $newLogo);
            delete_old_quotation_logo($oldLogo, $newLogo);
        }

        log_activity('settings.updated', 'Updated quotation logo/company settings.');
        flash('success', 'Quotation logo and company settings updated.');
    } catch (Throwable $e) {
        flash('error', $e->getMessage());
    }
    redirect('/settings/index.php');
}

$company = get_company_info();
$logoUrl = get_quotation_logo_url();
?>
<div class="page-head"><div><h1>Settings</h1><p>Company details, quotation logo, and quotation defaults.</p></div></div>

<form method="post" enctype="multipart/form-data">
<?= csrf_field() ?>
<div class="card app-card p-4 mb-4">
    <h2>Quotation Logo & Company Info</h2>
    <div class="row g-4 align-items-start">
        <div class="col-md-4">
            <label class="form-label">Current Logo Preview</label>
            <div class="logo-preview">
                <?php if ($logoUrl !== ''): ?>
                    <img src="<?= e($logoUrl) ?>" alt="Quotation logo" onerror="this.style.display='none';this.nextElementSibling.style.display='grid';">
                    <span>F</span>
                <?php else: ?>
                    <span style="display:grid">F</span>
                <?php endif; ?>
            </div>
        </div>
        <div class="col-md-8">
            <label class="form-label">Upload New Quotation Logo</label>
            <input class="form-control" type="file" name="quotation_logo" accept=".jpg,.jpeg,.png,.webp,image/jpeg,image/png,image/webp">
            <div class="form-text">Accepted formats: JPG, PNG, WEBP. Maximum size: 2MB.</div>
        </div>
        <div class="col-md-6"><label class="form-label">Company Name</label><input class="form-control" name="company_name" value="<?= e($company['name']) ?>" maxlength="160" required></div>
        <div class="col-md-6"><label class="form-label">Company Phone</label><input class="form-control" name="company_phone" value="<?= e($company['phone']) ?>" maxlength="80"></div>
        <div class="col-md-6"><label class="form-label">Company Email</label><input class="form-control" type="email" name="company_email" value="<?= e($company['email']) ?>" maxlength="190"></div>
        <div class="col-md-6"><label class="form-label">Company Address</label><input class="form-control" name="company_address" value="<?= e($company['address']) ?>" maxlength="255"></div>
    </div>
    <button class="btn btn-primary mt-4" type="submit"><i class="bi bi-save me-1"></i>Save Settings</button>
</div>

<div class="card app-card p-4">
    <h2>Quotation Defaults</h2>
    <div class="row g-3">
        <div class="col-md-4"><label class="form-label">Default VAT Percent</label><input class="form-control" type="number" step="0.01" min="0" name="default_vat_percent" value="<?= e(get_setting('default_vat_percent', '5')) ?>"></div>
        <div class="col-md-4">
            <label class="form-label">Visitor Quotation Branding</label>
            <select class="form-select" name="visitor_branding">
                <option value="1" <?= get_setting('visitor_branding', '1') === '1' ? 'selected' : '' ?>>Enabled</option>
                <option value="0" <?= get_setting('visitor_branding', '1') === '0' ? 'selected' : '' ?>>Disabled</option>
            </select>
        </div>
        <div class="col-12"><label class="form-label">Default Terms</label><textarea class="form-control" name="default_terms" rows="4"><?= e(get_setting('default_terms')) ?></textarea></div>
    </div>
    <button class="btn btn-primary mt-4" type="submit"><i class="bi bi-save me-1"></i>Save Settings</button>
</div>
</form>
<?php require_once __DIR__ . '/../../app/includes/footer.php'; ?>
