<?php $this->extend('layouts/main'); $this->section('content');
$title = ($customer['company_name'] ?: $customer['first_name'].' '.$customer['last_name']);
$sym = ['GBP'=>'£','EUR'=>'€','USD'=>'$'][$settings['regional']['currency']??'GBP'] ?? '£';
?>

<div class="d-flex justify-content-between align-items-start mb-3">
    <a href="/fieldly/public/index.php/customers" class="btn btn-sm btn-outline-secondary"><i class="bi bi-arrow-left me-1"></i> Customers</a>
    <div class="d-flex gap-2">
        <?php if (in_array('edit_customers', $permissions)): ?>
        <a href="/fieldly/public/index.php/customers/<?= $customer['id'] ?>/edit" class="btn btn-sm btn-outline-primary"><i class="bi bi-pencil me-1"></i> Edit</a>
        <form method="POST" action="/fieldly/public/index.php/customers/<?= $customer['id'] ?>/delete" data-confirm="Move to 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 endif; ?>
        <?php if (in_array('edit_jobs', $permissions)): ?>
        <a href="/fieldly/public/index.php/jobs/create?customer_id=<?= $customer['id'] ?>" class="btn btn-sm btn-primary"><i class="bi bi-plus me-1"></i> New Job</a>
        <?php endif; ?>
        <?php if (in_array('edit_invoices', $permissions)): ?>
        <a href="/fieldly/public/index.php/invoices/create?customer_id=<?= $customer['id'] ?>" class="btn btn-sm btn-outline-success"><i class="bi bi-receipt me-1"></i> Invoice</a>
        <?php endif; ?>
    </div>
</div>

<!-- Header Card -->
<div class="card mb-3">
    <div class="card-body">
        <div class="d-flex align-items-center gap-3">
            <div class="rounded-circle d-flex align-items-center justify-content-center fw-bold text-white" style="width:56px;height:56px;font-size:20px;background:#4f46e5;flex-shrink:0">
                <?= strtoupper(substr($customer['first_name'],0,1).substr($customer['last_name'],0,1)) ?>
            </div>
            <div>
                <h4 class="mb-0 fw-bold"><?= esc($customer['first_name'].' '.$customer['last_name']) ?></h4>
                <?php if ($customer['company_name']): ?><div class="text-muted"><?= esc($customer['company_name']) ?></div><?php endif; ?>
                <div class="d-flex gap-2 mt-1 flex-wrap">
                    <span class="badge bg-light text-dark"><?= esc($customer['ref']) ?></span>
                    <span class="badge" style="background:<?= $customer['status']==='active'?'#22c55e':'#94a3b8' ?>"><?= ucfirst(esc($customer['status'])) ?></span>
                    <span class="badge bg-light text-dark"><?= ucfirst(esc($customer['type'])) ?></span>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Tabs -->
<div class="fieldly-tabs">
    <?php foreach (['overview'=>'Overview','jobs'=>'Jobs ('.count($jobs).')','quotes'=>'Quotes ('.count($quotes).')','invoices'=>'Invoices ('.count($invoices).')','notes'=>'Notes ('.count($notes).')','files'=>'Files ('.count($files).')','addresses'=>'Addresses'] as $t=>$label): ?>
    <a class="fieldly-tab <?= $tab===$t?'active':'' ?>" href="/fieldly/public/index.php/customers/<?= $customer['id'] ?>?tab=<?= $t ?>"><?= $label ?></a>
    <?php endforeach; ?>
</div>

