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

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

<?php
$statusPill = static function (string $status): string {
    $class = $status === 'active' ? 'active' : 'inactive';
    return '<span class="enterprise-status-pill enterprise-status-pill--' . esc($class) . '">' . esc(ucfirst($status)) . '</span>';
};
$typeLabel = static function (?string $type): string {
    return match ($type) {
        'product' => 'Product',
        'service' => 'Service',
        'project' => 'Project',
        default   => '—',
    };
};
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Contracts',
    'workspaceCrumbCurrent' => 'Site #' . $site['id'],
    'workspaceTitle'        => esc($site['name']),
    'workspaceDesc'         => 'Site profile and assigned customers.',
    'workspaceAction'       => [
        'href'  => base_url('contracts/customers/create?site_id=' . $site['id']),
        'label' => 'New Customer',
        'icon'  => 'fa-solid fa-plus',
    ],
]) ?>

<?php if (session()->getFlashdata('success')): ?>
    <div class="alert alert-success py-2 small"><?= esc(session()->getFlashdata('success')) ?></div>
<?php endif; ?>
<?php if (session()->getFlashdata('error')): ?>
    <div class="alert alert-danger py-2 small"><?= esc(session()->getFlashdata('error')) ?></div>
<?php endif; ?>

<div class="row g-4">
    <div class="col-lg-4">
        <article class="enterprise-settings-card">
            <div class="enterprise-settings-card__head">
                <i class="fa-solid fa-location-dot"></i>
                <h2 class="enterprise-settings-card__title">Site Information</h2>
            </div>
            <dl class="row mb-0 small">
                <dt class="col-sm-4 text-muted">Name</dt>
                <dd class="col-sm-8 fw-semibold"><?= esc($site['name']) ?></dd>
                <dt class="col-sm-4 text-muted">Code</dt>
                <dd class="col-sm-8"><code><?= esc($site['code']) ?></code></dd>
                <dt class="col-sm-4 text-muted">Type</dt>
                <dd class="col-sm-8"><?= esc($site['site_type'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Address</dt>
                <dd class="col-sm-8"><?= esc(trim(implode(', ', array_filter([$site['address'], $site['city'], $site['country']]))) ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Status</dt>
                <dd class="col-sm-8"><?= $statusPill($site['status']) ?></dd>
                <dt class="col-sm-4 text-muted">Description</dt>
                <dd class="col-sm-8"><?= esc($site['description'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Notes</dt>
                <dd class="col-sm-8"><?= esc($site['notes'] ?: '—') ?></dd>
            </dl>
            <div class="d-grid gap-2 mt-3">
                <a href="<?= base_url('contracts/sites/' . $site['id'] . '/edit') ?>" class="btn btn-outline-primary btn-sm">
                    <i class="fa-solid fa-pen me-1"></i> Edit Site
                </a>
                <a href="<?= base_url('contracts/sites') ?>" class="btn btn-outline-secondary btn-sm">Back to List</a>
            </div>
        </article>

        <article class="enterprise-settings-card mt-4">
            <div class="enterprise-settings-card__head">
                <i class="fa-solid fa-user-plus"></i>
                <h2 class="enterprise-settings-card__title">Assign Customer</h2>
            </div>
            <p class="small text-muted mb-3">Link an existing customer to this site.</p>
            <?php if (count($available) > 0): ?>
                <form action="<?= base_url('contracts/sites/' . $site['id'] . '/assign') ?>" method="post">
                    <?= csrf_field() ?>
                    <div class="enterprise-field mb-3">
                        <label class="enterprise-field-label">Customer</label>
                        <select name="company_id" class="form-select" required>
                            <option value="">— Select customer —</option>
                            <?php foreach ($available as $c): ?>
                                <option value="<?= $c['id'] ?>">
                                    <?= esc($c['name']) ?>
                                    <?php if ($c['code']): ?>(<?= esc($c['code']) ?>)<?php endif; ?>
                                    <?php if (! empty($c['site_id'])): ?> — on another site<?php endif; ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <button type="submit" class="btn btn-primary btn-sm w-100">
                        <i class="fa-solid fa-link me-1"></i> Assign to Site
                    </button>
                </form>
            <?php else: ?>
                <p class="small text-muted mb-0">All active customers are already assigned here, or none exist yet.</p>
                <a href="<?= base_url('contracts/customers/create?site_id=' . $site['id']) ?>" class="btn btn-outline-primary btn-sm mt-3 w-100">
                    <i class="fa-solid fa-plus me-1"></i> Create New Customer
                </a>
            <?php endif; ?>
        </article>
    </div>

    <div class="col-lg-8">
        <div class="enterprise-data-panel">
            <div class="enterprise-data-panel__head">
                <h2 class="enterprise-data-panel__title">Customers at this site</h2>
                <p class="enterprise-data-panel__subtitle"><?= count($customers) ?> customer<?= count($customers) === 1 ? '' : 's' ?> assigned.</p>
            </div>
            <div class="enterprise-data-panel__body">
                <div class="table-responsive">
                    <table class="enterprise-data-table">
                        <thead>
                            <tr>
                                <th>Customer</th>
                                <th>Type</th>
                                <th>Email</th>
                                <th>Status</th>
                                <th class="text-end">Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($customers as $c): ?>
                                <tr>
                                    <td>
                                        <a href="<?= base_url('contracts/customers/' . $c['id']) ?>" class="enterprise-data-table__primary text-decoration-none">
                                            <?= esc($c['name']) ?>
                                        </a>
                                        <?php if ($c['code']): ?>
                                            <div class="text-muted small"><code><?= esc($c['code']) ?></code></div>
                                        <?php endif; ?>
                                    </td>
                                    <td><?= esc($typeLabel($c['customer_type'] ?? null)) ?></td>
                                    <td><?= esc($c['email'] ?: '—') ?></td>
                                    <td><?= $statusPill($c['status']) ?></td>
                                    <td class="text-end">
                                        <form action="<?= base_url('contracts/sites/' . $site['id'] . '/unassign/' . $c['id']) ?>" method="post" class="d-inline"
                                              onsubmit="return confirm('Remove this customer from the site?');">
                                            <?= csrf_field() ?>
                                            <button type="submit" class="btn btn-sm btn-outline-secondary" title="Remove from site">
                                                <i class="fa-solid fa-unlink"></i>
                                            </button>
                                        </form>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                            <?php if (count($customers) === 0): ?>
                                <tr><td colspan="5" class="text-center text-muted py-5">No customers assigned to this site yet. Use Assign Customer on the left.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

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