<?php
defined('_JEXEC') or die;

use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;
use Joomla\CMS\HTML\HTMLHelper;

/** @var \Joomla\Component\Panggilan\Site\View\Display\HtmlView $this */

$activeYear = $this->activeYear;
$currentMonth = (int) date('n');
$currentYear = (int) date('Y');
?>
<div class="com-panggilan py-4">
    <div class="container>
        <div class=" page-header text-center mb-5">
        <h2 id="panggilan-title"><?php echo Text::sprintf('COM_PANGGILAN_TITLE_ITEMS', $activeYear); ?></h2>
    </div>

    <div class="row panggilan-layout">
        <!-- Sidebar Filter Tahun -->
        <aside class="col-lg-3 col-md-4 panggilan-sidebar">
            <div class="card sticky-top" style="top: 20px;">
                <div class="list-group list-group-flush bg-transparent">
                    <?php if (empty($this->availableYears)): ?>
                        <div class="text-muted small p-3">Belum ada data</div>
                    <?php else: ?>
                        <?php foreach ($this->availableYears as $year): ?>
                            <a href="<?php echo Route::_('index.php?option=com_panggilan&view=display&year=' . $year); ?>"
                                class="panggilan-year-link <?php echo ($year == $activeYear) ? 'active' : ''; ?>">
                                <span>Tahun <?php echo $year; ?></span>
                                <?php if ($year == $activeYear): ?>
                                    <i class="icon-chevron-right small"></i>
                                <?php endif; ?>
                            </a>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </div>
            </div>
        </aside>

        <!-- Main Content -->
        <main class="col-lg-9 col-md-8 p-0">
            <div class="panggilan-accordion" id="panggilanAccordion">
                <?php foreach ($this->items as $monthNum => $items): ?>
                    <?php
                    $count = count($items);
                    $isOpen = ($monthNum == $currentMonth && $activeYear == $currentYear);
                    ?>
                    <div class="accordion-item">
                        <h2 class="accordion-header" id="heading<?php echo $monthNum; ?>">
                            <button class="accordion-button <?php echo $isOpen ? '' : 'collapsed'; ?>" type="button"
                                data-bs-toggle="collapse" data-bs-target="#collapse<?php echo $monthNum; ?>"
                                aria-expanded="<?php echo $isOpen ? 'true' : 'false'; ?>"
                                aria-controls="collapse<?php echo $monthNum; ?>">
                                <div class="d-flex flex-grow-1 align-items-center pe-2">
                                    <span class="text-start ps-2 fw-bold">
                                        <?php echo $this->monthNames[$monthNum]; ?>
                                    </span>
                                    <?php if ($count > 0): ?>
                                        <span class="badge panggilan-badge rounded-pill ms-auto shadow-sm"><?php echo $count; ?>
                                            Perkara</span>
                                    <?php endif; ?>
                                </div>
                            </button>
                        </h2>
                        <div id="collapse<?php echo $monthNum; ?>"
                            class="accordion-collapse collapse <?php echo $isOpen ? 'show' : ''; ?>"
                            aria-labelledby="heading<?php echo $monthNum; ?>" data-bs-parent="#panggilanAccordion">
                            <div class="accordion-body p-0">
                                <?php if ($count > 0): ?>
                                    <div class="table-responsive">
                                        <table class="table panggilan-table w-100">
                                            <thead>
                                                <tr>
                                                    <th style="width: 25%;">Pihak</th>
                                                    <th style="width: 30%;">No. Perkara</th>
                                                    <th style="width: 25%;">Tanggal Sidang</th>
                                                    <th class="text-center" style="width: 20%;">File</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php foreach ($items as $item): ?>
                                                    <tr>
                                                        <td class="fw-bold text-dark">
                                                            <?php echo $this->escape($item->party_name); ?>
                                                        </td>
                                                        <td>
                                                            <span
                                                                class="panggilan-case-number"><?php echo $this->escape($item->case_number); ?></span>
                                                        </td>
                                                        <td class="text-secondary text-nowrap">
                                                            <?php echo HTMLHelper::_('date', $item->hearing_date, 'd/m/Y'); ?>
                                                        </td>
                                                        <td class="text-center">
                                                            <?php if ($item->is_embeddable): ?>
                                                                <button class="btn btn-action-link btn-view-file mx-auto"
                                                                    data-preview-url="<?php echo $this->escape($item->preview_url); ?>"
                                                                    data-original-url="<?php echo $this->escape($item->original_url); ?>"
                                                                    title="Preview File">
                                                                    <i class="icon-link"></i>
                                                                </button>
                                                            <?php else: ?>
                                                                <a href="<?php echo $this->escape($item->original_url); ?>"
                                                                    target="_blank" class="btn btn-action-link mx-auto"
                                                                    title="Buka File">
                                                                    <i class="icon-link"></i>
                                                                </a>
                                                            <?php endif; ?>
                                                        </td>
                                                    </tr>
                                                <?php endforeach; ?>
                                            </tbody>
                                        </table>
                                    </div>
                                <?php else: ?>
                                    <div class="p-5 text-center text-muted">
                                        Tidak ada panggilan sidang.
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </main>
    </div>
</div>
</div>

<!-- PDF Modal -->
<div class="modal fade" id="panggilanPdfModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-xl modal-dialog-centered modal-dialog-scrollable">
        <div class="modal-content" style="height: 90vh;">
            <div class="modal-header py-2">
                <h5 class="modal-title fs-6">Preview Dokumen</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body p-0 bg-light h-100 position-relative">

                <!-- Loader -->
                <div id="pdfLoader" class="position-absolute top-50 start-50 translate-middle text-center d-none">
                    <div class="spinner-border text-primary" role="status"></div>
                    <div class="mt-2 small text-muted">Memuat dokumen...</div>
                </div>

                <!-- Error Message -->
                <div id="pdfError" class="position-absolute top-50 start-50 translate-middle text-center d-none w-75">
                    <div class="mb-3 text-danger">
                        <span class="icon-warning fs-1"></span>
                    </div>
                    <p class="text-muted">Preview tidak tersedia atau diblokir oleh browser.</p>
                    <a href="#" id="pdfErrorBtn" target="_blank" class="btn btn-primary">
                        Buka di Tab Baru
                    </a>
                </div>

                <iframe src="" id="panggilanPdfFrame" class="w-100 h-100 border-0"
                    allow="autoplay; encrypted-media"></iframe>
            </div>
            <div class="modal-footer py-1 bg-light">
                <button type="button" class="btn btn-sm btn-secondary" data-bs-dismiss="modal">Tutup</button>
                <a href="#" id="panggilanPdfFallback" target="_blank" class="btn btn-sm btn-primary">
                    <span class="icon-external-link"></span> Buka di Tab Baru
                </a>
            </div>
        </div>
    </div>
</div>