<?php $this->extend("layouts/main"); $this->section("content"); $title = "Time Tracking"; ?>
<div class="d-flex justify-content-between align-items-center mb-3">
    <div class="d-flex gap-3">
        <div><div class="text-muted small">Total</div><div class="fw-bold"><?php echo floor($totalMins/60) ?>h <?php echo $totalMins%60 ?>m</div></div>
        <div><div class="text-muted small">Billable</div><div class="fw-bold text-success"><?php echo floor($billableMins/60) ?>h <?php echo $billableMins%60 ?>m</div></div>
    </div>
    <?php if (in_array("log_time", $permissions)): ?>
    <button class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#logModal"><i class="bi bi-plus me-1"></i> Log Time</button>
    <?php endif; ?>
</div>
<form method="GET" class="row g-2 mb-3">
    <?php if (in_array("edit_time", $permissions)): ?><div class="col-md-3"><select name="user_id" class="form-select"><option value="">All Engineers</option><?php foreach ($users as $u): ?><option value="<?php echo $u["id"] ?>" <?php echo ($userId??"")==$u["id"]?"selected":"" ?>><?php echo esc($u["first_name"]." ".$u["last_name"]) ?></option><?php endforeach; ?></select></div><?php endif; ?>
    <div class="col-md-3"><select name="job_id" class="form-select"><option value="">All Jobs</option><?php foreach ($jobs as $j): ?><option value="<?php echo $j["id"] ?>" <?php echo ($jobId??"")==$j["id"]?"selected":"" ?>><?php echo esc($j["number"]) ?> <?php echo esc($j["title"]) ?></option><?php endforeach; ?></select></div>
    <div class="col-md-2"><input type="date" name="date_from" class="form-control" value="<?php echo esc($dateFrom??"") ?>"/></div>
    <div class="col-md-2"><input type="date" name="date_to" class="form-control" value="<?php echo esc($dateTo??"") ?>"/></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>Date</th><th>Engineer</th><th>Job</th><th>Duration</th><th>Billable</th><th>Notes</th><th></th></tr></thead>
<tbody>
<?php foreach ($entries as $e): ?>
<tr>
    <td style="font-size:13px"><?php echo date("d M Y",strtotime($e["date"])) ?></td>
    <td style="font-size:13px"><?php echo esc($e["first_name"]." ".$e["last_name"]) ?></td>
    <td style="font-size:13px"><?php if ($e["job_id"]): ?><a href="/fieldly/public/index.php/jobs/<?php echo $e["job_id"] ?>"><?php echo esc($e["job_number"]) ?></a><?php endif; ?></td>
    <td class="fw-bold"><?php echo floor($e["duration_mins"]/60) ?>h <?php echo $e["duration_mins"]%60 ?>m</td>
    <td><?php echo $e["billable"]?"<span class="badge bg-success">Yes</span>":"<span class="badge bg-secondary">No</span>" ?></td>
    <td style="font-size:13px"><?php echo esc($e["notes"]) ?></td>
    <td><?php if (in_array("edit_time", $permissions)): ?><form method="POST" action="/fieldly/public/index.php/time/<?php echo $e["id"] ?>/delete" data-confirm="Delete this entry?"><?php echo csrf_field() ?><button class="btn btn-sm btn-outline-danger btn-icon"><i class="bi bi-trash3"></i></button></form><?php endif; ?></td>
</tr>
<?php endforeach; ?>
<?php if (empty($entries)): ?><tr><td colspan="7" class="text-center text-muted py-4">No time entries</td></tr><?php endif; ?>
</tbody>
</table></div></div>
<!-- Log Time Modal -->
<div class="modal fade" id="logModal" tabindex="-1"><div class="modal-dialog modal-dialog-centered"><div class="modal-content">
    <form method="POST" action="/fieldly/public/index.php/time/log"><?php echo csrf_field() ?>
    <div class="modal-header"><h5 class="modal-title">Log Time</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div>
    <div class="modal-body"><div class="row g-3">
        <div class="col-12"><label class="form-label">Job *</label><select name="job_id" class="form-select" required><option value="">Select job...</option><?php foreach ($jobs as $j): ?><option value="<?php echo $j["id"] ?>"><?php echo esc($j["number"]." — ".$j["title"]) ?></option><?php endforeach; ?></select></div>
        <div class="col-md-6"><label class="form-label">Date *</label><input type="date" name="date" class="form-control" value="<?php echo date("Y-m-d") ?>" required/></div>
        <div class="col-md-6"><label class="form-label">Duration (mins) *</label><input type="number" name="duration_mins" class="form-control" min="1" required/></div>
        <div class="col-md-6"><label class="form-label">Hourly Rate</label><input type="number" name="rate" class="form-control" step="0.01" value="<?php echo $currentUser["hourly_rate"]??0 ?>"/></div>
        <div class="col-md-6 d-flex align-items-end pb-1"><div class="form-check"><input type="checkbox" name="billable" class="form-check-input" id="logBillable" checked/><label class="form-check-label" for="logBillable">Billable</label></div></div>
        <div class="col-12"><label class="form-label">Notes</label><textarea name="notes" class="form-control" rows="2"></textarea></div>
    </div></div>
    <div class="modal-footer"><button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button><button type="submit" class="btn btn-primary">Log Time</button></div>
    </form>
</div></div></div>
<?php $this->endSection(); ?>