<?php
include('session_validator.php');
include("includes/db.php");

$alert = "";
if ($_SERVER["REQUEST_METHOD"] === "POST") {
    if (isset($_POST['submit'])) {
        $subject_code = $conn->real_escape_string($_POST["subject_code"]);
        $particulars = $_POST["particulars"];
        $display_seqs = $_POST["display_seq"];
        $marks = $_POST["marks"];
        $remarks = $_POST["remarks"];

        for ($i = 0; $i < count($particulars); $i++) {
            $p = $conn->real_escape_string($particulars[$i]);
            $d = intval($display_seqs[$i]);
            $m = floatval($marks[$i]);
            $r = $conn->real_escape_string($remarks[$i]);

            $sql = "INSERT INTO internal_evaluation (subject_code, particulars, display_seq, marks, remarks)
                    VALUES ('$subject_code', '$p', $d, $m, '$r')";
            if (!$conn->query($sql)) {
                $alert = "error";
                $error_message = $conn->error;
                break;
            }
        }
        if ($alert !== "error") {
            $alert = "success";
        }
    } elseif (isset($_POST['update'])) {
        $id = intval($_POST['eval_id']);
        $particulars = $conn->real_escape_string($_POST['edit_particulars']);
        $display_seq = intval($_POST['edit_display_seq']);
        $marks = floatval($_POST['edit_marks']);
        $remarks = $conn->real_escape_string($_POST['edit_remarks']);

        $sql = "UPDATE internal_evaluation SET particulars='$particulars', display_seq=$display_seq, marks=$marks, remarks='$remarks' WHERE id=$id";
        if ($conn->query($sql)) {
            $alert = "success";
        } else {
            $alert = "error";
            $error_message = $conn->error;
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Internal Evaluation</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <!-- CSS FILES -->
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/font-awesome.min.css">
    <link rel="stylesheet" href="css/sweetalert/sweetalert2.min.css">
    <link rel="stylesheet" href="css/main.css">
</head>

<body class="top-navbar-fixed">
<div class="main-wrapper">
    <?php include('includes/topbar.php'); ?>
    <div class="content-wrapper">
        <div class="content-container">
            <?php include('includes/leftbar.php'); ?>

            <div class="main-page">
                <div class="container-fluid">
                    <div class="row page-title-div">
                        <div class="col-md-6">
                            <h2 class="title">Add Internal Evaluation Record</h2>
                        </div>
                    </div>
                    <div class="row breadcrumb-div">
                        <div class="col-md-6">
                            <ul class="breadcrumb">
                                <li><a href="dashboard.php"><i class="fa fa-home"></i> Home</a></li>
                                <li>Internal Evaluation</li>
                                <li class="active">Add Record</li>
                            </ul>
                        </div>
                    </div>
                </div>

                <div class="container-fluid">
                    <div class="panel">
                        <div class="panel-heading">
                            <div class="panel-title"><h5>Enter Internal Evaluation Details</h5></div>
                        </div>
                        <div class="panel-body">
                            <form method="POST">
                                <div class="row">
                                <div class="form-group col-md-6">
                                    <label>Select Subject Code</label>
                                    <select name="subject_code" class="form-control" required>
                                        <option value="">-- Select Subject --</option>
                                        <?php
                                        $subs = $conn->query("SELECT subject_code, subject_name FROM subjects ORDER BY subject_name");
                                        while ($row = $subs->fetch_assoc()) {
                                            echo "<option value='{$row['subject_code']}'>{$row['subject_code']} - {$row['subject_name']}</option>";
                                        }
                                        ?>
                                    </select>
                                </div>
</div>
<div class="row">
                                <div id="row-wrapper">
                                    <div class="form-row row-item">
                                        <div class="form-group col-md-3">
                                            <input type="text" name="particulars[]" placeholder="Particulars" class="form-control" required>
                                        </div>
                                        <div class="form-group col-md-2">
                                            <input type="number" name="display_seq[]" placeholder="Display Seq" class="form-control" required>
                                        </div>
                                        <div class="form-group col-md-2">
                                            <input type="number" name="marks[]" step="0.01" placeholder="Marks" class="form-control" required>
                                        </div>
                                        <div class="form-group col-md-3">
                                            <input type="text" name="remarks[]" placeholder="Remarks" class="form-control">
                                        </div>
                                        <div class="form-group col-md-2">
                                            <button type="button" class="btn btn-success add-row">+</button>
                                        </div>
                                    </div>
                                </div>
                                </div>
                                <button type="submit" name="submit" class="btn btn-primary">Add Records</button>
                            </form>

                            <hr>

                            <!-- Modal -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <form method="POST">
        <div class="modal-header">
          <h5 class="modal-title" id="editModalLabel">Edit Record</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          <input type="hidden" name="eval_id" id="eval_id">
          <div class="form-group">
            <label>Particulars</label>
            <input type="text" name="edit_particulars" id="edit_particulars" class="form-control" required>
          </div>
          <div class="form-group">
            <label>Display Seq</label>
            <input type="number" name="edit_display_seq" id="edit_display_seq" class="form-control" required>
          </div>
          <div class="form-group">
            <label>Marks</label>
            <input type="number" step="0.01" name="edit_marks" id="edit_marks" class="form-control" required>
          </div>
          <div class="form-group">
            <label>Remarks</label>
            <input type="text" name="edit_remarks" id="edit_remarks" class="form-control">
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
          <button type="submit" name="update" class="btn btn-primary">Update</button>
        </div>
      </form>
    </div>
  </div>
</div>

                            <h4>Existing Internal Evaluation Records</h4>
                            <!-- Table and button for edit -->
<table class="table table-bordered table-striped">
  <thead>
    <tr>
      <th>#</th>
      <th>Subject Code</th>
      <th>Particulars</th>
      <th>Display Seq</th>
      <th>Marks</th>
      <th>Remarks</th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody>
    <?php
    $res = $conn->query("SELECT * FROM internal_evaluation ORDER BY subject_code, display_seq");
    $sn = 1;
    while ($row = $res->fetch_assoc()) {
        echo "<tr>
          <td>{$sn}</td>
          <td>{$row['subject_code']}</td>
          <td>{$row['particulars']}</td>
          <td>{$row['display_seq']}</td>
          <td>{$row['marks']}</td>
          <td>{$row['remarks']}</td>
          <td><button class='btn btn-sm btn-info edit-btn' 
                      data-id='{$row['id']}' 
                      data-particulars='{$row['particulars']}' 
                      data-display_seq='{$row['display_seq']}' 
                      data-marks='{$row['marks']}' 
                      data-remarks='{$row['remarks']}' 
                      data-toggle='modal' data-target='#editModal'>Edit</button></td>
        </tr>";
        $sn++;
    }
    ?>
  </tbody>
</table>

                        </div>
                    </div>
                </div> <!-- /.container-fluid -->
            </div>
        </div>
    </div>
</div>

<!-- JS FILES -->
<script src="js/jquery/jquery-2.2.4.min.js"></script>
<script src="js/bootstrap/bootstrap.min.js"></script>
<script src="js/pace/pace.min.js"></script>
<script src="js/lobipanel/lobipanel.min.js"></script>
<script src="js/iscroll/iscroll.js"></script>
<script src="js/prism/prism.js"></script>
<script src="js/select2/select2.min.js"></script>
<script src="js/main.js"></script>
<script src="js/sweetalert/sweetalert2.min.js"></script>
<script>
$(document).ready(function () {

  $('.edit-btn').on('click', function () {
    $('#eval_id').val($(this).data('id'));
    $('#edit_particulars').val($(this).data('particulars'));
    $('#edit_display_seq').val($(this).data('display_seq'));
    $('#edit_marks').val($(this).data('marks'));
    $('#edit_remarks').val($(this).data('remarks'));
  });

    // Add new row
    $('.panel-body').on('click', '.add-row', function () {
        let row = `<div class="form-row row-item">
            <div class="form-group col-md-3">
                <input type="text" name="particulars[]" class="form-control" placeholder="Particulars" required>
            </div>
            <div class="form-group col-md-2">
                <input type="number" name="display_seq[]" class="form-control" placeholder="Display Seq" required>
            </div>
            <div class="form-group col-md-2">
                <input type="number" name="marks[]" step="0.01" class="form-control" placeholder="Marks" required>
            </div>
            <div class="form-group col-md-3">
                <input type="text" name="remarks[]" class="form-control" placeholder="Remarks">
            </div>
            <div class="form-group col-md-2">
                <button type="button" class="btn btn-danger remove-row">-</button>
            </div>
        </div>`;
        $('#row-wrapper').append(row);
    });

    // Remove row
    $('.panel-body').on('click', '.remove-row', function () {
        $(this).closest('.row-item').remove();
    });

    <?php if ($alert === "success"): ?>
    Swal.fire({
        icon: 'success',
        title: 'Success!',
        text: 'Record(s) added successfully.',
        timer: 2000,
        showConfirmButton: false
    });
    <?php elseif ($alert === "error"): ?>
    Swal.fire({
        icon: 'error',
        title: 'Error!',
        text: '<?= addslashes($error_message) ?>',
        timer: 3000,
        showConfirmButton: false
    });
    <?php endif; ?>
});
</script>
</body>
</html>
