<?php $this->extend("layouts/main"); $this->section("content"); $title = "Calendar"; ?>
<div class="d-flex justify-content-between align-items-center mb-3">
    <div class="d-flex gap-2">
        <a href="/fieldly/public/index.php/calendar?from=<?php echo date("Y-m-01",strtotime($from." -1 month")) ?>&to=<?php echo date("Y-m-t",strtotime($from." -1 month")) ?>" class="btn btn-sm btn-outline-secondary"><i class="bi bi-chevron-left"></i></a>
        <span class="fw-bold align-self-center"><?php echo date("F Y",strtotime($from)) ?></span>
        <a href="/fieldly/public/index.php/calendar?from=<?php echo date("Y-m-01",strtotime($from." +1 month")) ?>&to=<?php echo date("Y-m-t",strtotime($from." +1 month")) ?>" class="btn btn-sm btn-outline-secondary"><i class="bi bi-chevron-right"></i></a>
    </div>
    <?php if (in_array("edit_calendar", $permissions)): ?>
    <button class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#visitModal"><i class="bi bi-plus me-1"></i> Schedule Visit</button>
    <?php endif; ?>
</div>
<!-- Visits List -->
<div class="row g-3">
    <div class="col-lg-8">
        <div class="card"><div class="card-header"><h6 class="mb-0">Scheduled Visits</h6></div><div class="card-body p-0">
        <table class="table mb-0">
            <thead><tr><th>Date/Time</th><th>Job</th><th>Customer</th><th>Status</th><th></th></tr></thead>
            <tbody>
            <?php foreach ($visits as $v): ?>
            <tr>
                <td><div class="fw-semibold" style="font-size:13px"><?php echo date("D d M",strtotime($v["scheduled_start"])) ?></div><div class="text-muted small"><?php echo date("H:i",strtotime($v["scheduled_start"])) ?><?php if ($v["scheduled_end"]): ?> – <?php echo date("H:i",strtotime($v["scheduled_end"])) ?><?php endif; ?></div></td>
                <td><a href="/fieldly/public/index.php/jobs/<?php echo $v["job_id"] ?>" class="text-decoration-none fw-semibold" style="font-size:13px"><?php echo esc($v["job_number"]) ?></a><div class="text-muted small"><?php echo esc($v["job_title"]) ?></div></td>
                <td style="font-size:13px"><?php echo esc($v["company_name"]?:$v["first_name"]." ".$v["last_name"]) ?></td>
                <td><span class="badge bg-<?php echo ["scheduled"=>"primary","in_progress"=>"warning","completed"=>"success","cancelled"=>"secondary"][$v["status"]]??"secondary" ?>"><?php echo ucfirst(str_replace("_"," ",$v["status"])) ?></span></td>
                <td><?php if (in_array("edit_calendar", $permissions)): ?><form method="POST" action="/fieldly/public/index.php/calendar/visit/<?php echo $v["id"] ?>/delete" data-confirm="Delete this visit?"><?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($visits)): ?><tr><td colspan="5" class="text-center text-muted py-4">No visits scheduled this month</td></tr><?php endif; ?>
            </tbody>
        </table>
        </div></div>
    </div>
    <!-- Jobs with scheduled dates -->
    <div class="col-lg-4">
        <div class="card"><div class="card-header"><h6 class="mb-0">Jobs with Dates This Month</h6></div><div class="card-body p-0">
        <table class="table table-sm mb-0">
            <tbody>
            <?php foreach ($jobs as $j): ?>
            <tr><td><a href="/fieldly/public/index.php/jobs/<?php echo $j["id"] ?>" class="text-decoration-none fw-semibold small"><?php echo esc($j["number"]) ?></a><div class="text-muted" style="font-size:11px"><?php echo esc($j["title"]) ?></div></td><td class="text-end"><div style="font-size:11px"><?php echo date("d M",strtotime($j["scheduled_date"])) ?></div><span class="badge" style="font-size:10px;background:<?php echo esc($j["status_color"]) ?>"><?php echo esc($j["status_name"]) ?></span></td></tr>
            <?php endforeach; ?>
            <?php if (empty($jobs)): ?><tr><td class="text-center text-muted py-3 small">No jobs scheduled</td></tr><?php endif; ?>
            </tbody>
        </table>
        </div></div>
    </div>
</div>
<!-- Schedule Visit Modal -->
<div class="modal fade" id="visitModal" tabindex="-1"><div class="modal-dialog modal-dialog-centered"><div class="modal-content">
    <form method="POST" action="/fieldly/public/index.php/calendar/visit"><?php echo csrf_field() ?>
    <div class="modal-header"><h5 class="modal-title">Schedule Visit</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 ($allJobs as $j): ?><option value="<?php echo $j["id"] ?>"><?php echo esc($j["number"]." — ".$j["title"]) ?></option><?php endforeach; ?></select></div>
        <div class="col-12"><label class="form-label">Title</label><input type="text" name="title" class="form-control" placeholder="Visit title..."/></div>
        <div class="col-md-6"><label class="form-label">Start *</label><input type="datetime-local" name="scheduled_start" class="form-control" required/></div>
        <div class="col-md-6"><label class="form-label">End</label><input type="datetime-local" name="scheduled_end" class="form-control"/></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">Schedule</button></div>
    </form>
</div></div></div>
<?php $this->endSection(); ?>