<?php
    // session_start();
    include "../../db.php";
    require '../../auth.php';
    restrictAccess(['Associate']);
    date_default_timezone_set('Asia/Kolkata');
    $currentTime = date('M j, Y g:i a');

    if ( !isset($_SESSION['uid']) && !isset($_SESSION['uref']) && !isset($_SESSION['utype']) ) {
        header("Location: logout.php");
        exit();
    }

    $adm_sql = mysqli_query($con, "SELECT * FROM users WHERE uid = '$_SESSION[uid]' ");
    if($adm_row = mysqli_fetch_assoc($adm_sql)){
        $uadmin = $adm_row["uadmin"];
        $admin_ref = $adm_row["admin_ref"];
    }
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <?php include "include/meta.php"; ?>
    <?php include "include/css.php"; ?>
    <?php include "include/php-functions.php"; ?>
</head>

<body>
    <!--  Header -->
    <?php 
    include "include/header.php"; 
    ?>
    <!-- #header -->

    <section id="inner-body">
        <div class="body-content">
            <div class="container-fluid">
                <?php if(isset($_GET["edit_id"])){
                    $edit_run = mysqli_query($con, "SELECT s_name FROM status WHERE s_id = '$_GET[edit_id]'");
                    if($edit_row = mysqli_fetch_assoc($edit_run)){
                    ?>
                <?php }
                } else{?>
                <div class="card mt-3">
                    <h5 class="card-header text-dark text-center font-weight-bold">New Entry</h5>
                    <div class="card-body">
                        <form class="user" method="post" enctype="multipart/form-data">
                            <div class="row justify-content-center ">
                                <div class="col-md-6 mb-2">
                                    <label for="name">Name <span>*</span></label>
                                    <input type="text" class="form-control form-control-sm" id="name"
                                        placeholder="Enter Name" name="name" required>
                                </div>
                                <div class="col-md-6 mb-2">
                                    <label for="phone">Phone No. <span>*</span></label>
                                    <input type="number" class="form-control form-control-sm" id="phone" minlength="10"
                                        maxlength="10" placeholder="Enter Phone No." name="phone" required>
                                </div>
                                <div class="col-md-6 mb-2">
                                    <label for="whatsapp">WhatsApp No.</label>
                                    <input type="number" class="form-control form-control-sm" id="whatsapp"
                                        minlength="10" maxlength="10" placeholder="Enter WhatsApp no." name="whatsapp">
                                </div>
                                <div class="col-md-6 mb-2">
                                    <label for="email">Email Id <span>*</span></label>
                                    <input type="email" class="form-control form-control-sm mb-2" id="email"
                                        placeholder="Enter Email Id." name="email" required>
                                </div>
                                <div class="col-md-6 mb-2">
                                    <label for="age">Enter Age</label>
                                    <input type="text" class="form-control form-control-sm mb-2" id="age"
                                        placeholder="Enter Age" name="age">
                                </div>
                                <div class="col-md-6 mb-2">
                                    <label for="gender">Select Gender</label>
                                    <select name="gender" class="form-control form-control-sm mb-2" id="gender">
                                        <option value=""></option>
                                        <option value="Male">Male</option>
                                        <option value="Femail">Femail</option>
                                        <option value="Other">Other</option>
                                    </select>
                                </div>
                                <div class="col-md-6 mb-2">
                                    City
                                    <input type="text" class="form-control form-control-sm mb-2" id="city" name="city"
                                        placeholder="Enter City Name">
                                </div>
                                <div class="col-md-6 mb-2">
                                    Ditrict
                                    <input type="text" class="form-control form-control-sm mb-2" id="dist" name="dist"
                                        placeholder="Enter District Name">
                                </div>
                                <div class="col-md-12 mb-2">
                                    <label for="address">Address</label>
                                    <textarea class="form-control form-control-sm" id="address"
                                        placeholder="Enter Full Address" name="address"></textarea>
                                </div>
                                <div class="col-md-6 mb-2">
                                    <label for="department">Department <span>*</span></label>
                                    <select name="category" id="DepartmentId" class="form-control mb-3" required>
                                        <option value="">Select Department</option>
                                        <?php 
                                            $sql = "SELECT * FROM clg_course_cat WHERE recycle ='1' ORDER BY sl_no ASC";
                                            $run = mysqli_query($con, $sql);
                                            while ( $rows = mysqli_fetch_assoc($run) ) {
                                                echo "<option value='$rows[sl_no]'>$rows[cat_name]</option>";
                                            }
                                        ?>
                                    </select>
                                </div>
                                <div class="col-md-6 mb-2">
                                    <label for="status">Status <span>*</span></label>
                                    <select name="status" id="status" class="form-control mb-3" required>
                                        <?php 
                                            $sql = "SELECT * FROM status WHERE s_by = '$uadmin' ORDER BY s_pos ASC";
                                            $run = mysqli_query($con, $sql);
                                            while ( $rows = mysqli_fetch_assoc($run) ) {
                                            echo "<option value='$rows[s_id]'>$rows[s_name]</option>";
                                            }
                                        ?>
                                    </select>
                                </div>
                                <div class="col-md-12 mb-2">
                                    <label for="abt">About</label>
                                    <textarea class="form-control form-control-sm" id="abt" name="abt"></textarea>
                                </div>
                                <div class="col-md-12 mb-2">
                                    <label for="abt2">About 2</label>
                                    <textarea class="form-control form-control-sm" id="abt2" name="abt2"
                                        rows="1"></textarea>
                                </div>
                                <div class="col-ms-12 mb-2">
                                    <label for="abt3">About 3</label>
                                    <textarea class="form-control form-control-sm mb-2" id="abt3" name="abt3"
                                        rows="1"></textarea>
                                </div>
                                <div class="col-ms-12 mb-2">
                                    <label for="abt4">About 4</label>
                                    <textarea class="form-control form-control-sm mb-2" id="abt4" name="abt4"
                                        rows="1"></textarea>
                                </div>
                                <div class="col-ms-12 mb-2">
                                    <label for="abt5">About 5</label>
                                    <textarea class="form-control form-control-sm mb-2" id="abt5" name="abt5"
                                        rows="1"></textarea>
                                </div>
                                <div class="col-ms-12 mb-2">
                                    <label for="abt6">About 6</label>
                                    <textarea class="form-control form-control-sm mb-2" id="abt6" name="abt6"
                                        rows="1"></textarea>
                                </div>
                                <div class="col-ms-12 mb-2">
                                    <label for="abt7">About 7</label>
                                    <textarea class="form-control form-control-sm mb-2" id="abt7" name="abt7"
                                        rows="1"></textarea>
                                </div>
                                <div class="col-ms-12 mb-2">
                                    <label for="abt8">About 8</label>
                                    <textarea class="form-control form-control-sm mb-2" id="abt8" name="abt8"
                                        rows="1"></textarea>
                                </div>
                            </div>

                            <button type="submit" class="btn btn-success btn-user m-2" name="submit_details">
                                <i class="fa fa-paper-plane"></i> Submit Details
                            </button>
                            <a href="index.php" class="btn btn-warning btn-user m-2"> <i class="fas fa-ban"></i>
                                Cancel </a>
                        </form>
                    </div>
                </div>
                <?php }?>
            </div>
        </div>

        <?php include "include/footer.php"; ?>
    </section>

    <!-- JavaScript Libraries -->

    <?php include "include/js.php"; ?>

