<?php
    // session_start();
    include "../db.php";
    require '../auth.php';
    restrictAccess(['Employee']);
    date_default_timezone_set('Asia/Kolkata');
    $currentTime = date('M j, Y g:i a');

    if ( !isset($_SESSION['uid']) && !isset($_SESSION['uref']) && !isset($_SESSION['utype']) ) {
        header("Location: logout.php");
        exit();
    }

    $userDetails_sql = mysqli_query($con, "SELECT * FROM users WHERE uid = '$_SESSION[uid]' ");
    if($userDetails_row = mysqli_fetch_assoc($userDetails_sql)){
        $userName = $userDetails_row["uname"];
        $userState_id = $userDetails_row["state"];
        $userDist_id = $userDetails_row["dist"];
        $admin_ref = $userDetails_row["admin_ref"];
    }
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <?php include "include/meta.php"; ?>
    <?php include "include/css.php"; ?>
    <?php include "include/php-functions.php"; ?>
    <link href="https://owlcarousel2.github.io/OwlCarousel2/assets/owlcarousel/assets/owl.carousel.min.css"
        rel="stylesheet">
    <link href="https://owlcarousel2.github.io/OwlCarousel2/assets/owlcarousel/assets/owl.theme.default.min.css"
        rel="stylesheet">
</head>
<style>
.event-card {
    background-color: #fff;
    padding: 15px;
    border-radius: 11px;
    box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px;
    width: 100%;
}

.event-info {
    margin-bottom: 10px;
}

.event-date {
    font-size: 16px;
    color: #434f7a;
    margin-bottom: 20px;
}

.event-title {
    font-size: 24px;
    margin-bottom: 10px;
    max-width: 322px;
    width: 100%;
    line-height: 1;
    color: #132152;
    font-weight: 600;
}

.event-location {
    font-size: 18px;
    color: #132152;
    margin-bottom: 30px;
    font-weight: 600;
}

.event-info img {
    object-fit: cover;
}

.event-attendees p {
    font-weight: 400;
    color: #434f7a;
}

.attend-button {
    display: block;
    background-color: #132152;
    color: #fff;
    border: none;
    padding: 12px 24px;
    border-radius: 8px;
    cursor: pointer;
    font-family: "Poppins", sans-serif;
    font-weight: 400;
    transition: background-color 0.3sease, transform 0.2sease;
}

.attend-button:hover {
    background-color: #ef852d;
    transform: scale(1.05);
    color: #fff;
}

