    <?php
    if (isset($_SESSION['user_id'])) {

        $sar = $_POST['sar'] ?? date("m");
        $jil = $_POST['jil'] ?? date("Y");
        $sar_next = $sar + 1;
        $jil_next = $jil;

        if ($sar_next > 12) {
            $sar_next = 1;
            $jil_next++;
        }

        $sql = "SELECT att.id, class.name, tlesson.lessonName, cag.name, cag.inter, ognoo, att.lessonid, class.sname, att.tuluv, tlesson.cag FROM att 
                INNER JOIN class ON att.classid = class.id 
                    INNER JOIN tlesson ON att.lessonid = tlesson.id 
                        INNER JOIN cag ON att.cagid = cag.id 
                            WHERE att.this_on = '" . $this_on . "' and att.tid = '" . $_SESSION['user_id'] . "' 
                                AND ognoo >= '$jil-$sar-01' AND ognoo < '$jil_next-$sar_next-01' ORDER BY ognoo DESC , cag.name DESC";
        _selectNoParam(
            $stmt,
            $count,
            $sql,
            $id,
            $class,
            $lesson,
            $cag,
            $cag_inter,
            $ognoo,
            $lessonid,
            $sname,
            $atttuluv,
            $lcag
        );

        $json = [];
        if ($count > 0) {
            while (_fetch($stmt)) {
                $calen = new stdClass;
                $times = explode('-', $cag_inter);

                if (count($times) !== 2) {
                    $startTime = "00:00";
                    $endTime = "23:59";
                }

                $startTime = trim($times[0]);
                $endTime = trim($times[1]);

                $calen->id = $id;
                $calen->calendarId = $id;
                $calen->title = "$sname, $cag, $lesson";
                $calen->body = "$sname $class ангийн $ognoo өдөр, $cag ирц бүртгэл";
                $calen->start = $ognoo . "T" . $startTime;  // ISO 8601 формат
                $calen->end = $ognoo . "T" . $endTime;  // ISO 8601 формат
                $calen->category = "time";
                $calen->isAllDay = false;
                $calen->color = "#421414ff";
                $calen->bgColor = "#9e5fff";
                $json[] = $calen;
            }
            echo json_encode($json);
        } else {
            echo "<div class='alert alert-warning'>$jil оны $sar сард ирц бүртгэл олдсонгүй!</div>";
        }
    } else {
        echo "Та нэвтэрч ороогүй байна!";
    }
