<?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"><?= __('maid_documents') ?></h2>
</div>

<?php if (isset($_GET['success'])): ?>
    <div class="alert alert-success border-0 shadow-sm mb-4 rounded-4" role="alert">
        <i class="fas fa-check-circle me-2"></i> <?= htmlspecialchars($_GET['success']) ?>
    </div>
<?php endif; ?>

<div class="table-container">
    <div class="table-responsive">
        <table class="table align-middle">
            <thead>
                <tr>
                    <th><?= __('id') ?></th>
                    <th>Maid</th>
                    <th>Document Type</th>
                    <th>File</th>
                    <th><?= __('date') ?></th>
                    <th><?= __('status') ?></th>
                    <th class="text-end"><?= __('actions') ?></th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($documents)): ?>
                    <tr>
                        <td colspan="7" class="text-center py-5 text-muted"><?= __('no_data_found') ?></td>
                    </tr>
                <?php else: ?>
                    <?php foreach ($documents as $doc): ?>
                        <tr>
                            <td class="text-secondary fw-semibold">#<?= htmlspecialchars($doc['id']) ?></td>
                            <td>
                                <span class="fw-bold text-dark">
                                    <i class="fas fa-user-nurse text-secondary" style="<?= isRTL() ? 'margin-left: 4px;' : 'margin-right: 4px;' ?>"></i> <?= htmlspecialchars($doc['maid_name']) ?>
                                </span>
                            </td>
                            <td>
                                <span class="badge badge-light text-dark border fw-normal">
                                    <?= htmlspecialchars($doc['document_type_name']) ?>
                                </span>
                            </td>
                            <td>
                                <a href="<?= htmlspecialchars($doc['file_path']) ?>" target="_blank" class="btn btn-sm btn-light text-primary">
                                    <i class="fas fa-file-alt"></i> View
                                </a>
                            </td>
                            <td class="text-secondary"><?= date('M d, Y', strtotime($doc['created_at'])) ?></td>
                            <td>
                                <?php if ($doc['status'] === 'approved'): ?>
                                    <span class="badge badge-success"><?= __('approved') ?></span>
                                <?php elseif ($doc['status'] === 'rejected'): ?>
                                    <span class="badge badge-danger"><?= __('rejected') ?></span>
                                    <?php if (!empty($doc['admin_note'])): ?>
                                        <div class="small text-muted mt-1" style="font-size: 10px; max-width: 150px;"><?= htmlspecialchars($doc['admin_note']) ?></div>
                                    <?php endif; ?>
                                <?php elseif ($doc['status'] === 'expired'): ?>
                                    <span class="badge badge-danger">Expired</span>
                                <?php else: ?>
                                    <span class="badge badge-warning"><?= htmlspecialchars(ucfirst($doc['status'])) ?></span>
                                <?php endif; ?>
                            </td>
                             <td class="text-end">
                                <?php if ($doc['status'] === 'pending'): ?>
                                    <a href="?page=maid_documents&action=approve&id=<?= $doc['id'] ?>" class="btn btn-light btn-sm text-success me-1" onclick="return confirm('Approve this document?')"><i class="fas fa-check"></i></a>
                                    <button class="btn btn-light btn-sm text-danger" onclick="openRejectModal(<?= $doc['id'] ?>)"><i class="fas fa-times"></i></button>
                                <?php else: ?>
                                    <span class="text-muted small">Reviewed</span>
                                <?php endif; ?>
                            </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=maid_documents&p=<?= $page - 1 ?>" 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=maid_documents&p=<?= $i ?>" 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=maid_documents&p=<?= $page + 1 ?>" 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>

<!-- Reject Modal -->
<div class="modal fade" id="rejectModal" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content border-0 shadow rounded-4">
            <div class="modal-header border-0 px-4 pt-4">
                <h5 class="modal-title fw-bold">Reject Document</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form action="?page=maid_documents&action=reject" method="POST">
                <input type="hidden" name="id" id="reject_doc_id">
                <div class="modal-body p-4">
                    <div class="mb-3">
                        <label class="form-label text-secondary small fw-bold">Rejection Reason (Admin Note)</label>
                        <textarea name="admin_note" class="form-control" rows="3" placeholder="Explain why the document was rejected..." required></textarea>
                    </div>
                </div>
                <div class="modal-footer border-0 p-4 pt-0">
                    <button type="button" class="btn btn-light rounded-pill px-4" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-danger rounded-pill px-4">Reject Document</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
function openRejectModal(id) {
    document.getElementById('reject_doc_id').value = id;
    new bootstrap.Modal(document.getElementById('rejectModal')).show();
}
</script>

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