<?php

declare(strict_types=1);

// Debug Logging
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    error_log('AdminDocuments POST: ' . print_r($_POST, true));
    error_log('AdminDocuments FILES: ' . print_r($_FILES, true));
}

require_once __DIR__ . '/includes/admin_layout.php';

library_bootstrap();

$errors = [];

// Handle POST request
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Check for max_input_vars or post_max_size issues
    if (empty($_POST) && empty($_FILES) && (int)($_SERVER['CONTENT_LENGTH'] ?? 0) > 0) {
        $errors[] = 'The uploaded file exceeds the server limit (post_max_size). Please try a smaller file or contact admin.';
    } else {
        $action = $_POST['action'] ?? '';
        $id = isset($_POST['id']) ? (int)$_POST['id'] : 0;

        try {
            if ($action === 'create_document') {
                library_create_document($_POST, $_FILES['document_file'] ?? [], $_FILES['cover_file'] ?? []);
                library_set_flash('success', library_trans('document_created_success'));
                header('Location: /admin_documents.php');
                exit;
            } elseif ($action === 'update_document') {
                if (!$id) {
                    throw new RuntimeException(library_trans('invalid_document_id'));
                }
                library_update_document($id, $_POST, $_FILES['document_file'] ?? [], $_FILES['cover_file'] ?? []);
                library_set_flash('success', library_trans('document_updated_success'));
                header('Location: /admin_documents.php');
                exit;
            } elseif ($action === 'delete_document') {
                if (!$id) {
                    throw new RuntimeException(library_trans('invalid_document_id'));
                }
                library_delete_document($id);
                library_set_flash('success', library_trans('document_deleted_success'));
                header('Location: /admin_documents.php');
                exit;
            }
        } catch (Throwable $exception) {
            $errors[] = $exception->getMessage();
            error_log('AdminDocuments Error: ' . $exception->getMessage());
        }
    }
}

// Search & Pagination Logic
$page = isset($_GET['page']) ? max(1, (int)$_GET['page']) : 1;
$limit = 10;
$offset = ($page - 1) * $limit;
$search = isset($_GET['search']) ? trim($_GET['search']) : '';

$result = library_fetch_documents_paginated(false, ['q' => $search], $limit, $offset);
$documents = $result['data'];
$totalDocuments = $result['total'];
$totalPages = (int)ceil($totalDocuments / $limit);

$categories = library_get_categories();
$allSubcategories = library_get_subcategories(null);
$types = library_get_types();
$lang = library_get_language();

admin_render_header(library_trans('material_entry'), 'documents');
?>
<!-- Page Content -->
<?php if ($errors): ?>
    <div class="alert alert-danger">
        <h6 class="alert-heading fw-bold"><i class="bi bi-exclamation-triangle-fill me-2"></i><?= library_trans('submission_error') ?></h6>
        <ul class="mb-0 ps-3">
            <?php foreach ($errors as $e): ?>
                <li><?= h($e) ?></li>
            <?php endforeach; ?>
        </ul>
    </div>
<?php endif; ?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <p class="text-secondary mb-0"><?= library_trans('manage_documents_desc') ?></p>
    <button class="btn btn-primary" onclick="openCreateModal()">
        <i class="bi bi-plus-lg me-1"></i> <?= library_trans('add_new_document') ?>
    </button>
</div>

<!-- Search Bar -->
<div class="card shadow-sm border-0 mb-4">
    <div class="card-body">
        <form method="get" action="/admin_documents.php" class="row g-2 align-items-center">
            <div class="col-auto flex-grow-1">
                <input type="text" name="search" class="form-control" placeholder="<?= library_trans('search_docs_placeholder') ?>" value="<?= h($search) ?>">
            </div>
            <div class="col-auto">
                <button type="submit" class="btn btn-outline-primary">
                    <i class="bi bi-search"></i> <?= library_trans('search') ?>
                </button>
                <?php if ($search): ?>
                    <a href="/admin_documents.php" class="btn btn-outline-secondary"><?= library_trans('clear') ?></a>
                <?php endif; ?>
            </div>
        </form>
    </div>
</div>

