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

<?php if (isset($_GET['success'])): ?>
    <div class="alert alert-success alert-dismissible fade show border-0 shadow-sm mb-4 rounded-4" role="alert">
        <i class="fas fa-check-circle me-2"></i> <?= htmlspecialchars($_GET['success']) ?>
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>
<?php endif; ?>

<?php if (isset($_GET['error'])): ?>
    <div class="alert alert-danger alert-dismissible fade show border-0 shadow-sm mb-4 rounded-4" role="alert">
        <i class="fas fa-exclamation-circle me-2"></i> <?= htmlspecialchars($_GET['error']) ?>
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>
<?php endif; ?>

<div class="row">
    <div class="col-md-3 mb-4">
        <div class="card border-0 shadow-sm" style="border-radius: 12px;">
            <div class="card-body p-0">
                <div class="list-group list-group-flush" style="border-radius: 12px; overflow: hidden;">
                    <a href="?page=lookups&tab=countries" class="list-group-item list-group-item-action <?= $tab === 'countries' ? 'active bg-primary text-white' : 'text-secondary' ?> py-3 border-0">
                        <i class="fas fa-globe" style="<?= isRTL() ? 'margin-left: 8px;' : 'margin-right: 8px;' ?>"></i> <?= __('countries') ?>
                    </a>
                    <a href="?page=lookups&tab=cities" class="list-group-item list-group-item-action <?= $tab === 'cities' ? 'active bg-primary text-white' : 'text-secondary' ?> py-3 border-0">
                        <i class="fas fa-city" style="<?= isRTL() ? 'margin-left: 8px;' : 'margin-right: 8px;' ?>"></i> <?= __('cities') ?>
                    </a>
                    <a href="?page=lookups&tab=nationalities" class="list-group-item list-group-item-action <?= $tab === 'nationalities' ? 'active bg-primary text-white' : 'text-secondary' ?> py-3 border-0">
                        <i class="fas fa-flag" style="<?= isRTL() ? 'margin-left: 8px;' : 'margin-right: 8px;' ?>"></i> <?= __('nationalities') ?>
                    </a>
                    <a href="?page=lookups&tab=languages" class="list-group-item list-group-item-action <?= $tab === 'languages' ? 'active bg-primary text-white' : 'text-secondary' ?> py-3 border-0">
                        <i class="fas fa-language" style="<?= isRTL() ? 'margin-left: 8px;' : 'margin-right: 8px;' ?>"></i> <?= __('languages') ?>
                    </a>
                    <a href="?page=lookups&tab=specialties" class="list-group-item list-group-item-action <?= $tab === 'specialties' ? 'active bg-primary text-white' : 'text-secondary' ?> py-3 border-0">
                        <i class="fas fa-star" style="<?= isRTL() ? 'margin-left: 8px;' : 'margin-right: 8px;' ?>"></i> <?= __('specialties') ?>
                    </a>
                    <a href="?page=lookups&tab=skills" class="list-group-item list-group-item-action <?= $tab === 'skills' ? 'active bg-primary text-white' : 'text-secondary' ?> py-3 border-0">
                        <i class="fas fa-tools" style="<?= isRTL() ? 'margin-left: 8px;' : 'margin-right: 8px;' ?>"></i> <?= __('skills') ?>
                    </a>
                    <a href="?page=lookups&tab=service_models" class="list-group-item list-group-item-action <?= $tab === 'service_models' ? 'active bg-primary text-white' : 'text-secondary' ?> py-3 border-0">
                        <i class="fas fa-briefcase" style="<?= isRTL() ? 'margin-left: 8px;' : 'margin-right: 8px;' ?>"></i> <?= __('service_models') ?>
                    </a>
                    <a href="?page=lookups&tab=accommodation_types" class="list-group-item list-group-item-action <?= $tab === 'accommodation_types' ? 'active bg-primary text-white' : 'text-secondary' ?> py-3 border-0">
                        <i class="fas fa-home" style="<?= isRTL() ? 'margin-left: 8px;' : 'margin-right: 8px;' ?>"></i> <?= __('accommodation_types') ?>
                    </a>
                    <a href="?page=lookups&tab=interview_platforms" class="list-group-item list-group-item-action <?= $tab === 'interview_platforms' ? 'active bg-primary text-white' : 'text-secondary' ?> py-3 border-0">
                        <i class="fas fa-video" style="<?= isRTL() ? 'margin-left: 8px;' : 'margin-right: 8px;' ?>"></i> <?= __('interview_platforms') ?>
                    </a>
                    <a href="?page=lookups&tab=document_types" class="list-group-item list-group-item-action <?= $tab === 'document_types' ? 'active bg-primary text-white' : 'text-secondary' ?> py-3 border-0">
                        <i class="fas fa-file-alt" style="<?= isRTL() ? 'margin-left: 8px;' : 'margin-right: 8px;' ?>"></i> <?= __('document_types') ?>
                    </a>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-9">
        <div class="card border-0 shadow-sm" style="border-radius: 20px;">
            <div class="card-body p-0">
                <div class="d-flex justify-content-between align-items-center p-4 border-bottom">
                    <h5 class="mb-0 fw-bold"><?= __($tab) ?></h5>
                    <button class="btn btn-primary btn-sm rounded-pill px-3" data-bs-toggle="modal" data-bs-target="#addModal">
                        <i class="fas fa-plus"></i> <?= __('add_new') ?>
                    </button>
                </div>
                <div class="table-responsive">
                    <table class="table align-middle mb-0">
                        <thead class="bg-light">
                            <tr>
                                <th class="ps-4"><?= __('id') ?></th>
                                <th><?= __('name_en') ?></th>
                                <th><?= __('name_ar') ?></th>
                                <?php if($tab === 'cities'): ?>
                                <th><?= __('countries') ?></th>
                                <?php endif; ?>
                                <th><?= __('status') ?></th>
                                <th class="text-end pe-4"><?= __('actions') ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if(empty($data)): ?>
                            <tr>
                                <td colspan="6" class="text-center py-5 text-muted"><?= __('no_data_found') ?></td>
                            </tr>
                            <?php else: ?>
                                <?php foreach($data as $row): ?>
                                <tr>
                                    <td class="ps-4 text-secondary">#<?= htmlspecialchars($row['id']) ?></td>
                                    <td class="fw-bold text-dark"><?= htmlspecialchars($row['name_en'] ?? $row['name']) ?></td>
                                    <td class="text-secondary"><?= htmlspecialchars($row['name_ar'] ?? '-') ?></td>
                                    <?php if($tab === 'cities'): ?>
                                    <td class="text-secondary"><?= htmlspecialchars($row['country_name'] ?? '-') ?></td>
                                    <?php endif; ?>
                                    <td>
                                        <span class="badge <?= ($row['status'] ?? 'active') === 'active' ? 'badge-success' : 'badge-secondary' ?>">
                                            <?= htmlspecialchars(ucfirst($row['status'] ?? 'active')) ?>
                                        </span>
                                    </td>
                                    <td class="text-end pe-4">
                                        <!-- Edit button -->
                                        <button class="btn btn-light btn-sm text-primary edit-btn me-1"
                                                data-bs-toggle="modal"
                                                data-bs-target="#editModal"
                                                data-id="<?= $row['id'] ?>"
                                                data-name-en="<?= htmlspecialchars($row['name_en'] ?? $row['name'] ?? '') ?>"
                                                data-name-ar="<?= htmlspecialchars($row['name_ar'] ?? '') ?>"
                                                data-country-id="<?= $row['country_id'] ?? '' ?>"
                                                data-status="<?= htmlspecialchars($row['status'] ?? 'active') ?>">
                                            <i class="fas fa-edit"></i>
                                        </button>

                                        <!-- Toggle Status button -->
                                        <?php if (($row['status'] ?? 'active') === 'active'): ?>
                                            <a href="?page=lookups&action=toggle_status&tab=<?= $tab ?>&id=<?= $row['id'] ?>&status=inactive"
                                               class="btn btn-light btn-sm text-warning me-1"
                                               title="Deactivate item"
                                               onclick="return confirm('Are you sure you want to deactivate this item?')">
                                                <i class="fas fa-ban"></i>
                                            </a>
                                        <?php else: ?>
                                            <a href="?page=lookups&action=toggle_status&tab=<?= $tab ?>&id=<?= $row['id'] ?>&status=active"
                                               class="btn btn-light btn-sm text-success me-1"
                                               title="Activate item"
                                               onclick="return confirm('Are you sure you want to activate this item?')">
                                                <i class="fas fa-check-circle"></i>
                                            </a>
                                        <?php endif; ?>

                                        <!-- Delete button -->
                                        <a href="?page=lookups&action=delete&tab=<?= $tab ?>&id=<?= $row['id'] ?>" 
                                           class="btn btn-light btn-sm text-danger" 
                                           onclick="return confirm('Are you sure you want to delete this item?')">
                                            <i class="fas fa-trash"></i>
                                        </a>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Add Modal -->
