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

    $sql = "SELECT * FROM student_details WHERE st_no = '$_GET[id]'";
    $run = mysqli_query($con,$sql);
    $row = mysqli_fetch_assoc($run);
?>

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

<head>
    <?php include "include/meta.php"; ?>
    <?php include "include/css.php"; ?>
    <?php include "include/php-functions.php"; ?>
    <script>
    let SESS = [];
    SESS['uid'] = "<?php echo $_SESSION['uid']; ?>";
    SESS['uref'] = "<?php echo $_SESSION['uref']; ?>";
    SESS['uadmin'] = "<?php echo $uadmin;?>";
    // console.log(SESS)
    </script>
</head>

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

    <section id="inner-body">
        <div class="body-content">
            <div class="container-fluid">
                <div class="card mt-3">
                    <h5 class="card-header text-dark text-center font-weight-bold">Edit Student Details</h5>
                    <div class="card-body">
                        <form class="user" method="post" enctype="multipart/form-data">
                            <h4 class="mb-3 badge badge-secondary"> Student's Details</h4>
                            <div class="row justify-content-center ">
                                <div class="col-md-6 mb-2">
                                    Name <span>*</span>
                                    <input type="text" class="form-control form-control-sm mb-2" id="name"
                                        placeholder="" name="name" value="<?php echo $row['st_name'];  ?>" required>
                                </div>
                                <div class="col-md-6 mb-2">
                                    WhatsApp No.
                                    <input type="number" class="form-control form-control-sm mb-2" id="whatsapp"
                                        placeholder="" name="whatsapp" value="<?php echo $row['st_wp_no'];  ?>">
                                </div>
                                <div class="col-md-6 mb-2">
                                    Phone No. <span>*</span>
                                    <input type="number" class="form-control form-control-sm mb-2" id="phone"
                                        placeholder="" name="phone" value="<?php echo $row['st_ph'];  ?>" required>
                                </div>
                                <div class="col-md-6 mb-2">
                                    Email Id
                                    <input type="email" class="form-control form-control-sm mb-2" id="email"
                                        placeholder="" name="email" value="<?php echo $row['st_email'];  ?>">
                                </div>
                                <div class="col-md-6 mb-2">
                                    Age
                                    <input type="text" class="form-control form-control-sm mb-2" id="age" placeholder=""
                                        name="age" value="<?php echo $row['st_age'];  ?>">
                                </div>
                                <div class="col-md-6 mb-2">
                                    Gender
                                    <select name="gender" id="gender" class="form-control form-control-sm mb-3">
                                        <option value="<?php echo $row['st_gender'];  ?>">
                                            <?php echo $row['st_gender'];  ?></option>
                                        <option value="Male">Male</option>
                                        <option value="Female">Female</option>
                                        <option value="Others">Others</option>
                                    </select>
                                </div>
                                <div class="col-md-6 mb-2">
                                    City
                                    <input type="text" class="form-control form-control-sm mb-2" id="city"
                                        placeholder="" name="city" value="<?php echo $row['st_city'];  ?>">
                                </div>
                                <div class="col-md-6 mb-2">
                                    Ditrict
                                    <input type="text" class="form-control form-control-sm mb-2" id="dist"
                                        placeholder="" name="dist" value="<?php echo $row['st_dist'];  ?>">
                                </div>
                                <div class="col-md-12 mb-2">
                                    Full Address
                                    <textarea class="form-control form-control-sm mb-2" id="address"
                                        name="address"><?php echo $row['st_address'];  ?></textarea>
                                </div>
                                <div class="col-md-12 mb-2">
                                    About Student (Qualification)
                                    <textarea class="form-control form-control-sm mb-2" id="abt"
                                        name="abt"><?php echo $row['st_about'];  ?></textarea>
                                </div>
                                <div class="col-md-12 mb-2">
                                    About
                                    <textarea class="form-control form-control-sm mb-2" id="abt2"
                                        name="abt2"><?php echo $row['st_about2'];  ?></textarea>
                                </div>
                                <div class="col-md-12 mb-2">
                                    About
                                    <textarea class="form-control form-control-sm mb-2" id="abt3"
                                        name="abt3"><?php echo $row['st_about3'];  ?></textarea>
                                </div>
                                <div class="col-md-12 mb-2">
                                    About
                                    <textarea class="form-control form-control-sm mb-2" id="abt4"
                                        name="abt4"><?php echo $row['st_about4'];  ?></textarea>
                                </div>
                                <div class="col-md-12 mb-2">
                                    About
                                    <textarea class="form-control form-control-sm mb-2" id="abt5"
                                        name="abt5"><?php echo $row['st_about5'];  ?></textarea>
                                </div>
                                <div class="col-md-12 mb-2">
                                    About
                                    <textarea class="form-control form-control-sm mb-2" id="abt6"
                                        name="abt6"><?php echo $row['st_about6'];  ?></textarea>
                                </div>
                                <div class="col-md-12 mb-2">
                                    About
                                    <textarea class="form-control form-control-sm mb-2" id="abt7"
                                        name="abt7"><?php echo $row['st_about7'];  ?></textarea>
                                </div>
                                <div class="col-md-12 mb-2">
                                    About
                                    <textarea class="form-control form-control-sm mb-2" id="abt8"
                                        name="abt8"><?php echo $row['st_about8'];  ?></textarea>
                                </div>
                                <div class="col-md-6 mb-2">
                                    Department <span>*</span>
                                    <select name="category" id="category" class="form-control mb-3" required>
                                        <?php
                                            $cat_sql = mysqli_query($con, "SELECT * FROM clg_course_cat WHERE sl_no = '$row[st_category]' ");
                                            if($cat_row = mysqli_fetch_assoc($cat_sql)){
                                                $cat_name = $cat_row["cat_name"];
                                            }
                                            ?>
                                        <option value="<?php echo $row['st_category'];  ?>">
                                            <?php echo $cat_name;  ?> </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">
                                    Status <span>*</span>
                                    <select name="status" id="status" class="form-control mb-3" required>
                                        <?php
                                            $stat_sql = "SELECT * FROM status WHERE s_id = '{$row['st_status']}'";
                                            $stat_result = mysqli_query($con,$stat_sql);
                                            $stat_row = mysqli_fetch_assoc($stat_result);
                                            $stat = $stat_row['s_name'];
                                            ?>
                                        <option value="<?php echo $row['st_status'];  ?>">
                                            <?php echo $stat; ?> </option>
                                        <?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>

                            <button type="submit" class="btn btn-success btn-user my-2" name="update_details">
                                <i class="fa fa-upload"></i> Update Details
                            </button>
                            <a href="all-Data.php" class="btn btn-warning btn-user text-ehite m-2"> <i
                                    class="fa fa-ban"></i>
                                Cancel </a>
                        </form>
                    </div>
                </div>
            </div>
        </div>

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

    <!-- JavaScript Libraries -->

    <?php include "include/js.php"; ?>
    <script>
    document.getElementById('state-id').addEventListener('input', function() {
        var inputVal = this.value.trim();
        var dataList = document.getElementById('state-list');
        var hiddenInput = document.getElementById('state-hidden-id');
        var stateId = '';

        for (var option of dataList.options) {
            if (option.value === inputVal) {
                stateId = option.getAttribute('data-id');
                break;
            }
        }

        hiddenInput.value = stateId;

        if (stateId) {
            fetchDistricts(stateId);
        }
    });

    function fetchDistricts(stateId) {
        var xhr = new XMLHttpRequest();
        xhr.open("POST", "fetch_districts.php", true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4 && xhr.status === 200) {
                document.getElementById('dist-list').innerHTML = xhr.responseText;
            }
        };

        xhr.send("state_id=" + stateId);
    }

    document.getElementById('dist-id').addEventListener('input', function() {
        var inputVal = this.value.trim();
        var dataList = document.getElementById('dist-list');
        var hiddenInput = document.getElementById('dist-hidden-id');
        var distId = '';

        for (var option of dataList.options) {
            if (option.value === inputVal) {
                distId = option.getAttribute('data-id');
                break;
            }
        }

        hiddenInput.value = distId;
    });
    </script>

