<?= $this->extend('layouts/enterprise') ?>

<?= $this->section('content') ?>

<?php
$selectedTz = old('timezone', $company['timezone']);
?>

<section class="enterprise-workspace-hero">
    <div class="enterprise-workspace-crumb">
        <span class="enterprise-crumb-pill">Administration</span>
        <span class="enterprise-crumb-sep">/</span>
        <span class="enterprise-crumb-current">Company Settings</span>
    </div>
    <h1 class="enterprise-workspace-title">Company Configuration Workspace</h1>
    <p class="enterprise-workspace-desc">
        Configure your organization identity, default currency paradigm, regional preferences,
        tax identifiers, and corporate branding assets.
    </p>
</section>

<?php if (session()->getFlashdata('errors')): ?>
    <div class="alert alert-danger py-2 small enterprise-alert">
        <ul class="mb-0 ps-3">
            <?php foreach ((array) session()->getFlashdata('errors') as $err): ?>
                <li><?= esc(is_array($err) ? implode(', ', $err) : $err) ?></li>
            <?php endforeach; ?>
        </ul>
    </div>
<?php endif; ?>

<div class="enterprise-settings-grid">
    <div class="enterprise-settings-main">
        <form action="<?= base_url('settings/company') ?>" method="post" novalidate>
            <?= csrf_field() ?>

            <article class="enterprise-settings-card">
                <div class="enterprise-settings-card__head">
                    <i class="fa-solid fa-gear"></i>
                    <h2 class="enterprise-settings-card__title">General Company Configurations</h2>
                </div>

                <div class="enterprise-settings-fields">
                    <div class="enterprise-field">
                        <label class="enterprise-field-label" for="company_name">Company Display Name</label>
                        <input type="text" id="company_name" name="name" class="form-control" value="<?= esc(old('name', $company['name'])) ?>" required>
                    </div>
                    <div class="enterprise-field">
                        <label class="enterprise-field-label" for="legal_name">Registered Legal Entity</label>
                        <input type="text" id="legal_name" name="legal_name" class="form-control" value="<?= esc(old('legal_name', $company['legal_name'])) ?>">
                    </div>
                    <div class="enterprise-field">
                        <label class="enterprise-field-label" for="currency_code">Default Currency Paradigm</label>
                        <select id="currency_code" name="currency_code" class="form-select" required>
                            <?php foreach ($currencies as $code => $meta): ?>
                                <option
                                    value="<?= esc($code) ?>"
                                    <?= old('currency_code', $company['currency_code']) === $code ? 'selected' : '' ?>
                                >
                                    <?= esc($meta['label']) ?> (<?= esc($code) ?> - <?= esc($meta['symbol']) ?>)
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="enterprise-field">
                        <label class="enterprise-field-label" for="timezone">Primary Operational Timezone</label>
                        <select id="timezone" name="timezone" class="form-select" required>
                            <?php
                            $hasSelected = array_key_exists($selectedTz, $timezones);
                            if (! $hasSelected && $selectedTz):
                            ?>
                                <option value="<?= esc($selectedTz) ?>" selected><?= esc($selectedTz) ?></option>
                            <?php endif; ?>
                            <?php foreach ($timezones as $tz => $label): ?>
                                <option value="<?= esc($tz) ?>" <?= $selectedTz === $tz ? 'selected' : '' ?>><?= esc($label) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="enterprise-field">
                        <label class="enterprise-field-label" for="commercial_registration">Commercial Registration (CR)</label>
                        <input type="text" id="commercial_registration" name="commercial_registration" class="form-control" value="<?= esc(old('commercial_registration', $company['commercial_registration'])) ?>" placeholder="e.g. 12345-1">
                    </div>
                    <div class="enterprise-field">
                        <label class="enterprise-field-label" for="tax_id">Tax / VAT Identifier</label>
                        <input type="text" id="tax_id" name="tax_id" class="form-control" value="<?= esc(old('tax_id', $company['tax_id'])) ?>">
                    </div>
                    <div class="enterprise-field">
                        <label class="enterprise-field-label" for="locale">Locale Context</label>
                        <select id="locale" name="locale" class="form-select">
                            <?php foreach ($locales as $code => $label): ?>
                                <option value="<?= esc($code) ?>" <?= old('locale', $company['locale']) === $code ? 'selected' : '' ?>><?= esc($label) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="enterprise-field">
                        <label class="enterprise-field-label" for="date_format">Date Format Standard</label>
                        <select id="date_format" name="date_format" class="form-select">
                            <?php foreach ($dateFormats as $format => $label): ?>
                                <option value="<?= esc($format) ?>" <?= old('date_format', $company['date_format']) === $format ? 'selected' : '' ?>><?= esc($label) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="enterprise-field">
                        <label class="enterprise-field-label" for="fiscal_year_start_month">Fiscal Year Commencement</label>
                        <select id="fiscal_year_start_month" name="fiscal_year_start_month" class="form-select">
                            <?php foreach ($fiscalMonths as $num => $label): ?>
                                <option value="<?= (int) $num ?>" <?= (int) old('fiscal_year_start_month', $company['fiscal_year_start_month']) === (int) $num ? 'selected' : '' ?>><?= esc($label) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="enterprise-field">
                        <label class="enterprise-field-label" for="phone">Primary Contact Line</label>
                        <input type="text" id="phone" name="phone" class="form-control" value="<?= esc(old('phone', $company['phone'])) ?>" placeholder="+973 ...">
                    </div>
                </div>
            </article>

            <article class="enterprise-settings-card">
                <div class="enterprise-settings-card__head">
                    <i class="fa-solid fa-address-card"></i>
                    <h2 class="enterprise-settings-card__title">Contact &amp; Location Registry</h2>
                </div>

                <div class="enterprise-settings-fields">
                    <div class="enterprise-field" style="grid-column: 1 / -1;">
                        <label class="enterprise-field-label" for="address">Registered Business Address</label>
                        <textarea id="address" name="address" class="form-control" rows="2"><?= esc(old('address', $company['address'])) ?></textarea>
                    </div>
                    <div class="enterprise-field">
                        <label class="enterprise-field-label" for="city">City / Municipality</label>
                        <input type="text" id="city" name="city" class="form-control" value="<?= esc(old('city', $company['city'])) ?>">
                    </div>
                    <div class="enterprise-field">
                        <label class="enterprise-field-label" for="country">Country / Territory</label>
                        <input type="text" id="country" name="country" class="form-control" value="<?= esc(old('country', $company['country'])) ?>">
                    </div>
                    <div class="enterprise-field">
                        <label class="enterprise-field-label" for="postal_code">Postal Code</label>
                        <input type="text" id="postal_code" name="postal_code" class="form-control" value="<?= esc(old('postal_code', $company['postal_code'])) ?>">
                    </div>
                    <div class="enterprise-field">
                        <label class="enterprise-field-label" for="email">Corporate Email</label>
                        <input type="email" id="email" name="email" class="form-control" value="<?= esc(old('email', $company['email'])) ?>">
                    </div>
                    <div class="enterprise-field">
                        <label class="enterprise-field-label" for="website">Public Website</label>
                        <input type="url" id="website" name="website" class="form-control" value="<?= esc(old('website', $company['website'])) ?>" placeholder="https://">
                    </div>
                </div>

                <div class="enterprise-settings-card__footer">
                    <a href="<?= base_url('settings') ?>" class="btn btn-outline-secondary">Cancel</a>
                    <button type="submit" class="btn btn-primary">Save Configurations</button>
                </div>
            </article>
        </form>
    </div>

    <aside class="enterprise-settings-aside">
        <article class="enterprise-settings-card">
            <div class="enterprise-settings-card__head">
                <i class="fa-solid fa-image"></i>
                <h2 class="enterprise-settings-card__title">Corporate Branding</h2>
            </div>
            <p class="enterprise-settings-card__intro">
                Upload your organization logo for reports, invoices, and the application header.
            </p>

            <div class="enterprise-logo-panel">
                <div class="enterprise-logo-preview">
                    <?php if (! empty($company['logo_path'])): ?>
                        <img src="<?= base_url($company['logo_path']) ?>" alt="<?= esc($company['name']) ?>">
                    <?php else: ?>
                        <i class="fa-solid fa-building"></i>
                    <?php endif; ?>
                </div>
                <div class="enterprise-logo-name"><?= esc($company['name']) ?></div>
            </div>
        </article>

        <article class="enterprise-settings-card">
            <div class="enterprise-settings-card__head">
                <i class="fa-solid fa-upload"></i>
                <h2 class="enterprise-settings-card__title">Logo Asset Upload</h2>
            </div>

            <form action="<?= base_url('settings/company/logo') ?>" method="post" enctype="multipart/form-data">
                <?= csrf_field() ?>
                <div class="enterprise-field mb-2">
                    <label class="enterprise-field-label">Brand Image File</label>
                    <input type="file" name="logo" accept="image/jpeg,image/png,image/webp" class="form-control" required>
                </div>
                <button type="submit" class="btn btn-sm btn-outline-secondary w-100">Upload Logo</button>
            </form>

            <?php if (! empty($company['logo_path'])): ?>
                <form action="<?= base_url('settings/company/logo/remove') ?>" method="post" class="mt-2" onsubmit="return confirm('Remove the company logo?');">
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-sm btn-outline-danger w-100">Remove Logo</button>
                </form>
            <?php endif; ?>

            <p class="form-text small mt-3 mb-0">JPEG, PNG or WEBP · Max 2 MB · Transparent background recommended.</p>
        </article>

        <article class="enterprise-settings-card">
            <div class="enterprise-settings-card__head">
                <i class="fa-solid fa-puzzle-piece"></i>
                <h2 class="enterprise-settings-card__title">Modules & Security</h2>
            </div>
            <p class="enterprise-settings-card__intro">
                Enable modules for your company and configure TPIN security policies.
            </p>
            <div class="enterprise-settings-card__footer">
                <a href="<?= base_url('settings/modules') ?>" class="btn btn-sm btn-outline-secondary w-100">
                    Configure Modules & TPIN
                </a>
            </div>
        </article>

        <article class="enterprise-settings-card">
            <div class="enterprise-settings-card__head">
                <i class="fa-solid fa-coins"></i>
                <h2 class="enterprise-settings-card__title">Currency Reference</h2>
            </div>
            <p class="enterprise-settings-card__intro">
                Active currency configuration applied to all financial modules and reporting outputs.
            </p>

            <div class="enterprise-settings-fields enterprise-settings-fields--single">
                <div class="enterprise-field">
                    <label class="enterprise-field-label">Active Currency Code</label>
                    <div class="enterprise-field-readonly enterprise-field-readonly--mono"><?= esc($company['currency_code']) ?></div>
                </div>
                <div class="enterprise-field">
                    <label class="enterprise-field-label">Display Symbol</label>
                    <div class="enterprise-field-readonly"><?= esc($company['currency_symbol']) ?></div>
                </div>
                <div class="enterprise-field">
                    <label class="enterprise-field-label">Decimal Precision</label>
                    <div class="enterprise-field-readonly">
                        <?= (int) $company['decimal_places'] ?> Decimal Place<?= (int) $company['decimal_places'] === 1 ? '' : 's' ?>
                    </div>
                </div>
            </div>
        </article>
    </aside>
</div>

<?= $this->endSection() ?>