<div class="card shadow-sm border-0">
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="table-light">
                    <tr>
                        <th class="ps-4"><?= library_trans('id') ?></th>
                        <th><?= library_trans('cover') ?></th>
                        <th><?= library_trans('title_author') ?></th>
                        <th><?= library_trans('type_category') ?></th>
                        <th><?= library_trans('year') ?></th>
                        <th class="text-end pe-4"><?= library_trans('actions') ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($documents)): ?>
                        <tr><td colspan="6" class="text-center py-5 text-muted"><?= library_trans('no_documents_found') ?></td></tr>
                    <?php else: ?>
                        <?php foreach ($documents as $doc): ?>
                            <tr>
                                <td class="ps-4 text-muted small">#<?= $doc['id'] ?></td>
                                <td>
                                    <?php if (!empty($doc['cover_image_path'])): ?>
                                        <img src="/<?= h($doc['cover_image_path']) ?>" alt="Cover" class="rounded" style="width: 40px; height: 50px; object-fit: cover;">
                                    <?php else: ?>
                                        <div class="bg-light rounded d-flex align-items-center justify-content-center text-muted small" style="width: 40px; height: 50px;">
                                            <i class="bi bi-image"></i>
                                        </div>
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <?php $title = library_localized_document_title($doc, $lang); ?>
                                    <div class="fw-medium text-dark" dir="<?= library_text_dir($title, $lang) ?>"><?= h($title) ?></div>
                                    <?php if (!empty($doc['author'])): ?>
                                        <div class="small text-primary mt-1"><i class="bi bi-person me-1"></i><?= h($doc['author']) ?></div>
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <?php $typeLabel = library_localized_value($doc['type_en'] ?? null, $doc['type_ar'] ?? null, $lang); ?>
                                    <?php if ($typeLabel !== ''): ?>
                                        <span class="badge bg-info bg-opacity-10 text-info border border-info border-opacity-25 mb-1" dir="<?= library_text_dir($typeLabel, $lang) ?>"><?= h($typeLabel) ?></span><br>
                                    <?php endif; ?>
                                    <?php $categoryLabel = library_localized_value($doc['cat_en'] ?? $doc['category'] ?? null, $doc['cat_ar'] ?? $doc['category_ar'] ?? null, $lang); ?>
                                    <?php if ($categoryLabel !== ''): ?>
                                        <span class="badge bg-light text-dark border" dir="<?= library_text_dir($categoryLabel, $lang) ?>"><?= h($categoryLabel) ?></span>
                                    <?php endif; ?>
                                    <?php $subcategoryLabel = library_localized_value($doc['sub_en'] ?? $doc['sub_category'] ?? null, $doc['sub_ar'] ?? $doc['sub_category_ar'] ?? null, $lang); ?>
                                    <?php if ($subcategoryLabel !== ''): ?>
                                        <i class="bi bi-chevron-right text-muted small"></i>
                                        <span class="badge bg-light text-dark border" dir="<?= library_text_dir($subcategoryLabel, $lang) ?>"><?= h($subcategoryLabel) ?></span>
                                    <?php endif; ?>
                                </td>
                                <td><?= h((string)$doc['publish_year']) ?></td>
                                <td class="text-end pe-4">
                                    <a href="/document.php?id=<?= $doc['id'] ?>" target="_blank" class="btn btn-sm btn-outline-secondary me-1" title="<?= library_trans('view') ?>">
                                        <i class="bi bi-eye"></i>
                                    </a>
                                    <button class="btn btn-sm btn-outline-primary me-1" 
                                            onclick='openEditModal(<?= json_encode($doc) ?>)' title="<?= library_trans('edit') ?>">
                                        <i class="bi bi-pencil"></i>
                                    </button>
                                    <button class="btn btn-sm btn-outline-danger" onclick="deleteDocument(<?= $doc['id'] ?>)" title="<?= library_trans('delete') ?>">
                                        <i class="bi bi-trash"></i>
                                    </button>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        
        <!-- Pagination -->
        <?php if ($totalPages > 1): ?>
            <div class="p-3 border-top">
                <?php library_render_pagination($page, $totalPages, '/admin_documents.php'); ?>
            </div>
        <?php endif; ?>
    </div>
</div>

