<?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();
$log = new common();
$objadm = new class_admin();
$val = new validation();
$perm_array = array();

if ($_POST['officer_add_btn']) {
    $officer_code = $val->ValidateString($_REQUEST['officer_code']);
    $officer_name = $val->ValidateString($_REQUEST['officer_name']);
    $officer_email = $val->ValidateString($_REQUEST['officer_email']);

    $remove = array("'", '"', ",");
    $roll1 = str_replace($remove, "", $officer_code);
    $roll = strtoupper($roll1);

    $name1 = str_replace($remove, "", $officer_name);
    $name = ucfirst($name1);

    $perm = implode(",", $_POST['perm']);

    $error = 0;
    if ((strlen($roll) != 6)) {
        $msg = "Code length should be 6 characters long.";
        $error++;
    } elseif (empty($name)) {
        $msg = "Please Fill The Name.";
        $error++;
    } elseif (empty($officer_email)) {
        $msg = "Please Enter The Email Id.";
        $error++;
    } elseif ($val->validateemail($officer_email) == false) {
        $msg = "Please Validate The Email.";
        $error++;
    } elseif ($error == 0) {
        if (!empty($_REQUEST['hdn_edit_roll'])) {

            $edit_roll = $val->ValidateString($_REQUEST['hdn_edit_roll']);

            $query1 = "UPDATE isr_officer_main_tbl set name = '" . $name . "',perm = '" . $perm . "', email = '" . $officer_email . "'  WHERE roll = '" . $edit_roll . "'";
            $ObjDB->sproc_name = $query1;
            $ObjDB->ExecuteQuery();

            $msg = "Officer updated Successfully";
            $activity = "Officer updated: " . $edit_roll;
            $query1_1 = "INSERT INTO isr_actions_tbl (roll, activity, datetime) VALUES('" . $admin_roll . "','" . $activity . "','" . $datetime . "')";
            $ObjDB->sproc_name = $query1_1;
            $ObjDB->ExecuteQuery();

            $name = '';
            $roll = '';
            $officer_email = '';
            $edit_roll = '';
            
            
        header("Location: people-officer.php");
            
        } else {

            $objadm->roll = $roll;
            $SELECT = $objadm->GetOfficerList();

            $objadm->email = $officer_email;
            $SELECT_EMAIL = $objadm->GetOfficerList();

            IF (mysqli_num_rows($SELECT) > 0) {
                $msg = "This code Already exist.";
            } ELSE IF (mysqli_num_rows($SELECT_EMAIL) > 0) {
                $msg = "This Email Id Already exist.";
            } ELSE {
                $query1 = "INSERT INTO isr_officer_main_tbl (roll,name,password,perm,email) 
                    VALUES('" . $roll . "','" . $name . "','" . md5($roll) . "','" . $perm . "', '" . $officer_email . "')";
                $ObjDB->sproc_name = $query1;
                $ObjDB->ExecuteQuery();

                $msg = "Officer Added Successfully";
                $activity = "Officer Added: " . $roll;

                $query1_1 = "INSERT INTO isr_actions_tbl (roll, activity, datetime) VALUES('" . $admin_roll . "','" . $activity . "','" . $datetime . "')";
                $ObjDB->sproc_name = $query1_1;
                $ObjDB->ExecuteQuery();

                $name = '';
                $roll = '';
                $officer_email = '';
            }
        }
    }
}

if (!empty($_REQUEST['action'])) {
    if ($_REQUEST['action'] == "delete") {
        $delete_roll = $val->ValidateString($_REQUEST['roll']);
        $query = "DELETE FROM isr_officer_main_tbl where roll='" . $delete_roll . "'";
        $ObjDB->sproc_name = $query;
        $ObjDB->ExecuteQuery();
        
         header("Location: people-officer.php");
        
    } elseif ($_REQUEST['action'] == "edit") {
        $edit_roll = $val->ValidateString($_REQUEST['roll']);
        $objadm->roll = $edit_roll;
        $query_val = $objadm->GetOfficerList();
        
        while($resVal = mysqli_fetch_array($query_val)){
            $roll = $resVal['roll'];
            $name = $resVal['name'];
            $perm = $resVal['perm'];
            $officer_email = $resVal['email'];
        }
        $edit_roll = $roll;
        
        $perm_array = explode(",", $perm);
        $dis = "readonly";
        
    }
}

