OpenNIT

Quelltext des aktuellen Standes

Fassung 1.1.1

company.php

37 KB
<?php
$this->layout('layouts/main', [
    'page_title' => $company['name'] ?? 'Firma',
    'breadcrumbs' => [],
    'full_width' => true,
    'current_route' => 'contacts',
    'extra_css' => ['/assets/css/contacts.css?v=' . filemtime(dirname(__DIR__, 2) . '/public/assets/css/contacts.css')],
    'extra_js'  => ['/assets/js/contacts.js?v=' . filemtime(dirname(__DIR__, 2) . '/public/assets/js/contacts.js')],
]);

$company   = $company ?? [];
$contacts  = $contacts ?? [];
$canManage = $can_manage ?? false;
$moveTargets = $move_targets ?? [];
$canMove   = $canManage && !empty($contacts) && !empty($moveTargets);
$csrfToken = $this->csrf();
$view      = $this;

// Normalisiert eine Telefonnummer fuer tel:-Links (nur Ziffern und fuehrendes +).
$telHref = function (string $number): string {
    return 'tel:' . preg_replace('/(?!^\+)[^0-9]/', '', $number);
};

// Rendert einen Kontaktkanal als klickbaren Chip (tel:/mailto:/url) oder Text.
// $variant: 'chip' (Standard, Sidebar) oder 'inline' (schlanke Zeile in der Kontaktliste).
$channelChip = function (array $ch, string $variant = 'chip') use ($view, $telHref): string {
    $value = (string)($ch['value'] ?? '');
    if ($value === '') {
        return '';
    }
    $type = $ch['channel_type'] ?? 'other';
    $label = trim((string)($ch['label'] ?? ''));
    $display = $label !== '' ? $label . ': ' . $value : $value;
    $cls = $variant === 'inline' ? 'cb-contact-link' : 'cb-action-chip';

    $href = match ($type) {
        'phone', 'mobile', 'fax' => $telHref($value),
        'email'   => 'mailto:' . $value,
        // Defense-in-Depth: nur http/https als Link rendern
        'website' => preg_match('#^https?://#i', $value) ? $value : null,
        default   => null,
    };

    if ($href === null) {
        return '<span class="' . $cls . '">' . $view->e($display) . '</span>';
    }
    $rel = $type === 'website' ? ' target="_blank" rel="noopener"' : '';
    return '<a href="' . $view->e($href) . '"' . $rel . ' class="' . $cls . '">' . $view->e($display) . '</a>';
};
?>