.location-container {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.separator {
    color: #ccc;
    font-weight: bold;
}


@media (max-width: 570px) {
    .attend-button {
        width: 100%;
        max-width: unset;
    }
}
</style>

<body>
    <!--  Header -->
    <?php 
    include "include/header.php"; 
    ?>
    <!-- #header -->

    <section id="inner-body">
        <div class="body-content">
            <div class="container-fluid">
                <?php
                if(isset($_GET["college"])){
                    $college_id = $_GET["college"];
                    ?>
                <div class="row mt-3">
                    <div class="col-md-9 mb-2 mb-lg-0">
                        <?php
                            $details_sql = mysqli_query($con, "SELECT * FROM users WHERE uid = '$college_id' ");
                            if($details_row = mysqli_fetch_assoc($details_sql)){
                                ?>
                        <div class="card rounded p-2">
                            <div class="row align-items-center">
                                <?php
                                $wallpaper_sql = mysqli_query($con, "SELECT * FROM data WHERE data_catagory = 'wallpaper' and data_recycle = '1' AND data_up_for = '$college_id' ");
                                if(mysqli_num_rows($wallpaper_sql) > 0){
                                ?>
                                <div class="col-md-12 mb-2 position-relative">
                                    <div class="owl-carousel owl-theme owl-loaded owl-drag">
                                        <div class="owl-stage-outer">
                                            <div class="owl-stage"
                                                style="transform: translate3d(-1527px, 0px, 0px); transition: all 0.25s ease 0s; width: 3334px;">
                                                <?php
                                                while($wallpaper_row = mysqli_fetch_assoc($wallpaper_sql)){
                                                ?>
                                                <div class="owl-item cloned"
                                                    style="width: 128.906px; margin-right: 10px;">
                                                    <div class="item">
                                                        <img src="../upload/wallpaper/<?php echo $wallpaper_row["data_img"]; ?>"
                                                            class="img-fluid" alt="">
                                                    </div>
                                                </div>
                                                <?php } ?>
                                            </div>
                                        </div>
                                        <div class="owl-nav disabled">
                                        </div>
                                    </div>
                                </div>
                                <?php } ?>
                                <div class="col-md-9 mb-2 mb-lg-0">
                                    <h4><?php echo $details_row["uname"];?></h4>
                                </div>
                                <div class="col-md-3 mb-2">
                                    <img src="../upload/logo/<?php echo $details_row["ulogo"];?>" class="img-fluid"
                                        alt="">
                                </div>
                                <div class="col-md-12 mb-2">
                                    <h5 style="background: #40599b;padding: 5px;border-radius: 8px;color: #fff;">Contact
                                        Details:</h5>
                                    <div class="row">
                                        <div class="col-md-6">
                                            <ul style="list-style:none;">
                                                <?php if (!empty($details_row["ucontact"])): ?>
                                                <li>
                                                    <a
                                                        href="tel:<?php echo htmlspecialchars($details_row["ucontact"]); ?>">
                                                        <i class="fa fa-phone"></i>
                                                        <?php echo htmlspecialchars($details_row["ucontact"]); ?>
                                                    </a>
                                                </li>
                                                <?php endif; ?>
                                                <?php if (!empty($details_row["uwhatsapp"])): ?>
                                                <li>
                                                    <a href="https://wa.me/<?php echo htmlspecialchars($details_row["uwhatsapp"]); ?>"
                                                        target="_blank">
                                                        <i class="fab fa-whatsapp"></i>
                                                        <?php echo htmlspecialchars($details_row["uwhatsapp"]); ?>
                                                    </a>
                                                </li>
                                                <?php endif; ?>
                                                <?php if (!empty($details_row["uemail"])): ?>
                                                <li>
                                                    <a
                                                        href="mailto:<?php echo htmlspecialchars($details_row["uemail"]); ?>">
                                                        <i class="fa fa-envelope"></i>
                                                        <?php echo htmlspecialchars($details_row["uemail"]); ?>
                                                    </a>
                                                </li>
                                                <?php endif; ?>
                                                <?php if(!empty($details_row["saddress"])){?>
                                                <li>Address: <?php echo $details_row["saddress"];?></li>
                                                <?php } if(!empty($details_row["pin"])){?>
                                                <li>Pin Code: <?php echo $details_row["pin"];?></li>
                                                <?php } if(!empty($details_row["ucountry"])){
                                                    $country_sql = mysqli_query($con, "SELECT * FROM countries WHERE id = '$details_row[ucountry]'");
                                                    if($country_row = mysqli_fetch_assoc($country_sql)){
                                                        $country_name = $country_row["name"];
                                                    }
                                                    ?>
                                                <li>Country: <?php echo $country_name;?></li>
                                                <?php } if(!empty($details_row["state"])){
                                                    $state_sql = mysqli_query($con, "SELECT * FROM state WHERE id = '$details_row[state]'");
                                                    if($state_row = mysqli_fetch_assoc($state_sql)){
                                                        $state_name = $state_row["name"];
                                                    }
                                                    ?>
                                                <li>State: <?php echo $state_name;?></li>
                                                <?php } if(!empty($details_row["dist"])){
                                                    $dist_sql = mysqli_query($con, "SELECT * FROM dist WHERE id = '$details_row[dist]'");
                                                    if($dist_row = mysqli_fetch_assoc($dist_sql)){
                                                        $dist_name = $dist_row["distName"];
                                                    }
                                                    ?>
                                                <li>District: <?php echo $dist_name;?></li>
                                                <?php } ?>
                                            </ul>
                                        </div>

                                    </div>
                                </div>
                                <?php if(!empty($details_row["uabout"])){?>
                                <hr>
                                <div class="col-md-12 mb-2">
                                    <h5 style="background: #40599b;padding: 5px;border-radius: 8px;color: #fff;">About
                                        College:</h5>
                                    <div id="ck-img-preview">
                                        <?php echo $details_row["uabout"];?>
                                    </div>
                                </div>
                                <?php } if(!empty($details_row["uOabout"])){?>
                                <hr>
                                <div class="col-md-12 mb-2">
                                    <h5 style="background: #40599b;padding: 5px;border-radius: 8px;color: #fff;">Other
                                        Details About College:</h5>
                                    <div id="ck-img-preview">
                                        <?php echo $details_row["uOabout"];?>
                                    </div>
                                </div>
                                <?php }
                                
                                $facility_sql = mysqli_query($con, "SELECT * FROM data WHERE data_catagory = 'Facility' and data_recycle = '1' AND data_up_for = '$college_id' ");
                                if($facility_row = mysqli_fetch_assoc($facility_sql)){
                                ?>
                                <hr>
                                <div class="col-md-12 mb-2">
                                    <h5 style="background: #40599b;padding: 5px;border-radius: 8px;color: #fff;">College
                                        Facilities:</h5>
                                    <div id="ck-img-preview">
                                        <?php echo $facility_row["data_long_des"];?>
                                    </div>
                                </div>
                                <?php }
                                
                                $dept_sql = mysqli_query($con, "SELECT * FROM data WHERE data_catagory = 'Department' and data_recycle = '1' AND data_up_for = '$college_id' ");
                                if(mysqli_num_rows($dept_sql) > 0){
                                ?>
                                <hr>
                                <div class="col-md-12 mb-2">
                                    <h5 style="background: #40599b;padding: 5px;border-radius: 8px;color: #fff;">College
                                        Departments:</h5>
                                    <div class="row">
                                        <?php
                                        while($dept_row = mysqli_fetch_assoc($dept_sql)){
                                            $dep_sql = mysqli_query($con, "SELECT * FROM clg_course_cat WHERE sl_no = '$dept_row[data_short_des]' ");
                                            if($dep_row = mysqli_fetch_assoc($dep_sql)){
                                        ?>
                                        <div class="col-3">
                                            <div class="card text-center p-2">
                                                <h6 class="m-0"><?php echo $dep_row["cat_name"];?></h6>
                                            </div>
                                        </div>
                                        <?php } }?>
                                    </div>
                                </div>
                                <?php }
                                $course_sql = mysqli_query($con, "SELECT * FROM data WHERE data_catagory = 'Course' and data_recycle = '1' AND data_up_for = '$college_id' ");
                                if(mysqli_num_rows($course_sql) > 0){
                                ?>
                                <hr>
                                <div class="col-md-12 mb-2">
                                    <h5 style="background: #40599b;padding: 5px;border-radius: 8px;color: #fff;">College
                                        Cources:</h5>
                                    <div class="row">
                                        <?php
                                        while($course_row = mysqli_fetch_assoc($course_sql)){
                                            $cou_sql = mysqli_query($con, "SELECT * FROM clg_course WHERE sl_no = '$course_row[data_title]' ");
                                            if($cou_row = mysqli_fetch_assoc($cou_sql)){
                                        ?>
                                        <div class="col-3">
                                            <div class="card text-center p-2">
                                                <h6 class="m-0"><?php echo $cou_row["course_name"];?></h6>
                                            </div>
                                        </div>
                                        <?php } }?>
                                    </div>
                                </div>
                                <?php }
                                $admission_sql = mysqli_query($con, "SELECT * FROM data WHERE data_catagory = 'Admission' and data_recycle = '1' AND data_up_for = '$college_id' ");
                                if($admission_row = mysqli_fetch_assoc($admission_sql)){
                                ?>
                                <hr>
                                <div class="col-md-12 mb-2">
                                    <h5 style="background: #40599b;padding: 5px;border-radius: 8px;color: #fff;">College
                                        Admission:</h5>
                                    <div id="ck-img-preview">
                                        <?php echo $admission_row["data_long_des"];?>
                                    </div>
                                </div>
                                <?php }
                                $entrance_sql = mysqli_query($con, "SELECT * FROM data WHERE data_catagory = 'cEntrance' and data_recycle = '1' AND data_up_for = '$college_id' ");
                                if($entrance_row = mysqli_fetch_assoc($entrance_sql)){
                                ?>
                                <hr>
                                <div class="col-md-12 mb-2">
                                    <h5 style="background: #40599b;padding: 5px;border-radius: 8px;color: #fff;">College
                                        Entrance Details:</h5>
                                    <div id="ck-img-preview">
                                        <?php echo $entrance_row["data_long_des"];?>
                                    </div>
                                    <?php if (!empty($entrance_row["data_seo_title"])): 
                                        $entrance_list = explode(',', $entrance_row["data_seo_title"]); ?>

                                    <h6>Entrance Exam List:</h6>
                                    <div class="row align-items-center">
                                        <?php 
                                            foreach ($entrance_list as $sl_no): // Loop through each entrance exam
                                                $sl_no = trim($sl_no); // Trim spaces to avoid errors
                                                $e_sql = mysqli_query($con, "SELECT * FROM data WHERE data_catagory = 'entrance' AND data_sl_no = '".mysqli_real_escape_string($con, $sl_no)."' ");
                                                if ($e_row = mysqli_fetch_assoc($e_sql)): ?>
                                        <div class="col-3 mb-2">
                                            <div class="card rounded p-2 text-center">
                                                <?php echo htmlspecialchars($e_row["data_title"]); ?>
                                            </div>
                                        </div>
                                        <?php endif; 
                                            endforeach; ?>
                                    </div>

                                    <?php endif; ?>

                                </div>
                                <?php }
                                $placement_sql = mysqli_query($con, "SELECT * FROM data WHERE data_catagory = 'Placement' and data_recycle = '1' AND data_up_for = '$college_id' ");
                                if($placement_row = mysqli_fetch_assoc($placement_sql)){
                                ?>
                                <hr>
                                <div class="col-md-12 mb-2">
                                    <h5 style="background: #40599b;padding: 5px;border-radius: 8px;color: #fff;">College
                                        Placement Details:</h5>
                                    <div id="ck-img-preview">
                                        <?php echo $placement_row["data_long_des"];?>
                                    </div>
                                </div>
                                <div class="col-md-12 mb-2">
                                    <h5>College Placement Companies:</h5>
                                    <div class="row">
                                        <?php 
                                        $cplace_sql = mysqli_query($con, "SELECT * FROM data WHERE data_catagory = 'placementCompany' and data_recycle = '1' AND data_up_for = '$college_id' ");
                                        while($cplace_row = mysqli_fetch_assoc($cplace_sql)){
                                            ?>
                                        <div class="col-3 mb-2">
                                            <div class="card rounded p-2 overflow-hidden">
                                                <img src="../upload/company/<?php echo $cplace_row["data_img"];?>"
                                                    style="height:150px; width:auto;"
                                                    alt="<?php echo $cplace_row["data_title"];?>">
                                                <h6 class="text-center mt-2 mb-1">
                                                    <?php echo $cplace_row["data_title"];?></h6>
                                            </div>
                                        </div>
                                        <?php } ?>
                                    </div>
                                </div>
                                <?php }   
                                $dpc_sql = mysqli_query($con, "SELECT * FROM data WHERE data_catagory = 'gallery' AND data_up_for = '$college_id' ");

                                $images = [];
                                $videos = [];

                                while ($dpc_row = mysqli_fetch_assoc($dpc_sql)) {
                                    if ($dpc_row["data_subCategory"] == "Image") {
                                        $images[] = $dpc_row;
                                    } elseif ($dpc_row["data_subCategory"] == "Video") {
                                        // Convert YouTube short URL to an embeddable format
                                        $videoUrl = $dpc_row["data_img"];
                                        $videoID = "";

                                        if (strpos($videoUrl, "youtu.be") !== false) {
                                            $videoID = explode("/", parse_url($videoUrl, PHP_URL_PATH))[1];
                                        } elseif (strpos($videoUrl, "youtube.com/watch") !== false) {
                                            parse_str(parse_url($videoUrl, PHP_URL_QUERY), $queryParams);
                                            $videoID = $queryParams["v"] ?? "";
                                        }

                                        if (!empty($videoID)) {
                                            $dpc_row["videoID"] = $videoID;
                                            $videos[] = $dpc_row;
                                        }
                                    }
                                }
                                ?>

                                <?php if (!empty($images)) { ?>
                                <hr>
                                <div class="col-md-12 mb-2">
                                    <h5 style="background: #40599b;padding: 5px;border-radius: 8px;color: #fff;">College
                                        Gallery - Images:</h5>
                                    <div class="row">
                                        <?php foreach ($images as $img) { ?>
                                        <div class="col-md-3 mb-2">
                                            <img src="../upload/gallery/<?php echo htmlspecialchars($img["data_img"]); ?>"
                                                class="img-fluid" style="height:100px;"
                                                alt="<?php echo htmlspecialchars($img['data_title']); ?>">
                                        </div>
                                        <?php } ?>
                                    </div>
                                </div>
                                <?php } ?>

                                <?php if (!empty($videos)) { ?>
                                <hr>
                                <div class="col-md-12 mb-2">
                                    <h5 style="background: #40599b;padding: 5px;border-radius: 8px;color: #fff;">College
                                        Gallery - Videos:</h5>
                                    <div class="row">
                                        <?php foreach ($videos as $vid) { ?>
                                        <div class="col-md-4 mb-2">
                                            <iframe width="100%" height="200"
                                                src="https://www.youtube.com/embed/<?php echo htmlspecialchars($vid["videoID"]); ?>"
                                                frameborder="0" allowfullscreen>
                                            </iframe>
                                        </div>
                                        <?php } ?>
                                    </div>
                                </div>
                                <?php } 
                                $faq_sql = mysqli_query($con, "SELECT * FROM data WHERE data_catagory = 'FAQ' AND data_up_for = '$college_id' ");
                                if(mysqli_num_rows($faq_sql)>0){
                                    ?>
                                <hr>
                                <div class="col-md-12 mb-2">
                                    <h5 style="background: #40599b;padding: 5px;border-radius: 8px;color: #fff;">FAQ's:
                                    </h5>
                                    <div class="accordion" id="faqAccordion">
                                        <?php
                                        $index = 1; // Unique ID for each accordion item
                                        while ($faq_row = mysqli_fetch_assoc($faq_sql)) {
                                            ?>
                                        <div class="accordion-item">
                                            <h2 class="accordion-header" id="heading<?php echo $index; ?>">
                                                <button class="accordion-button collapsed" type="button"
                                                    data-bs-toggle="collapse"
                                                    data-bs-target="#collapse<?php echo $index; ?>"
                                                    aria-expanded="false" aria-controls="collapse<?php echo $index; ?>">
                                                    Q<?php echo $index;?>.
                                                    <?php echo htmlspecialchars($faq_row["data_short_des"]); ?>
                                                </button>
                                            </h2>
                                            <div id="collapse<?php echo $index; ?>" class="accordion-collapse collapse"
                                                aria-labelledby="heading<?php echo $index; ?>"
                                                data-bs-parent="#faqAccordion">
                                                <div class="accordion-body">
                                                    <strong>Ans:</strong>
                                                    <?php echo nl2br(htmlspecialchars($faq_row["data_long_des"])); ?>
                                                </div>
                                            </div>
                                        </div>
                                        <?php $index++; }?>
                                    </div>
                                </div>
                                <?php } 
                                $tag_sql = mysqli_query($con, "SELECT * FROM data WHERE data_catagory = 'Tag' AND data_up_for = '$college_id' ");
                                if(mysqli_num_rows($tag_sql)>0){
                                    ?>
                                <hr>
                                <div class="col-md-12 mb-2">
                                    <h5 style="background: #40599b;padding: 5px;border-radius: 8px;color: #fff;">Tags:
                                    </h5>
                                    <div class="row align-items-center">
                                        <?php
                                        while ($tag_row = mysqli_fetch_assoc($tag_sql)) {
                                            ?>
                                        <div class="col-3 mb-2">
                                            <div class="card rounded text-center p-2">
                                                <a href="<?php echo $tag_row["data_long_des"];?>"
                                                    class="text-decoration-none"><i class="fa fa-tag"
                                                        aria-hidden="true"></i>&nbsp;
                                                    <?php echo $tag_row["data_short_des"];?></a>
                                            </div>
                                        </div>
                                        <?php }?>
                                    </div>
                                </div>
                                <?php }?>

                            </div>
                        </div>
                        <?php } ?>
                    </div>
                    <div class="col-md-3 mb-2 mb-lg-0">
                        <div class="card">
                            <div class="card-header">
                                <h5 class="m-0 text-center" style="color:#40599b;">Other Colleges</h5>
                            </div>
                            <div class="card-body">
                                <?php
                                    $clg_sql = mysqli_query($con, "SELECT * FROM users WHERE utype = 'College' AND udisplay != 'disable' AND uStatus = '1' ");
                                    while($clg_row = mysqli_fetch_assoc($clg_sql)){
                                        if($clg_row["uid"] != $college_id){
                                        ?>
                                <div class="card rounded overflow-hidden">
                                    <div class="row align-items-center">
                                        <div class="col-4">
                                            <div class="logo" style="background: #ddd;padding: 10px;">
                                                <img src="../upload/logo/<?php echo $clg_row['ulogo'];?>"
                                                    class="img-fluid" alt="<?php echo $clg_row["uname"];?>">
                                            </div>
                                        </div>
                                        <div class="col-8">
                                            <a href="view-colleges.php?college=<?php echo $clg_row["uid"];?>"
                                                class="text-decoration-none text-dark">
                                                <h6 class="m-0"><?php echo $clg_row["uname"];?></h6>
                                            </a>
                                        </div>
                                    </div>
                                </div>
                                <?php } }?>
                            </div>
                        </div>
                    </div>
                </div>
                <?php } else{ ?>
                <div class="card mt-3">
                    <div class="card-header">
                        <div class="row">
                            <div class="col-md-4 mb-2 mb-lg-0">
                                <h5 class="m-0">All College List</h5>
                            </div>
                            <div class="col-md-8 mb-2 mb-lg-0 text-end">
                                <form action="view-colleges.php" method="GET" onsubmit="return redirectToView();">
                                    <div class="d-flex">
                                        <div class="form-group w-100">
                                            <input type="hidden" name="country" id="country-id">
                                            <input type="text" list="country-list" class="form-control form-control-sm"
                                                id="country-input" placeholder="Select Country from the list">
                                            <datalist id="country-list">
                                                <?php 
                                                    $sql = "SELECT id, name FROM countries ORDER BY name ASC";
                                                    $res = mysqli_query($con, $sql);
                                                    while ($row = mysqli_fetch_assoc($res)) {
                                                ?>
                                                <option data-id="<?php echo $row['id']; ?>"
                                                    value="<?php echo $row['name']; ?>"></option>
                                                <?php } ?>
                                            </datalist>
                                        </div>
                                        <div class="form-group w-100">
                                            <input type="hidden" name="state" id="state-id">
                                            <input type="text" list="state-list" class="form-control form-control-sm"
                                                id="state-input" placeholder="Select State from the list">
                                            <datalist id="state-list"></datalist>
                                        </div>

                                        <div class="form-group w-100">
                                            <input type="hidden" name="dist" id="district-id">
                                            <input type="text" list="district-list" class="form-control form-control-sm"
                                                id="district-input" placeholder="Select District from the list">
                                            <datalist id="district-list"></datalist>
                                        </div>
                                        <div class="form-group w-100">
                                            <select name="dept" class="form-control form-control-sm" id="dept">
                                                <option value="">Select Department From List</option>
                                                <?php 
                                                    $dept_sql = mysqli_query($con, "SELECT * FROM clg_course_cat WHERE recycle = '1' ORDER BY sl_no DESC");
                                                    while ($dept_row = mysqli_fetch_assoc($dept_sql)) {
                                                ?>
                                                <option value="<?php echo $dept_row["sl_no"]; ?>"
                                                    data-cat-id="<?php echo $dept_row["cat_id"]; ?>">
                                                    <?php echo $dept_row["cat_name"]; ?>
                                                </option>
                                                <?php } ?>
                                            </select>
                                        </div>

                                        <div class="form-group w-100">
                                            <select name="course" class="form-control form-control-sm" id="course">
                                                <option value="">Select Course From List</option>
                                            </select>
                                        </div>
                                        <div class="form-group w-100">
                                            <input type="text" list="Second" class="form-control form-control-sm"
                                                id="SecondFilterValue" name="search" placeholder="Select College">
                                            <datalist id="Second">
                                                <?php 
                                                    $esql = "SELECT DISTINCT uname FROM users WHERE utype = 'College' AND udisplay !='disable' AND uStatus = '1' ORDER BY uname ASC";
                                                    $eres = mysqli_query($con, $esql);
                                                    while ($erow = mysqli_fetch_assoc($eres)) {
                                                ?>
                                                <option value="<?php echo $erow['uname']; ?>">
                                                    <?php echo $erow['uname']; ?></option>
                                                <?php } ?>
                                            </datalist>
                                        </div>
                                        <button type="submit" class="btn btn-primary mb-2 btn-sm">
                                            <i class="fa fa-search"></i>
                                        </button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                    <div class="card-body">
                        <div class="row justify-content-center align-items-center">
                            <?php
                            $andWhere = "utype = 'College' AND udisplay != 'disable' AND uStatus = '1' ";
                            $AndWherec = ""; // Initialize additional condition variable
                            
                            if (isset($_GET["search"]) && !empty($_GET["search"])) {
                                $clg_name = mysqli_real_escape_string($con, $_GET["search"]);
                                $andWhere .= " AND uname = '$clg_name' ";
                            }
                            
                            if (isset($_GET["country"]) && !empty($_GET["country"])) {
                                $country = mysqli_real_escape_string($con, $_GET["country"]);
                                $andWhere .= " AND ucountry = '$country' ";
                            }
                            
                            if (isset($_GET["state"]) && !empty($_GET["state"])) {
                                $state = mysqli_real_escape_string($con, $_GET["state"]);
                                $andWhere .= " AND state = '$state' ";
                            }
                            
                            if (isset($_GET["dist"]) && !empty($_GET["dist"])) {
                                $dist = mysqli_real_escape_string($con, $_GET["dist"]);
                                $andWhere .= " AND dist = '$dist' ";
                            }
                            
                            if (!empty($_GET['course'])) {
                                $course_up_for = [];
                                $course_sql = mysqli_query($con, "SELECT data_up_for FROM data WHERE data_catagory = 'Course' AND data_title = '".mysqli_real_escape_string($con, $_GET['course'])."'");
                                
                                while ($course_row = mysqli_fetch_assoc($course_sql)) {
                                    if (!empty($course_row['data_up_for'])) {
                                        $course_up_for[] = $course_row['data_up_for'];
                                    }
                                }
                            
                                if (!empty($course_up_for)) {
                                    $course_up_for_str = implode(',', $course_up_for);
                                    $AndWherec .= " AND FIND_IN_SET(uid, '$course_up_for_str')";
                                } else {
                                    $AndWherec .= " AND 1=0"; // No valid courses found
                                }
                            }
                            
                            if (!empty($_GET['dept'])) {
                                $dept_up_for = [];
                                $dept_sql = mysqli_query($con, "SELECT data_up_for FROM data WHERE data_catagory = 'Department' AND data_short_des = '".mysqli_real_escape_string($con, $_GET['dept'])."'");
                                
                                while ($dept_row = mysqli_fetch_assoc($dept_sql)) {
                                    if (!empty($dept_row['data_up_for'])) {
                                        $dept_up_for[] = $dept_row['data_up_for'];
                                    }
                                }
                            
                                if (!empty($dept_up_for)) {
                                    $dept_up_for_str = implode(',', $dept_up_for);
                                    $AndWherec .= " AND FIND_IN_SET(uid, '$dept_up_for_str')";
                                } else {
                                    $AndWherec .= " AND 1=0"; // No valid departments found
                                }
                            }
                            
                            $clg_sql = mysqli_query($con, "SELECT * FROM users WHERE $andWhere $AndWherec");
                            
                            while($clg_row = mysqli_fetch_assoc($clg_sql)){
                                ?>
                            <div class="col-md-3 mb-2">
                                <div class="event-card">
                                    <div class="event-info">
                                        <div class="row align-items-center">
                                            <div class="col-8">
                                                <h3 class="event-title"><?php echo $clg_row["uname"];?></h3>
                                            </div>
                                            <div class="col-4">
                                                <?php 
                                                $logo_path = "../upload/logo/" . $clg_row['ulogo'];

                                                if (!empty($clg_row['ulogo']) && file_exists($logo_path)) { ?>
                                                <img src="<?php echo $logo_path; ?>" class="img-fluid"
                                                    alt="<?php echo $clg_row['uname']; ?>">
                                                <?php } else { ?>
                                                <img src="../assets/image/clg-demo.jpg" class="img-fluid"
                                                    alt="<?php echo $clg_row['uname']; ?>">
                                                <?php } ?>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="event-attendees">
                                        <p class="event-date m-0">
                                            <i class="fa-solid fa-map"></i> <?php echo $clg_row["saddress"];?>
                                        </p>
                                        <div class="location-container event-date">
                                            <?php if (!empty($clg_row["ucountry"])) { 
                                                $country_sql = mysqli_query($con, "SELECT * FROM countries WHERE id = '$clg_row[ucountry]'");
                                                if ($country_row = mysqli_fetch_assoc($country_sql)) {
                                                    $country_name = $country_row["name"];
                                                } ?>
                                            <span><i class="fa fa-flag" aria-hidden="true"></i>
                                                <?php echo $country_name; ?></span>
                                            <?php } ?>

                                            <?php if (!empty($clg_row["state"])) { 
                                                $state_sql = mysqli_query($con, "SELECT * FROM state WHERE id = '$clg_row[state]'");
                                                if ($state_row = mysqli_fetch_assoc($state_sql)) {
                                                    $state_name = $state_row["name"];
                                                } ?>
                                            <span class="separator">|</span>
                                            <span><i class="fa-solid fa-location-dot"></i>
                                                <?php echo $state_name; ?></span>
                                            <?php } ?>

                                            <?php if (!empty($clg_row["dist"])) { 
                                                $dist_sql = mysqli_query($con, "SELECT * FROM dist WHERE id = '$clg_row[dist]'");
                                                if ($dist_row = mysqli_fetch_assoc($dist_sql)) {
                                                    $dist_name = $dist_row["distName"];
                                                } ?>
                                            <span class="separator">|</span>
                                            <span><i class="fa fa-city"></i> <?php echo $dist_name; ?></span>
                                            <?php } ?>
                                        </div>
                                    </div>
                                    <a href="view-colleges.php?college=<?php echo $clg_row["uid"];?>"
                                        class="text-decoration-none attend-button btn">
                                        View Details
                                    </a>
                                </div>
                            </div>
                            <?php } ?>
                        </div>
                    </div>
                </div>
                <?php }?>
            </div>
        </div>

        <?php include "include/footer.php"; ?>
    </section>

    <!-- JavaScript Libraries -->

    <?php include "include/js.php"; ?>
    <script>
    function redirectToView() {
        var selectedCollege = document.getElementById("SecondFilterValue").value.trim();
        var country = document.getElementById("country-id").value.trim();
        var state = document.getElementById("state-id").value.trim();
        var district = document.getElementById("district-id").value.trim();
        var dept = document.getElementById("dept").value.trim();
        var course = document.getElementById("course").value.trim();

        var params = new URLSearchParams();

        if (country !== "") {
            params.append("country", country);
        }
        if (state !== "") {
            params.append("state", state);
        }
        if (district !== "") {
            params.append("dist", district);
        }
        if (dept !== "") {
            params.append("dept", dept);
        }
        if (course !== "") {
            params.append("course", course);
        }
        if (selectedCollege !== "") {
            params.append("search", selectedCollege);
        }

        if (params.toString() !== "") {
            window.location.href = "view-colleges.php?" + params.toString();
            return false; // Prevent default form submission
        }

        return false;
    }

    // Assign country ID from datalist
    document.getElementById("country-input").addEventListener("input", function() {
        var selectedOption = document.querySelector("#country-list option[value='" + this.value + "']");
        document.getElementById("country-id").value = selectedOption ? selectedOption.getAttribute("data-id") :
            "";
    });

    // Fetch states based on country selection
    document.getElementById("country-input").addEventListener("change", function() {
        var countryId = document.getElementById("country-id").value;
        if (countryId !== "") {
            fetch("get_states.php?country=" + countryId)
                .then(response => response.text())
                .then(data => {
                    document.getElementById("state-list").innerHTML = data;
                    document.getElementById("state-id").value = "";
                    document.getElementById("state-input").value = "";
                });
        }
    });

    // Assign state ID from datalist
    document.getElementById("state-input").addEventListener("input", function() {
        var selectedOption = document.querySelector("#state-list option[value='" + this.value + "']");
        document.getElementById("state-id").value = selectedOption ? selectedOption.getAttribute("data-id") :
            "";
    });

    // Fetch districts based on state selection
    document.getElementById("state-input").addEventListener("change", function() {
        var stateId = document.getElementById("state-id").value;
        if (stateId !== "") {
            fetch("get_districts.php?state=" + stateId)
                .then(response => response.text())
                .then(data => {
                    document.getElementById("district-list").innerHTML = data;
                    document.getElementById("district-id").value = "";
                    document.getElementById("district-input").value = "";
                });
        }
    });

    // Assign district ID from datalist
    document.getElementById("district-input").addEventListener("input", function() {
        var selectedOption = document.querySelector("#district-list option[value='" + this.value + "']");
        document.getElementById("district-id").value = selectedOption ? selectedOption.getAttribute("data-id") :
            "";
    });

    // Fetch courses based on department selection
    document.getElementById("dept").addEventListener("change", function() {
        var deptId = this.value;
        if (deptId !== "") {
            fetch("get_courses.php?dept=" + deptId)
                .then(response => response.text())
                .then(data => {
                    document.getElementById("course").innerHTML = data;
                });
        }
    });
    </script>


    <script>
    $(document).ready(function() {
        // Country selection event
        $('#country-input').on('change', function() {
            var countryName = $(this).val();
            var countryOption = $('#country-list option').filter(function() {
                return this.value === countryName;
            });

            if (countryOption.length > 0) {
                var countryId = countryOption.data('id');
                $('#country-id').val(countryId);

                // Fetch states based on country ID
                $.ajax({
                    url: 'fetch-state.php',
                    type: 'POST',
                    data: {
                        country_id: countryId
                    },
                    success: function(data) {
                        $('#state-list').html(data);
                        $('#state-id').val('');
                        $('#state-input').val('');
                        $('#district-list').html('');
                        $('#district-id').val('');
                        $('#district-input').val('');
                    },
                    error: function() {
                        alert('Error loading states. Please try again.');
                    }
                });
            } else {
                $('#country-id').val('');
                $('#state-list').html('');
                $('#district-list').html('');
            }
        });

        // State selection event
        $('#state-input').on('change', function() {
            var stateName = $(this).val();
            var stateOption = $('#state-list option').filter(function() {
                return this.value === stateName;
            });

            if (stateOption.length > 0) {
                var stateId = stateOption.data('id');
                $('#state-id').val(stateId);

                // Fetch districts based on state ID
                $.ajax({
                    url: 'fetch-district.php',
                    type: 'POST',
                    data: {
                        state_id: stateId
                    },
                    success: function(data) {
                        $('#district-list').html(data);
                        $('#district-id').val('');
                        $('#district-input').val('');
                    },
                    error: function() {
                        alert('Error loading City. Please try again.');
                    }
                });
            } else {
                $('#state-id').val('');
                $('#district-list').html('');
            }
        });

        // District selection event
        $('#district-input').on('change', function() {
            var districtName = $(this).val();
            var districtOption = $('#district-list option').filter(function() {
                return this.value === districtName;
            });

            if (districtOption.length > 0) {
                $('#district-id').val(districtOption.data('id'));
            } else {
                $('#district-id').val('');
            }
        });

        $("#dept").change(function() {
            var sl_no = $(this).val();
            var cat_id = $("#dept option:selected").data("cat-id");

            if (sl_no !== "") {
                $.ajax({
                    url: "get_courses.php",
                    type: "POST",
                    data: {
                        sl_no: sl_no,
                        cat_id: cat_id
                    },
                    success: function(response) {
                        $("#course").html(response);
                    }
                });
            } else {
                $("#course").html('<option value="">Select From List</option>');
            }
        });
    });
    </script>

    <script src="https://owlcarousel2.github.io/OwlCarousel2/assets/vendors/jquery.min.js">
    </script>
    <script src="https://owlcarousel2.github.io/OwlCarousel2/assets/owlcarousel/owl.carousel.js">
    </script>
    <script>
    var owl = $('.owl-carousel');
    owl.owlCarousel({
        items: 1,
        // items change number for slider display on desktop

        loop: true,
        margin: 10,
        autoplay: true,
        autoplayTimeout: 3000,
        autoplayHoverPause: true
    });
    </script>

</body>

</html>