<?php if ($tab==='overview'): ?>
<div class="row g-3">
    <div class="col-md-6">
        <div class="card">
            <div class="card-header"><h6 class="mb-0">Contact Details</h6></div>
            <div class="card-body">
                <?php if ($customer['email']): ?><div class="detail-row"><span class="detail-label">Email</span><span class="detail-value"><a href="mailto:<?= esc($customer['email']) ?>"><?= esc($customer['email']) ?></a></span></div><?php endif; ?>
                <?php foreach ($customer['phones'] as $p): ?>
                <div class="detail-row"><span class="detail-label"><?= esc($p['type']) ?></span><span class="detail-value"><a href="tel:<?= esc($p['number']) ?>"><?= esc($p['number']) ?></a></span></div>
                <?php endforeach; ?>
                <?php if ($customer['vat_number']): ?><div class="detail-row"><span class="detail-label">VAT Number</span><span class="detail-value"><?= esc($customer['vat_number']) ?></span></div><?php endif; ?>
            </div>
        </div>
    </div>
    <div class="col-md-6">
        <div class="card">
            <div class="card-header"><h6 class="mb-0">Primary Address</h6></div>
            <div class="card-body">
                <?php $billing = array_filter($customer['addresses'], fn($a) => $a['type']==='billing'); $addr = reset($billing); ?>
                <?php if ($addr): ?>
                    <address class="mb-0" style="font-size:14px">
                        <?= esc($addr['line1']) ?><?php if ($addr['line2']): ?><br><?= esc($addr['line2']) ?><?php endif; ?>
                        <?php if ($addr['town']): ?><br><?= esc($addr['town']) ?><?php endif; ?>
                        <?php if ($addr['postcode']): ?><br><strong><?= esc($addr['postcode']) ?></strong><?php endif; ?>
                    </address>
                <?php else: ?><span class="text-muted small">No address on file</span><?php endif; ?>
            </div>
        </div>
    </div>
    <?php if ($customer['notes']): ?>
    <div class="col-12">
        <div class="card">
            <div class="card-header"><h6 class="mb-0">Notes</h6></div>
            <div class="card-body" style="font-size:14px;white-space:pre-wrap"><?= esc($customer['notes']) ?></div>
        </div>
    </div>
    <?php endif; ?>
</div>

<?php elseif ($tab==='jobs'): ?>
<div class="card">
    <div class="card-body p-0">
        <table class="table table-hover mb-0">
            <thead><tr><th>Job</th><th>Status</th><th>Scheduled</th><th>Priority</th><th></th></tr></thead>
            <tbody>
            <?php foreach ($jobs as $j): ?>
            <tr>
                <td><a href="/fieldly/public/index.php/jobs/<?= $j['id'] ?>" class="fw-semibold text-decoration-none"><?= esc($j['number']) ?> — <?= esc($j['title']) ?></a></td>
                <td><span class="badge" style="background:<?= esc($j['status_color']) ?>"><?= esc($j['status_name']) ?></span></td>
                <td style="font-size:13px"><?= $j['scheduled_date'] ? date('d M Y', strtotime($j['scheduled_date'])) : '—' ?></td>
                <td><span class="priority-<?= esc($j['priority']) ?>"><?= ucfirst(esc($j['priority'])) ?></span></td>
                <td><a href="/fieldly/public/index.php/jobs/<?= $j['id'] ?>" class="btn btn-sm btn-outline-secondary"><i class="bi bi-eye"></i></a></td>
            </tr>
            <?php endforeach; ?>
            <?php if (empty($jobs)): ?><tr><td colspan="5" class="text-center text-muted py-3">No jobs</td></tr><?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?php elseif ($tab==='invoices'): ?>
