<?php
include('session_validator.php');
?>
<!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>SMS Admin Subject Creation</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" media="screen">
    <link rel="stylesheet" href="css/font-awesome.min.css" media="screen">
    <link rel="stylesheet" href="css/animate-css/animate.min.css" media="screen">
    <link rel="stylesheet" href="css/lobipanel/lobipanel.min.css" media="screen">
    <link rel="stylesheet" href="css/prism/prism.css" media="screen">
    <link rel="stylesheet" href="css/select2/select2.min.css">
    <link rel="stylesheet" href="css/main.css" media="screen">
    <script src="js/modernizr/modernizr.min.js"></script>
</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">Subject Creation</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> Subjects </li>
                                    <li class="">Create Subject</li>
                              

                                <button style="margin-left: 10%;" type="button" class="btn btn-primary mb-3" data-toggle="modal" data-target="#addSubjectModal">
                                    <i class="fa fa-plus"></i> Add New Subject
                                </button>
                                  </ul>
                            </div>
                        </div>
                    </div>

                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-md-12">
                                

                                <div class="panel">
                                    <div class="panel-heading">
                                        <div class="panel-title">
                                            <h5>Subjects List</h5>
                                        </div>
                                    </div>
                                    <div class="panel-body">
                                        <div class="table-responsive">
                                            <table id="subjectsTable" class="table table-striped table-bordered">
                                                <thead class="thead-dark">
                                                    <tr>
                                                        <th>ID</th>
                                                        <th>Subject Code</th>
                                                        <th>Subject Name</th>
                                                        <th>Theory Marks</th>
                                                        <th>Internal Marks</th>
                                                        <th>Theory Credit</th>
                                                        <th>Internal Credit</th>
                                                        <th>Type</th>
                                                        <th>Action</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php
                                                    include("includes/db.php");
                                                    $query = "SELECT * FROM subjects";
                                                    $result = $conn->query($query);

                                                    while ($row = $result->fetch_assoc()) {
                                                        echo "<tr>
                                                                <td>{$row['id']}</td>
                                                                <td>{$row['subject_code']}</td>
                                                                <td>{$row['subject_name']}</td>
                                                                <td>{$row['theory_full_marks']}</td>
                                                                <td>{$row['internal_full_marks']}</td>
                                                                <td>{$row['theory_credit_hour']}</td>
                                                                <td>{$row['internal_credit_hour']}</td>
                                                                <td><span class='badge badge-" . ($row['type'] == 'active' ? 'success' : 'warning') . "'>{$row['type']}</span></td>
                                                                <td>
                                                                    <button class='btn btn-success btn-sm edit-btn' data-id='{$row['id']}' data-code='{$row['subject_code']}'
                                                                    data-name='{$row['subject_name']}'
                                                                    data-theory='{$row['theory_full_marks']}'
                                                                    data-internal='{$row['internal_full_marks']}'
                                                                    data-thcredit='{$row['theory_credit_hour']}'
                                                                    data-incredit='{$row['internal_credit_hour']}'
                                                                    data-type='{$row['type']}'

                                                                        data-toggle='modal' data-target='#editSubjectModal'>
                                    <i class='fa fa-edit'></i> Edit
                                </button>
                                                                </td>
                                                            </tr>";
                                                    }
                                                    $conn->close();
                                                    ?>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
<!-- <button class='btn btn-success btn-sm edit-btn' data-id='{$row['id']}' data-code='{$row['subject_code']}' data-name='{$row['subject_name']}' data-toggle='modal' data-target='#editSubjectModal'>
                                    <i class='fa fa-edit'></i> Edit
                                </button> -->
                    <!-- Add Subject Modal -->
                    <div class="modal fade" id="addSubjectModal" tabindex="-1" role="dialog" aria-labelledby="addSubjectModalLabel" aria-hidden="true">
                        <div class="modal-dialog" role="document">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <h5 class="modal-title" id="addSubjectModalLabel">Add New Subject</h5>
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                        <span aria-hidden="true">&times;</span>
                                    </button>
                                </div>
                                <div class="modal-body">
                                    <form method="POST" action="process_subject.php">
                                        <div class="form-group">
                                            <label for="subjectCode">Subject Code:</label>
                                            <input type="text" id="subjectCode" name="subjectCode" class="form-control" required>
                                        </div>

                                        <div class="form-group">
                                            <label for="subjectName">Subject Name:</label>
                                            <input type="text" id="subjectName" name="subjectName" class="form-control" required>
                                        </div>

                                        <div class="form-group">
                                            <label for="theory_full_marks">Theory Full Marks:</label>
                                            <input type="number" step="0.01" id="theory_full_marks" name="theory_full_marks" class="form-control" required>
                                        </div>

                                        <div class="form-group">
                                            <label for="internal_full_marks">Internal Full Marks:</label>
                                            <input type="number" step="0.01" id="internal_full_marks" name="internal_full_marks" class="form-control" required>
                                        </div>

                                        <div class="form-group">
    <label for="theory_credit_hour">Theory Credit Hour:</label>
    <input
        type="number"
        id="theory_credit_hour"
        name="theory_credit_hour"
        class="form-control"
        min="0"
        step="0.01"
        inputmode="decimal"
        required
    >
