<?php if (!$session->is_logged_in()) {
    redirect_to("index.php");
}
?>
<?php require_once("includes/initialize.php");
$id = $session->user_id;
$user_data = Userdata::find_by_id($session->user_id);
$biodata = Biodata::find_by_id($user_data->formno, $user_data->email);
// $biodata = Biodata::find_by_id($session->user_id);
$session_first = SESSIONCLASS::session_authenticate_second();


//echo "Duup: ".$session_first->semester;
//echo $biodata->programme;
$dp = DEPARTMENTCLASS::department_authenticate($biodata->courses);
// $display_course = COURSECLASS::fetch_all_course($biodata->courses, $biodata->programme, $session_class->semester);

$dept = $biodata->courses;
$level = $biodata->programme;
//$semester =$session_class->semester;

$semester =$session_first->semester;

// $semester = 'Second';

$level = $level . " " . "I";
$display_course = COURSECLASS::fetch_all_course($biodata->courses, $level, $semester);//$session_class->semester);
$total_units = 0;
?>

<script src="../javascripts/jquery.min.js"></script>
<script src="../javascripts/bootstrap.min.js" type="text/javascript"></script>


<?php if (($level == "ND II") or ($level == "HND II") and (($tag == "first_sem") or ($tag == "second_sem"))){ ?>
    <div class="art-content-layout">
        <div class="art-content-layout-row">

            <div class="art-layout-cell layout-item-0" style="width: 100%">

                <marquee behavior="alternate" scrolldelay="200" onmouseover="this.setAttribute('scrollamount',0,0);"
                         onmouseout="this.setAttribute('scrollamount',6,0);">
                    <div style="color:#F00; font-style:italic; font-weight:bolder;">NOTE: <span
                                style="color:#F00; font-style:italic;">Select Department, level and semester to borrow or register re-run courses </span>
                    </div>
                </marquee>


            </div>
        </div>
    </div>
<div class="art-content-layout">
    <div class="art-content-layout-row" align="center">
        <div class="art-layout-cell layout-item-0" style="width: 100%; margin:0; padding:0;">


            <div class="container" id="#tab">

                <div id="disabled">
                <span id="department1">
                    <select name="department" id="department" class="action">
                        <option value="">Select Department</option>
                        <?php foreach ($alldp as $all_dp)

                            echo "<option value=" . $all_dp->department . ">" . trim(strtoupper($all_dp->department)) . "<option>";
                        ?>
                    </select>
                </span>
                    <span id="level1">
                    <select name="level" id="level" class="action">
                        <option value="">Select Level</option>
                        <option value="HND I">HND I</option>
                        <option value="ND I">ND I</option>
                    </select>
                </span>
                    <span id="semester1">
                    <select name="semester" id="semester" class="action">
                        <option value="">Select Semester</option>
                        <?php echo "<option value=" . $session_class->semester . ">" . $session_class->semester . "</option>"; ?>
                    </select>
                </span>
                </div>

            </div>
        </div>
    </div>

    <?php } ?>
    <br/>

    <div class="art-content-layout">
        <div class="art-content-layout-row">

            <div class="art-layout-cell layout-item-0" style="width: 100%">

                <marquee behavior="alternate" scrolldelay="200" onmouseover="this.setAttribute('scrollamount',0,0);"
                         onmouseout="this.setAttribute('scrollamount',6,0);">
                    <div style="color:#F00; font-style:italic; font-weight:bolder;">NOTE: <span
                                style="color:#F00; font-style:italic;">Register by clicking on your preferred courses</span>
                    </div>
                </marquee>


            </div>
        </div>
    </div>


    <div class="art-content-layout">
        <div class="art-content-layout-row">

            <div class="art-layout-cell layout-item-0" style="width: 100%">


                <div style="text-transform:uppercase; color:#099; font-weight:bolder; border-bottom:inset #000 thin;">
                    second semester courses
                </div>
                <!--<hr style="width:100%;">
                <br />-->
                <?php if ($message != "") {
                    echo output_message($message);
                } else {
                    echo "<br/>";
                }
                //$message =$transaction->responsecode;

                ?>

            </div>
        </div>
    </div>


    <?php
    if (($level == "ND I") or ($level == "HND I"))
    {
        // if ($session_class->semester != "active")
        // { echo "Semester Not Active"; }
        ?>


        <div class="art-content-layout">
            <div class="art-content-layout-row" align="center">
                <div class="art-layout-cell layout-item-0" style="width: 100%; margin:0; padding:0;">
                    <div>
                    </div>
                            
                    <!--Left column-->
                    <h3 style="color:#033;">Current Semester Courses</h3>
                    <form method="post" id="insert_data">
                        <table id="mytable2" border="2" align="center" class="">
                            <tr>
                                <th>Course Code</th>
                                <th>Course Title</th>
                                <th>Status</th>
                                <th>Unit</th>
                                <th>Select<input type="checkbox" name="selectAll" id="selectAll" class="check-tab2"/>
                                </th>
                            </tr>

                            <?php foreach ($display_course as $display_cours)
                                echo '<tr>'
                                    . '<td>' . $display_cours->course_code . '</td>'
                                    . '<td>' . $display_cours->course_title . '</td>'
                                    . '<td>' . $display_cours->course_status . '</td>'
                                    . '<td>' . $display_cours->course_unit . '</td>'
                                    . '<td><input type="checkbox" class="check-tab2" name="check-tab2" value = ' . $display_cours->course_code . '</td>'
                                    . '</tr>'; 
                                    
                                    // for ($count = 0; $count < $units; $count++)
                                    // {
                                    //     $units = $display_cours->course_unit;
                                    //     $units = $units + $count;
                                    // }
                                    
                                    ?>

                        </table>
                        <!--<h7> Total Semester Units = <?// echo $units ?></h7>-->
                        <h6>C = Compulsory Courses</h6>
                        <h6>E = Elective Course(s)</h6>
                        <h6>CE = CompulsoryElective Course(s)</h6>
                        <br/>
                        <input type="submit" name="register" id="register" class="btn btn-info" value="Register"
                               align="center"/>
                        <!--<form method="post" action="Report.php" id="print">-->
                        <!--  <input type="submit" name="print" id="print" class="btn btn-info" value="Print" align="center" />
                      <!--</form>-->

                        <!-- </form>
                        </div>
                    </div>-->
                </div>


            </div>


        </div>

        <?php
    }
    else if (($level == "ND II") or ($level == "HND II"))
    {
    ?>

    <!-- left column for ND II and HND II-->

    <div class="art-content-layout">
        <div class="art-content-layout-row">
            <div class="art-layout-cell layout-item-0" style="width: 45%; margin:0; padding:0;">
                <div>
                </div>

                <!-- Left column-->
                <h3 style="color:#033;">Current Semester Courses</h3>
                <form method="post" id="insert_data">
                    <table id="mytable2" border="2" align="center" class="">
                        <tr>
                            <th>Course Code</th>
                            <th>Course Title</th>
                            <th>Status</th>
                            <th>Unit</th>
                            <th>Select<input type="checkbox" name="selectAll" id="selectAll" class="check-tab2"/></th>
                        </tr>

                        <?php foreach ($display_course as $display_cours)
                            $units= $display_cours->course_unit;
                                $total_units= $count + $units;
                            echo '<tr>'
                                . '<td>' . $display_cours->course_code . '</td>'
                                . '<td>' . $display_cours->course_title . '</td>'
                                . '<td>' . $display_cours->course_status . '</td>'
                                . '<td>' . $display_cours->course_unit . '</td>'
                                . '<td><input type="checkbox" class="check-tab2" name="check-tab2" value = ' . $display_cours->course_code . '</td>'
                                . '</tr>'; ?>

                    </table>
                        <h6>C = Compulsory Courses</h6>
                        <h6>E = Elective Course(s)</h6>
                        <h6>CE = CompulsoryElective Course(s)</h6>
                        <br/>
                    <input type="submit" name="register" id="register" class="btn btn-info" value="Register"
                           align="center"/>
                    <!--<input type="checkbox" name="selectAll" id="selectAll" class="check-tab2" value="CheckAll" align="right"/>-->
                    <!--<form method="post" action="Report.php" id="print">-->
                    <!--<input type="submit" name="print" id="action" class="btn btn-info" value="Print" align="center" />
                <!--</form>-->

                    <!-- </form>
                    </div>
                </div>
            </div>


        </div>-->


            </div>
            <!-- The left column for ND II and HND II ends here-->


            <div class="art-layout-cell layout-item-0" style="width: 45%">

                <!--right column-->

                <h3 style="color:#033;">Available Semester Courses</h3>
                <table id="mytable" class="" align="left" border="2">
                    <thead>
                    <tr>
                        <th>Course Code</th>
                        <th>Course Title</th>
                        <th>Unit</th>
                        <th>Select</th>
                    </tr>
                    </thead>
                </table>

            </div>
        </div>
    </div>
</div>

<?php } ?>

