<?php if (!$session->is_logged_in()) {
    redirect_to("index.php");
}
?>
<?php require_once("includes/initialize.php");
$user_data = Userdata::find_by_id($session->user_id);
$biodata = Biodata::find_by_id($user_data->formno, $user_data->email);
$session_class = SESSIONCLASS::session_authenticate();
$dp = DEPARTMENTCLASS::department_authenticate($biodata->courses);


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

$level = $level . " " . "II";
$display_course = COURSECLASS::fetch_all_course($biodata->courses, $level, $session_class->semester);

$count = 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/>
    <?php if ($level == "HND II" or $level == "ND II") {
    } else { ?>
        <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 your preferred courses here</span>
                        </div>
                    </marquee>


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

    <?php } ?>


    <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;">
                    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"))
    {
        ?>

        <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>Unit</th>
                                <th>Select</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_unit . '</td>'
                                    . '<td><input type="checkbox" name="check-tab2" value = ' . $display_cours->course_code . '</td>'
                                    . '</tr>'; ?>

                        </table><table>
                            <tr>
                                <th>
                                    <? $total_units ?>
                                </th>
                            </tr>
                        </table>
                        <br/>
                        <input type="button" name="selectAll" id="selectAll" class="btn btn-info" value="CheckAll"
                               align="right"/>
                        <!--<form method="post" action="Report.php" id="print">-->
                        <!--  <input type="submit" name="print" id="print" class="btn btn-info" value="Print" align="center" />
                      <!--</form>-->
                        <input type="submit" name="register" id="register" class="btn btn-info" value="Register"
                               align="center"/>
                        <!-- </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>


                <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>Unit</th>
                            <th>Select</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_unit . '</td>'
                                . '<td><input type="checkbox" name="check-tab2" value = ' . $display_cours->course_code . '</td>'
                                . '</tr>'; ?>

                    </table>
                    <br/>
                    <input type="button" name="selectAll" id="selectAll" class="btn btn-info" 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>-->
                    <input type="submit" name="register" id="register" class="btn btn-info" value="Register"
                           align="center"/>
                    <!-- </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);
                        }
                    })
                }
            }
        });


        //Onload
        $.ajax({
            url: 'ses_sem.php',
            success: function (data) {
                $('#mytable2').html(data);
            }
        });


        $("#print").on('click', function (e) {
            e.preventDefault(e);
            $.ajax({
                method: "GET",
                url: "Report.php",
                success: function (data) {
                    //alert(data);
                    //document.write("<a href='Report.php' rel='noopener noreferrer' target='_blank'>Report</a>");
                    window.open('Report.php');
                }
            })
        });


        $('#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 + ',';
                }
            }
            console.log(course_code);
            $.ajax({
                method: "GET",
                url: "insert_courses.php",
                data: {course_code: course_code},
                success: function (data) {
                    if (data) {
                        //alert(data);
                        window.open('Report.php');
                        $('#register').attr('disabled', 'disabled');
                    } else {
                        alert(data);
                    }
                }

            })
        });
// dont cut
    });


    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--;
            }
    }

    // caculate total/sum value
    var table = document.getElementById("mytable2"),
        checkboxes = document.getElementsByName("check-tab2"), sumVal = 0;
    getSum();
    function getSum() {
        // for(var i = 1; i < table.rows.length; i++){
        //        sumVal = sumVal + parseInt(table.rows[i].cells[2].innerHTML);
        // }
        // document.getElementById("sumV").innerHTML = "Total Units = "+sumVal;

        for (var i = 0; i < checkboxes.length; i++) {
            if (checkboxes[i].checked) {
                var index;
                sumVal = sumVal + table.rows[index].cells[2].innerHTML;
            }
            console.log("Sum => " + sumVal);

        }
    }


</script>        
                
                
    
   
          
