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

$action = $_REQUEST['action'];

if (!empty($_POST['add_external_btn'])) {
    $remove = array("'", '"');
    $name1 = str_replace($remove, "", $_POST['name']);
    $addr1 = str_replace($remove, "", $_POST['address']);
    $email1 = str_replace($remove, "", $_POST['email']);
    $phone1 = str_replace($remove, "", $_POST['phone']);
    
    $name1 = $val->ValidateString($name1);
    $addr1 = $val->ValidateString($addr1);
    $email1 = $val->ValidateString($email1);
    $phone1 = $val->ValidateString($phone1);

    $min = "1970-01-01";
    $max = "1990-12-31";
    $dob = rand_date($min, $max);
    $dp = explode("-", $dob);
    $otp = $dp[2] . $dp[1] . $dp[0];

    $ct = 0;
    do {
        $base = rand(12222, 98888);
        $SELECT = "select COUNT(*) from isr_tac_main_tbl where substring(roll,4,5) = '" . $base . "'";
        $ObjDB->sproc_name = $SELECT;
        $res_SELECT = $ObjDB->SelectQuery();
        list($ct) = mysqli_fetch_row($res_SELECT);
    } while ($ct > 0);

    $login = "TAC" . $base;

    if (empty($name1) && $error == 0) {
        $msg = "Please Fill the Name.";
        $error++;
    } elseif (empty($addr1) && $error == 0) {
        $msg = "Please Fill the Address.";
        $error++;
    } elseif (empty($email1) && $error == 0) {
        $msg = "Please Fill The Email.";
        $error++;
    } elseif (empty($phone1) && $error == 0) {
        $msg = "Please Fill the Phone Number.";
        $error++;
    } elseif ($error == 0) {
        
        $datetime = DATE('Y-m-d H:i:s');
        
        $query1 = "INSERT INTO isr_tac_main_tbl (roll,name,dob,otp,address,email,phone) VALUES('$login', '$name1', '$dob', '$otp', '$addr1', '$email1', '$phone1')";
        $ObjDB->sproc_name = $query1;
        $ObjDB->ExecuteQuery();
        
        $msg = "Teacher added Successfully";
        $activity = "New Teacher Added: " . $login;
        $query1_act = "INSERT INTO isr_actions_tbl (roll, activity, datetime) VALUES('" . $admin_roll . "','" . $activity . "','" . $datetime . "')";
        $ObjDB->sproc_name = $query1_act;
        $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">Add External SUP/TAC</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="phone" class="form-label">Phone:</label>
                                    <input type="text" maxlength="250" class="form-control" name="phone" id="phone" placeholder="" required="" value="<?php echo $phone; ?>">
                                </div>
                                <div class="col-lg-3">
                                    <label for="address" class="form-label">Address:</label>
                                    <input type="text" maxlength="250" class="form-control" name="address" id="address" placeholder="" required="" value="<?php echo $address; ?>" />
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-lg-3">
                                    <INPUT TYPE="submit"  class="submit btn btn-danger btn-sm" id="add_external_btn" name="add_external_btn" value="Save">
                                </div>
                            </div>
                        </form>
                    </div>
                    
                    <div class="table-responsive">
                        <table class="table table-bordered align-middle">
                            <thead>
                                <tr>
                                     <th>Login</th>
        <th>Name</th>
        <th>Email</th>
        <th>PW / OTP</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
        $objadm->fac_status = "B";
        $res_query = $objadm->GetExternalList();
    while ($row = mysqli_fetch_array($res_query)) {
		$roll = $row['roll'];
		$name = $row['name'];
		$pww = $row['password'];
		$otp = $row['otp'];
		$email = $row['email'];
		if ($pww != '' and $pww != NULL) {$pw_show = "Taken";}
		else {$pw_show = $otp;}
        ?>
    <tr>
        <td><?php echo $roll; ?></td>
        <td><?php echo $name; ?></td>
        <td><?php echo $email; ?></td>
        <td><?php echo $pw_show; ?></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");
?>