<?php

session_name('small_admin_session');

session_start();

error_reporting(0);

include('includes/dbconnection.php');
include('includes/flash.php');

if (isset($_POST['submit'])) {

  $sid = $_SESSION['edid'];

  $studentname = $_POST['studentname'];

  $regno = $_POST['regno'];

  $sex = $_POST['sex'];

  $age = $_POST['age'];

  $stream = $_POST['stream'];

  $section = $_POST['section'];

  $class = $_POST['class'];





  $sql = "update students set studentName=:studentname,studentno=:regno,gender=:sex,age=:age,class=:class,stream=:stream,section=:section where id='$sid'";

  $query = $dbh->prepare($sql);

  $query->bindParam(':studentname', $studentname, PDO::PARAM_STR);

  $query->bindParam(':regno', $regno, PDO::PARAM_STR);

  $query->bindParam(':sex', $sex, PDO::PARAM_STR);

  $query->bindParam(':age', $age, PDO::PARAM_STR);

  $query->bindParam(':stream', $stream, PDO::PARAM_STR);

  $query->bindParam(':class', $class, PDO::PARAM_STR);

  $query->bindParam(':section', $section, PDO::PARAM_STR);





  $query->execute();

  if ($query->execute()) {

    echo "<script>alert('updated successfully.');window.location.href='student_list11.php';</script>"; exit;

  } else {

    echo "<script>alert('something went wrong, please try again later');</script>";

  }

}



if (isset($_POST['save'])) {

  $sid = $_SESSION['edid'];

  $parentname = $_POST['parentname'];

  $relation = $_POST['relation'];

  $occupation = $_POST['occupation'];

  $phone = $_POST['phone'];

  $contact = $_POST['contact'];

  $email = $_POST['email'];

  $sql = "update students set parentName=:parentname,relation=:relation,occupation=:occupation,contactno=:phone,nextphone=:contact,email=:email where id='$sid'";

  $query = $dbh->prepare($sql);

  $query->bindParam(':parentname', $parentname, PDO::PARAM_STR);

  $query->bindParam(':relation', $relation, PDO::PARAM_STR);

  $query->bindParam(':occupation', $occupation, PDO::PARAM_STR);

  $query->bindParam(':phone', $phone, PDO::PARAM_STR);

  $query->bindParam(':contact', $contact, PDO::PARAM_STR);

  $query->bindParam(':email', $email, PDO::PARAM_STR);

  $query->execute();

  if ($query->execute()) {

    echo "<script>alert('updated successfully.');window.location.href='student_list11.php';</script>"; exit;

  } else {

    echo "<script>alert('something went wrong, please try again later');</script>";

  }

}



if (isset($_POST['pass'])) {

  $sid = $_SESSION['edid'];

  $country = $_POST['country'];

  $district = $_POST['district'];

  $state = $_POST['state'];

  $village = $_POST['village'];

  $sql = "update students set country=:country,district=:district,state=:state,village=:village where id='$sid'";

  $query = $dbh->prepare($sql);

  $query->bindParam(':country', $country, PDO::PARAM_STR);

  $query->bindParam(':district', $district, PDO::PARAM_STR);

  $query->bindParam(':state', $state, PDO::PARAM_STR);

  $query->bindParam(':village', $village, PDO::PARAM_STR);

  $query->execute();

  if ($query->execute()) {

    echo "<script>alert('updated successfully.');window.location.href='student_list11.php';</script>"; exit;

  } else {

    echo "<script>alert('something went wrong, please try again later');</script>";

  }

}



if (isset($_POST['save2'])) {

  $sid = $_SESSION['edid'];

  $studentimage = $_FILES["studentimage"]["name"];

  move_uploaded_file($_FILES["studentimage"]["tmp_name"], "studentimages/" . $_FILES["studentimage"]["name"]);

  $sql = "update students set studentImage=:studentimage where id='$sid' ";

  $query = $dbh->prepare($sql);

  $query->bindParam(':studentimage', $studentimage, PDO::PARAM_STR);

  $query->execute();

  if ($query->execute()) {

    echo "<script>alert('updated successfully.');window.location.href='student_list11.php';</script>"; exit;

  } else {

    echo "<script>alert('something went wrong, please try again later');</script>";

  }

}



if (isset($_POST['save3'])) {

  $sid = $_SESSION['edid'];

  $new_password = $_POST['password'];

  $password = password_hash($new_password, PASSWORD_BCRYPT);

  $update_query = "UPDATE students SET password = :password WHERE id = '$sid' ";

  $update_stmt = $dbh->prepare($update_query);

  $update_stmt->bindParam(':password', $password, PDO::PARAM_STR);

  $update_stmt->execute();

  if ($update_stmt->execute()) {

    echo "<script>alert('Password Reset successfully.');window.location.href='student_list11.php';</script>"; exit;

  } else {

    echo "<script>alert('something went wrong, please try again later');</script>";

  }

}

?>







<!-- Content Wrapper. Contains page content -->

<div class="card-body">

  <!-- Main content -->

  <section class="content">

    <div class="container-fluid">

      <div class="row">

        <?php

        $eid = $_POST['edit_id'];

        $ret = mysqli_query($con, "select * from  students where id='$eid'");

        $cnt = 1;

        while ($row = mysqli_fetch_array($ret)) {

          $_SESSION['edid'] = $row['id'];

        ?>

          <div class="col-md-3">

            <!-- Profile Image -->

            <div class="card card-primary card-outline">

              <div class="card-body box-profile">

                <div class="text-center">

                  <img class="img-circle"

                    src="studentimages/<?php echo htmlentities($row['studentImage']); ?>" width="150" height="150" class="user-image"

                    alt="User profile picture">

                </div>



                <h3 class="profile-username text-center"><?php echo $row['name']; ?></h3>







                <p class="text-muted text-center"><strong></strong></p>



                <ul class="list-group list-group-unbordered mb-3">

                  <li class="list-group-item">

                    <b>Email</b> <a class="float-right"><?php echo $row['email']; ?></a>

                  </li>

                  <li class="list-group-item">

                    <b>Contact No</b> <a class="float-right"><?php echo $row['contactno']; ?> </a>

                  </li>



                </ul>



              </div>

              <!-- /.card-body -->

            </div>

            <!-- /.card -->

          </div>

          <!-- /.col -->

          <div class="col-md-9">

            <div class="card">

              <div class="card-header p-2">

                <ul class="nav nav-pills">
                  <li class="nav-item"><a class="nav-link active" href="#password" data-toggle="tab">Update Password</a></li>

                </ul>

              </div><!-- /.card-header -->

              <div class="card-body">

                <div class="tab-content">

                  <div class="active tab-pane" id="password">

                    <div class="row">

                      <form role="form" id="" method="post" enctype="multipart/form-data" class="form-horizontal">

                        <input name="password" class="form-control" id="password" value="<?php echo $row['studentno'] ?? $row['studentRegNo']; ?>" type="hidden">

                        <p>Click the button below to reset the student password to: <strong><?php echo $row['studentno'] ?? $row['studentRegNo']; ?></strong></p>

                        <div class="modal-footer text-right">

                          <button type="submit" name="save3" class="btn btn-primary">Reset Password</button>

                        </div>



                      </form>

                    </div>

                  </div>




                  <!-- /.tab-pane -->

                <?php

              } ?>

                </div>

                <!-- /.tab-content -->

              </div><!-- /.card-body -->

  </section>

  <!-- /.content -->

</div>

<!-- /.content-wrapper -->