<?php
session_start();
if(isset($_SESSION['username'])){
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">


  <?php include 'includes/styles.php'; ?>
    <title> Manage Psychomotor |  <?php echo $school_name; ?> </title>
    <?php
    $module_name = 'Psychomotor';
    include "check-admin-privilege.php";
    ?>

</head>
<body class="hold-transition sidebar-mini layout-fixed">
<div class="wrapper">

  <!-- Navbar -->
    <?php include 'includes/notifications.php'; ?>
  <!-- /.navbar -->

  <!-- Main Sidebar Container -->
<?php include "includes/sidebar.php";?>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <div class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1 class="m-0">Psychomotor</h1>
          </div><!-- /.col -->
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="<?php echo $conn->base_url();?>">Home</a></li>
              <li class="breadcrumb-item active">Manage Psychomotor</li>
            </ol>
          </div><!-- /.col -->
        </div><!-- /.row -->
      </div><!-- /.container-fluid -->
    </div>
    <!-- /.content-header -->

    <!-- Main content -->
    <section class="content">
      <div class="container-fluid">
   
        <div class="row">
          <section class="col-lg-12 connectedSortable">
           <a href="javascript:void(0);" data-toggle='modal' data-target='#add-psychomotor'><button class="btn btn-outline-success">Add Psychomotor </button></a>
          </section>
       
          <section class="col-lg-12 connectedSortable">
              <?php
              $sql = "SELECT *  FROM psychomotor";
              $conn->query($sql);
              $result = $conn->fetchMultiple();
              ?>
              <div class="card">
                  <div class="card-header">
                      <h3 class="card-title"> Manage Psychomotors </h3>
                  </div>
                  <!-- /.card-header -->
                  <div class="card-body">
                      <table id="teacher" class="table table-bordered table-striped">
                          <thead>
                          <tr>
                              <th>#</th>
                              <th>Name</th>
                              <th>Action</th>
                          </tr>
                          </thead>
                          <tbody>
                          <?php
                          $i = 1;
                          if($conn->rowCount()){
                              foreach ($result as $psychomotor):
                                  $name = $psychomotor->name;
                                  $psychomotor_id = $psychomotor->id;
                                  ?>
                                  <tr>
                                      <td><?php echo $i++; ?></td>
                                      <td><?php echo $name;?></td>
                                      <td>
                                          <a href="javascript:void();"  data-toggle='modal' data-target='#edit-psychomotor<?php echo $psychomotor_id;?>'>
                                              <i class="fa fa-edit" data-toggle="tooltip" title="Edit"></i>
                                          </a>&nbsp;

                                           <a href="javascript:void();" data-toggle='modal' data-target='#delete-psychomotor<?php echo $psychomotor_id;?>'>
                                              <i class="fa fa-trash" data-toggle="tooltip" title="Delete"></i>
                                          </a>
                                        
                                      </td>
                                  </tr>

                              <?php endforeach; ?>
                              <?php
                          }else{
                              ?> <tr>
                                  <td colspan="4" class="text-center">No Data in table</td>
                              </tr><?php
                          }

                          ?>
                          </tbody>
                          <tfoot>
                          <tr>
                               <th>#</th>
                              <th>Name</th>
                              <th>Action</th>
                          </tr>
                          </tfoot>
                      </table>
                  </div>
                  <!-- /.card-body -->
              </div>


          </section>
        </div>

          <div id="show-msg">
             
          </div>
        <!-- /.row (main row) -->
      </div><!-- /.container-fluid -->
    </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->
  <?php include "includes/footer.php";?>

           <!-- add psychmotor Modal start -->
            <div class="modal fade" id="add-psychomotor" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" data-backdrop='static' data-keyboard='false'>
                <div class="modal-dialog modal-md" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title" id="exampleModalLabel">Add New Psychomotor</h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"></button>
                        </div>
                        <div class="modal-body">
                            <form action="" method="post" id="psychomotor-add">
                                <div class="form-group">
                                    <label for="name">Enter Name</label>
                                    <input type="text" name="name" placeholder="Enter psychomotor name" class="form-control" required="" data-parsley-trigger='keyup' id="name">
                                </div>

                                <div class="modal-footer" style="margin-bottom: -10px;">
                                    <button class="btn" data-dismiss="modal"><i class="flaticon-cancel-12"></i> Cancel</button>
                                    <input type="submit" class="btn btn-outline-success" value="Add Psychomotor" id="submit" name="add_psychomotor">
                                </div>

                                 <div id="success-msg"></div>
                            </form>



                        </div>
                    </div>
                </div>
            </div>
         <!-- end psychmotor Modal  -->


  <?php 
   foreach ($result as $motor) {
          $name = $motor->name;
          $id = $motor->id;
     # run html delete modal
    ?>

    <!-- Delete member Modal start -->
        <div class="modal fade" id="delete-psychomotor<?php echo $id;?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" data-backdrop='static' data-keyboard='false'>
            <div class="modal-dialog modal-md" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="exampleModalLabel">Delete Psychomotor name <?php echo $name; ?></h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <h4>Are you sure you want to delete:  <span style="color: #f00;"><?php echo $name; ?>?</span></h4>
                        <form action="" method="post">
                            <div class="form-group">
                                <input type="hidden" name="id" value="<?php echo $id; ?>">
                                <div class="form-group">

                                </div>
                            </div>

                            <div class="modal-footer" style="margin-bottom: -10px;">
                                <button class="btn" data-dismiss="modal"><i class="flaticon-cancel-12"></i> Cancel</button>
                                <input type="submit" class="btn btn-outline-danger" value="Delete psychomotor" id="submit" name="delete_psychomotor">
                            </div>
                        </form>

                       

                    </div>
                </div>
            </div>
        </div>
        <!-- Delete member Modal end -->

         <!-- edit psychmotor Modal start -->
            <div class="modal fade" id="edit-psychomotor<?php echo $id;?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" data-backdrop='static' data-keyboard='false'>
                <div class="modal-dialog modal-md" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title" id="exampleModalLabel">Edit Psychomotor</h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"></button>
                        </div>
                        <div class="modal-body">
                            <form action="" method="post" id="psychomotor-edit">
                                <div class="form-group">
                                    <label for="name">Enter Name</label>
                                    <input type="text" name="name" placeholder="Enter psychomotor name" class="form-control" required="" data-parsley-trigger='keyup' id="name" value="<?php echo $name; ?>">

                                    <input type="hidden" name="item_id" value="<?php echo $id; ?>">
                                </div>

                                <div class="modal-footer" style="margin-bottom: -10px;">
                                    <button class="btn" data-dismiss="modal"><i class="flaticon-cancel-12"></i> Cancel</button>
                                    <input type="submit" class="btn btn-outline-primary" value="Update Psychomotor" id="submit" name="edit_psychomotor">
                                </div>

                                 <div id="success-msg"></div>
                            </form>



                        </div>
                    </div>
                </div>
            </div>
         <!-- end psychmotor Modal  -->

    <?php

   }

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

     $id = $_POST['id'];
  
   //delete teacher from database

     $sql = "DELETE  FROM psychomotor WHERE id =:id";
     $conn->query($sql);
     $conn->bind(":id", $id);
     try{

      $conn->execute();
        echo "<p class='alert alert-success alert-dismissible fade show' role='alert'>
                    <i class='fas fa-check-circle'></i> Item deleted successfully.
                  <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\">
                             <span aria-hidden=\"true\">×</span>
                  </button>
                   <meta http-equiv='refresh' content='3; manage-psychomotor'>
                  
           </p>";

     }catch(PDOException $err){

      echo $err->getMessage();

     }


   }

   if(isset($_POST['edit_psychomotor'])){
     $name = $_POST['name'];
     $item_id = $_POST['item_id'];

     //check if details aready exists

    $sql = "SELECT * FROM psychomotor WHERE name=:name";
    $conn->query($sql);
    $conn->bind(':name', $name);


    $rowCount = $conn->rowCount();

    if($rowCount > 0){

        echo "<p class='alert alert-danger alert-dismissible fade show text-center' role='alert'>
              <i class='fas fa-ban'></i>
               This psychomotor name already exists. Pls edit it to save changes.
                  <button type='button' class='close' data-dismiss='alert' aria-label='Close'>
                             <span aria-hidden='true'>×</span>
                  </button>
           </p>";

           return false;

    }else{
            //process  updating  of data into database

        $sql = "UPDATE psychomotor SET name =:name WHERE id =:id";
        $conn->query($sql);
        $conn->bind(":name", $name);
        $conn->bind(":id", $item_id);


        $send = $conn->execute();
        if ($send) {
            echo "<p class='alert alert-success alert-dismissible fade show' role='alert'>
                    <i class='fas fa-check-circle'></i> Psychomotor name was updated successfully.
                  <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\">
                             <span aria-hidden=\"true\">×</span>
                  </button>
                  <meta http-equiv='refresh' content='3; manage-psychomotor'>
           </p>";
        } else {
            echo "<p class='alert alert-danger alert-dismissible fade show' role='alert'>
                    <i class='fas fa-ban'></i> An error occurred while updating data.
                  <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\">
                             <span aria-hidden=\"true\">×</span>
                  </button>
                  <meta http-equiv='refresh' content='4; manage-psychomotor'>
           </p>";
        }
   


    }



   }

  ?>
    <script>
        $(function () {
            $("#teacher").DataTable({
                "responsive": true, "lengthChange": false, "autoWidth": false,
                "buttons": ["copy", "csv", "pdf", "print"]
            }).buttons().container().appendTo('#teacher_wrapper .col-md-6:eq(0)');
            $('#example2').DataTable({
                "paging": true,
                "lengthChange": false,
                "searching": true,
                "ordering": true,
                "info": true,
                "autoWidth": false,
                "responsive": true
            });
        });
    </script>
    <script>
        $(document).ready(function(){
            $('[data-toggle="tooltip"]').tooltip();
        });
    </script>

        <script>
        $('#psychomotor-add').parsley();
        $('#psychomotor-add').on('submit', function (event) {
            event.preventDefault();
            if ($('#psychomotor-add').parsley().isValid()) {
                $.ajax({
                    url: 'process-add-psychomotor.php',
                    method: 'POST',
                    data: new FormData(this),
                    contentType: false,
                    cache: false,
                    processData: false,
                    beforeSend: function () {
                        $('#submit').attr('disabled', 'disabled');
                        $('#submit').val('Saving psychomotor details, pls wait.....');
                    },
                    success: function (data) {
                        $('#psychomotor-add').parsley().reset();
                        $('#submit').attr('disabled', false);
                        $('#submit').val('Submit');
                        $('#success-msg').html(data);
                    }
                });
            }

        });
    </script>

    <?php
  }else{
      ?>
    <!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, maximum-scale=1, shrink-to-fit=no">
        <title>Login to continue |  <?php echo $school_name; ?> </title>

        <?php include 'includes/styles.php'; ?>

        <!-- END PAGE LEVEL PLUGINS/CUSTOM STYLES -->

    </head>
    <body>
    <div class="container" style="margin-top: 20%;">
        <div class="row">
            <div class="col-lg-12 col-md-12">
                <div class="section-title text-center">
                    <h6> You need to login to continue </h6>
                    <div class="spinner-border text-info" role="status">
                        <span class="sr-only">Loading...</span>
                        <meta http-equiv='refresh' content='3; <?php echo $link; ?>'>
                    </div>
                    <p>Redirecting to the login  page...</p>
                </div>
            </div>
        </div>
    </div>

    <?php
    }

  ?>
