﻿﻿??????????????
﻿﻿??????????????
<?php
   include('session.php');
?>

<?php

include 'connection.php';

$courses = [];
$courseResult = $conn->query("SELECT id, course FROM course ORDER BY course ASC");

if ($courseResult && $courseResult->num_rows > 0) {
    while ($row = $courseResult->fetch_assoc()) {
        $courses[] = $row;
    }
}

if(isset($_GET['idk']))
{
     $delsql = "DELETE FROM placement_students WHERE id=".$_GET['idk'];
     $conn->query($delsql);
      exit;
}


// Fetch students, including the password hash for the edit functionality.
$studentsSql = "SELECT * FROM placement_students ORDER BY id ASC";
$result = $conn->query($studentsSql);
$a = 0;
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Add Customers | Xcel Training Institute</title>
    <!-- Favicon icon -->
    <link rel="icon" type="image/png" href="../images/logo-wide.png">
    <!-- Base Styling  -->
    <link rel="stylesheet" href="assets/main/css/fonts.css">
    <link rel="stylesheet" href="assets/main/css/style.css">
    <script src="https://code.jquery.com/jquery-3.6.4.min.js" integrity="sha256-oP6HI9z1XaZNBrJURtCoUT5SUnxFr8s3BzRl+cbzUq8=" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <style>
    .btn{
            background-color: red;
            border: none;
            color: white;
            padding: 5px 5px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 20px;
            margin: 4px 2px;
            cursor: pointer;
            border-radius: 20px;
        }
        .green{
            background-color: #199319;
        }
        .red{
            background-color: red;
        }
        .ct{
            margin-left: 10px
        }
        </style>
</head>

<body>
    <div id="main-wrapper" class="show">

      <?php
         include "sidebar.php";
      ?>

      <?php
         include "header.php";
      ?>

      <div class="content-body">
        <div class="warper container-fluid">
          <div class="new-patients main_container">

            <div class="row">
              <div class="col-lg-12">
                <div class="card">
                  <div class="card-header">
                    <!-- REPLACE: Title -->
                    <h4 class="card-title">Add Placement Student</h4>
                  </div>
                  <div class="card-body">
                    <div class="basic-form">
                      <!-- REPLACE: Form action and fields -->
                      <form action="insert_placement.php" method="post" enctype="multipart/form-data">
                        <div class="row">
                          <div class="col-xl-4">
  <div class="form-group">
    <label class="form-label">Student Name</label>
    <input type="text" id="student_name" class="form-control" placeholder="Student Name" name="student_name" required>
  </div>
</div>
<div class="col-xl-4">
  <div class="form-group">
    <label class="form-label">Admission No</label>
    <input type="text" id="admission_no" class="form-control" placeholder="Admission No" name="admission_no" required>
  </div>
</div>
<div class="col-xl-4">
  <div class="form-group">
    <label class="form-label">Roll No</label>
    <input type="text" id="roll_no" class="form-control" placeholder="Roll No" name="roll_no" required>
  </div>
</div>
<div class="col-xl-4">
  <div class="form-group">
    <label class="form-label">Father Name</label>
    <input type="text" id="father_name" class="form-control" placeholder="Father Name" name="father_name" required>
  </div>
</div>


<div class="col-xl-4">
  <div class="form-group">
    <label class="form-label">Course</label>
    <select class="form-control" name="course" id="course" required>
      <option value="">-- Select Course --</option>
      <?php foreach ($courses as $course): ?>
        <option value="<?php echo htmlspecialchars($course['course']); ?>">
          <?php echo htmlspecialchars($course['course']); ?>
        </option>
      <?php endforeach; ?>
    </select>
  </div>
</div>




 


<div class="col-xl-4">
  <div class="form-group">
    <label class="form-label">Student Email</label>
    <input type="email" id="email" class="form-control" placeholder="Student Email" name="email" required>
  </div>
</div>
<div class="col-xl-4">
  <div class="form-group">
    <label class="form-label">Student Mobile</label>
    <input type="text" id="mobile" class="form-control" placeholder="Student Mobile" name="mobile" required>
  </div>
</div>
<!-- ADDED: Password field -->
<div class="col-xl-4">
  <div class="form-group">
    <label class="form-label">Photo Preview</label><br>
    <img id="photo_preview" src="" alt="Photo" style="width:120px;height:120px;border:1px solid #ccc;display:none;">
  </div>
</div>
<input type="hidden" name="existing_photo" id="existing_photo">

<!-- ADDED: Company Name field -->
<div class="col-xl-4">
  <div class="form-group">
    <label class="form-label">Company Name</label>
    <input type="text" id="company_name" class="form-control" placeholder="Company Name" name="company_name" required>
  </div>