</body>

</html>


<?php
if(isset($_POST['submit_details'])){
    $name = mysqli_real_escape_string($con, strip_tags($_POST['name']));
    $whatsapp = mysqli_real_escape_string($con, strip_tags($_POST['whatsapp']));
    $phone = mysqli_real_escape_string($con, strip_tags($_POST['phone']));
    $email = mysqli_real_escape_string($con, strip_tags($_POST['email']));     
    $quali = "";

    $status = mysqli_real_escape_string($con, strip_tags($_POST['status']));
    $laptop = "";
             
    $address = mysqli_real_escape_string($con, strip_tags($_POST['address']));
    $city = mysqli_real_escape_string($con, strip_tags($_POST['city']));
    $dist = mysqli_real_escape_string($con, strip_tags($_POST['dist']));

    $age = mysqli_real_escape_string($con, strip_tags($_POST['age']));
    $gender = mysqli_real_escape_string($con, strip_tags($_POST['gender']));

    $abt = mysqli_real_escape_string($con, strip_tags($_POST['abt']));
    $status = mysqli_real_escape_string($con, strip_tags($_POST['status']));
    $category = mysqli_real_escape_string($con, strip_tags($_POST['category']));
            
    $abt = mysqli_real_escape_string($con, strip_tags($_POST['abt']));
    $abt2 = mysqli_real_escape_string($con, strip_tags($_POST['abt2']));
    $abt3 = mysqli_real_escape_string($con, strip_tags($_POST['abt3']));
    $abt4 = mysqli_real_escape_string($con, strip_tags($_POST['abt4']));
    $abt5 = mysqli_real_escape_string($con, strip_tags($_POST['abt5']));
    $abt6 = mysqli_real_escape_string($con, strip_tags($_POST['abt6']));
    $abt7 = mysqli_real_escape_string($con, strip_tags($_POST['abt7']));
    $abt8 = mysqli_real_escape_string($con, strip_tags($_POST['abt8']));

    echo $sql = "INSERT INTO student_details (st_name,  st_ph, st_wp_no, st_email, st_age, st_gender, st_city, st_dist, st_address, st_qualification, st_about,  st_status, uploadDT, st_by, st_current_status, st_laptop, st_category, st_admin, st_about2,  st_about3,  st_about4,  st_about5,  st_about6,  st_about7, st_about8) 
    VALUES ('$name', '$phone', '$whatsapp','$email', '$age', '$gender','$city','$dist','$address','$quali','$abt','$status','$currentTime', '$_SESSION[uid]','$status','$laptop', '$category', '$uadmin', '$abt2','$abt3','$abt4','$abt5','$abt6','$abt7','$abt8')";

                
    if(mysqli_query($con, $sql)){
        ?>
<script>
alert("Record updated successfully");
window.location = 'status_view.php';
</script>
<?php }else{ ?>
<script>
alert('OOPS !!!!');
setTimeout(() => {
    window.location = 'index.php';
}, 500);
</script>
<?php }  } ?>