</div>

<div class="form-group">
    <label for="internal_credit_hour">Internal Credit Hour:</label>
    <input
        type="number"
        id="internal_credit_hour"
        name="internal_credit_hour"
        class="form-control"
        min="0"
        step="0.01"
        inputmode="decimal"
        required
    >
</div>


                                        <div class="form-group">
                                            <label for="sub_type">Subject Type:</label>
                                            <select name="sub_type" id="sub_type" class="form-control" required>
                                                <option value="active">Active</option>
                                                <option value="Non-Credit">Non-Credit</option>
                                            </select>
                                        </div>

                                        <button type="submit" class="btn btn-primary">Add Subject</button>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>

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

    <!-- Edit Subject Modal -->
    <div class="modal fade" id="editSubjectModal" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Edit Subject</h5>
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                </div>
                <div class="modal-body">
                    <form method="POST" action="update_subject.php">
                        <input type="hidden" name="subject_id" id="edit_subject_id">
                        <div class="form-group">
                            <label>Subject Code:</label>
                            <input type="text" name="subject_code" id="edit_subject_code" class="form-control" required>
                        </div>
                        <div class="form-group">
                            <label>Subject Name:</label>
                            <input type="text" name="subject_name" id="edit_subject_name" class="form-control" required>
                        </div>
                        <div class="form-group">
                            <label>Theory Full Marks:</label>
                            <input type="number" name="theory_full_marks" id="edit_theory_full_marks" class="form-control" required>
                        </div>
                        <div class="form-group">
                            <label>Internal Full Marks:</label>
                            <input type="number" name="internal_full_marks" id="edit_internal_full_marks" class="form-control" required>
                        </div>
                        <div class="form-group">
    <label>Theory Credit Hour:</label>
    <input
        type="number"
        name="theory_credit_hour"
        id="edit_theory_credit_hour"
        class="form-control"
        min="0"
        step="0.01"
        inputmode="decimal"
        required
    >
</div>

<div class="form-group">
    <label>Internal Credit Hour:</label>
    <input
        type="number"
        name="internal_credit_hour"
        id="edit_internal_credit_hour"
        class="form-control"
        min="0"
        step="0.01"
        inputmode="decimal"
        required
    >
</div>

                        <div class="form-group">
                            <label>Type:</label>
                            <select name="type" id="edit_type" class="form-control" required>
                                <option value="active">Active</option>
                                <option value="Non-Credit">Non-Credit</option>
                            </select>
                        </div>
                        <button type="submit" class="btn btn-success">Update Subject</button>
                    </form>
                </div>
            </div>
        </div>
    </div>

    
    <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>
        $(document).on("click", ".edit-btn", function () {
    var id = $(this).data("id");
    var sub_code = $(this).data("code");
    var sub_name = $(this).data("name");
    var th_marks = $(this).data("theory");
    var pr_marks = $(this).data("internal");
    var th_credit = $(this).data("thcredit");
    var pr_credit = $(this).data("incredit");
    var sub_type = $(this).data("type");

    $("#edit_subject_id").val(id);
    $("#edit_subject_code").val(sub_code);
    $("#edit_subject_name").val(sub_name);
    $("#edit_theory_full_marks").val(th_marks);
    $("#edit_internal_full_marks").val(pr_marks);
    $("#edit_theory_credit_hour").val(th_credit);
    $("#edit_internal_credit_hour").val(pr_credit);
    $("#edit_type").val(sub_type);
});

    </script>
    <script>
        $(document).ready(function () {
            $('#subjectsTable').DataTable();
            $(".js-states").select2();
        });
    </script>
</body>

</html>