</div>
<!-- ADDED: salary field -->
<div class="col-xl-4">
  <div class="form-group">
    <label class="form-label">Salary</label>
    <input type="text" id="salary" class="form-control" placeholder="Salary" name="salary" required>
  </div>
</div>
<!-- ADDED: Location field -->
<div class="col-xl-4">
  <div class="form-group">
    <label class="form-label">Location</label>
    <input type="text" id="location" class="form-control" placeholder="Location" name="location" required>
  </div>
</div>
                        </div>
                        <div class="form-group text-right">
                          <button type="submit" class="btnn float-end">Create</button>
                        </div>
                      </form>
                      </div>
                    </div>
                  </div>
                </div>
              </div>

              <div class="col-md-12">
                <div class="card shadow">
                  <div class="card-header">
                    <!-- REPLACE: Title -->
                    <h4 class="card-title">Students</h4>
                  </div>
                  <div class="card-body">
                    <div class="table-responsive">
                      <!-- REPLACE: Table structure and data -->
                      <table id="example1" class="display nowrap">
  <thead>
    <tr>
      <th>SL No</th>
      <th>Photo</th>
      <th>Student Name</th>
      <th>Admission No</th>
      <th>Roll No</th>
      <th>Father Name</th>
      <th>Course</th>
      <th>Email</th>
      <th>Mobile</th>
      <th>Company Name</th>
      <th>Salary</th>
      <th>Location</th>
      <th>Action</th>
      <th>Active Status</th>
    </tr>
  </thead>

  <tbody>
    <?php while ($rows = $result->fetch_assoc()) { ?>
      <tr id="<?= $rows['id']; ?>">

        <td><?= ++$a; ?></td>

        <!-- PHOTO -->
        <td>
          <?php if (!empty($rows['photo']) && file_exists($rows['photo'])) { ?>
            <img src="<?= htmlspecialchars($rows['photo']); ?>"
                 style="width:50px;height:50px;border-radius:50%;">
          <?php } else { ?>
            <span>No Photo</span>
          <?php } ?>
        </td>

        <td><?= htmlspecialchars($rows['student_name']); ?></td>
        <td><?= htmlspecialchars($rows['admission_no']); ?></td>
        <td><?= htmlspecialchars($rows['roll_no']); ?></td>
        <td><?= htmlspecialchars($rows['father_name']); ?></td>
        <td><?= htmlspecialchars($rows['course']); ?></td>
        <td><?= htmlspecialchars($rows['email']); ?></td>
        <td><?= htmlspecialchars($rows['mobile']); ?></td>
        <td><?= htmlspecialchars($rows['company_name']); ?></td>
        <td><?= htmlspecialchars($rows['salary']); ?></td>
        <td><?= htmlspecialchars($rows['location']); ?></td>

        <!-- ACTION -->
        <td>
          <a href="#"
             class="edit"
             data-id="<?= $rows['id']; ?>"
             data-student_name="<?= htmlspecialchars($rows['student_name'], ENT_QUOTES); ?>"
             data-admission_no="<?= htmlspecialchars($rows['admission_no'], ENT_QUOTES); ?>"
             data-roll_no="<?= htmlspecialchars($rows['roll_no'], ENT_QUOTES); ?>"
             data-father_name="<?= htmlspecialchars($rows['father_name'], ENT_QUOTES); ?>"
             data-course="<?= htmlspecialchars($rows['course'], ENT_QUOTES); ?>"
             data-email="<?= htmlspecialchars($rows['email'], ENT_QUOTES); ?>"
             data-mobile="<?= htmlspecialchars($rows['mobile'], ENT_QUOTES); ?>"
             data-company_name="<?= htmlspecialchars($rows['company_name'], ENT_QUOTES); ?>"
             data-salary="<?= htmlspecialchars($rows['salary'], ENT_QUOTES); ?>"
             data-location="<?= htmlspecialchars($rows['location'], ENT_QUOTES); ?>"
             data-photo="<?= htmlspecialchars($rows['photo'], ENT_QUOTES); ?>">
             <span class="fas fa-edit"></span>
          </a>

           

          <!-- DELETE -->
          <a class="remove ct"
             href="#"
             data-id="<?= $rows['id']; ?>"
             title="DELETE">
            <span class="fas fa-trash-alt"></span>
          </a>
        </td>

        <!-- STATUS -->
        <td>
          <?php if ($rows['status'] == 1) { ?>
            <a href="toggle_placement_status.php?id=<?= $rows['id']; ?>&action=deactivate"
               class="btn red">Deactivate</a>
          <?php } else { ?>
            <a href="toggle_placement_status.php?id=<?= $rows['id']; ?>&action=activate"
               class="btn green">Activate</a>
          <?php } ?>
        </td>

      </tr>
    <?php } ?>
  </tbody>
