<?php

require_once __DIR__ . '/_cms_helpers.php';

$lang =
    $lang ?? 'en';

?>

<section class="ec-timeline-wrap ec-reveal ec-hover-lift ec-glow-hover">

    <h2 class="section-title">
        <i class="fa fa-timeline text-primary"></i>
        <?= pcb_h(t('conf.important_dates', 'المواعيد المهمة')) ?>
    </h2>

    <?php if (!empty($timelineItems)): ?>

        <div class="ec-timeline">

            <?php foreach ($timelineItems as $item): ?>

                <?php
                $days = floor(($item['timestamp'] - time()) / 86400);

                $statusClass = match($item['status']) {
                    'open' => 'ec-status-open',
                    'closed' => 'ec-status-closed',
                    default => 'ec-status-upcoming'
                };

                $statusLabel = match($item['status']) {
                    'open' => ($lang === 'ar' ? 'قريب' : 'Soon'),
                    'closed' => ($lang === 'ar' ? 'منتهي' : 'Closed'),
                    default => ($lang === 'ar' ? 'قادمة' : 'Upcoming')
                };
                ?>

                <div class="ec-timeline-item">

                    <div class="ec-timeline-dot"></div>

                    <div class="ec-timeline-card">

                        <div class="ec-timeline-date">
                            <?= pcb_h(date('Y-m-d', $item['timestamp'])) ?>
                        </div>

                        <div class="ec-timeline-title">
                            <?= pcb_h($item['label']) ?>
                        </div>

                        <div class="ec-timeline-status <?= $statusClass ?>">
                            <i class="fa fa-circle"></i>
                            <?= pcb_h($statusLabel) ?>
                        </div>

                        <div class="ec-countdown">

                            <?php if ($days > 0): ?>

                                <?= $lang === 'ar'
                                    ? ('متبقي ' . $days . ' يوم')
                                    : ($days . ' days remaining') ?>

                            <?php elseif ($days == 0): ?>

                                <?= $lang === 'ar'
                                    ? 'اليوم'
                                    : 'Today' ?>

                            <?php else: ?>

                                <?= $lang === 'ar'
                                    ? ('منذ ' . abs($days) . ' يوم')
                                    : (abs($days) . ' days ago') ?>

                            <?php endif; ?>

                        </div>

                    </div>

                </div>

            <?php endforeach; ?>

        </div>

    <?php else: ?>

        <div class="alert alert-info rounded-4">
            <?= pcb_h(t('conf.no_dates', 'لا توجد مواعيد منشورة حالياً.')) ?>
        </div>

    <?php endif; ?>

</section>
