<?php $this->extend('layouts/main'); $this->section('content'); $title = 'Jobs'; ?>
<div class="d-flex justify-content-between align-items-center mb-3">
    <?php if (in_array('edit_jobs', $permissions)): ?>
    <a href="/fieldly/public/index.php/jobs/create" class="btn btn-primary"><i class="bi bi-plus-lg me-1"></i> New Job</a>
    <?php endif; ?>
</div>
<div class="fieldly-tabs mb-3">
    <a class="fieldly-tab <?php echo $tab==="active"?"active":"" ?>" href="/fieldly/public/index.php/jobs?tab=active">Active <span class="badge bg-secondary ms-1"><?php echo $counts["active"] ?></span></a>
    <a class="fieldly-tab <?php echo $tab==="completed"?"active":"" ?>" href="/fieldly/public/index.php/jobs?tab=completed">Completed <span class="badge bg-secondary ms-1"><?php echo $counts["completed"] ?></span></a>
    <a class="fieldly-tab <?php echo $tab==="deleted"?"active":"" ?>" href="/fieldly/public/index.php/jobs?tab=deleted">Deleted <span class="badge bg-secondary ms-1"><?php echo $counts["deleted"] ?></span></a>
</div>
<form method="GET" class="row g-2 mb-3">
    <input type="hidden" name="tab" value="<?php echo esc($tab) ?>"/>
    <div class="col-md-5"><input type="text" name="search" class="form-control" placeholder="Search jobs..." value="<?php echo esc($search ?? "") ?>"/></div>
    <div class="col-md-3"><select name="status" class="form-select"><option value="">All Statuses</option><?php foreach ($jobStatuses as $s): ?><option value="<?php echo $s["id"] ?>" <?php echo ($status??"")==$s["id"]?"selected":"" ?>><?php echo esc($s["name"]) ?></option><?php endforeach; ?></select></div>
    <div class="col-md-2"><select name="type" class="form-select"><option value="">All Types</option><?php foreach ($jobTypes as $t): ?><option value="<?php echo $t["id"] ?>" <?php echo ($type??"")==$t["id"]?"selected":"" ?>><?php echo esc($t["name"]) ?></option><?php endforeach; ?></select></div>
    <div class="col-md-2"><button type="submit" class="btn btn-outline-secondary w-100"><i class="bi bi-search"></i> Filter</button></div>
</form>
<div class="card">
    <div class="card-body p-0">
        <table class="table table-hover mb-0">
            <thead><tr><th>Job</th><th>Customer</th><th>Status</th><th>Scheduled</th><th>Priority</th><th></th></tr></thead>
            <tbody>
            <?php if (empty($jobs)): ?>
                <tr><td colspan="6" class="text-center text-muted py-4">No jobs found</td></tr>
            <?php else: ?>
                <?php foreach ($jobs as $j): ?>
                <tr>
                    <td><a href="/fieldly/public/index.php/jobs/<?php echo $j["id"] ?>" class="fw-semibold text-decoration-none"><?php echo esc($j["number"]) ?></a><div class="text-muted small"><?php echo esc($j["title"]) ?></div></td>
                    <td style="font-size:13px"><?php echo esc($j["company_name"] ?: $j["first_name"]." ".$j["last_name"]) ?></td>
                    <td><span class="badge" style="background:<?php echo esc($j["status_color"]) ?>"><?php echo esc($j["status_name"]) ?></span></td>
                    <td style="font-size:13px"><?php echo $j["scheduled_date"] ? date("d M Y", strtotime($j["scheduled_date"])) : "—" ?></td>
                    <td><span class="priority-<?php echo esc($j["priority"]) ?>"><?php echo ucfirst(esc($j["priority"])) ?></span></td>
                    <td>
                        <div class="table-actions">
                            <a href="/fieldly/public/index.php/jobs/<?php echo $j["id"] ?>" class="btn btn-sm btn-outline-secondary"><i class="bi bi-eye"></i></a>
                            <?php if (in_array("edit_jobs", $permissions)): ?>
                            <a href="/fieldly/public/index.php/jobs/<?php echo $j["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/jobs/<?php echo $j["id"] ?>/delete" data-confirm="Move to recycle bin?" class="d-inline"><?php echo 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/jobs/<?php echo $j["id"] ?>/restore" class="d-inline"><?php echo 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(); ?>