</table>

                      </div>
                    </div>
                  </div>
                </div>
              </div>

            </div>

          </div>
        </div>
      </div>

      <?php
        include('footer.php');
      ?>

    </div>

    <!-- INSERT: Edit Student Modal -->
    <div class="modal fade" id="editStudentModal" tabindex="-1" role="dialog" aria-labelledby="editStudentModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">

      <form id="editStudentForm" enctype="multipart/form-data">

        <div class="modal-header">
          <h5 class="modal-title" id="editStudentModalLabel">Edit Placement Student</h5>
          <button type="button" class="close" data-dismiss="modal">
            <span>×</span>
          </button>
        </div>

        <div class="modal-body">
          <input type="hidden" name="id" id="edit_id">
          <input type="hidden" name="existing_photo" id="edit_existing_photo">

          <div class="row">

            <div class="col-xl-4">
              <div class="form-group">
                <label class="form-label">Student Name</label>
                <input type="text" class="form-control" name="student_name" id="edit_student_name" required>
              </div>
            </div>

            <div class="col-xl-4">
              <div class="form-group">
                <label class="form-label">Admission No</label>
                <input type="text" class="form-control" name="admission_no" id="edit_admission_no" required>
              </div>
            </div>

            <div class="col-xl-4">
              <div class="form-group">
                <label class="form-label">Roll No</label>
                <input type="text" class="form-control" name="roll_no" id="edit_roll_no" required>
              </div>
            </div>

            <div class="col-xl-4">
              <div class="form-group">
                <label class="form-label">Father Name</label>
                <input type="text" class="form-control" name="father_name" id="edit_father_name" required>
              </div>
            </div>

            <!-- Course dropdown -->
            <div class="col-xl-4">
              <div class="form-group">
                <label class="form-label">Course</label>
                <select class="form-control" name="course" id="edit_course" required>
                  <option value="">-- Select Course --</option>
                  <?php foreach ($courses as $course): ?>
                    <option value="<?= htmlspecialchars($course['course']); ?>">
                      <?= htmlspecialchars($course['course']); ?>
                    </option>
                  <?php endforeach; ?>
                </select>
              </div>
            </div>

            <div class="col-xl-4">
              <div class="form-group">
                <label class="form-label">Student Email</label>
                <input type="email" class="form-control" name="email" id="edit_email" required>
              </div>
            </div>

            <div class="col-xl-4">
              <div class="form-group">
                <label class="form-label">Student Mobile</label>
                <input type="text" class="form-control" name="mobile" id="edit_mobile" required>
              </div>
            </div>

            <!-- Company Name -->
            <div class="col-xl-4">
              <div class="form-group">
                <label class="form-label">Company Name</label>
                <input type="text" class="form-control" name="company_name" id="edit_company_name" required>
              </div>
            </div>
            <!-- Salary -->
            <div class="col-xl-4">
              <div class="form-group">
                <label class="form-label">Salary</label>
                <input type="text" class="form-control" name="salary" id="edit_salary" required>
              </div>
            </div>

            <!-- Location -->
            <div class="col-xl-4">
              <div class="form-group">
                <label class="form-label">Location</label>
                <input type="text" class="form-control" name="location" id="edit_location" required>
              </div>
            </div>

            <!-- Photo preview -->
            <div class="col-xl-4">
              <div class="form-group">
                <label class="form-label">Current Photo</label><br>
                <img id="edit_photo_preview" src="" style="width:120px;height:120px;border:1px solid #ccc;">
              </div>
            </div>

            <!-- Change photo -->
            <div class="col-xl-4">
              <div class="form-group">
                <label class="form-label">Change Photo (optional)</label>
                <input type="file" class="form-control" name="photo" id="edit_photo">
              </div>
            </div>

          </div>
        </div>

        <div class="modal-footer">
          <button type="button" class="btnn" data-dismiss="modal">Close</button>
          <button type="submit" class="btnn">Save Changes</button>
        </div>

      </form>

    </div>
  </div>
</div>


    <!-- REPLACE: JS handlers -->
    <script type="text/javascript">