</body>

</html>


<?php
if(isset($_POST['update_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']));

             
    $address = mysqli_real_escape_string($con, strip_tags($_POST['address']));
    $city = mysqli_real_escape_string($con, strip_tags($_POST['state_id']));
    if(empty($city)){
        $city = $row['st_city'];
    }
    $dist = mysqli_real_escape_string($con, strip_tags($_POST['district_id']));
    if(empty($dist)){
        $dist = $row['st_dist'];
    }

    $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']));
    $status = mysqli_real_escape_string($con, strip_tags($_POST['status']));
    $category = mysqli_real_escape_string($con, strip_tags($_POST['category']));

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


    $sql = " UPDATE student_details SET st_name='$name', st_ph='$phone', st_wp_no='$whatsapp', st_email='$email', st_age='$age', st_gender='$gender', st_city='$city', st_dist='$dist', st_address='$address', st_about='$abt', st_about2='$abt2', st_about3='$abt3', st_about4='$abt4', st_about5='$abt5', st_about6='$abt6', st_about7='$abt7', st_about8='$abt8', st_status='$status', st_category='$category' WHERE st_no = '$_GET[id]'";

                
    if(mysqli_query($con, $sql)){
        ?>
<script>
alert("Record updated successfully. <?php echo $city . ' ' . $dist; ?>");
window.location = "edit_candidate.php?id=<?php echo $_GET['id'] ?>";
</script>
<?php } else{?>
<script>
alert('OOPS !!!!');
setTimeout(() => {
    window.location = "edit_candidate.php?id=<?php echo $_GET['id'] ?>";
}, 500);
</script>
<?php } } ?>