<?php
includeContainer('_library_lib.php', 1);
setSessionLang();
$socInfo = getSocInfo();

$showDonationLink = 0;
if (isModEnabled('don') && getDolGlobalString('DONATION_ENABLE_PUBLIC')) {
    $showDonationLink = 1;
}

$showEventGoalRaised = 0;
if (isModEnabled('don') && isModEnabled('eventorganization') && getDolGlobalString('WS_'.strtoupper($website->ref).'_' . 'SHOW_EVENT_GOAL_RAISED_AMOUNTS')) {
    $showEventGoalRaised = 1;
}

$showParticipateLink = 0;
if (isModEnabled('eventorganization') && getDolGlobalString('WS_'.strtoupper($website->ref).'_' . 'SHOW_EVENT_PARTICIPATE_LINK')) {
    $showParticipateLink = 1;
}
?>
<div class="container-fluid causes">
    <div class="container">
        <div class="section-title" id="section-events" style="scroll-margin-top: 100px;">
          <div class="line left-line"></div>
          <h1><?php echo $weblangs->trans("actionsEvents"); ?></h1>
          <div class="line right-line"></div>
        </div>
        <div class="row g-4">

            <?php 
            $maxevents = GETPOSTINT('maxevents') ? GETPOSTINT('maxevents') : 3;
            $countofevents = 0;
            $sqlCount = "SELECT count(p.rowid) as countofevents 
                    FROM ".MAIN_DB_PREFIX."projet as p 
                    WHERE p.fk_statut IN (0,1) AND p.usage_organize_event IN (1) 
                    ORDER BY p.datec DESC ";
            $resqlCount = $db->query($sqlCount);
            if ($resqlCount) {
                $objCount = $db->fetch_object($resqlCount);
                $countofevents = $objCount->countofevents ? $objCount->countofevents : 0;
            }
        
            $sql = "SELECT p.rowid, p.title, p.description, p.datec, p.dateo, p.datee, p.budget_amount, p.fk_statut 
                    FROM ".MAIN_DB_PREFIX."projet as p 
                    WHERE p.fk_statut IN (0,1) AND p.usage_organize_event IN (1) 
                    ORDER BY p.datec DESC 
                    LIMIT ".$maxevents;
            $resql = $db->query($sql);
            if ($resql) {
                $countofblogs = $db->num_rows($resql);
                $i=1;
                while ($obj = $db->fetch_object($resql)) {
                    ?>
                    <div class="col-lg-6 col-xl-4 event-item" id="event-<?php echo $i; ?>">
                        <div class="causes-item">
                            <div class="causes-img">
                                <?php
                                    $urlforimage = getImageFromHtmlContent($obj->description ? $obj->description : "");
                                    if (empty($urlforimage)) {
                                        $urlforimage = 'medias/image/'.$website->ref.'/noimage.jpg';
                                    }
                                ?>
                                <img src="<?php echo $urlforimage; ?>" class="img-fluid w-100" style="height: 275px; object-fit: cover;" alt="Image">
                                <div class="causes-link pb-2 px-3">
                                    <?php
                                        $progress = 100;
                                        if ($obj->budget_amount > 0 && $showEventGoalRaised) {
                                            $sqlCollected = "SELECT SUM(amount) as total_collected 
                                                             FROM ".MAIN_DB_PREFIX."don 
                                                             WHERE fk_projet = ".$obj->rowid;
                                            $resqlCollected = $db->query($sqlCollected);
                                            $collected = 0;
                                            if ($resqlCollected) {
                                                $objCollected = $db->fetch_object($resqlCollected);
                                                $collected = $objCollected->total_collected ? $objCollected->total_collected : 0;
                                            }
                                            $progress = round(($collected / $obj->budget_amount) * 100, 2);
                                        }
                                    ?>
                                    <?php if ($showEventGoalRaised) { ?>
                                        <small class="text-white"><i class="fas fa-chart-bar secondary-color me-2"></i><?php echo $weblangs->trans("goal"); ?>: <?php print price(price2num($obj->budget_amount, 'MT'), 1, '', 1, -1); ?> €</small>
                                        <small class="text-white"><i class="fa fa-thumbs-up secondary-color me-2"></i><?php echo $weblangs->trans("raised"); ?>: <?php print price(price2num($collected, 'MT'), 1, '', 1, -1); ?> €</small>
                                    <?php } ?>    
                                </div>
                                <div class="causes-dination p-2">
                                    <?php if ($showDonationLink) { ?>
                                        <?php if (getDolGlobalInt('WS_' . strtoupper($website->ref) . '_PAYMENT_IN_FRAME')) { ?>
                                            <a href="page.php?donation=<?php echo $obj->rowid; ?>" class="btn flat-button text-white py-2 px-3"><?php echo $weblangs->trans("donateNow"); ?></a>
                                        <?php } else { ?>
                                            <a href="<?php echo $socInfo['link_donation'] . '?project_id='.urlencode($obj->rowid); ?>" target="new" class="btn flat-button text-white py-2 px-3"><?php echo $weblangs->trans("donateNow"); ?></a>
                                        <?php } ?>
                                    <?php } ?>
                                    
                                    <?php if ($showParticipateLink) { ?>
                                        <?php $encodedsecurekey = dol_hash(getDolGlobalString('EVENTORGANIZATION_SECUREKEY').'conferenceorbooth'.((int) $obj->rowid), 'md5'); ?>
                                        <a href="<?php echo $socInfo['link_participation'] . '&id='.urlencode($obj->rowid).'&securekey='.urlencode($encodedsecurekey); ?>" target="new" class="btn flat-button text-white py-2 px-3"><?php echo $weblangs->trans("Participate"); ?></a>
                                    <?php } ?>
                                </div>
                            </div>
                            
                            
                            <div class="progress">
                              <div class="progress-bar" role="progressbar" style="width: <?php echo $progress; ?>%;" aria-valuenow="<?php echo $progress; ?>" aria-valuemin="0" aria-valuemax="100">
                                <?php if ($showEventGoalRaised) { ?>
                                <span><?php echo $progress; ?>%</span>
                                <?php } ?>
                              </div>
                            </div>

                            
                            <div class="causes-content p-4">
                                <span class="text-body">
                                    <i class="fas fa-calendar-alt me-2"></i> <?php echo dol_print_date($obj->datec, 'daytext', 'tzuser', $weblangs); ?>
                                </span>
                                <h4 class="py-3"><?php echo $obj->title; ?></h4>

                                <a href="page.php?id=<?php echo $obj->rowid; ?>" class="btn flat-button text-white py-2 px-3 float-end"><?php echo $weblangs->trans("readMore"); ?></a>
                            </div>
                        </div>
                    </div>
                    <?php
                    $i++;
                }
            }
            ?>
        </div>
        
        <?php if ($maxevents <= $countofevents) { ?>
            <div class="row">
                <div class="col-lg-7 text-center py-5 section-title">
                    <div class="line left-line left-gradient"></div>
                    <a href="javascript:void(0);" class="btn flat-button text-white bgmaincolor" id="loadMoreEventsBtn"><?php echo $weblangs->trans("loadMore"); ?> (<?php echo $maxevents; ?>/<?php echo $countofevents; ?>)</a>
                    <div class="line right-line right-gradient"></div>
                </div>
            </div>
        <?php } ?>
        <script>
            document.getElementById('loadMoreEventsBtn').addEventListener('click', function() {
            const currentUrl = new URL(window.location.href);
            const maxEventsParam = <?php echo $maxevents; ?>;
            const newMaxEvents = parseInt(maxEventsParam) + 3;
            currentUrl.searchParams.set('maxevents', newMaxEvents);
            currentUrl.hash = '#event-' + (parseInt(maxEventsParam) + 1).toString();
            window.location.href = currentUrl.toString();
            });
        </script>
    </div>
</div>