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

    <style>
        .card-title a {
            font-size: 16px;
            border-bottom: 1px solid #707478;
            color: #707478;
        }
        .card-title a:hover{
            border-bottom: 1px solid #1A2229;
            color: #1A2229;
        }
        .btn-app:first-child{
            margin-left:0 !important;
        }
    </style>


  <?php include 'includes/styles.php'; ?>
    <title>Report Card Comments|  <?php echo $school_name; ?> </title>
    <?php
    $module_name = 'Report Card Comments';
    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">Comments</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">comments</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="add-class"><button class="btn btn-outline-success">Add a class</button></a>-->
<!--          </section>-->
        <section class="col-lg-12 connectedSortable">
                        <div class="card">
                            <div class="card-header" style="background: #17a2b8; color: white;">
                                <h3 class="card-title" style="font-size: 15px;"> <i class="fa fa-star"></i> Manage Comments </h3>
                            </div>
                            <div class="card-header">
                                <h3 class="card-title"><a href="<?php echo $conn->base_url().'add-comment'; ?>"> <i class="fa fa-plus"></i> Add or edit comments</a> </h3>
                            </div>
                            <!-- /.card-header -->
                            <div class="card-body">
                                <div>
                                    <div class="card-header" style="background: #c3570f; color: white; margin: 20px 0 2px;">
                                        <h3 class="card-title" style="font-size: 17px;"> <i class="fa fa-star"></i> View comments </h3>
                                    </div>
                                    <table id="teacher" class="table table-bordered table-striped">
                                        <thead>
                                        <tr>
                                            <th>#</th>
                                            <th>From (%) </th>
                                            <th>Up to (%)</th>
                                            <th>Teacher Comment</th>
                                            <th>Principal Comment</th>
                                            <th>Action</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <?php
                                        $sql = "SELECT * FROM comments";
                                        $conn->query($sql);
                                        $result_details = $conn->fetchMultiple();
                                        $i = 1;
                                        if($conn->rowCount()){
                                            foreach ($result_details as $student_details):
                                                $comment_id = $student_details->id;
                                                $percent_from= $student_details->percent_from;
                                                $percent_upto= $student_details->percent_upto;
                                                $teacher_comment = $student_details->class_teacher_comment;
                                                $principal_comment = $student_details->principal_comment;

                                                ?>
                                                <tr>
                                                    <td><?php echo $i++; ?></td>
                                                    <td><?php echo $percent_from;?></td>
                                                    <td><?php echo $percent_upto;?></td>
                                                    <td><?php echo $teacher_comment;?></td>
                                                    <td><?php echo $principal_comment;?></td>
                                                    <td>
                                                        <a href="javascript:void();" data-toggle="modal" data-target="#delete-comment<?php echo $comment_id; ?>">
                                                            <i class="fa fa-trash" data-toggle="tooltip" title="Delete comment"></i>
                                                        </a>&nbsp;

                                                    </td>
                                                </tr>

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

                                        ?>
                                        </tbody>
                                        <tfoot>
                                        <tr>
                                            <th>#</th>
                                            <th>From (%) </th>
                                            <th>Up to (%)</th>
                                            <th>Teacher Comment</th>
                                            <th>Principal Comment</th>
                                            <th>Action</th>
                                        </tr>
                                        </tfoot>
                                    </table>

                                </div>
                                <?php
                                    foreach ($result_details as $student_details) {
                                        $comment_id = $student_details->id;
                                        # run html delete modal
                                        ?>
                                        <!-- Delete member Modal start -->
                                        <div class="modal fade" id="delete-comment<?php echo $comment_id;?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                                            <div class="modal-dialog modal-md" role="document">
                                                <div class="modal-content">
                                                    <div class="modal-header">
                                                        <h5 class="modal-title" id="exampleModalLabel">Delete comment </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 do this?
                                                            <br><small class="text-warning">This action will also delete both teacher and principal's comment.</small></h4>
                                                        <form action="" method="post">
                                                            <div class="form-group">
                                                                <input type="hidden" name="id" value="<?php echo $comment_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 Comment" id="submit" name="delete_comment">
                                                            </div>
                                                        </form>

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

                                        <?php

                                    }




                                ?>

                            </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";?>

    <?php
    if(isset($_POST['delete_comment'])){

        $id = $_POST['id'];

        //delete teacher from database

        $sql = "DELETE  FROM comments WHERE id =:id";
        $conn->query($sql);
        $conn->bind(":id", $id);
        try{
            $link = $base_url.'report-comments';
            $conn->execute();
            echo "<script>
                         toastr['success']('Comment deleted successfully.');
                         </script>  <meta http-equiv='refresh' content='3; $link'>";

        }catch(PDOException $err){

            echo $err->getMessage();

        }


    }

    ?>

    <script>
        $(document).ready(function(){
            $('[data-toggle="tooltip"]').tooltip();
        });
    </script>

    <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>

    <?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
    }

  ?>
