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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    verify_csrf();
    try {
        $companyName = trim((string)post('company_name'));
        $companyAddress = trim((string)post('company_address'));
        $companyPhone = trim((string)post('company_phone'));
        $companyEmail = trim((string)post('company_email'));

        if ($companyName === '' || strlen($companyName) > 150) {
            throw new RuntimeException('Company name is required and must be 150 characters or less.');
        }
        if (strlen($companyAddress) > 500) {
            throw new RuntimeException('Company address must be 500 characters or less.');
        }
        if (strlen($companyPhone) > 50) {
            throw new RuntimeException('Company phone must be 50 characters or less.');
        }
        if ($companyEmail !== '' && (!filter_var($companyEmail, FILTER_VALIDATE_EMAIL) || strlen($companyEmail) > 150)) {
            throw new RuntimeException('Company email is invalid.');
        }

        $oldLogo = get_setting('quotation_logo_path', '');
        $newLogo = save_quotation_logo_upload($_FILES['quotation_logo'] ?? []);

        update_setting('company_name', $companyName);
        update_setting('company_address', $companyAddress);
        update_setting('company_phone', $companyPhone);
        update_setting('company_email', $companyEmail);

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

        log_activity('quotation_branding_updated', 'User changed quotation logo/company information.');
        flash('success', 'Quotation branding settings updated successfully.');
    } catch (Throwable $e) {
        flash('error', $e->getMessage());
    }
    redirect('/settings/quotation-branding.php');
}

$company = get_company_info();
$logoUrl = get_quotation_logo_url();
?>
<div class="page-head">
    <div><h1>Quotation Branding Settings</h1><p>Update the logo and company information shown on quotation print and PDF.</p></div>
</div>

<form class="card app-card p-4" method="post" enctype="multipart/form-data">
    <?= csrf_field() ?>
    <h2>Quotation Logo & Company Information</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="150" 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="50">
        </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="150">
        </div>
        <div class="col-md-6">
            <label class="form-label">Company Address</label>
            <textarea class="form-control" name="company_address" rows="3" maxlength="500"><?= e($company['address']) ?></textarea>
        </div>
    </div>
    <button class="btn btn-primary mt-4" type="submit"><i class="bi bi-save me-1"></i>Save Branding Settings</button>
</form>
<?php require_once __DIR__ . '/../../app/includes/footer.php'; ?>