<div class="modal fade" id="addModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content border-0 shadow" style="border-radius: 20px;">
            <div class="modal-header border-0 px-4 pt-4">
                <h5 class="modal-title fw-bold">Add New <?= htmlspecialchars(ucfirst(rtrim($tab, 's'))) ?></h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form action="?page=lookups&action=store" method="POST">
                <input type="hidden" name="tab" value="<?= htmlspecialchars($tab) ?>">
                <div class="modal-body p-4">
                    <?php if($tab === 'cities'): ?>
                    <div class="mb-3">
                        <label class="form-label text-secondary small fw-bold">Select Country</label>
                        <select name="country_id" class="form-select" required>
                            <?php foreach($countries as $c): ?>
                                <option value="<?= $c['id'] ?>"><?= htmlspecialchars($c['name_en']) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <?php endif; ?>

                    <div class="mb-3">
                        <label class="form-label text-secondary small fw-bold">Name (English)</label>
                        <input type="text" name="name_en" class="form-control" placeholder="Enter name in English" required>
                    </div>
                    
                    <?php if($tab !== 'interview_platforms'): ?>
                    <div class="mb-3">
                        <label class="form-label text-secondary small fw-bold">Name (Arabic)</label>
                        <input type="text" name="name_ar" class="form-control" placeholder="Enter name in Arabic">
                    </div>
                    <?php endif; ?>
                </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-primary rounded-pill px-4">Save Item</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Edit Modal -->