<div class="cb-fullwidth">
    <div class="content-header">
        <div class="cb-header-with-logo">
            <?php if (!empty($company['image_path'])): ?>
                <img src="<?= $view->e($company['image_path']) ?>" alt="<?= $this->te('Logo von {firma}', ['firma' => $company['name'] ?? '']) ?>" class="cb-company-logo">
            <?php endif; ?>
            <div>
                <h1><?= $view->e($company['name'] ?? '') ?></h1>
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="/"><?= $this->te('Home') ?></a></li>
                    <li class="breadcrumb-item"><a href="/contacts"><?= $this->te('Adressbuch') ?></a></li>
                    <li class="breadcrumb-item active"><?= $view->e($company['name'] ?? '') ?></li>
                </ol>
            </div>
        </div>
        <div class="content-header-actions">
            <?php if (!empty($contacts)): ?>
                <a class="btn btn-default" href="/contacts/companies/<?= (int)$company['id'] ?>/export/vcf" title="<?= $this->te('Alle Kontakte als vCard exportieren') ?>"><?= $this->te('vCard') ?></a>
                <a class="btn btn-default" href="/contacts/companies/<?= (int)$company['id'] ?>/export/csv" title="<?= $this->te('Alle Kontakte als CSV exportieren') ?>">CSV</a>
            <?php endif; ?>
            <?php if ($canMove): ?>
                <button type="button" class="btn btn-default" data-modal-open="moveContactsModal" title="<?= $this->te('Kontakte in eine andere Firma verschieben') ?>"><?= $this->te('Verschieben') ?></button>
            <?php endif; ?>
            <?php if ($canManage): ?>
                <button type="button" class="btn btn-default" data-modal-open="editCompanyModal"><?= $this->te('Bearbeiten') ?></button>
                <button type="button" class="btn btn-primary" data-modal-open="newContactModal">
                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="18" height="18" aria-hidden="true" focusable="false"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
                    <?= $this->te('Kontakt hinzufügen') ?>
                </button>
            <?php endif; ?>
        </div>
    </div>

    <div class="cb-body">
        <div class="cb-layout">
            <!-- Main: Kontakte -->
            <main class="cb-main">
                <?php if (empty($contacts)): ?>
                    <div class="cb-empty">
                        <span class="cb-empty-icon" aria-hidden="true">&#128100;</span>
                        <h3><?= $this->te('Keine Kontakte') ?></h3>
                        <p><?= $this->te('F&uuml;r diese Firma wurden noch keine Kontakte angelegt.') ?></p>
                        <?php if ($canManage): ?>
                            <button type="button" class="cb-btn cb-btn-primary" data-modal-open="newContactModal"><?= $this->te('Ersten Kontakt anlegen') ?></button>
                        <?php endif; ?>
                    </div>
                <?php else: ?>
                    <div class="cb-contacts-list">
                        <?php foreach ($contacts as $contact):
                            $cName = trim(($contact['last_name'] ?? '') . ', ' . ($contact['first_name'] ?? ''));
                        ?>
                            <div class="cb-contact-card">
                                <?php if (!empty($contact['image_path'])): ?>
                                    <img src="<?= $view->e($contact['image_path']) ?>" alt="<?= $this->te('Foto von {name}', ['name' => $cName]) ?>" class="cb-contact-avatar-img">
                                <?php else: ?>
                                <div class="cb-contact-avatar" aria-hidden="true">
                                    <?= $view->e(strtoupper(mb_substr($contact['first_name'] ?? '?', 0, 1))) ?><?= $view->e(strtoupper(mb_substr($contact['last_name'] ?? '?', 0, 1))) ?>
                                </div>
                                <?php endif; ?>
                                <div class="cb-contact-main">
                                    <div class="cb-contact-name"><?= $view->e($cName) ?></div>
                                    <?php if (!empty($contact['position'])): ?>
                                        <div class="cb-contact-position"><?= $view->e($contact['position']) ?></div>
                                    <?php endif; ?>
                                </div>
                                <div class="cb-contact-details">
                                    <?php if (!empty($contact['phone'])): ?>
                                        <a href="<?= $view->e($telHref($contact['phone'])) ?>" class="cb-contact-link">
                                            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="14" height="14" aria-hidden="true" focusable="false"><path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72c.127.96.361 1.903.7 2.81a2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45c.907.339 1.85.573 2.81.7A2 2 0 0 1 22 16.92z"/></svg>
                                            <?= $view->e($contact['phone']) ?>
                                        </a>
                                    <?php endif; ?>
                                    <?php if (!empty($contact['mobile'])): ?>
                                        <a href="<?= $view->e($telHref($contact['mobile'])) ?>" class="cb-contact-link">
                                            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="14" height="14" aria-hidden="true" focusable="false"><rect x="5" y="2" width="14" height="20" rx="2" ry="2"/><line x1="12" y1="18" x2="12.01" y2="18"/></svg>
                                            <?= $view->e($contact['mobile']) ?>
                                        </a>
                                    <?php endif; ?>
                                    <?php if (!empty($contact['email'])): ?>
                                        <a href="mailto:<?= $view->e($contact['email']) ?>" class="cb-contact-link">
                                            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="14" height="14" aria-hidden="true" focusable="false"><path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"/><polyline points="22,6 12,13 2,6"/></svg>
                                            <?= $view->e($contact['email']) ?>
                                        </a>
                                    <?php endif; ?>
                                    <?php foreach ($contact['channels'] ?? [] as $ch): ?>
                                        <?= $channelChip($ch, 'inline') ?>
                                    <?php endforeach; ?>
                                </div>
                                <?php if (!empty($contact['notes'])): ?>
                                    <div class="cb-contact-notes"><?= $view->e($contact['notes']) ?></div>
                                <?php endif; ?>
                                <div class="cb-contact-actions">
                                    <a class="cb-btn-icon" href="/contacts/contacts/<?= (int)$contact['id'] ?>/vcard" title="<?= $this->te('Als vCard exportieren') ?>" aria-label="<?= $this->te('Kontakt {name} als vCard exportieren', ['name' => $cName]) ?>">&#11015;</a>
                                    <?php if ($canMove): ?>
                                        <button type="button" class="cb-btn-icon" data-modal-open="moveContactsModal" data-move-contact="<?= (int)$contact['id'] ?>" title="<?= $this->te('In andere Firma verschieben') ?>" aria-label="Kontakt <?= $view->e($cName) ?> in andere Firma verschieben">&#8646;</button>
                                    <?php endif; ?>
                                    <?php if ($canManage): ?>
                                        <button type="button" class="cb-btn-icon" data-modal-open="editContact<?= (int)$contact['id'] ?>Modal" title="<?= $this->te('Bearbeiten') ?>" aria-label="<?= $this->te('Kontakt {name} bearbeiten', ['name' => $cName]) ?>">&#9998;</button>
                                        <form action="/contacts/contacts/<?= (int)$contact['id'] ?>" method="POST" class="cb-inline-form"
                                              data-confirm="Kontakt wirklich löschen?">
                                            <input type="hidden" name="_csrf" value="<?= $view->e($csrfToken) ?>">
                                            <input type="hidden" name="_method" value="DELETE">
                                            <button type="submit" class="cb-btn-icon cb-btn-danger" title="<?= $this->te('Löschen') ?>" aria-label="<?= $this->te('Kontakt {name} löschen', ['name' => $cName]) ?>">&#128465;</button>
                                        </form>
                                    <?php endif; ?>
                                </div>
                            </div>

                            <?php if ($canManage): ?>
                            <!-- Edit Contact Modal -->
                            <div id="editContact<?= (int)$contact['id'] ?>Modal" class="cb-modal" role="dialog" aria-modal="true" aria-labelledby="editContact<?= (int)$contact['id'] ?>Title" hidden>
                                <div class="cb-modal-backdrop" data-close></div>
                                <div class="cb-modal-dialog">
                                    <div class="cb-modal-header">
                                        <h2 id="editContact<?= (int)$contact['id'] ?>Title"><?= $this->te('Kontakt bearbeiten') ?></h2>
                                        <button type="button" class="cb-modal-close" data-close aria-label="<?= $this->te('Dialog schließen') ?>">&times;</button>
                                    </div>
                                    <form action="/contacts/contacts/<?= (int)$contact['id'] ?>" method="POST" enctype="multipart/form-data">
                                        <input type="hidden" name="_csrf" value="<?= $view->e($csrfToken) ?>">
                                        <input type="hidden" name="_method" value="PUT">
                                        <div class="cb-modal-body">
                                            <div class="cb-form-group">
                                                <label><?= $this->te('Foto') ?></label>
                                                <?php if (!empty($contact['image_path'])): ?>
                                                    <div class="cb-image-preview">
                                                        <img src="<?= $view->e($contact['image_path']) ?>" alt="<?= $this->te('Aktuelles Foto von {name}', ['name' => $cName]) ?>">
                                                        <label class="cb-image-remove"><input type="checkbox" name="remove_image" value="1"> <?= $this->te('Bild entfernen') ?></label>
                                                    </div>
                                                <?php endif; ?>
                                                <input type="file" name="image" class="cb-input" accept="image/jpeg,image/png,image/gif,image/webp">
                                            </div>
                                            <div class="cb-form-row">
                                                <div class="cb-form-group">
                                                    <label><?= $this->te('Vorname *') ?></label>
                                                    <input type="text" name="first_name" class="cb-input" value="<?= $view->e($contact['first_name'] ?? '') ?>" required>
                                                </div>
                                                <div class="cb-form-group">
                                                    <label><?= $this->te('Nachname *') ?></label>
                                                    <input type="text" name="last_name" class="cb-input" value="<?= $view->e($contact['last_name'] ?? '') ?>" required>
                                                </div>
                                            </div>
                                            <div class="cb-form-group">
                                                <label><?= $this->te('Position') ?></label>
                                                <input type="text" name="position" class="cb-input" value="<?= $view->e($contact['position'] ?? '') ?>">
                                            </div>
                                            <div class="cb-form-row">
                                                <div class="cb-form-group">
                                                    <label><?= $this->te('Telefon') ?></label>
                                                    <input type="text" name="phone" class="cb-input" value="<?= $view->e($contact['phone'] ?? '') ?>">
                                                </div>
                                                <div class="cb-form-group">
                                                    <label><?= $this->te('Mobil') ?></label>
                                                    <input type="text" name="mobile" class="cb-input" value="<?= $view->e($contact['mobile'] ?? '') ?>">
                                                </div>
                                            </div>
                                            <div class="cb-form-group">
                                                <label><?= $this->t('E-Mail') ?></label>
                                                <input type="email" name="email" class="cb-input" value="<?= $view->e($contact['email'] ?? '') ?>">
                                            </div>
                                            <?= $view->render('contacts/_channel_fields', ['channels' => $contact['channels'] ?? []]) ?>
                                            <div class="cb-form-group">
                                                <label><?= $this->te('Notizen') ?></label>
                                                <textarea name="notes" class="cb-input cb-textarea" rows="2"><?= $view->e($contact['notes'] ?? '') ?></textarea>
                                            </div>
                                        </div>
                                        <div class="cb-modal-footer">
                                            <button type="button" class="cb-btn cb-btn-secondary" data-close><?= $this->te('Abbrechen') ?></button>
                                            <button type="submit" class="cb-btn cb-btn-primary"><?= $this->te('Speichern') ?></button>
                                        </div>
                                    </form>
                                </div>
                            </div>
                            <?php endif; ?>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </main>

            <!-- Sidebar: Firmendaten -->
            <aside class="cb-sidebar">
                <div class="cb-sidebar-card">
                    <h3><?= $this->te('Firmendaten') ?></h3>
                    <dl class="cb-info-list">
                        <?php if (!empty($company['customer_number'])): ?>
                            <dt><?= $this->te('Kundennummer') ?></dt>
                            <dd><?= $view->e($company['customer_number']) ?></dd>
                        <?php endif; ?>
                        <?php if (!empty($company['address'])): ?>
                            <dt><?= $this->te('Adresse') ?></dt>
                            <dd class="cb-info-address">
                                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="14" height="14" aria-hidden="true" focusable="false"><path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z"/><circle cx="12" cy="10" r="3"/></svg>
                                <span><?= nl2br($view->e($company['address'])) ?></span>
                            </dd>
                        <?php endif; ?>
                        <?php if (!empty($company['phone'])): ?>
                            <dt><?= $this->te('Telefon') ?></dt>
                            <dd>
                                <a href="<?= $view->e($telHref($company['phone'])) ?>" class="cb-action-chip cb-action-chip-sm">
                                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="12" height="12" aria-hidden="true" focusable="false"><path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72c.127.96.361 1.903.7 2.81a2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45c.907.339 1.85.573 2.81.7A2 2 0 0 1 22 16.92z"/></svg>
                                    <?= $view->e($company['phone']) ?>
                                </a>
                            </dd>
                        <?php endif; ?>
                        <?php if (!empty($company['email'])): ?>
                            <dt><?= $this->t('E-Mail') ?></dt>
                            <dd>
                                <a href="mailto:<?= $view->e($company['email']) ?>" class="cb-action-chip cb-action-chip-sm">
                                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="12" height="12" aria-hidden="true" focusable="false"><path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"/><polyline points="22,6 12,13 2,6"/></svg>
                                    <?= $view->e($company['email']) ?>
                                </a>
                            </dd>
                        <?php endif; ?>
                        <?php if (!empty($company['website'])): ?>
                            <dt><?= $this->te('Website') ?></dt>
                            <dd>
                                <a href="<?= $view->e($company['website']) ?>" target="_blank" rel="noopener" class="cb-action-chip cb-action-chip-sm">
                                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="12" height="12" aria-hidden="true" focusable="false"><circle cx="12" cy="12" r="10"/><line x1="2" y1="12" x2="22" y2="12"/><path d="M12 2a15.3 15.3 0 0 1 4 10 15.3 15.3 0 0 1-4 10 15.3 15.3 0 0 1-4-10 15.3 15.3 0 0 1 4-10z"/></svg>
                                    <?= $view->e($company['website']) ?>
                                </a>
                            </dd>
                        <?php endif; ?>
                        <?php if (!empty($company['channels'])): ?>
                            <dt><?= $this->te('Weitere Kontaktwege') ?></dt>
                            <dd>
                                <div class="cb-channel-chips">
                                    <?php foreach ($company['channels'] as $ch): ?>
                                        <?= $channelChip($ch) ?>
                                    <?php endforeach; ?>
                                </div>
                            </dd>
                        <?php endif; ?>
                        <?php if (!empty($company['notes'])): ?>
                            <dt><?= $this->te('Notizen') ?></dt>
                            <dd><?= nl2br($view->e($company['notes'])) ?></dd>
                        <?php endif; ?>
                        <dt><?= $this->te('Erstellt') ?></dt>
                        <dd><?= $view->e($view->date($company['created_at'] ?? null, 'd.m.Y')) ?></dd>
                        <?php if (!empty($company['created_by_name'])): ?>
                            <dt><?= $this->te('Erstellt von') ?></dt>
                            <dd><?= $view->e($company['created_by_name']) ?></dd>
                        <?php endif; ?>
                    </dl>
                </div>

                <?php
                $companyContracts = $company_contracts ?? [];
                $activeContracts = array_filter($companyContracts, fn($c) => in_array($c['status'], ['active', 'expiring']));
                $displayContracts = array_slice($companyContracts, 0, 5);
                ?>
                <?php if (!empty($companyContracts)): ?>
                <div class="cb-sidebar-card">
                    <h3>
                        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16" style="vertical-align:-2px;margin-right:2px" aria-hidden="true" focusable="false"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/></svg>
                        <?= $this->te('Vertr&auml;ge ({anzahl} aktiv)', ['anzahl' => count($activeContracts)]) ?>
                    </h3>
                    <div class="cb-contract-list">
                        <?php foreach ($displayContracts as $ct):
                            $ctStatusLabels = ['active' => 'Aktiv', 'expiring' => 'K&uuml;ndigung', 'expired' => 'Abgelaufen', 'cancelled' => 'Gek&uuml;ndigt', 'future' => 'Zuk&uuml;nftig'];
                            if ($ct['status'] === 'expiring' && empty($ct['auto_renew']) && empty($ct['cancellation_notice_date'])) {
                                $ctStatusLabels['expiring'] = 'L&auml;uft aus';
                            }
                        ?>
                        <a href="/contracts/<?= (int)$ct['id'] ?>" class="cb-contract-item">
                            <span class="cb-contract-status cb-cstatus-<?= $view->e($ct['status']) ?>">
                                <span class="cb-cstatus-dot"></span>
                                <?= $ctStatusLabels[$ct['status']] ?? $view->e($ct['status']) ?>
                            </span>
                            <span class="cb-contract-title"><?= $view->e($ct['title']) ?></span>
                            <?php if (!empty($ct['cost_amount'])): ?>
                                <?php
                                $intervalSuffix = match ($ct['cost_interval'] ?? 'monthly') {
                                    'yearly'    => '/J.',
                                    'quarterly' => '/Qu.',
                                    'one_time'  => ' einmalig',
                                    default     => '/Mt.',
                                };
                                ?>
                                <span class="cb-contract-cost"><?= $this->e($this->money((float)$ct['cost_amount'])) ?><?= $intervalSuffix ?></span>
                            <?php endif; ?>
                        </a>
                        <?php endforeach; ?>
                    </div>
                    <a href="/contracts/company/<?= (int)$company['id'] ?>" class="cb-contract-link">
                        <?php if (count($companyContracts) > 5): ?>
                            <?= $this->te('Alle {anzahl} Vertr&auml;ge &amp; Dokumente &rarr;', ['anzahl' => count($companyContracts)]) ?>
                        <?php else: ?>
                            <?= $this->te('Vertr&auml;ge &amp; Angebote &amp; Dokumente &rarr;') ?>
                        <?php endif; ?>
                    </a>
                </div>
                <?php elseif ($can_view_contracts ?? false): ?>
                <div class="cb-sidebar-card">
                    <h3>
                        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16" style="vertical-align:-2px;margin-right:2px" aria-hidden="true" focusable="false"><path d="M21.44 11.05l-9.19 9.19a6 6 0 0 1-8.49-8.49l9.19-9.19a4 4 0 0 1 5.66 5.66l-9.2 9.19a2 2 0 0 1-2.83-2.83l8.49-8.48"/></svg>
                        <?= $this->te('Dokumente &amp; Angebote') ?>
                    </h3>
                    <a href="/contracts/company/<?= (int)$company['id'] ?>" class="cb-contract-link">
                        <?= $this->te('Firmendokumente &amp; Angebote verwalten &rarr;') ?>
                    </a>
                </div>
                <?php endif; ?>

                <?php if ($canManage): ?>
                <div class="cb-sidebar-card">
                    <h3><?= $this->te('Aktionen') ?></h3>
                    <div class="cb-sidebar-actions">
                        <button type="button" class="cb-action-btn" data-modal-open="editCompanyModal">
                            <span aria-hidden="true">&#9998;</span> <?= $this->te('Firma bearbeiten') ?>
                        </button>
                        <form action="/contacts/companies/<?= (int)$company['id'] ?>" method="POST"
                              data-confirm="Firma und alle Kontakte wirklich löschen?">
                            <input type="hidden" name="_csrf" value="<?= $view->e($csrfToken) ?>">
                            <input type="hidden" name="_method" value="DELETE">
                            <button type="submit" class="cb-action-btn cb-action-danger">
                                <span aria-hidden="true">&#128465;</span> <?= $this->te('Firma l&ouml;schen') ?>
                            </button>
                        </form>
                    </div>
                </div>
                <?php endif; ?>
            </aside>
        </div>
    </div>
