<?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();

$error = 0;
$msg = "";
$var = "show1";

if (!empty($_POST['add_external_rac_btn'])) {
    $remove = array("'", '"');
    $name1 = str_replace($remove, "", $_POST['name']);
    $organization1 = str_replace($remove, "", $_POST['organization']);
    $email1 = str_replace($remove, "", $_POST['email']);

    $name1 = $val->ValidateString($name1);
    $organization1 = $val->ValidateString($organization1);
    $email1 = $val->ValidateString($email1);

    $ct = 0;
    do {
        $base = rand(1222, 9888);
        $SELECT = "select COUNT(*) from isr_gsam_rac_externals_tbl where substring(rac_code,4,4) = '" . $base . "'";
        $ObjDB->sproc_name = $SELECT;
        $res_SELECT = $ObjDB->SelectQuery();
        list($ct) = mysqli_fetch_row($res_SELECT);
    } while ($ct > 0);

    $rac_code = "RAC" . $base;

    $a = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
    $z = strlen($a);
    $s = '';
    for ($i = 0; $i < 4; $i++) {
        $x = rand(0, $z);
        $s .= $a[$x];
    }
    $code = $s;

    if (empty($name1) && $error == 0) {
        $msg = "Please Fill the Name.";
        $error++;
    } elseif (empty($organization1) && $error == 0) {
        $msg = "Please Fill the Organization.";
        $error++;
    } elseif (empty($email1) && $error == 0) {
        $msg = "Please Fill The Email.";
        $error++;
    } elseif (!empty($email1) && $error == 0 && $val->validateemail($email1) == false) {
        $msg = "Please Validate The Email Id.";
        $error++;
    } elseif ($error == 0) {
        $objadm->email = $email1;
        $query_checkemailid = $objadm->GetExtrnalRACList();
        if (mysqli_num_rows($query_checkemailid) > 0) {
            $result_checkemailid = mysqli_fetch_assoc($query_checkemailid);
            $rac_code = $result_checkemailid['rac_code'];
            $msg = "<FONT SIZE='+1' COLOR=red>Email Id is already exist!</FONT><BR><BR>";
        } else {
            $login = $code . $rac_code;
            $password = md5($login);

            $datetime = DATE('Y-m-d H:i:s');

            $query1 = "INSERT INTO isr_gsam_rac_externals_tbl (rac_name, rac_email, rac_code, code, rac_address,  rac_password) VALUES('$name1', '$email1', '$rac_code', '$code', '$organization1', '$password')";
            $ObjDB->sproc_name = $query1;
            $ObjDB->ExecuteQuery();

            $activity = "New RAC Member added: " . $login;
            $query1 = "INSERT INTO isr_actions_tbl (roll, activity, datetime) VALUES('" . $admin_roll . "','" . $activity . "','" . $datetime . "')";
            $ObjDB->sproc_name = $query1;
            $ObjDB->ExecuteQuery();

            $msg = "External RAC added Successfully";
        }
    }
}
?>
<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">Add External RAC</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-3">
                                    <label for="name" class="form-label">Name:</label>
                                    <input type="text" maxlength="250" class="form-control" name="name" id="name" placeholder="" required="" value="<?php echo $name; ?>" />
                                </div>
                                <div class="col-lg-3">
                                    <label for="email" class="form-label">Email:</label>
                                    <input type="email" maxlength="250" class="form-control" name="email" id="email" placeholder="" required="" value="<?php echo $email; ?>">
                                </div>
                                <div class="col-lg-3">
                                    <label for="address" class="form-label">Organization:</label>
                                    <input type="text" maxlength="250" class="form-control" name="organization" id="organization" placeholder="" required="" value="<?php echo $organization; ?>" />
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-lg-3">
                                    <INPUT TYPE="submit"  class="submit btn btn-danger btn-sm" id="add_external_rac_btn" name="add_external_rac_btn" value="Save">
                                </div>
                            </div>
                        </form>
                    </div>

                    <div class="table-responsive">
                        <table class="table table-bordered align-middle" id="rac_table_id">
                            <thead>
                                <tr>
                                    <th>RAC Code</th>
                                    <th>Name</th>
                                    <th>Email</th>
                                    <th>Organization</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $objadm->rac_email = "";
                                $res_query = $objadm->GetExtrnalRACList();
                                while ($row = mysqli_fetch_array($res_query)) {
                                    $rac_code = $row['rac_code'];
                                    $name = $row['rac_name'];
                                    $email = $row['rac_email'];
                                    $rac_address = $row['rac_address'];
                                    ?>
                                    <tr>
                                        <td><?php echo $rac_code; ?></td>
                                        <td><?php echo $name; ?></td>
                                        <td><?php echo $email; ?></td>
                                        <td><?php echo $rac_address; ?></td>
                                    </tr>
                                    <?php
                                }
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>    
<?php
$pagemaincontent = ob_get_contents();
ob_clean();
$PAGENAME = 'people_external';
$HTMLTITLE = '';
$HTMLDESCRIPTION = '';
$HTMLKEYWORDS = '';
include("admin-master.php");
?>
<script type="text/javascript">
    $("#rac_table_id").DataTable({
        dom: '<"top"lBf>rt<"bottom"ip><"clear">',
        buttons: [
            {
                extend: 'excelHtml5',
                text: 'Download Excel',
                title: 'Data Export',
                className: 'btn btn-success'
            }
        ]
    });
</script>