<div class="card">
    <div class="card-body p-0">
        <table class="table table-hover mb-0">
            <thead><tr><th>Invoice</th><th>Status</th><th>Total</th><th>Balance</th><th>Due</th><th></th></tr></thead>
            <tbody>
            <?php foreach ($invoices as $inv): ?>
            <tr>
                <td><a href="/fieldly/public/index.php/invoices/<?= $inv['id'] ?>" class="fw-semibold text-decoration-none"><?= esc($inv['number']) ?></a><div class="text-muted small"><?= esc($inv['title']) ?></div></td>
                <td><span class="badge bg-<?= ['paid'=>'success','overdue'=>'danger','draft'=>'secondary','sent'=>'primary','part_paid'=>'warning','void'=>'dark'][$inv['status']]??'secondary' ?>"><?= ucfirst(str_replace('_',' ',$inv['status'])) ?></span></td>
                <td><?= $sym.number_format((float)$inv['total'],2) ?></td>
                <td><?= $sym.number_format((float)$inv['balance_due'],2) ?></td>
                <td style="font-size:13px"><?= $inv['due_date'] ? date('d M Y',strtotime($inv['due_date'])) : '—' ?></td>
                <td><a href="/fieldly/public/index.php/invoices/<?= $inv['id'] ?>" class="btn btn-sm btn-outline-secondary"><i class="bi bi-eye"></i></a></td>
            </tr>
            <?php endforeach; ?>
            <?php if (empty($invoices)): ?><tr><td colspan="6" class="text-center text-muted py-3">No invoices</td></tr><?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?php elseif ($tab==='quotes'): ?>
<div class="card">
    <div class="card-body p-0">
        <table class="table table-hover mb-0">
            <thead><tr><th>Quote</th><th>Status</th><th>Total</th><th>Valid Until</th><th></th></tr></thead>
            <tbody>
            <?php foreach ($quotes as $q): ?>
            <tr>
                <td><a href="/fieldly/public/index.php/quotes/<?= $q['id'] ?>" class="fw-semibold text-decoration-none"><?= esc($q['number']) ?></a><div class="text-muted small"><?= esc($q['title']) ?></div></td>
                <td><span class="badge bg-<?= ['draft'=>'secondary','sent'=>'primary','accepted'=>'success','declined'=>'danger','converted'=>'info'][$q['status']]??'secondary' ?>"><?= ucfirst($q['status']) ?></span></td>
                <td><?= $sym.number_format((float)$q['total'],2) ?></td>
                <td style="font-size:13px"><?= $q['valid_until'] ? date('d M Y',strtotime($q['valid_until'])) : '—' ?></td>
                <td><a href="/fieldly/public/index.php/quotes/<?= $q['id'] ?>" class="btn btn-sm btn-outline-secondary"><i class="bi bi-eye"></i></a></td>
            </tr>
            <?php endforeach; ?>
            <?php if (empty($quotes)): ?><tr><td colspan="5" class="text-center text-muted py-3">No quotes</td></tr><?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?php elseif ($tab==='notes'): ?>
<div class="row">
    <div class="col-md-8">
        <?php foreach ($notes as $n): ?>
        <div class="note-item"><div class="note-text"><?= nl2br(esc($n['text'])) ?></div><div class="note-meta"><?= date('d M Y H:i',strtotime($n['created_at'])) ?></div></div>
        <?php endforeach; ?>
        <?php if (empty($notes)): ?><p class="text-muted">No notes yet.</p><?php endif; ?>
    </div>
    <?php if (in_array('manage_notes', $permissions)): ?>
    <div class="col-md-4">
        <div class="card">
            <div class="card-header"><h6 class="mb-0">Add Note</h6></div>
            <div class="card-body">
                <form method="POST" action="/fieldly/public/index.php/customers/<?= $customer['id'] ?>/note">
                    <?= csrf_field() ?>
                    <textarea name="text" class="form-control mb-2" rows="4" placeholder="Note..." required></textarea>
                    <button type="submit" class="btn btn-primary btn-sm w-100">Add Note</button>
                </form>
            </div>
        </div>
    </div>
    <?php endif; ?>
</div>

