<?php

include('includes/session1.php');

error_reporting(0);

include('includes/dbconnection.php');
require_once 'includes/session_term_helper.php';
$stResult = resolveCurrentSessionTerm($con);
$currentSessionName = $stResult['session_name'];
$currentTermName    = $stResult['term_name'];

include('includes/flash.php');



if (!isset($_SESSION['sid'])) {

  header('location:logoutteacher.php');

  exit();

} else {

  $teacher_id = $_SESSION['sid'];

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

    $adno = $_POST['fullname'];

    $term = $_POST['term'];

    $session = $_POST['session'];

    $class = $_POST['class'];

    $subject = $_POST['subject'];

    $filename = $_FILES['file']['name'];

    $filetype = $_FILES['file']['type'];

    $filesize = $_FILES['file']['size'];

    $filedata = addslashes(file_get_contents($_FILES['file']['tmp_name']));

    $query = mysqli_query($con, "insert into  document(File_Name,Term,session,class,student_type,filename,file_type,file_size,file_data,subject_material,teacher_id,Subject_name) value('$adno','$term','$session','$class','All Student','$filename','$filetype' ,' $filesize' ,'$filedata','Yes','$teacher_id','$subject')") or die(mysqli_error($con));



    if ($query) {

      flash_alert('Document Upload Successfully');

      header("Location: add_document_teacher.php?tab_id=$tabSessionID"); exit;

      $msg = "";

    } else {

      flash_alert('Something Went Wrong. Please try again.');

    }

  }





  // Fetch classes

  $sql = "SELECT DISTINCT class FROM class_section ORDER BY class ASC";

  $result = $con->query($sql);





  // Fetch classes

  $sql1 = "SELECT DISTINCT session_name FROM school_sessions ORDER BY session_name DESC";

  $result1 = $con->query($sql1);



?>

  <!DOCTYPE html>

  <html>

  <?php @include("includes/head.php"); ?>



  <body class="hold-transition sidebar-mini layout-fixed">

    <div class="wrapper">

      <!-- Navbar -->

      <?php @include("includes/headert.php"); ?>

      <!-- /.navbar -->



      <!-- Main Sidebar Container -->

      <?php @include("includes/sidebart.php"); ?>



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

      <div class="content-wrapper">

        <!-- Content Header (Page header) -->

         <section class="content-header premium-header">

    <div class="container-fluid">

        <div class="header-glass-card">

            <div class="row align-items-center">

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

                    <div class="title-section">

                        <div class="icon-box bg-primary-gradient shadow-sm">

                            <i class="fas fa-folder-open"></i>

                        </div>

                        <div>

                            <h1 class="premium-title text-primary">

                                Document Management

                                <span class="premium-subtitle text-secondary">Add Document</span>

                            </h1>
                            <div class="mt-1">
                              <?php if ($currentSessionName): ?>
                                <span class="badge badge-primary mr-1">
                                  <i class="fas fa-calendar-alt"></i>
                                  <?= htmlspecialchars($currentSessionName) ?>
                                </span>
                              <?php endif; ?>
                              <?php if ($currentTermName): ?>
                                <span class="badge badge-success">
                                  <i class="fas fa-bookmark"></i>
                                  <?= htmlspecialchars($currentTermName) ?>
                                </span>
                              <?php else: ?>
                                <span class="badge badge-warning">
                                  <i class="fas fa-exclamation-triangle"></i> No active term
                                </span>
                              <?php endif; ?>
                            </div>

                        </div>

                    </div>

                </div>

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

                    <nav aria-label="breadcrumb">

                        <ol class="breadcrumb premium-breadcrumb">

                            <li class="breadcrumb-item"><a href="teacher_dashboard.php" class="text-primary">Home</a></li>

                            <li class="breadcrumb-item active" aria-current="page">Add Document</li>

                        </ol>

                    </nav>

                </div>

            </div>

        </div>

    </div>

