<?php
/**
 * @var object $appointments
 * @var string $dateFrom
 * @var string $dateTo
 * @var string $status
 * @var string $employeeId
 * @var string $customerId
 * @var string $search
 * @var array $employees
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-calendar-alt me-2 text-gold"></i>Appointments</h5>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-list card-icon"></i>
            <h6 class="card-title">All Appointments</h6>
        </div>
        <div class="card-header-right">
            <a href="<?= url('appointments/calendar') ?>" class="btn btn-sm btn-outline-gold me-2">
                <i class="fas fa-calendar me-1"></i>Calendar
            </a>
            <a href="<?= url('reception') ?>" class="btn btn-sm btn-outline-gold me-2">
                <i class="fas fa-clipboard-list me-1"></i>Reception
            </a>
            <?php if (can('appointments_create')): ?>
                <a href="<?= url('appointments/create') ?>" class="btn btn-sm btn-gold">
                    <i class="fas fa-plus me-1"></i>New Appointment
                </a>
            <?php endif; ?>
        </div>
    </div>
    <div class="card-body">
        <form method="GET" action="<?= url('appointments') ?>" class="row g-2 mb-3">
            <div class="col-md-2">
                <label class="form-label small">Date From</label>
                <input type="date" name="date_from" class="form-control form-control-sm bg-transparent"
                       value="<?= e($dateFrom) ?>">
            </div>
            <div class="col-md-2">
                <label class="form-label small">Date To</label>
                <input type="date" name="date_to" class="form-control form-control-sm bg-transparent"
                       value="<?= e($dateTo) ?>">
            </div>
            <div class="col-md-2">
                <label class="form-label small">Status</label>
                <select name="status" class="form-select form-select-sm bg-transparent">
                    <option value="">All Status</option>
                    <?php foreach (appointmentStatuses() as $val => $label): ?>
                        <option value="<?= $val ?>" <?= $status === $val ? 'selected' : '' ?>><?= e($label) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-2">
                <label class="form-label small">Employee</label>
                <select name="employee_id" class="form-select form-select-sm bg-transparent">
                    <option value="">All Employees</option>
                    <?php foreach ($employees as $emp): ?>
                        <option value="<?= $emp->id ?>" <?= $employeeId == $emp->id ? 'selected' : '' ?>>
                            <?= e($emp->first_name . ' ' . $emp->last_name) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-2">
                <label class="form-label small">Search</label>
                <input type="text" name="search" class="form-control form-control-sm bg-transparent"
                       placeholder="Customer name/phone..." value="<?= e($search) ?>">
            </div>
            <div class="col-md-2 d-flex align-items-end gap-1">
                <button type="submit" class="btn btn-sm btn-gold"><i class="fas fa-filter me-1"></i>Filter</button>
                <a href="<?= url('appointments') ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-times"></i></a>
            </div>
        </form>

        <div class="table-responsive">
            <table class="table table-dark table-hover mb-0">
                <thead>
                    <tr>
                        <th width="50">#</th>
                        <th>Customer</th>
                        <th>Services</th>
                        <th>Employee</th>
                        <th>Date & Time</th>
                        <th class="text-center">Status</th>
                        <th width="140" class="text-center">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($appointments->data)): ?>
                        <tr>
                            <td colspan="7" class="text-center text-muted py-4">
                                <i class="fas fa-calendar-times fa-2x mb-2"></i>
                                <p class="mb-0">No appointments found</p>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php $i = ($appointments->currentPage - 1) * $appointments->perPage + 1; ?>
                        <?php foreach ($appointments->data as $apt): ?>
                            <tr>
                                <td><?= $i++ ?></td>
                                <td>
                                    <a href="<?= url('customers/show/' . $apt->customer_id) ?>" class="text-gold text-decoration-none">
                                        <strong><?= e($apt->customer_first_name . ' ' . $apt->customer_last_name) ?></strong>
                                    </a>
                                    <br><small class="text-muted"><?= e($apt->customer_phone ?? '') ?></small>
                                </td>
                                <td><?= e($apt->service_names ?? '-') ?></td>
                                <td><?= e($apt->employee_names ?? '-') ?></td>
                                <td>
                                    <?= formatDate($apt->start_time) ?>
                                    <br><small class="text-muted">
                                        <?= date(TIME_FORMAT, strtotime($apt->start_time)) ?> -
                                        <?= date(TIME_FORMAT, strtotime($apt->end_time)) ?>
                                    </small>
                                </td>
                                <td class="text-center">
                                    <span class="badge bg-<?= statusBadge($apt->status) ?>">
                                        <?= e(ucwords(str_replace('_', ' ', $apt->status))) ?>
                                    </span>
                                </td>
                                <td class="text-center">
                                    <div class="btn-group btn-group-sm">
                                        <a href="<?= url('appointments/show/' . $apt->id) ?>" class="btn btn-outline-info" title="View">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                        <?php if (can('appointments_edit')): ?>
                                            <a href="<?= url('appointments/edit/' . $apt->id) ?>" class="btn btn-outline-gold" title="Edit">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                        <?php endif; ?>
                                        <?php if (can('appointments_delete')): ?>
                                            <button type="button" class="btn btn-outline-danger btn-delete"
                                                    data-id="<?= $apt->id ?>" title="Delete">
                                                <i class="fas fa-trash"></i>
                                            </button>
                                        <?php endif; ?>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <?php if ($appointments->totalPages > 1): ?>
            <div class="card-footer d-flex justify-content-between align-items-center">
                <div class="text-muted small">
                    Showing <?= (($appointments->currentPage - 1) * $appointments->perPage) + 1 ?>
                    to <?= min($appointments->currentPage * $appointments->perPage, $appointments->total) ?>
                    of <?= $appointments->total ?> entries
                </div>
                <?= paginateLinks($appointments, url('appointments')) ?>
            </div>
        <?php endif; ?>
    </div>
</div>

