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

$log = new common();
$objadm = new class_admin();
$val = new validation();

$IdStr = $_REQUEST['IdStr'];
$IdStr = $val->ValidateString($IdStr);
$objadm->unique_code = $IdStr;
$objadm->user_roll = $_SESSION['user_sesroll'];

$GetTempCourseDetails = $objadm->GetTempCourseDetails();

while ($row = mysqli_fetch_array($GetTempCourseDetails)) {
    $bsms_code = $row['bsms_code'];
    $bsms_credit = $row['bsms_credit'];
    $phd_code = $row['phd_code'];
    $phd_credit = $row['phd_credit'];
    $course_title = $row['title'];
    $slot = $row['slot'];
    $sem = $row['sem'];
}

if ($_POST['teacher_save_btn']) {
    $bsms_code_up = $_REQUEST['bsms_code'];
    $bsms_code_up = $val->ValidateString($bsms_code_up);
    $objadm->sub_code = $bsms_code_up;
    $teacher_select = $val->ValidateString($_REQUEST['teacher_select']);
    $objadm->fac_code = $teacher_select;

    $objadm->InsertAuxFaculty();
}


if (!empty($_REQUEST['facOrder']) && !empty($_REQUEST['sr']) && !empty($_REQUEST['SubCode'])) {

    $facOrder = $val->ValidateString($_REQUEST['facOrder']);
    $srVal = $val->ValidateString($_REQUEST['sr']);
    $SubCode = $val->ValidateString($_REQUEST['SubCode']);

    $objadm->facOrder = $facOrder;
    $objadm->srVal = $srVal;
    $objadm->SubCode = $SubCode;

    $objadm->UpdateFacultyOrder();

    header("Location: course-teacher-allocation.php?IdStr=" . $IdStr);
}


if (!empty($_REQUEST['srDel']) && (!empty($_REQUEST['subDel']))) {
    $objadm->srDel = $val->ValidateString($_REQUEST['srDel']);
    $objadm->subDel = $val->ValidateString($_REQUEST['subDel']);

    $objadm->DeleteFacultyAllocation();

    header("Location: course-teacher-allocation.php?IdStr=" . $IdStr);
}

/* Getting the List of Teachers */
$objadm->fac_status = 'Full';
$result_faculty = $objadm->GetFacultyList();

/* Getting the List of Teachers according to the Subject Code */
$objadm->sub_code = $bsms_code;
$ResultTempFacList = $objadm->GetTempFacultyList();
?>
<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">Edit Teacher Allocation</h5>
                </div>
                <div class="card-body">
                    <form action="" method="POST" id="">
                        <table class="table table-bordered w-75 mx-auto">
                            <tr>
                                <td><strong>BSMS Code</strong></td>
                                <td><?php echo $bsms_code; ?></td>
                            </tr>
                            <tr>
                                <td><strong>Course Title</strong></td>
                                <td><?php echo $course_title; ?></td>
                            </tr>
                            <tr>
                                <td><strong>Select Teacher</strong></td>
                                <td>
                                    <select id="teacher_select" name="teacher_select" class="form-select w-75" required="">
                                        <option value="">----- Select Teacher -----</option>
                                        <?php while ($rowFac = mysqli_fetch_assoc($result_faculty)) { ?>
                                            <option value="<?php echo $rowFac['roll'] ?>"><?php echo $rowFac['name'] ?></option>
                                        <?php } ?>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2" class="text-left">
                                    <input type="hidden" name="bsms_code" id="bsms_code" value="<?php echo $bsms_code; ?>"/>
                                    <input type="submit" name="teacher_save_btn" id="teacher_save_btn" class="btn btn-danger px-4"  style="background: #a9181b;border:none;"/>
                                </td>
                            </tr>
                        </table>
                    </form>
                    <!-- Divider -->
                    <div class="section-divider"></div>

                    <!-- Teachers Selected Table -->
                    <h5 class="text-center mb-3">Teachers Selected</h5>
                    <table class="table table-bordered align-middle text-center">
                        <thead class="table-light">
                            <tr>
                                <th style="width: 50px;"></th>
                                <th style="width: 50px;">#</th>
                                <th class="text-start">Name</th>
                                <th style="width: 100px;">Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $c = 1;
                            while ($RowRes = mysqli_fetch_array($ResultTempFacList)) {

                                $fac_roll = $RowRes['fac_code'];

                                $fac_order = $RowRes['fac_order'];
                                $sr = $RowRes['sr'];
                                ?>
                                <tr>
                                    <td>
                                        <?php if ($c > 1) { ?>
                                            <a href="?IdStr=<?php echo $IdStr; ?>&facOrder=<?php echo $fac_order; ?>&sr=<?php echo $sr; ?>&SubCode=<?php echo $bsms_code; ?>"><i class="fa fa-arrow-up"></i></a>
                                        <?php } else { ?>
                                            <i class="fa fa-user" style="color: green;"></i>
                                        <?php } ?>
                                    </td>
                                    <td><?php echo $c; ?></td>
                                    <td class="text-start"><?php echo $RowRes['name']; ?></td>
                                    <td><a href="javascript:void(0);" class="btn btn-danger shadow btn-xs sharp" onclick="FunTeacherAllocationDelete('<?php echo $sr; ?>', '<?php echo $bsms_code; ?>');"><i class="fa fa-trash"></i></a></td>
                                </tr>
                                <?php $c++;
                            }
                            ?>
                        </tbody>
                    </table>

                </div>


            </div>
        </div>

    </div>
</div>
<?php
$pagemaincontent = ob_get_contents();
ob_clean();
$PAGENAME = 'teacher-allocation';
$HTMLTITLE = '';
$HTMLDESCRIPTION = '';
$HTMLKEYWORDS = '';
include("admin-master.php");
?>
<script type="text/javascript">
    function FunTeacherAllocationDelete(Sr, SubCode) {
        if(confirm("Are You sure want to delete?")){
           window.location.href="course-teacher-allocation.php?srDel="+Sr+"&subDel="+SubCode+"&IdStr=<?php echo $IdStr;  ?>";
        }
    }
</script>