</section>





        <!-- Main content -->

        <section class="content">

          <div class="container-fluid">

            <div class="row ">

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

                <!-- general form elements -->

                <div class="card card-primary">

                  <div class="card-header">

                    <h5 class="card-title font-weight-bold"><i class="fas fa-file-alt"></i> Add Subject Material</h5>

                  </div>

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

                  <!-- form start -->

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

                    <div class="card-body">

                      <span style="color: brown">

                        <h5>Document Details</h5>

                      </span>

                      <hr>

                      <div class="row">



                        <div class="form-group col-md-4">

                          <label for="doc_name">Document Name</label>

                          <input type="text" class="form-control" id="fullname" name="fullname" placeholder="Document Name" required>

                        </div>

                        <div class="form-group col-md-4">

                          <label for="session">Subject</label>

                          <select class="form-control" name="subject" id="subject" required>

                            <option value="">-- Select Subject--</option>

                            <?php

                            $query = $con->query("SELECT id, name FROM subject_name ORDER BY id DESC");

                            while ($row = $query->fetch_assoc()) {



                              echo "<option value='{$row['name']}'>{$row['name']}</option>";

                            }

                            ?>

                          </select>

                        </div>



                        <div class="form-group col-md-4">

                          <label for="age">Class</label>

                          <select type="select" class="form-control" id="class" name="class" required>

                            <option value=""></option>

                            <option value="All Students">All Students</option>

                            <?php while ($row = $result->fetch_assoc()): ?>

                              <option value="<?= htmlspecialchars($row["class"]) ?>"><?= htmlspecialchars($row["class"]) ?></option>

                            <?php endwhile; ?>

                          </select>

                          </select>

                        </div>





                      </div>

                      <div class="row">

                        <div class="form-group col-md-4">

                          <label for="sex">Session</label>

                          <select type="select" class="form-control" id="session" name="session" required>

                            <option value=""></option>

                            <option value="All">All Session</option>

                            <?php while ($row = $result1->fetch_assoc()): ?>

                              <option value="<?= htmlspecialchars($row["session_name"]) ?>"><?= htmlspecialchars($row["session_name"]) ?></option>

                            <?php endwhile; ?>

                          </select>

                        </div>

                        <div class="form-group col-md-4">

                          <label for="age">Term</label>

                          <select type="select" class="form-control" id="term" name="term">

                            <option value=""></option>

                            <?php

                            $query4 = "SELECT DISTINCT term_name FROM school_terms ORDER BY term_name ASC";

                            $result4 = $con->query($query4);

                            while ($row4 = $result4->fetch_assoc()) {

                              echo "<option value='{$row4['term_name']}'>{$row4['term_name']}</option>";

                            }

                            ?>

                          </select>

                        </div>









                        <div class="form-group col-md-4">

                          <label for="file">Upload File:</label>

                          <input type="file" id="file" name="file" accept=".pdf,.doc,.docx,.txt,.ppt,.png,.jpeg,.jpg" required class="form-control">

                        </div>



                      </div>







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

                      <div class="card-footer">

                        <center><button type="submit" name="submit" id="submit" class="btn btn-primary"><i class="fas fa-check-circle"></i> Submit</button></center>

                      </div>

                  </form>

                </div>

                <!-- /.card -->

              </div>

              <!-- /.col -->

            </div>

            <!-- /.row -->

          </div>

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

        </section>

        <!-- /.content -->

      </div>

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

      <?php @include("includes/footer.php"); ?>



    </div>





    <!-- ./wrapper -->

    <?php @include("includes/foot.php"); ?>



    <script src="dist/js/select2.min.js"></script>



    <script>

      // In your Javascript (external .js resource or <script> tag)

      $(document).ready(function() {

        $('#class').select2();



      });

      $(document).ready(function() {

        $('#st').select2();



      });

      $(document).ready(function() {

        $('#session').select2();



      });

      $(document).ready(function() {

        $('#term').select2();



      });

      $(document).ready(function() {

        $('#subject').select2();



      });

    </script>



  </body>



  </html>

<?php

} ?>