<?php
function events($module, $date) {
	$edate = explode(",", $date);
    if (count($edate) < 3) return []; // Geçersiz tarih formatı
    $cgun = $edate[0]; $cay = $edate[1];  $cyil = $edate[2];
	global $conn; global $luye; 
	$result = [];
	if ($module === "news") {
		    // Veritabanı sorgusu
        $sqlh = mysqli_query($conn, "SELECT * FROM ".$_SESSION["lang"]." WHERE ne='haber' $luye");  while ($rsh = mysqli_fetch_assoc($sqlh)) {
            $dates = explode('-', $rsh["t4"]);
            if (count($dates) < 3) continue; // Geçersiz tarih formatı
            $startDate = date_create($rsh["t4"]);
            $endDate = !empty($rsh["t9"]) ? date_create($rsh["t9"]) : null;
            if ($endDate) {
                $dateDifference = date_diff($startDate, $endDate);
                
                for ($x = 0; $x <= $dateDifference->days; $x++) {
                    $ddates = explode('-', date('Y-m-d', strtotime($rsh["t4"].' +'.$x.' day')));
                    if (count($ddates) < 3) continue;
                    if (($ddates[2] == $cgun && $ddates[1] == $cay && $ddates[0] == $cyil) || ($rsh["s3"] == 1)) {
                        $result[] = $rsh;
                        break; // Tekrarlamayı önlemek için döngüden çık
                    }
                }
            } else {
                if (($dates[2] == $cgun && $dates[1] == $cay && $dates[0] == $cyil) || ($rsh["s3"] == 1)) {
                    $result[] = $rsh;
                }
            }
        }
	}elseif ($module === "accounting") {
		$sql = mysqli_query($conn, "SELECT * FROM t4 WHERE (ne='senet' OR ne='cek') $luye ORDER BY t2 ASC"); while ($rs = mysqli_fetch_assoc($sql)) {
			$dates = explode('-', $rs["t4"]);
			if (($dates[2] == $cgun && $dates[1] == $cay && $dates[0] == $cyil) || ($dates[2] == $cgun && $rs["s3"] == 1)) {
				$rsc = mysqli_fetch_assoc(mysqli_query($conn, "SELECT id,t1 FROM t5 WHERE id = '{$rs["s1"]}' LIMIT 1"));
				$rs["uname"] = $rsc ? $rsc["t1"] : null;
				$result[] = $rs;
			}
		}
	}elseif ($module === "note") {
		$sql = mysqli_query($conn, "SELECT * FROM genel WHERE ne='not' ORDER BY t2 ASC"); while ($rs = mysqli_fetch_assoc($sql)) {
			$t5s = explode(',', $rs["t5"]);
			$dates = explode('-', $rs["t4"]);
			if ($rs["t9"] !== ""){
				$dateDifference = date_diff(date_create($rs["t4"] . " 00:00:00"), date_create($rs["t9"] . " 00:00:00"));
				for ($x = 0; $x <= $dateDifference->days; $x++) {
					$ddates = explode('-', date('Y-m-d', strtotime($rs["t4"] . ' + ' . $x . ' day')));
					if (($ddates[2] == $cgun && $ddates[1] == $cay && $ddates[0] == $cyil) || ($ddates[2] == $cgun && $rs["s3"] == 1)){
						if (in_array($_SESSION["user_id"], $t5s) || $_SESSION["user_id"] == $rs["s19"]) {
							$result[] = $rs;
						}
					}
				}
			} else {
				if (($dates[2] == $cgun && $dates[1] == $cay && $dates[0] == $cyil) || ($dates[2] == $cgun && $rs["s3"] == 1)) {
					if (in_array($_SESSION["user_id"], $t5s) || $_SESSION["user_id"] == $rs["s19"]) {
						$result[] = $rs;
					}
				}
			}
		}
	}elseif ($module === "tasks") {
		$sql = mysqli_query($conn, "SELECT * FROM genel WHERE ne='gorev' AND s13<>3 $luye ORDER BY t2 ASC"); while ($rs = mysqli_fetch_assoc($sql)) {
			$dates = explode('-', $rs["t10"]);
			if ($dates[2] == $cgun && $dates[1] == $cay && $dates[0] == $cyil) {
				$result[] = $rs;
			}
		}
	}elseif ($module === "service") {
		$sql = mysqli_query($conn, "SELECT * FROM t4 WHERE ne='hizmet' $luye"); while ($rs = mysqli_fetch_assoc($sql)) {
			$dates = explode('-', $rs["t4"]);
			if (($dates[2] == $cgun && $dates[1] == $cay && $dates[0] == $cyil) || ($dates[2] == $cgun && $rs["s3"] == 1)) {
				$rsc = mysqli_fetch_assoc(mysqli_query($conn, "SELECT id,t1 FROM t5 WHERE id = '{$rs["s1"]}' LIMIT 1"));
				$rs["uname"] = $rsc ? $rsc["t1"] : null;
				$result[] = $rs;
			}
		}
	}elseif ($module === "bids") {
		$sql = mysqli_query($conn, "SELECT * FROM t4 WHERE ne='service' $luye ORDER BY t2 ASC"); while ($rs = mysqli_fetch_assoc($sql)) {
			$dates = explode('-', $rs["t10"]);
			if ($dates[2] == $cgun && $dates[1] == $cay && $dates[0] == $cyil) {
				$rsc = mysqli_fetch_assoc(mysqli_query($conn, "SELECT id,t1 FROM t5 WHERE id = '{$rs["s1"]}' LIMIT 1"));
				$rs["uname"] = $rsc ? $rsc["t1"] : null;
				$result[] = $rs;
			}
		}
	}elseif ($module === "reqs") {
		$sql = mysqli_query($conn, "SELECT * FROM t2 WHERE ne='request'"); while ($rs = mysqli_fetch_assoc($sql)) {
			$dates = explode('-', date('Y-m-d', strtotime($rs["ts2"])));
			if ($dates[2] == $cgun && $dates[1] == $cay && $dates[0] == $cyil) {
				$rsc = mysqli_fetch_assoc(mysqli_query($conn, "SELECT id,t1 FROM t5 WHERE id = '{$rs["s3"]}' LIMIT 1"));
				$rs["uname"] = $rsc ? $rsc["t1"] : null;
				$result[] = $rs;
			}
		}
	}elseif ($module === "relations") {
		$sql = mysqli_query($conn, "SELECT * FROM t1 WHERE ne='relations' $luye ORDER BY t2 ASC");
		while ($rs = mysqli_fetch_assoc($sql)) {
			$dates = explode('-', $rs["t4"]);
			if ($dates[2] == $cgun && $dates[1] == $cay && $dates[0] == $cyil) {
				$rsc = mysqli_fetch_assoc(mysqli_query($conn, "SELECT id,t1 FROM t5 WHERE id = '{$rs["s1"]}' LIMIT 1"));
				$rs["uname"] = $rsc ? $rsc["t1"] : null;
				$result[] = $rs;
			}
		}
	}
	return $result;
}
?>