</div>

<?php if ($canMove): ?>
<!-- Move Contacts Modal -->
<div id="moveContactsModal" class="cb-modal" role="dialog" aria-modal="true" aria-labelledby="moveContactsTitle" hidden>
    <div class="cb-modal-backdrop" data-close></div>
    <div class="cb-modal-dialog">
        <div class="cb-modal-header">
            <h2 id="moveContactsTitle"><?= $this->te('Kontakte verschieben') ?></h2>
            <button type="button" class="cb-modal-close" data-close aria-label="<?= $this->te('Dialog schließen') ?>">&times;</button>
        </div>
        <form action="/contacts/companies/<?= (int)$company['id'] ?>/contacts/move" method="POST">
            <input type="hidden" name="_csrf" value="<?= $view->e($csrfToken) ?>">
            <div class="cb-modal-body">
                <div class="cb-form-group">
                    <label for="f-47f1-target_company_id"><?= $this->te('Ziel-Firma *') ?></label>
                    <select id="f-47f1-target_company_id" name="target_company_id" class="cb-input" required>
                        <option value=""><?= $this->te('– Firma auswählen –') ?></option>
                        <?php foreach ($moveTargets as $target): ?>
                            <option value="<?= (int)$target['id'] ?>"><?= $view->e($target['name']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="cb-form-group">
                    <label><?= $this->te('Kontakte') ?></label>
                    <label class="cb-move-item cb-move-all">
                        <input type="checkbox" data-check-all="contact_ids[]">
                        <span><?= $this->te('Alle ausw&auml;hlen ({anzahl})', ['anzahl' => count($contacts)]) ?></span>
                    </label>
                    <div class="cb-move-list">
                        <?php foreach ($contacts as $contact):
                            $mName = trim(($contact['last_name'] ?? '') . ', ' . ($contact['first_name'] ?? ''));
                        ?>
                            <label class="cb-move-item">
                                <input type="checkbox" name="contact_ids[]" value="<?= (int)$contact['id'] ?>">
                                <span><?= $view->e($mName) ?><?php if (!empty($contact['position'])): ?> <small>&middot; <?= $view->e($contact['position']) ?></small><?php endif; ?></span>
                            </label>
                        <?php endforeach; ?>
                    </div>
                </div>
            </div>
            <div class="cb-modal-footer">
                <button type="button" class="cb-btn cb-btn-secondary" data-close><?= $this->te('Abbrechen') ?></button>
                <button type="submit" class="cb-btn cb-btn-primary"><?= $this->te('Verschieben') ?></button>
            </div>
        </form>
    </div>
</div>
<?php endif; ?>

<?php if ($canManage): ?>
<!-- New Contact Modal -->
<div id="newContactModal" class="cb-modal" role="dialog" aria-modal="true" aria-labelledby="newContactTitle" hidden>
    <div class="cb-modal-backdrop" data-close></div>
    <div class="cb-modal-dialog">
        <div class="cb-modal-header">
            <h2 id="newContactTitle"><?= $this->te('Neuen Kontakt anlegen') ?></h2>
            <button type="button" class="cb-modal-close" data-close aria-label="<?= $this->te('Dialog schließen') ?>">&times;</button>
        </div>
        <form action="/contacts/companies/<?= (int)$company['id'] ?>/contacts" method="POST" enctype="multipart/form-data">
            <input type="hidden" name="_csrf" value="<?= $view->e($csrfToken) ?>">
            <div class="cb-modal-body">
                <div class="cb-form-group">
                    <label for="f-47f1-image"><?= $this->te('Foto') ?></label>
                    <input id="f-47f1-image" type="file" name="image" class="cb-input" accept="image/jpeg,image/png,image/gif,image/webp">
                </div>
                <div class="cb-form-row">
                    <div class="cb-form-group">
                        <label for="f-47f1-first_name"><?= $this->te('Vorname *') ?></label>
                        <input id="f-47f1-first_name" type="text" name="first_name" class="cb-input" required>
                    </div>
                    <div class="cb-form-group">
                        <label for="f-47f1-last_name"><?= $this->te('Nachname *') ?></label>
                        <input id="f-47f1-last_name" type="text" name="last_name" class="cb-input" required>
                    </div>
                </div>
                <div class="cb-form-group">
                    <label for="f-47f1-position"><?= $this->te('Position') ?></label>
                    <input id="f-47f1-position" type="text" name="position" class="cb-input">
                </div>
                <div class="cb-form-row">
                    <div class="cb-form-group">
                        <label for="f-47f1-phone"><?= $this->te('Telefon') ?></label>
                        <input id="f-47f1-phone" type="text" name="phone" class="cb-input">
                    </div>
                    <div class="cb-form-group">
                        <label for="f-47f1-mobile"><?= $this->te('Mobil') ?></label>
                        <input id="f-47f1-mobile" type="text" name="mobile" class="cb-input">
                    </div>
                </div>
                <div class="cb-form-group">
                    <label for="f-47f1-email"><?= $this->t('E-Mail') ?></label>
                    <input id="f-47f1-email" type="email" name="email" class="cb-input">
                </div>
                <?= $view->render('contacts/_channel_fields', ['channels' => []]) ?>
                <div class="cb-form-group">
                    <label for="f-47f1-notes"><?= $this->te('Notizen') ?></label>
                    <textarea id="f-47f1-notes" name="notes" class="cb-input cb-textarea" rows="2"></textarea>
                </div>
            </div>
            <div class="cb-modal-footer">
                <button type="button" class="cb-btn cb-btn-secondary" data-close><?= $this->te('Abbrechen') ?></button>
                <button type="submit" class="cb-btn cb-btn-primary"><?= $this->te('Kontakt erstellen') ?></button>
            </div>
        </form>
    </div>
</div>

<!-- Edit Company Modal -->
<div id="editCompanyModal" class="cb-modal" role="dialog" aria-modal="true" aria-labelledby="editCompanyTitle" hidden>
    <div class="cb-modal-backdrop" data-close></div>
    <div class="cb-modal-dialog">
        <div class="cb-modal-header">
            <h2 id="editCompanyTitle"><?= $this->te('Firma bearbeiten') ?></h2>
            <button type="button" class="cb-modal-close" data-close aria-label="<?= $this->te('Dialog schließen') ?>">&times;</button>
        </div>
        <form action="/contacts/companies/<?= (int)$company['id'] ?>" method="POST" enctype="multipart/form-data">
            <input type="hidden" name="_csrf" value="<?= $view->e($csrfToken) ?>">
            <input type="hidden" name="_method" value="PUT">
            <div class="cb-modal-body">
                <div class="cb-form-group">
                    <label><?= $this->te('Firmenlogo') ?></label>
                    <?php if (!empty($company['image_path'])): ?>
                        <div class="cb-image-preview">
                            <img src="<?= $view->e($company['image_path']) ?>" alt="<?= $this->te('Aktuelles Logo von {firma}', ['firma' => $company['name'] ?? '']) ?>">
                            <label class="cb-image-remove"><input type="checkbox" name="remove_image" value="1"> <?= $this->te('Bild entfernen') ?></label>
                        </div>
                    <?php endif; ?>
                    <input type="file" name="image" class="cb-input" accept="image/jpeg,image/png,image/gif,image/webp">
                </div>
                <div class="cb-form-row">
                    <div class="cb-form-group">
                        <label for="f-47f1-name"><?= $this->te('Firmenname *') ?></label>
                        <input id="f-47f1-name" type="text" name="name" class="cb-input" value="<?= $view->e($company['name'] ?? '') ?>" required>
                    </div>
                    <div class="cb-form-group">
                        <label><?= $this->te('Kundennummer') ?></label>
                        <input type="text" name="customer_number" class="cb-input" value="<?= $view->e($company['customer_number'] ?? '') ?>" placeholder="<?= $this->te('z.B. KD-12345') ?>">
                    </div>
                </div>
                <div class="cb-form-group">
                    <label for="f-47f1-address"><?= $this->te('Adresse') ?></label>
                    <textarea id="f-47f1-address" name="address" class="cb-input cb-textarea" rows="3"><?= $view->e($company['address'] ?? '') ?></textarea>
                </div>
                <div class="cb-form-row">
                    <div class="cb-form-group">
                        <label><?= $this->te('Telefon') ?></label>
                        <input type="text" name="phone" class="cb-input" value="<?= $view->e($company['phone'] ?? '') ?>">
                    </div>
                    <div class="cb-form-group">
                        <label><?= $this->t('E-Mail') ?></label>
                        <input type="email" name="email" class="cb-input" value="<?= $view->e($company['email'] ?? '') ?>">
                    </div>
                </div>
                <div class="cb-form-group">
                    <label><?= $this->te('Website') ?></label>
                    <input type="url" name="website" class="cb-input" value="<?= $view->e($company['website'] ?? '') ?>" placeholder="https://...">
                </div>
                <?= $view->render('contacts/_channel_fields', ['channels' => $company['channels'] ?? []]) ?>
                <div class="cb-form-group">
                    <label><?= $this->te('Notizen') ?></label>
                    <textarea name="notes" class="cb-input cb-textarea" rows="2"><?= $view->e($company['notes'] ?? '') ?></textarea>
                </div>
            </div>
            <div class="cb-modal-footer">
                <button type="button" class="cb-btn cb-btn-secondary" data-close><?= $this->te('Abbrechen') ?></button>
                <button type="submit" class="cb-btn cb-btn-primary"><?= $this->te('Speichern') ?></button>
            </div>
        </form>
    </div>
</div>
<?php endif; ?>

<?= $this->partial('image_lightbox') ?>