<?php
/** @var array $data   Current values for all company keys */
/** @var string|null $flash  Success / info message */

$fields = [
    ['key' => 'company_name',      'label' => 'Company Name',       'type' => 'text',     'required' => true],
    ['key' => 'company_tin',        'label' => 'TIN (Tax ID Number)','type' => 'text'],
    ['key' => 'company_vat',        'label' => 'VAT Registration',   'type' => 'text'],
    ['key' => 'company_reg_number', 'label' => 'Registration Number','type' => 'text'],
    ['key' => 'company_address',    'label' => 'Street Address',     'type' => 'text'],
    ['key' => 'company_city',       'label' => 'City',               'type' => 'text'],
    ['key' => 'company_country',    'label' => 'Country',            'type' => 'text'],
    ['key' => 'company_phone',      'label' => 'Phone',              'type' => 'text'],
    ['key' => 'company_email',      'label' => 'Email',              'type' => 'email'],
    ['key' => 'company_website',    'label' => 'Website',            'type' => 'url'],
    ['key' => 'company_logo_url',   'label' => 'Logo URL',           'type' => 'url',    'hint' => 'Full URL to company logo (appears on PDFs)'],
    ['key' => 'company_bank_name',   'label' => 'Bank Name',         'type' => 'text'],
    ['key' => 'company_bank_account','label' => 'Bank Account',      'type' => 'text'],
];
?>

<div class="row">
    <div class="col-lg-10 col-xl-8 mx-auto">
        <div class="card shadow-sm">
            <div class="card-header bg-white border-bottom-0 pt-3">
                <h5 class="mb-0"><i class="fas fa-building me-2 text-primary"></i>Company Settings</h5>
                <p class="text-muted small mb-0 mt-1">
                    These details appear on quotations, invoices, and other documents sent to clients.
                    <?= e(APP_NAME) ?> is used as the default company name until you change it here.
                </p>
            </div>
            <div class="card-body">

                <?php if (!empty($flash)): ?>
                    <div class="alert alert-success alert-dismissible fade show" role="alert">
                        <i class="fas fa-check-circle me-1"></i> <?= e($flash) ?>
                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                    </div>
                <?php endif; ?>

                <form method="post" action="<?= url('/CompanySettings/save') ?>" class="rs-confirm-submit"
                      data-confirm-title="Save Company Settings?"
                      data-confirm-body="This will update the company details shown on all documents."
                      data-confirm-ok="Save"
                      data-confirm-danger="0">

                    <div class="row g-3">
                        <?php foreach ($fields as $f): ?>
                            <div class="<?= in_array($f['key'], ['company_address','company_name'], true) ? 'col-12' : 'col-md-6' ?>">
                                <label for="fld_<?= $f['key'] ?>" class="form-label small text-muted fw-semibold">
                                    <?= e($f['label']) ?>
                                    <?php if (!empty($f['required'])): ?><span class="text-danger">*</span><?php endif; ?>
                                </label>
                                <input
                                    type="<?= $f['type'] ?>"
                                    class="form-control"
                                    id="fld_<?= $f['key'] ?>"
                                    name="<?= e($f['key']) ?>"
                                    value="<?= e($data[$f['key']] ?? '') ?>"
                                    <?= !empty($f['required']) ? 'required' : '' ?>
                                >
                                <?php if (!empty($f['hint'])): ?>
                                    <div class="form-text small"><?= e($f['hint']) ?></div>
                                <?php endif; ?>
                            </div>
                        <?php endforeach; ?>
                    </div>

                    <hr class="my-4">
                    <div class="d-flex justify-content-end gap-2">
                        <a href="<?= url('/Home') ?>" class="btn btn-outline-secondary">Cancel</a>
                        <button type="submit" class="btn btn-primary px-4">
                            <i class="fas fa-save me-1"></i> Save Company Settings
                        </button>
                    </div>
                </form>

            </div>
        </div>

        <div class="card shadow-sm mt-3">
            <div class="card-body small text-muted">
                <i class="fas fa-info-circle me-1"></i>
                These settings are stored per-tenant. Only users with <strong>Admin</strong> or <strong>Executive</strong> roles can modify them.
                Changes are audited in the settings log.
            </div>
        </div>
    </div>
</div>