<?php include __DIR__ . '/../../layouts/header.php'; ?>
<?php include __DIR__ . '/../../layouts/sidebar.php'; ?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h2 class="mb-0"><?= __('interviews') ?></h2>
</div>

<!-- Filters -->
<div class="card border-0 shadow-sm mb-4" style="border-radius: 16px;">
    <div class="card-body p-3">
        <form action="" method="GET" class="row g-3 align-items-center">
            <input type="hidden" name="page" value="interviews">
            <div class="col-md-4">
                <div class="input-group">
                    <span class="input-group-text bg-white border-end-0" style="border-radius: 10px 0 0 10px;"><i class="fas fa-search text-muted"></i></span>
                    <input type="text" name="search" class="form-control border-start-0" placeholder="Search customer, vendor or maid..." value="<?= htmlspecialchars($_GET['search'] ?? '') ?>" style="border-radius: 0 10px 10px 0;">
                </div>
            </div>
            <div class="col-md-2">
                <select name="status" class="form-select" style="border-radius: 10px;">
                    <option value=""><?= __('all_status') ?></option>
                    <option value="pending" <?= ($_GET['status'] ?? '') === 'pending' ? 'selected' : '' ?>><?= __('pending') ?></option>
                    <option value="approved" <?= ($_GET['status'] ?? '') === 'approved' ? 'selected' : '' ?>><?= __('approved') ?></option>
                    <option value="completed" <?= ($_GET['status'] ?? '') === 'completed' ? 'selected' : '' ?>><?= __('completed') ?></option>
                    <option value="cancelled" <?= ($_GET['status'] ?? '') === 'cancelled' ? 'selected' : '' ?>><?= __('cancelled') ?></option>
                </select>
            </div>
            <div class="col-md-2">
                <input type="date" name="date" class="form-control" value="<?= htmlspecialchars($_GET['date'] ?? '') ?>" style="border-radius: 10px;">
            </div>
            <div class="col-md-2">
                <button type="submit" class="btn btn-primary w-100" style="border-radius: 10px;">Filter</button>
            </div>
            <div class="col-md-2">
                <a href="?page=interviews" class="btn btn-light w-100" style="border-radius: 10px;">Reset</a>
            </div>
        </form>
    </div>
</div>