<!-- Document Modal -->
<div class="modal fade" id="documentModal" tabindex="-1" aria-hidden="true" data-bs-backdrop="static" data-bs-keyboard="false">
  <div class="modal-dialog modal-lg modal-dialog-scrollable">
    <form class="modal-content" method="post" action="/admin_documents.php" id="documentForm" enctype="multipart/form-data">
        <div class="modal-header bg-primary text-white">
            <h5 class="modal-title" id="documentModalTitle"><?= library_trans('add_new_document') ?></h5>
            <div class="ms-auto">
                <button type="button" class="btn btn-link text-white text-decoration-none me-2" data-bs-dismiss="modal"><?= library_trans('cancel') ?></button>
                <button type="submit" class="btn btn-light text-primary fw-bold" id="saveButton">
                    <span class="spinner-border spinner-border-sm d-none" role="status" aria-hidden="true"></span>
                    <?= library_trans('save_changes') ?>
                </button>
            </div>
        </div>
        <div class="modal-body">
            <input type="hidden" name="action" id="doc_action" value="create_document">
            <input type="hidden" name="id" id="doc_id" value="">
            
            <div class="row g-3">
                <!-- Titles -->
                <div class="col-md-6">
                    <label class="form-label small fw-bold text-uppercase text-muted"><?= library_trans('name_en') ?> <span class="text-danger">*</span></label>
                    <div class="input-group">
                        <input type="text" class="form-control" name="title_en" id="doc_title_en" required>
                        <button class="btn btn-outline-secondary" type="button" onclick="translateText('doc_title_en', 'doc_title_ar', 'Arabic')" title="<?= library_trans('translation_to_arabic') ?>"><i class="bi bi-translate"></i></button>
                    </div>
                </div>
                <div class="col-md-6">
                    <label class="form-label small fw-bold text-uppercase text-muted"><?= library_trans('name_ar') ?></label>
                    <div class="input-group">
                        <input type="text" class="form-control" name="title_ar" id="doc_title_ar" dir="rtl">
                        <button class="btn btn-outline-secondary" type="button" onclick="translateText('doc_title_ar', 'doc_title_en', 'English')" title="<?= library_trans('translation_to_english') ?>"><i class="bi bi-translate"></i></button>
                    </div>
                </div>

                <!-- Basic Info -->
                <div class="col-md-6">
                    <label class="form-label small fw-bold text-uppercase text-muted"><?= library_trans('author') ?></label>
                    <input type="text" class="form-control" name="author" id="doc_author">
                </div>
                <div class="col-md-3">
                    <label class="form-label small fw-bold text-uppercase text-muted"><?= library_trans('publisher') ?></label>
                    <input type="text" class="form-control" name="publisher" id="doc_publisher">
                </div>
                <div class="col-md-3">
                    <label class="form-label small fw-bold text-uppercase text-muted"><?= library_trans('year') ?></label>
                    <input type="number" class="form-control" name="publish_year" id="doc_publish_year" min="1000" max="2100">
                </div>

                <!-- Classification -->
                <div class="col-md-4">
                    <label class="form-label small fw-bold text-uppercase text-muted"><?= library_trans('types') ?></label>
                    <select class="form-select" name="type_id" id="doc_type_id">
                        <option value=""><?= library_trans('select_type') ?></option>
                        <?php foreach ($types as $t): ?>
                            <?php $typeName = library_localized_value($t['name_en'] ?? null, $t['name_ar'] ?? null, $lang); ?>
                            <option value="<?= $t['id'] ?>" dir="<?= library_text_dir($typeName, $lang) ?>"><?= h($typeName) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-md-4">
                    <label class="form-label small fw-bold text-uppercase text-muted"><?= library_trans('category') ?></label>
                    <select class="form-select" name="category_id" id="doc_category_id" onchange="updateSubcategories()">
                        <option value=""><?= library_trans('select_category') ?></option>
                        <?php foreach ($categories as $c): ?>
                            <?php $categoryName = library_localized_value($c['name_en'] ?? null, $c['name_ar'] ?? null, $lang); ?>
                            <option value="<?= $c['id'] ?>" dir="<?= library_text_dir($categoryName, $lang) ?>"><?= h($categoryName) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-md-4">
                    <label class="form-label small fw-bold text-uppercase text-muted"><?= library_trans('subcategories') ?></label>
                    <select class="form-select" name="subcategory_id" id="doc_subcategory_id">
                        <option value=""><?= library_trans('select_subcategory') ?></option>
                    </select>
                </div>

                <!-- Details -->
                <div class="col-md-6">
                    <label class="form-label small fw-bold text-uppercase text-muted"><?= library_trans('country') ?></label>
                    <input type="text" class="form-control" name="country" id="doc_country">
                </div>
                <div class="col-md-6">
                    <label class="form-label small fw-bold text-uppercase text-muted"><?= library_trans('page_count') ?></label>
                    <input type="number" class="form-control" name="page_count" id="doc_page_count" min="0">
                </div>

                <!-- Summaries -->
                <div class="col-md-6">
                    <label class="form-label small fw-bold text-uppercase text-muted"><?= library_trans('summary_en') ?></label>
                    <div class="input-group">
                        <textarea class="form-control" name="summary_en" id="doc_summary_en" rows="3"></textarea>
                        <button class="btn btn-outline-secondary" type="button" onclick="translateText('doc_summary_en', 'doc_summary_ar', 'Arabic')" title="<?= library_trans('translation_to_arabic') ?>"><i class="bi bi-translate"></i></button>
                    </div>
                </div>
                <div class="col-md-6">
                    <label class="form-label small fw-bold text-uppercase text-muted"><?= library_trans('summary_ar') ?></label>
                    <div class="input-group">
                        <textarea class="form-control" name="summary_ar" id="doc_summary_ar" rows="3" dir="rtl"></textarea>
                        <button class="btn btn-outline-secondary" type="button" onclick="translateText('doc_summary_ar', 'doc_summary_en', 'English')" title="<?= library_trans('translation_to_english') ?>"><i class="bi bi-translate"></i></button>
                    </div>
                </div>

                <!-- Files -->
                <div class="col-md-6">
                    <label class="form-label small fw-bold text-uppercase text-muted"><?= library_trans('cover_image') ?></label>
                    <input class="form-control" type="file" name="cover_file" accept="image/*">
                    <div id="current_cover_preview" class="mt-2 d-none">
                        <small class="text-muted d-block mb-1"><?= library_trans('current_cover') ?></small>
                        <img src="" class="rounded border" style="height: 60px;">
                    </div>
                </div>
                <div class="col-md-6">
                    <label class="form-label small fw-bold text-uppercase text-muted"><?= library_trans('document_file') ?> <span id="file_required_indicator" class="text-danger">*</span></label>
                    <input class="form-control" type="file" name="document_file" id="document_file_input">
                    <div id="current_file_info" class="mt-2 d-none">
                        <small class="text-muted"><i class="bi bi-file-earmark"></i> <span id="current_filename"></span></small>
                    </div>
                    <small class="text-muted d-block mt-1" id="file_help_text"><?= library_trans('required_new_docs') ?></small>
                </div>

                <!-- Settings -->
                <div class="col-12">
                    <div class="card bg-light border-0">
                        <div class="card-body">
                            <h6 class="card-subtitle mb-2 text-muted text-uppercase small fw-bold"><?= library_trans('visibility_permissions') ?></h6>
                            <div class="row">
                                <div class="col-md-3">
                                    <select class="form-select form-select-sm" name="visibility" id="doc_visibility">
                                        <option value="public"><?= library_trans('visibility_public') ?></option>
                                        <option value="private"><?= library_trans('visibility_private') ?></option>
                                    </select>
                                </div>
                                <div class="col-md-9 d-flex align-items-center gap-3">
                                    <div class="form-check">
                                        <input class="form-check-input" type="checkbox" name="allow_download" id="doc_allow_download" value="1">
                                        <label class="form-check-label small" for="doc_allow_download"><?= library_trans('download') ?></label>
                                    </div>
                                    <div class="form-check">
                                        <input class="form-check-input" type="checkbox" name="allow_print" id="doc_allow_print" value="1">
                                        <label class="form-check-label small" for="doc_allow_print"><?= library_trans('print') ?></label>
                                    </div>
                                    <div class="form-check">
                                        <input class="form-check-input" type="checkbox" name="allow_copy" id="doc_allow_copy" value="1">
                                        <label class="form-check-label small" for="doc_allow_copy"><?= library_trans('copy') ?></label>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
  </div>
