<?php $this->extend('layouts/main'); $this->section('content'); $title = 'Customers'; ?>

<div class="d-flex justify-content-between align-items-center mb-3">
    <?php if (in_array('edit_customers', $permissions)): ?>
    <a href="/fieldly/public/index.php/customers/create" class="btn btn-primary"><i class="bi bi-plus-lg me-1"></i> New Customer</a>
    <?php endif; ?>
</div>

<!-- Tabs -->
<div class="fieldly-tabs mb-3">
    <a class="fieldly-tab <?= $tab==='active'?'active':'' ?>" href="/fieldly/public/index.php/customers?tab=active">Active <span class="badge bg-secondary ms-1"><?= $counts['active'] ?></span></a>
    <a class="fieldly-tab <?= $tab==='inactive'?'active':'' ?>" href="/fieldly/public/index.php/customers?tab=inactive">Inactive <span class="badge bg-secondary ms-1"><?= $counts['inactive'] ?></span></a>
    <a class="fieldly-tab <?= $tab==='deleted'?'active':'' ?>" href="/fieldly/public/index.php/customers?tab=deleted">Deleted <span class="badge bg-secondary ms-1"><?= $counts['deleted'] ?></span></a>
</div>

<!-- Filters -->
<form method="GET" class="row g-2 mb-3">
    <input type="hidden" name="tab" value="<?= esc($tab) ?>"/>
    <div class="col-md-6"><input type="text" name="search" class="form-control" placeholder="Search name, email, ref..." value="<?= esc($search ?? '') ?>"/></div>
    <div class="col-md-3">
        <select name="type" class="form-select">
            <option value="">All Types</option>
            <option value="individual" <?= ($type??'')==='individual'?'selected':'' ?>>Individual</option>
            <option value="business"   <?= ($type??'')==='business'?'selected':'' ?>>Business</option>
        </select>
    </div>
    <div class="col-md-2"><button type="submit" class="btn btn-outline-secondary w-100"><i class="bi bi-search"></i> Search</button></div>
    <?php if ($search || $type): ?>
    <div class="col-md-1"><a href="/fieldly/public/index.php/customers?tab=<?= esc($tab) ?>" class="btn btn-outline-danger w-100"><i class="bi bi-x"></i></a></div>
    <?php endif; ?>
</form>

<div class="card">
    <div class="card-body p-0">
        <table class="table table-hover mb-0">
            <thead><tr>
                <th>Customer</th><th>Type</th><th>Email</th><th>Phone</th><th>Status</th><th></th>
            </tr></thead>
            <tbody>
            <?php if (empty($customers)): ?>
                <tr><td colspan="6" class="text-center text-muted py-4">No customers found</td></tr>
            <?php else: ?>
                <?php foreach ($customers as $c): ?>
                <tr>
                    <td>
                        <a href="/fieldly/public/index.php/customers/<?= $c['id'] ?>" class="fw-semibold text-decoration-none"><?= esc($c['first_name'].' '.$c['last_name']) ?></a>
                        <?php if ($c['company_name']): ?><div class="text-muted small"><?= esc($c['company_name']) ?></div><?php endif; ?>
                        <div class="text-muted small"><?= esc($c['ref']) ?></div>
                    </td>
                    <td><span class="badge bg-light text-dark"><?= ucfirst(esc($c['type'])) ?></span></td>
                    <td style="font-size:13px"><?= esc($c['email']) ?></td>
                    <td style="font-size:13px"><?= esc($c['phones_list'] ?? '') ?></td>
                    <td><span class="status-badge" style="color:<?= $c['status']==='active'?'#22c55e':'#94a3b8' ?>"><?= ucfirst(esc($c['status'])) ?></span></td>
                    <td>
                        <div class="table-actions">
                            <a href="/fieldly/public/index.php/customers/<?= $c['id'] ?>" class="btn btn-sm btn-outline-secondary"><i class="bi bi-eye"></i></a>
                            <?php if (in_array('edit_customers', $permissions)): ?>
                            <a href="/fieldly/public/index.php/customers/<?= $c['id'] ?>/edit" class="btn btn-sm btn-outline-primary"><i class="bi bi-pencil"></i></a>
                            <?php if ($tab !== 'deleted'): ?>
                            <form method="POST" action="/fieldly/public/index.php/customers/<?= $c['id'] ?>/delete" data-confirm="Move this customer to the recycle bin?" class="d-inline">
                                <?= csrf_field() ?>
                                <button type="submit" class="btn btn-sm btn-outline-danger"><i class="bi bi-trash3"></i></button>
                            </form>
                            <?php else: ?>
                            <form method="POST" action="/fieldly/public/index.php/customers/<?= $c['id'] ?>/restore" class="d-inline">
                                <?= csrf_field() ?>
                                <button type="submit" class="btn btn-sm btn-outline-success"><i class="bi bi-arrow-counterclockwise"></i></button>
                            </form>
                            <?php endif; ?>
                            <?php endif; ?>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
            <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php $this->endSection(); ?>
