<?php
include_once "global.php";

$pageTitle = 'Calendar - ';
include_once 'header.php';
?>

<!--breadcrumb-->
<div class="page-breadcrumb d-none d-sm-flex align-items-center mb-3">
    <div class="breadcrumb-title pe-3">Events Management</div>
    <div class="ps-3">
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb mb-0 p-0">
                <li class="breadcrumb-item"><a href="javascript:;"></a>
                </li>
                <li class="breadcrumb-item active" aria-current="page">Calendar</li>
            </ol>
        </nav>
    </div>
    <div class="ms-auto">
        <a href="addevent">Add
            Events/Tasks/Reminders</a>
    </div>
</div>
<!--end breadcrumb-->
<h6 class="mb-0 text-uppercase">Calendar</h6>
<hr>
<div class="card">
    <div class="card-body">
        <div class="table-responsive">
            <div id='calendar'></div>
        </div>
    </div>
</div>
<h6 class="mb-0 text-uppercase">All Events & Tasks</h6>
<hr>
<div class="card">
    <div class="card-body">
        <div class="row g-3 mb-3">
            <div class="col-4">
                <select id="eventUserSelect" class="single-select-field" data-placeholder="Select user">
                    <option selected="" value="">Select...</option>
                    <?php
                    $users = $productClass->AllUsersArray();
                    foreach ($users as $user) {
                        $userId = $user["acc_id"];
                        $name = $user["acc_name"];
                        echo "<option value='$userId'>$name</option>";
                    }
                    ?>
                </select>
            </div>
            <div class="col-4"></div>
            <div class="col-4">
                <input type="search" class="form-control" id="eventSearch" placeholder="Search...">
            </div>
        </div>

        <ul class="nav nav-tabs nav-success" role="tablist">
            <?php
            $tabs = [
                'all' => 'All',
                'completed' => 'Completed',
                'pending' => 'Pending',
                'events' => 'Events',
                'tasks' => 'Tasks',
                'reminders' => 'Reminders'
            ];
            foreach ($tabs as $id => $label) {
                $activeClass = ($id == 'all') ? 'active' : '';
                echo "<li class='nav-item' role='presentation'>
                        <a class='nav-link $activeClass' data-bs-toggle='tab' href='#$id' role='tab' aria-selected='" . ($id == 'all' ? 'true' : 'false') . "'>
                            <div class='tab-title'>$label</div>
                        </a>
                      </li>";
            }
            ?>
        </ul>
        <div class="tab-content py-3">
            <?php
            $tabQueries = [
                'all' => ["SELECT * FROM events ORDER BY due_date ASC", []],
                'completed' => ["SELECT * FROM events WHERE status = ? ORDER BY due_date ASC", ['Complete']],
                'pending' => ["SELECT * FROM events WHERE status = ? ORDER BY due_date ASC", ['Pending']],
                'events' => ["SELECT * FROM events WHERE category = ? ORDER BY due_date ASC", ['Event']],
                'tasks' => ["SELECT * FROM events WHERE category = ? ORDER BY due_date ASC", ['Task']],
                'reminders' => ["SELECT * FROM events WHERE category = ? ORDER BY due_date ASC", ['Reminder']]
            ];

            foreach ($tabQueries as $tabId => [$sql, $params]) {
                $events = $dbF->getRows($sql, $params);
                $activeClass = ($tabId == 'all') ? 'show active' : '';
                echo "<div class='tab-pane fade $activeClass' id='$tabId' role='tabpanel'>";
                $crm->renderEvents($events);
                echo "</div>";
            }
            ?>
        </div>
    </div>
</div>

<?php
include_once 'footer.php';
?>