</div>

<!-- Delete Confirmation Form -->
<form method="post" action="/admin_documents.php" id="deleteForm">
    <input type="hidden" name="action" id="deleteAction" value="">
    <input type="hidden" name="id" id="deleteId" value="">
</form>

<script>
    let documentModal;
    const currentLang = <?= json_encode($lang) ?>;
    const allSubcategories = <?= json_encode($allSubcategories) ?>;

    document.addEventListener('DOMContentLoaded', function() {
        documentModal = new bootstrap.Modal(document.getElementById('documentModal'));
        
        // Form submission loading state
        document.getElementById('documentForm').addEventListener('submit', function() {
            const btn = document.getElementById('saveButton');
            btn.disabled = true;
            btn.querySelector('.spinner-border').classList.remove('d-none');
        });
    });

    function updateSubcategories(selectedSubId = null) {
        const catSelect = document.getElementById('doc_category_id');
        const subSelect = document.getElementById('doc_subcategory_id');
        const catId = catSelect.value;
        
        subSelect.innerHTML = '<option value=""><?= library_trans('select_subcategory') ?></option>';
        
        if (catId) {
            const subs = allSubcategories.filter(s => s.category_id == catId);
            subs.forEach(s => {
                const opt = document.createElement('option');
                opt.value = s.id;
                opt.textContent = currentLang === 'ar' ? (s.name_ar || s.name_en || '') : (s.name_en || s.name_ar || '');
                if (selectedSubId && s.id == selectedSubId) opt.selected = true;
                subSelect.appendChild(opt);
            });
        }
    }

    function openCreateModal() {
        document.getElementById('documentModalTitle').innerText = '<?= library_trans('add_new_document') ?>';
        document.getElementById('doc_action').value = 'create_document';
        document.getElementById('doc_id').value = '';
        document.getElementById('documentForm').reset();
        
        // UI Helpers for Create
        document.getElementById('file_required_indicator').classList.remove('d-none');
        document.getElementById('document_file_input').required = true;
        document.getElementById('file_help_text').innerText = '<?= library_trans('required_new_docs') ?>';

        // Clear previews
        document.getElementById('current_cover_preview').classList.add('d-none');
        document.getElementById('current_file_info').classList.add('d-none');
        
        // Reset dynamic selects
        updateSubcategories();
        
        documentModal.show();
    }

    function openEditModal(doc) {
        document.getElementById('documentModalTitle').innerText = '<?= library_trans('edit') ?>'; //  + doc.title_en;
        document.getElementById('doc_action').value = 'update_document';
        document.getElementById('doc_id').value = doc.id;
        
        // UI Helpers for Edit
        document.getElementById('file_required_indicator').classList.add('d-none');
        document.getElementById('document_file_input').required = false;
        document.getElementById('file_help_text').innerText = '<?= library_trans('leave_empty_keep') ?>';

        // Fill fields
        document.getElementById('doc_title_en').value = doc.title_en || '';
        document.getElementById('doc_title_ar').value = doc.title_ar || '';
        document.getElementById('doc_author').value = doc.author || '';
        document.getElementById('doc_publisher').value = doc.publisher || '';
        document.getElementById('doc_publish_year').value = doc.publish_year || '';
        document.getElementById('doc_country').value = doc.country || '';
        document.getElementById('doc_page_count').value = doc.page_count || '';
        document.getElementById('doc_summary_en').value = doc.summary_en || '';
        document.getElementById('doc_summary_ar').value = doc.summary_ar || '';
        
        document.getElementById('doc_type_id').value = doc.type_id || '';
        document.getElementById('doc_visibility').value = doc.visibility || 'public';
        
        document.getElementById('doc_allow_download').checked = !!parseInt(doc.allow_download);
        document.getElementById('doc_allow_print').checked = !!parseInt(doc.allow_print);
        document.getElementById('doc_allow_copy').checked = !!parseInt(doc.allow_copy);

        // Handle Category & Subcategory
        document.getElementById('doc_category_id').value = doc.category_id || '';
        updateSubcategories(doc.subcategory_id);

        // Previews
        const coverDiv = document.getElementById('current_cover_preview');
        if (doc.cover_image_path) {
            coverDiv.classList.remove('d-none');
            coverDiv.querySelector('img').src = '/' + doc.cover_image_path;
        } else {
            coverDiv.classList.add('d-none');
        }

        const fileDiv = document.getElementById('current_file_info');
        if (doc.file_name) {
            fileDiv.classList.remove('d-none');
            document.getElementById('current_filename').innerText = doc.file_name;
        } else {
            fileDiv.classList.add('d-none');
        }

        documentModal.show();
    }

    function deleteDocument(id) {
        if (confirm('<?= library_trans('confirm_delete') ?>')) {
            document.getElementById('deleteAction').value = 'delete_document';
            document.getElementById('deleteId').value = id;
            document.getElementById('deleteForm').submit();
        }
    }
</script>

<?php admin_render_footer(); ?>