<?php elseif ($tab==='files'): ?>
<div class="row">
    <div class="col-md-8">
        <div class="card">
            <div class="card-body p-0">
                <table class="table mb-0">
                    <thead><tr><th>File</th><th>Size</th><th>Uploaded</th></tr></thead>
                    <tbody>
                    <?php foreach ($files as $f): ?>
                    <tr><td><?= esc($f['original_name']) ?></td><td style="font-size:13px"><?= round($f['file_size']/1024,1) ?>KB</td><td style="font-size:13px"><?= date('d M Y',strtotime($f['created_at'])) ?></td></tr>
                    <?php endforeach; ?>
                    <?php if (empty($files)): ?><tr><td colspan="3" class="text-center text-muted py-3">No files</td></tr><?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <?php if (in_array('manage_files', $permissions)): ?>
    <div class="col-md-4">
        <div class="card">
            <div class="card-header"><h6 class="mb-0">Upload File</h6></div>
            <div class="card-body">
                <form method="POST" action="/fieldly/public/index.php/customers/<?= $customer['id'] ?>/file" enctype="multipart/form-data">
                    <?= csrf_field() ?>
                    <input type="file" name="file" class="form-control mb-2" required/>
                    <button type="submit" class="btn btn-primary btn-sm w-100"><i class="bi bi-upload me-1"></i> Upload</button>
                </form>
            </div>
        </div>
    </div>
    <?php endif; ?>
</div>

<?php elseif ($tab==='addresses'): ?>
<div class="row g-3">
    <?php foreach ($customer['addresses'] as $addr): ?>
    <div class="col-md-6">
        <div class="card">
            <div class="card-header d-flex justify-content-between">
                <span><span class="badge bg-light text-dark me-2"><?= ucfirst(esc($addr['type'])) ?></span><?= esc($addr['label']??'') ?></span>
                <?php if (in_array('edit_customers', $permissions)): ?>
                <form method="POST" action="/fieldly/public/index.php/customers/<?= $customer['id'] ?>/address/<?= $addr['id'] ?>/delete" data-confirm="Remove this address?">
                    <?= csrf_field() ?><button type="submit" class="btn btn-sm btn-outline-danger btn-icon"><i class="bi bi-trash3"></i></button>
                </form>
                <?php endif; ?>
            </div>
            <div class="card-body" style="font-size:14px">
                <address class="mb-0"><?= esc($addr['line1']) ?><?php if ($addr['line2']): ?><br><?= esc($addr['line2']) ?><?php endif; ?><?php if ($addr['town']): ?><br><?= esc($addr['town']) ?><?php endif; ?><?php if ($addr['postcode']): ?><br><strong><?= esc($addr['postcode']) ?></strong><?php endif; ?></address>
                <?php if ($addr['access_notes']): ?><div class="mt-2 text-muted small"><?= esc($addr['access_notes']) ?></div><?php endif; ?>
            </div>
        </div>
    </div>
    <?php endforeach; ?>
    <?php if (in_array('edit_customers', $permissions)): ?>
    <div class="col-md-6">
        <div class="card border-dashed">
            <div class="card-header"><h6 class="mb-0">Add Address</h6></div>
            <div class="card-body">
                <form method="POST" action="/fieldly/public/index.php/customers/<?= $customer['id'] ?>/address">
                    <?= csrf_field() ?>
                    <div class="row g-2">
                        <div class="col-6"><select name="type" class="form-select form-select-sm"><option value="billing">Billing</option><option value="site">Site</option></select></div>
                        <div class="col-6"><input type="text" name="label" class="form-control form-control-sm" placeholder="Label (optional)"/></div>
                        <div class="col-12"><input type="text" name="line1" class="form-control form-control-sm" placeholder="Address line 1" required/></div>
                        <div class="col-12"><input type="text" name="line2" class="form-control form-control-sm" placeholder="Address line 2"/></div>
                        <div class="col-6"><input type="text" name="town" class="form-control form-control-sm" placeholder="Town"/></div>
                        <div class="col-6"><input type="text" name="postcode" class="form-control form-control-sm" placeholder="Postcode" style="text-transform:uppercase"/></div>
                        <div class="col-12"><textarea name="access_notes" class="form-control form-control-sm" rows="2" placeholder="Access notes..."></textarea></div>
                        <div class="col-12"><button type="submit" class="btn btn-primary btn-sm w-100">Add Address</button></div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <?php endif; ?>
</div>
<?php endif; ?>

<?php $this->endSection(); ?>