$objadm->roll = "";
$officer_query = $objadm->GetOfficerList();
?>
<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">Manage Officer</h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <span style="color: red;"><?php echo $msg; ?></span>
                        <form action="" method="POST">
                            <div class="row">
                                <div class="col-lg-2">
                                    <label for="officer_code" class="form-label">Officer Code:</label>
                                    <input type="text" class="form-control" name="officer_code" id="officer_code" placeholder="" required="" value="<?php echo $roll; ?>" />
                                </div>
                                <div class="col-lg-3">
                                    <label for="officer_name" class="form-label">Officer Name:</label>
                                    <input type="text" class="form-control" name="officer_name" id="officer_name" placeholder="" required="" value="<?php echo $name; ?>" />
                                </div>
                                <div class="col-lg-3">
                                    <label for="officer_email" class="form-label">Officer Email:</label>
                                    <input type="email" class="form-control" name="officer_email" id="officer_email" placeholder="" required="" value="<?php echo $officer_email; ?>">
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-lg-12">
                                    <br/>
                                    <table class="table">
                                        <tr><td colspan=4>Check Permissions</td></tr>
                                        <tr>
                                            <td style='width:25%;vertical-align: top; border-right: solid gray 0px'><FONT COLOR="blue">Courses</FONT><BR> 				
                                                <div style="float: left; width: 20px; ">
                                                    <input type="checkbox" name="perm[]" value="CR" <?php echo (in_array("CR", $perm_array) ? "checked" : ""); ?> />
                                                </div><div style="float: left">Edit Courses</div><div style="clear: both"></div>

                                                <div style="float: left; width: 20px; ">
                                                    <input type="checkbox" name="perm[]" value="AL" <?php echo (in_array("AL", $perm_array) ? "checked" : ""); ?> />
                                                </div><div style="float: left">Allocate Course</div><div style="clear: both"></div>

                                                <div style="float: left; width: 20px">
                                                    <input type="checkbox" name="perm[]" value="PR" <?php echo (in_array("PR", $perm_array) ? "checked" : ""); ?> />
                                                </div><div style="float: left">Projects</div><div style="clear: both"></div>

                                                <div style="float: left; width: 20px">
                                                    <input type="checkbox" name="perm[]" value="AT" <?php echo (in_array("AT", $perm_array) ? "checked" : ""); ?> />
                                                </div><div style="float: left">Attendance</div><div style="clear: both"></div>


                                            </td>

                                            <td style='width:25%;vertical-align: top; border-right: solid gray 0px'><FONT COLOR="blue">Registration</FONT><BR> 				
                                                <div style="float: left; width: 20px; ">
                                                    <input type="checkbox" name="perm[]" value="RG" <?php echo (in_array("RG", $perm_array) ? "checked" : ""); ?> />
                                                </div><div style="float: left">Reg Control</div><div style="clear: both"></div>

                                                <div style="float: left; width: 20px; ">
                                                    <input type="checkbox" name="perm[]" value="LT" <?php echo (in_array("LT", $perm_array) ? "checked" : ""); ?> />
                                                </div><div style="float: left">Permit Late Reg</div><div style="clear: both"></div>

                                                <div style="float: left; width: 20px; ">
                                                    <input type="checkbox" name="perm[]" value="BL" <?php echo (in_array("BL", $perm_array) ? "checked" : ""); ?> />
                                                </div><div style="float: left">Block Reg</div><div style="clear: both"></div>

                                                <!-- <div style="float: left; width: 20px; ">
                               <input type="checkbox" name="perm[]" value="GC" <?php echo (in_array("GC", $perm_array) ? "checked" : ""); ?> />
                            </div><div style="float: left">Grade Change</div><div style="clear: both"></div> -->

                                                <div style="float: left; width: 20px; ">
                                                    <input type="checkbox" name="perm[]" value="BF" <?php echo (in_array("BF", $perm_array) ? "checked" : ""); ?> />
                                                </div><div style="float: left">Block Feedback</div><div style="clear: both"></div>

                                            </td>


                                            <td style='width:25%;vertical-align: top; border-right: solid gray 0px'><FONT COLOR="blue">People</FONT><BR>

                                                <div style="float: left; width: 20px; ">
                                                    <input type="checkbox" name="perm[]" value="ST" <?php echo (in_array("ST", $perm_array) ? "checked" : ""); ?> />
                                                </div><div style="float: left">Students</div><div style="clear: both"></div>

                                                <div style="float: left; width: 20px; ">
                                                    <input type="checkbox" name="perm[]" value="FA" <?php echo (in_array("FA", $perm_array) ? "checked" : ""); ?> />
                                                </div><div style="float: left">Faculty</div><div style="clear: both"></div>


                                                <div style="float: left; width: 20px; ">
                                                    <input type="checkbox" name="perm[]" value="EX" <?php echo (in_array("EX", $perm_array) ? "checked" : ""); ?> />
                                                </div><div style="float: left">External</div><div style="clear: both"></div>

                                                <!-- <div style="float: left; width: 20px; ">
                                <input type="checkbox" name="perm[]" value="PW" <?php echo (in_array("PW", $perm_array) ? "checked" : ""); ?> />
                            </div><div style="float: left">Password</div><div style="clear: both"></div> -->

                                                <div style="float: left; width: 20px; ">
                                                    <input type="checkbox" name="perm[]" value="FL" <?php echo (in_array("FL", $perm_array) ? "checked" : ""); ?> />
                                                </div><div style="float: left">Flags</div><div style="clear: both"></div>



                                            </td>

                                            <td style='width:25%;vertical-align: top;' align="top"><FONT COLOR="blue">Reports</FONT><BR>				
                                                <div style="float: left; width: 20px">
                                                    <input type="checkbox" name="perm[]" value="RP" <?php echo (in_array("RP", $perm_array) ? "checked" : ""); ?> />
                                                </div><div style="float: left">Registration</div><div style="clear: both"></div>

                                                <div style="float: left; width: 20px">
                                                    <input type="checkbox" name="perm[]" value="RS" <?php echo (in_array("RS", $perm_array) ? "checked" : ""); ?> />
                                                </div><div style="float: left">Results</div><div style="clear: both"></div>

                                                <div style="float: left; width: 20px">
                                                    <input type="checkbox" name="perm[]" value="MR" <?php echo (in_array("MR", $perm_array) ? "checked" : ""); ?> />
                                                </div><div style="float: left">Marks</div><div style="clear: both"></div>

                                                <div style="float: left; width: 20px">
                                                    <input type="checkbox" name="perm[]" value="FB" <?php echo (in_array("FB", $perm_array) ? "checked" : ""); ?> />
                                                </div><div style="float: left">Feedback</div><div style="clear: both"></div>


                                            </td>
                                        </tr>

                                        </tr>
                                    </table>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-lg-4">
                                    <lable>&nbsp;</lable>
                                     <input type="hidden" name="hdn_edit_roll" value="<?php echo $edit_roll; ?>" />
                                    <input type="submit" class="btn btn-danger" style="background: #a9181b;
                                           border: #a9181b; text-align: center;" value="Submit" name="officer_add_btn" id="officer_add_btn"/>
                                </div>
                            </div>

                        </form>
                    </div>

                    <div class="table-responsive">
                        <table class="table">
                            <tr>
                                <td valign="top">
                                    <table class="table table-bordered align-middle">
                                        <thead>
                                        <tr>
                                            <th align=center style="border-right:1px solid white; ">Code</th>
                                            <th align=left style="border-right:1px solid white; ">Name</th>
                                            <th align=left>Permission</th>
                                            <th>Delete</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <?php
                                        $query_list = $objadm->GetOfficerList();
                                        while ($row = mysqli_fetch_array($query_list)) {
                                            $roll = $row["roll"];
                                            ?>
                                            <tr>
                                                <td><?php echo $roll; ?></td>
                                                <td><A HREF="people-officer.php?action=edit&roll=<?php echo $roll; ?>"><?php echo $row["name"]; ?><br/><?php echo $row["email"]; ?></A></td>
                                                <td><?php echo str_replace(",", ", ", $row["perm"]) ?></td>
                                                <td><a onclick="return confirm('Are you sure you want to delete the officer inmformation?');" href="people-officer.php?action=delete&roll=<?php echo $roll; ?>">Delete</a></td>
                                                <?php
                                            }
                                            ?>
                                        </tbody>
                                    </table>
                                </td>
                                <td valign="top">
                                    <table class="table table-bordered align-middle">
                                        <tr><th colspan=2>Permissions Codes</TH></TR>
                                        <tr><td>AL<td>Allocate Courses
                                        <tr><td>AT<td>Attendance
                                        <tr><td>BF<td>Block Feedback
                                        <tr><td>BL<td>Block Registration
                                        <tr><td>CR<td>Edit Courses
                                        <tr><td>EX<td>Add / del External
                                        <tr><td>FA<td>Add / Del Faculty
                                        <tr><td>FB<td>Report - Feedback
                                        <tr><td>FL<td>Flags
                                        <!-- <tr><td>GC<td>Grade Change -->
                                        <tr><td>LT<td>Permit Late Registration
                                        <tr><td>MR<td>Marks Download
                                        <tr><td>PR<td>BSMS Projects
                                        <!-- <tr><td>PW<td>Password -->
                                        <tr><td>RG<td>Reg Control
                                        <tr><td>RP<td>Report - Registration 
                                        <tr><td>RS<td>Report - Results
                                        <tr><td>ST<td>Student's Status
                                    </table>
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<?php
$pagemaincontent = ob_get_contents();
ob_clean();
$PAGENAME = 'people_officer';
$HTMLTITLE = '';
$HTMLDESCRIPTION = '';
$HTMLKEYWORDS = '';
include("admin-master.php");
?>