<?php
include_once '../session/admin-session.php';
include_once '../classes/class_admin.php';
include_once '../classes/validation.php';
include_once '../classes/common.php';
include_once 'read-configration.php';
ob_start();

$ObjDB = new class_db();
$objadm = new class_admin();
$val = new validation();

$semb1 = $current_sem_num;

if ($_POST['course_add_drop_btn']) {
    $stu_roll = $val->ValidateString($_REQUEST["roll_no"]);

    $objadm->roll_number = $stu_roll;
    $RollNumberArr = $objadm->GetStudentDetailsbyRollNumber();

    $num = mysqli_num_rows($RollNumberArr);
    if ($num == 0) {
        $error_msg = "Incorrect Roll Number";
    } else {
        $row = mysqli_fetch_array($RollNumberArr);
        $stu_name = $row["name"];
        $batch = $row["batch"];
        $sem_now = $row["sem_now"];
        $reg_status = $row["reg_status"];
        $reg_file = "isr_reg_" . $batch . "_tbl";
        $curr_file = "isr_curriculum_tbl";
        $sub_list = 'c' . $semb1;
        $reg_closed = "N";
    }
}


if ($reg_closed == 'N') {
    if (!empty($_POST['add_subject_btn'])) {
        $stu_roll = $val->ValidateString($_REQUEST["roll_no"]);
        $sub_code = $val->ValidateString($_REQUEST["sub_code"]);
        $sem = $val->ValidateString($_REQUEST["sem"]);
        $datetime = date("Y-m-d H:i:s");
        $SELECT = "SELECT * FROM $reg_file WHERE stu_roll='$stu_roll' and sub_code='$sub_code' and sem ='$sem_now'";
        $ObjDB->sproc_name = $SELECT;
        $res_SELECT = $ObjDB->SelectQuery();
        if (mysqli_num_rows($res_SELECT) > 0) {
            $res_msg = "<b><big> $sub_code is already registered in sem = $sem_now </b></big>";
        } else {
            $queryinsert = "INSERT INTO $reg_file (b1_sem, sem, sub_list, sub_code, stu_roll, rs_ref) VALUES('$semb1', '$sem_now', '$sub_list', '$sub_code', '$stu_roll', '0')";
            $ObjDB->sproc_name = $queryinsert;
            $res_Insert = $ObjDB->ExecuteQuery();

            $res_msg = "<b><big> $sub_code is inserted in sem = $sem_now </b></big>";
            $activity = "Subject " . $sub_code . " added for " . $stu_roll . " in SEM " . $sem_now;
            $query1 = "INSERT INTO actions (roll, activity, datetime) VALUES('" . $admin_roll . "','" . $activity . "','" . $datetime . "')";
            $ObjDB->sproc_name = $query1;
            $res_Insert = $ObjDB->ExecuteQuery();
        }
    }
}
?>
<div class="container-fluid">
    <div class="row">
        <div class="col-xl-12 col-xxl-12 col-sm-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title">Control Course Add Drop</h5>
                </div>
                <div class="card-body">
                    <form action="" method="POST">
                        <div class="row"><div class="col-lg-12"><?php echo $error_msg; ?></div></div>
                        <div class="row">
                            <div class="col-lg-3">
                                <label>Roll Number:</label>
                                <div>
                                    <INPUT TYPE="text" NAME="roll_no" class="form-control input-sm" placeholder="Roll Number"/>
                                </div>
                            </div>
                            <div class="col-lg-3">
                                <input type="submit" value="Submit" name="course_add_drop_btn" class="btn btn-primary"/>
                            </div>
                        </div>
                    </form>
                    <?php if ($_POST['course_add_drop_btn'] && $num > 0) { ?>
                        <br/><br/>
                        <div class="table-responsive">
                            <table class="table table-bordered align-middle">
                                <tr><th>Name of the student</th><td><?php echo $stu_name; ?></td></tr>
                                <tr><th>Roll Number</th><td><?php echo $stu_roll; ?></td></tr>
                                <tr><th>Current Semester</th><td><?php echo $sem_now; ?></td></tr>
                                <tr><th>Current Batch</th><td><?php echo $batch; ?></td></tr>
                                </tr>
                            </table>
                            <br/>
                            <table class="table table-striped table-bordered table-condensed">
                                <tr><th colspan=5><div class=stu_name> Semester : <?php echo $sem_now; ?></div></th></tr>
                                <?php
                                $query = "SELECT a.reg_sr, a.sub_code, a.sub_flag, b.sub_title, b.sub_crdit from $reg_file a, isr_curriculum_tbl b where b.sub_list = a.sub_list and a.sem = '$sem_now' and a.stu_roll = '$stu_roll' and a.sub_code = b.sub_code order by a.sub_code";
                                $ObjDB->sproc_name = $query;
                                $res_query = $ObjDB->SelectQuery();
                                $j = 1;
                                $sum_credit = 0;
                                while ($row = mysqli_fetch_array($res_query)) {
                                    $reg_sr = $row["reg_sr"];
                                    $sub_code = $row["sub_code"];
                                    $sub_flag = $row["sub_flag"];
                                    $sub_credit = $row["sub_crdit"];
                                    $sub_title = $row["sub_title"];
                                    $sum_credit = $sum_credit + $sub_credit;

                                    echo "<TR><TD width=20>" . $j++;
                                    echo "<TD width=80>" . $sub_code;
                                    echo "<TD width=400>" . $sub_title;
                                    echo "<TD width=50>" . $sub_credit;
                                    echo "<TD width=50>";
                                    ?><A HREF="control-course-sub-del.php?stu_roll=<?php echo $stu_roll; ?>&reeg_sr_del=<?php echo $reg_sr; ?>&reeg_file=<?php echo $reg_file; ?>">Delete</A>
                                        <?php }
                                        ?>
                            </table>

                        </div>

                        <br/><br/>
                        <div class="table-responsive">
                             <div class="row"><div class="col-lg-12"><?php echo $res_msg; ?></div></div>

                            <form class="formone" METHOD="POST" ACTION="">
                                <INPUT TYPE="hidden" NAME="roll_no" VALUE="<?php echo $stu_roll; ?>">
                                 <input type="hidden" value="Submit" name="course_add_drop_btn"/>
                                <table class="table table-bordered align-middle">
                                    <tr><th colspan=2><div class=stu_name> Add Course</div></th></tr>
                                    <tr>
                                        <td>
                                            <SELECT NAME="sub_code" class="form-control">
                                                <OPTION value="">- - - - Select Course - - - - </OPTION>
                                                <?php
                                                $objadm->res_sem = $sub_list;
                                                $querysubject = $objadm->GetcurriculumList();
                                                while ($row = mysqli_fetch_array($querysubject)) {
                                                    $sub_code = $row["sub_code"];
                                                    $sub_title = $row["sub_title"];
                                                    ?>
                                                    <OPTION value="<?php echo $sub_code; ?>"><?php echo $sub_list." - ".$sub_code." - ".$sub_title; ?></OPTION>
                                                    <?php
                                                    }
                                                    ?>
                                            </SELECT>
                                        </td>
                                            <td>
                                                <INPUT TYPE="submit" name="add_subject_btn" value="Add" class="btn btn-primary">
                                            </td>
                                        </tr>
                                    </table>
                                </form>
                            </div>

                        <?php } ?>

                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php
    $pagemaincontent = ob_get_contents();
    ob_clean();
    $PAGENAME = 'control-course-add-drop';
    $HTMLTITLE = '';
    $HTMLDESCRIPTION = '';
    $HTMLKEYWORDS = '';
    include("admin-master.php");
    ?>