</article></div>
<style>
    #accept:hover {
        text-decoration: none;
        font-style: italic;
    }
</style>

<script type="text/javascript">
    $(document).ready(function () {

        $('#semester').change(function () {
            if ($(this).val() != '') {
                var department = $('#department').val();
                var level = $('#level').val();
                var semester = $('#semester').val();
                alert(department + level + semester);
                {
                    $.ajax({
                        method: "POST",
                        url: "fetch_courses.php",
                        data: {department: department, level: level, semester: semester},
                        success: function (data) {
                            alert(data);
                            $('#mytable').html(data);
                        }
                    })
                }
            }
        });

        //Checkbox to check all courses
        $('#selectAll').on('click', function (event) {
            if (this.checked) {
                $('.check-tab2').each(function () {
                    this.checked = true;
                });
            } else {
                $('.check-tab2').each(function () {
                    this.checked = false;
                });
            }
        });

        $('#register').on('click', function (e) {
            e.preventDefault(e);
            var table2 = document.getElementById("mytable2"),
                checkboxes = document.getElementsByName("check-tab2"),
                course_code = "",
                count = 0;
            for (var i = 0; i < checkboxes.length; i++) {
                if (checkboxes[i].checked) {
                    course_code += table2.rows[i + 1].cells[0].innerHTML + ',';
                }
            }
            if (course_code == "") {
                alert("Please Select Courses");
            }
            else {
                $.ajax({
                    method: "GET",
                    url: "insert_courses.php",
                    data: {course_code: course_code},
                    success: function (data) {
                        // alert(data);
                        if (data == "Registration Successful")
                        {
                            alert(data);
                            window.open('Report.php');
                            $('#register').attr('disabled', 'disabled');
                        }else {
                            alert(data);
                        }
                    }

                })
            }
        });

    });


    function tab1_To_tab2() {
        var table1 = document.getElementById("mytable"),
            table2 = document.getElementById("mytable2"),
            checkboxes = document.getElementsByName("check-tab1");
        for (var i = 0; i < checkboxes.length; i++)
            if (checkboxes[i].checked) {
                // create new row and cells
                var newRow = table2.insertRow(table2.length),
                    cell1 = newRow.insertCell(0),
                    cell2 = newRow.insertCell(1),
                    cell3 = newRow.insertCell(2),
                    cell4 = newRow.insertCell(3);
                // add values to the cells
                cell1.innerHTML = table1.rows[i + 1].cells[0].innerHTML;
                cell2.innerHTML = table1.rows[i + 1].cells[1].innerHTML;
                cell3.innerHTML = table1.rows[i + 1].cells[2].innerHTML;
                cell4.innerHTML = "<input type='checkbox' name='check-tab2' value = 'cell1.innerHTML'>";
                console.log(cell1.innerHTML);
                // var index = table1.rows[i+1].rowIndex;
                // table1.deleteRow(index);                
                // i--;
            }
    }


    function tab2_To_tab1() {
        var table1 = document.getElementById("mytable"),
            table2 = document.getElementById("mytable2"),
            checkboxes = document.getElementsByName("check-tab2");
        for (var i = 0; i < checkboxes.length; i++)
            if (checkboxes[i].checked) {
                var index = table2.rows[i + 1].rowIndex;
                table2.deleteRow(index);

                i--;
            }
    }


</script>        
                
                
    
   
          