$(document).ready(function () {

  // ============================
  // OPEN EDIT MODAL
  // ============================
  $(".edit").on("click", function (e) {
    e.preventDefault();

    var $el = $(this);

    $("#edit_id").val($el.data("id"));
    $("#edit_student_name").val($el.data("student_name"));
    $("#edit_admission_no").val($el.data("admission_no"));
    $("#edit_roll_no").val($el.data("roll_no"));
    $("#edit_father_name").val($el.data("father_name"));
    $("#edit_course").val($el.data("course"));
    $("#edit_email").val($el.data("email"));
    $("#edit_mobile").val($el.data("mobile"));
    $("#edit_company_name").val($el.data("company_name"));
    $("#edit_salary").val($el.data("salary"));
    $("#edit_location").val($el.data("location"));

    // ============================
    // PHOTO HANDLING
    // ============================
    var photoPath = $el.data("photo");

    if (photoPath && photoPath !== "") {
      $("#edit_photo_preview").attr("src", photoPath).show();
      $("#edit_existing_photo").val(photoPath);
    } else {
      $("#edit_photo_preview").hide();
      $("#edit_existing_photo").val("");
    }

    // Clear file input
    $("#edit_photo").val("");

    $("#editStudentModal").modal("show");
  });

  // ============================
  // SUBMIT EDIT FORM
  // ============================
  $("#editStudentForm").on("submit", function (e) {
    e.preventDefault();

    var formData = new FormData(this);

    $.ajax({
      url: "update_placement.php",
      type: "POST",
      data: formData,
      contentType: false,
      processData: false,
      success: function (resp) {
        alert("Student updated successfully");
        location.reload();
      },
      error: function () {
        alert("Failed to update student");
      }
    });
  });

  // ============================
  // MODAL CLOSE HANDLER
  // ============================
  $(document).on(
    "click",
    "#editStudentModal .close, #editStudentModal [data-dismiss='modal']",
    function (e) {
      e.preventDefault();
      $("#editStudentModal").modal("hide");
    }
  );

});
</script>



<script type="text/javascript">
$(document).ready(function () {

  $("#admission_no").autocomplete({
    source: function (request, response) {
      $.ajax({
        url: "search_student.php",
        dataType: "json",
        data: { q: request.term },
        success: function (data) {
          response(data);
        },
        error: function () {
          response([]);
        }
      });
    },
    minLength: 2,

    select: function (event, ui) {

      $.ajax({
        url: "get_placement_details.php",
        type: "GET",
        dataType: "json",
        data: { admission_no: ui.item.value },

        success: function (data) {
          if (data) {

            $("#student_name").val(data.student_name || "");
            $("#father_name").val(data.father_name || "");
            $("#email").val(data.email || "");
            $("#mobile").val(data.mobile || "");
            $("#course").val(data.course || "");

            // =========================
            // PHOTO AUTO LOAD (Preview)
            // =========================
            if (data.photo && data.photo !== "") {
              $("#photo_preview")
                .attr("src", data.photo)
                .show();

              $("#existing_photo").val(data.photo);
            } else {
              $("#photo_preview").hide();
              $("#existing_photo").val("");
            }

          }
        },

        error: function () {
          alert("Error fetching student details");
        }
      });

    }
  });

});
</script>

<script>
    // Check for success parameter in URL
    $(document).ready(function() {
        const urlParams = new URLSearchParams(window.location.search);
        if (urlParams.has('success')) {
            alert("Placement student added successfully!");
            // Optional: Remove the parameter from URL so refresh doesn't show alert again
            window.history.replaceState({}, document.title, window.location.pathname);
        }
    });
</script>


    <!-- popper js -->
    <script src="assets/plugins/popper/popper.min.js"></script>

    <!-- Bootstrap -->
    <script src="assets/plugins/bootstrap/js/bootstrap.js"></script>

    <!-- Moment -->
    <script src="assets/plugins/moment/moment.min.js"></script>

    <!-- Date Range Picker -->
    <script src="assets/plugins/daterangepicker/daterangepicker.min.js"></script>

    <!-- Datatable -->
    <script src="assets/plugins/datatables/jquery.dataTables.min.js"></script>
    <script src="assets/js/init-tdatatable.js"></script>

    <!-- Chart js -->
    <script src="assets/plugins/chart/chart/Chart.min.js"></script>
    <script src="assets/js/charts-custom.js"></script>

    <!-- Main Custom JQuery -->
    <script src="assets/js/toggleFullScreen.js"></script>
    <script src="assets/js/main.js"></script>

</body>
</html>

<script type="text/javascript">
    $(".remove").click(function(){
        var id = $(this).parents("tr").attr("id");

        if(confirm('Are you sure to remove this record ?'))
        {
            $.post("delete_student.php", { id: id })
                .done(function() {
                    $("#"+id).remove();
                    alert("Record removed successfully");
                })
                .fail(function() {
                    alert("Failed to delete student");
                });
        }
    });

     
    
    </script>