<div class="modal fade" id="editModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content border-0 shadow" style="border-radius: 20px;">
            <div class="modal-header border-0 px-4 pt-4">
                <h5 class="modal-title fw-bold">Edit <?= htmlspecialchars(ucfirst(rtrim($tab, 's'))) ?></h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form action="?page=lookups&action=update" method="POST">
                <input type="hidden" name="tab" value="<?= htmlspecialchars($tab) ?>">
                <input type="hidden" name="id" id="edit_id">
                <div class="modal-body p-4">
                    <?php if($tab === 'cities'): ?>
                    <div class="mb-3">
                        <label class="form-label text-secondary small fw-bold">Select Country</label>
                        <select name="country_id" id="edit_country_id" class="form-select" required>
                            <?php foreach($countries as $c): ?>
                                <option value="<?= $c['id'] ?>"><?= htmlspecialchars($c['name_en']) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <?php endif; ?>

                    <div class="mb-3">
                        <label class="form-label text-secondary small fw-bold">Name (English)</label>
                        <input type="text" name="name_en" id="edit_name_en" class="form-control" required>
                    </div>
                    
                    <?php if($tab !== 'interview_platforms'): ?>
                    <div class="mb-3">
                        <label class="form-label text-secondary small fw-bold">Name (Arabic)</label>
                        <input type="text" name="name_ar" id="edit_name_ar" class="form-control">
                    </div>
                    <?php endif; ?>

                    <div class="mb-3">
                        <label class="form-label text-secondary small fw-bold">Status</label>
                        <select name="status" id="edit_status" class="form-select" required>
                            <option value="active">Active</option>
                            <option value="inactive">Inactive</option>
                        </select>
                    </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-primary rounded-pill px-4">Update Item</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
document.querySelectorAll('.edit-btn').forEach(btn => {
    btn.addEventListener('click', function() {
        document.getElementById('edit_id').value = this.getAttribute('data-id');
        document.getElementById('edit_name_en').value = this.getAttribute('data-name-en');
        if (document.getElementById('edit_name_ar')) {
            document.getElementById('edit_name_ar').value = this.getAttribute('data-name-ar');
        }
        if (document.getElementById('edit_country_id')) {
            document.getElementById('edit_country_id').value = this.getAttribute('data-country-id');
        }
        document.getElementById('edit_status').value = this.getAttribute('data-status');
    });
});
</script>

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