<div class="table-container">
    <div class="table-responsive">
        <table class="table align-middle">
            <thead>
                <tr>
                    <th><?= __('id') ?></th>
                    <th><?= __('customer') ?></th>
                    <th><?= __('vendor') ?></th>
                    <th><?= __('date') ?> & <?= __('time') ?></th>
                    <th><?= __('link') ?></th>
                    <th><?= __('status') ?></th>
                    <th class="text-end"><?= __('actions') ?></th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($interviews)): ?>
                    <tr>
                        <td colspan="7" class="text-center py-5 text-muted"><?= __('no_data_found') ?></td>
                    </tr>
                <?php else: ?>
                    <?php foreach ($interviews as $interview): ?>
                        <tr>
                            <td class="text-secondary fw-semibold">#<?= htmlspecialchars($interview['id']) ?></td>
                            <td>
                                <span class="fw-bold text-dark"><i class="fas fa-user-circle text-secondary" style="<?= isRTL() ? 'margin-left: 4px;' : 'margin-right: 4px;' ?>"></i> <?= htmlspecialchars($interview['customer_name']) ?></span>
                            </td>
                            <td>
                                <span class="badge badge-secondary fw-normal">
                                    <i class="fas fa-building" style="<?= isRTL() ? 'margin-left: 4px;' : 'margin-right: 4px;' ?>"></i> <?= htmlspecialchars($interview['vendor_name']) ?>
                                </span>
                            </td>
                            <td>
                                <div class="fw-bold text-dark"><?= htmlspecialchars($interview['interview_date']) ?></div>
                                <div class="text-secondary" style="font-size: 12px;"><?= htmlspecialchars(date('h:i A', strtotime($interview['time_from']))) ?> - <?= htmlspecialchars(date('h:i A', strtotime($interview['time_to']))) ?></div>
                            </td>
                            <td>
                                <?php if ($interview['platform_name']): ?>
                                    <div class="fw-bold text-dark" style="font-size: 13px;"><i class="fas fa-video text-muted" style="<?= isRTL() ? 'margin-left: 4px;' : 'margin-right: 4px;' ?>"></i> <?= htmlspecialchars($interview['platform_name']) ?></div>
                                <?php endif; ?>
                                <?php if ($interview['translation_enabled']): ?>
                                    <div class="text-secondary" style="font-size: 11px; margin-top: 2px;">
                                        <i class="fas fa-language"></i> AI: <?= htmlspecialchars($interview['customer_language'] ?? '') ?> &rarr; <?= htmlspecialchars($interview['helper_language'] ?? '') ?>
                                    </div>
                                <?php endif; ?>
                                <?php if ($interview['meeting_link']): ?>
                                    <a href="<?= htmlspecialchars($interview['meeting_link']) ?>" target="_blank" class="btn btn-sm btn-light text-primary mt-1" style="font-size: 11px; padding: 2px 6px;"><i class="fas fa-external-link-alt"></i> Join</a>
                                <?php elseif (!$interview['platform_name']): ?>
                                    <span class="text-muted">-</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php if ($interview['status'] === 'completed'): ?>
                                    <span class="badge badge-success"><?= __('completed') ?></span>
                                <?php elseif ($interview['status'] === 'cancelled' || $interview['status'] === 'rejected'): ?>
                                    <span class="badge badge-danger"><?= __('rejected') ?></span>
                                <?php elseif ($interview['status'] === 'confirmed'): ?>
                                    <span class="badge badge-info"><?= __('active') ?></span>
                                <?php else: ?>
                                    <span class="badge badge-warning"><?= htmlspecialchars(ucfirst($interview['status'])) ?></span>
                                <?php endif; ?>
                            </td>
                            <td class="text-end">
                                <div class="dropdown d-inline-block">
                                    <button class="btn btn-light btn-sm dropdown-toggle" type="button" data-bs-toggle="dropdown">
                                        Actions
                                    </button>
                                    <ul class="dropdown-menu dropdown-menu-end shadow-sm border-0 rounded-3">
                                        <li><a class="dropdown-item py-2" href="?page=interviews&action=update_status&id=<?= $interview['id'] ?>&status=completed"><i class="fas fa-check-circle me-2 text-success"></i> Mark Completed</a></li>
                                        <li><a class="dropdown-item py-2" href="?page=interviews&action=update_status&id=<?= $interview['id'] ?>&status=cancelled"><i class="fas fa-times-circle me-2 text-danger"></i> Cancel</a></li>
                                        <li><hr class="dropdown-divider opacity-50"></li>
                                        <li><a class="dropdown-item py-2 text-danger" href="?page=interviews&action=delete&id=<?= $interview['id'] ?>" onclick="return confirm('Are you sure?')"><i class="fas fa-trash me-2"></i> Delete</a></li>
                                    </ul>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>

    <?php if ($totalPages > 1): ?>
    <div class="d-flex justify-content-end mt-4">
        <ul class="pagination mb-0 gap-2">
            <li class="page-item <?= $page <= 1 ? 'disabled' : '' ?>">
                <a class="page-link rounded-circle border-0 text-secondary bg-light" href="?page=interviews&p=<?= $page - 1 ?>&search=<?= urlencode($_GET['search'] ?? '') ?>&status=<?= urlencode($_GET['status'] ?? '') ?>&date=<?= urlencode($_GET['date'] ?? '') ?>" style="width:40px; height:40px; display:flex; align-items:center; justify-content:center;"><i class="fas <?= isRTL() ? 'fa-chevron-right' : 'fa-chevron-left' ?>"></i></a>
            </li>
            <?php for ($i = 1; $i <= $totalPages; $i++): ?>
                <li class="page-item <?= $i == $page ? 'active' : '' ?>">
                    <a class="page-link rounded-circle border-0 <?= $i == $page ? 'bg-primary text-white' : 'text-secondary bg-light' ?>" href="?page=interviews&p=<?= $i ?>&search=<?= urlencode($_GET['search'] ?? '') ?>&status=<?= urlencode($_GET['status'] ?? '') ?>&date=<?= urlencode($_GET['date'] ?? '') ?>" style="width:40px; height:40px; display:flex; align-items:center; justify-content:center;"><?= $i ?></a>
                </li>
            <?php endfor; ?>
            <li class="page-item <?= $page >= $totalPages ? 'disabled' : '' ?>">
                <a class="page-link rounded-circle border-0 text-secondary bg-light" href="?page=interviews&p=<?= $page + 1 ?>&search=<?= urlencode($_GET['search'] ?? '') ?>&status=<?= urlencode($_GET['status'] ?? '') ?>&date=<?= urlencode($_GET['date'] ?? '') ?>" style="width:40px; height:40px; display:flex; align-items:center; justify-content:center;"><i class="fas <?= isRTL() ? 'fa-chevron-left' : 'fa-chevron-right' ?>"></i></a>
            </li>
        </ul>
    </div>
    <?php endif; ?>
</div>

<?php include __DIR__ . '/../../layouts/footer.php'; ?>
