<?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>Add comment | <?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">Add comment </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">Add comment </li>
                        </ol>
                    </div><!-- /.col -->
                </div><!-- /.row -->
            </div><!-- /.container-fluid -->
        </div>
        <!-- /.content-header -->

        <!-- Main content -->
        <section class="content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-md-5">
                        <div class="card card-info">
                            <div class="card-header">
                                <h3 class="card-title"> Add new comment </h3>
                            </div>
                            <!-- /.card-header -->
                            <!-- form start -->
                            <form class="form-horizontal" method="post" id="comment-add">
                                <div class="card-body">
                                    <div class="form-group row">
                                        <label for="percent_from" class="col-sm-5 col-form-label">Percentage from <span style="color: #f00;">*</span></label>
                                        <div class="col-sm-7">
                                            <input type="number" min="0" class="form-control" id="percent_from" required data-parsley-trigger="keyup" name="percent_from" data-parsley-type="number">
                                        </div>
                                    </div>

                                    <div class="form-group row">
                                        <label for="percent_upto" class="col-sm-5 col-form-label">Percentage upto<span style="color: #f00;">*</span></label>
                                        <div class="col-sm-7">
                                            <input type="number" min="0" name="percent_upto" class="form-control" required data-parsley-trigger="keyup" id="percent_upto" data-parsley-type="number">
                                        </div>
                                    </div>

                                    <div class="form-group row">
                                        <label for="teacher_comment" class="col-sm-5 col-form-label">Teacher's Comment <span style="color: #f00;">*</span></label>
                                        <div class="col-sm-7">
                                            <input type="text" name="teacher_comment" class="form-control" required data-parsley-trigger="keyup" id="teacher_comment">
                                        </div>
                                    </div>

                                    <div class="form-group row">
                                        <label for="principal_comment" class="col-sm-5 col-form-label"> Principal's Comment <span style="color: #f00;">*</span></label>
                                        <div class="col-sm-7">
                                            <input type="text" name="principal_comment" class="form-control" required data-parsley-trigger="keyup" id="principal_comment">
                                        </div>
                                    </div>

                                    <div class="form-group row">
                                        <label for="" class="col-sm-3 col-form-label"></label>
                                        <div class="col-sm-9">
                                            <input type="submit" name="add_comment" value="Add Comment" class="btn btn-info" id="submit">
                                        </div>
                                    </div>

                                </div>
                                <div id="success-msg"></div>
                                <!-- /.card-body -->

                                <!-- /.card-footer -->
                            </form>
                        </div>
                        <!-- /.card -->

                    </div>

                    <div class="col-md-7">
                        <div class="card card-success">
                            <div class="card-header">
                                <h3 class="card-title"> Edit Comments </h3>
                            </div>
                            <div class="card-body">

                                    <?php
                                    //fetch added comments
                                    $sql = "SELECT * FROM comments";
                                    $conn->query($sql);
                                    $result = $conn->fetchMultiple();
                                    $row_count = $conn->rowCount();
                                    $count = 1;
                                    if($row_count > 0){
                                        foreach ($result as $comments){
                                            ?>
                                           <div class="col-12" id="accordion">
                                            <div class="card card-info card-outline">
                                                <a class="d-block w-100" data-toggle="collapse" href="#view-comment<?php echo $count; ?>">
                                                    <div class="card-header">
                                                        <h4 class="card-title w-100" style="color: #28a745;">
                                                            <span class="text-success" style="color: #000 !important; "> <?php echo $count .'.'; ?> </span><span>Edit comment for percentage between</span>
                                                            <?php echo $comments->percent_from . '-'. $comments->percent_upto; ?>
                                                        </h4>
                                                    </div>
                                                </a>
                                                <div id="view-comment<?php echo $count; ?>" class="collapse" data-parent="#accordion">
                                                    <div class="card-body">
                                                        <form action="" id="comment-update-form<?php echo $comments->id; ?>" method="post">

                                                            <div class="form-group row">
                                                                <label for="percent_from<?php echo $comments->id; ?>" class="col-sm-12 col-form-label">Percentage from <span style="color: #f00;">*</span></label>
                                                                <div class="col-sm-12">
                                                                    <input type="number" min="0" class="form-control" id="percent_from<?php echo $comments->id; ?>" required data-parsley-trigger="keyup" name="percentage_from" data-parsley-type="number" value="<?php echo $comments->percent_from ?>">
                                                                </div>
                                                            </div>

                                                            <div class="form-group row">
                                                                <label for="percent_upto<?php echo $comments->id; ?>" class="col-sm-12 col-form-label">Percentage upto<span style="color: #f00;">*</span></label>
                                                                <div class="col-sm-12">
                                                                    <input type="number" min="0" name="percentage_upto" class="form-control" required data-parsley-trigger="keyup" id="percent_upto<?php echo $comments->id; ?>" data-parsley-type="number" value="<?php echo $comments->percent_upto; ?>">
                                                                </div>
                                                            </div>

                                                            <div class="form-group row">
                                                                <label for="teacher_comment<?php echo $comments->id; ?>" class="col-sm-12 col-form-label"> Teacher's Comment <span style="color: #f00;">*</span></label>
                                                                <div class="col-sm-12">
                                                                    <input type="text" name="teachers_comment" class="form-control" required data-parsley-trigger="keyup" id="teacher_comment<?php echo $comments->id; ?>" value="<?php echo $comments->class_teacher_comment; ?>" style="height: 60px;">
                                                                </div>
                                                            </div>
                                                            <div class="form-group row">
                                                                <label for="principal_comment<?php echo $comments->id; ?>" class="col-sm-12 col-form-label"> Principal's Comment <span style="color: #f00;">*</span></label>
                                                                <div class="col-sm-12">
                                                                    <input type="text" name="principals_comment" class="form-control" required data-parsley-trigger="keyup" id="principal_comment<?php echo $comments->id; ?>" value="<?php echo $comments->principal_comment; ?>" style="height: 60px;">
                                                                </div>
                                                            </div>
                                                            <input type="hidden" name="comment_id" value="<?php echo $comments->id; ?>">

                                                            <div class="form-group row">
                                                                <label for="" class="col-sm-3 col-form-label"></label>
                                                                <div class="col-sm-9">
                                                                    <input type="submit" name="add_comment<?php echo $comments->id; ?>" value="Update Comment" class="btn btn-danger" id="submit<?php echo $comments->id; ?>">
                                                                </div>
                                                            </div>


                                                        </form>
                                                    </div>
                                                    <div id="success-msg<?php echo $comments->id; ?>"></div>
                                                </div>
                                            </div>
                                           </div>

                                            <?php
                                            $count++;
                                        }
                                    }else{
                                        //no comments added yet
                                        ?> <p class="text-warning text-center">No comments have been added yet.</p> <?php
                                    }
                                    ?>


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

                    </div>

                </div>

                <!-- /.row (main row) -->
            </div><!-- /.container-fluid -->
        </section>
        <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->
    <?php include "includes/footer.php";?>
    <script>
        $('#comment-add').parsley();
        $('#comment-add').on('submit', function (event) {
            event.preventDefault();
            if($('#comment-add').parsley().isValid()){
                $.ajax({
                    url: 'process-add-comment.php',
                    method: 'POST',
                    data: new FormData(this),
                    contentType: false,
                    cache: false,
                    processData: false,
                    beforeSend: function () {
                        $('#submit').attr('disabled', 'disabled');
                        $('#submit').val('Adding comment details, pls wait.....');
                    },
                    success: function (data) {
                        $('#comment-add').parsley().reset();
                        $('#submit').attr('disabled', false);
                        $('#submit').val('Add Comment');
                        $('#success-msg').html(data);
                    }
                });
            }

        });
    </script>
<?php
foreach ($result as $comments) {
    ?>
    <script>
        $('#comment-update-form<?php echo $comments->id; ?>').parsley();
        $('#comment-update-form<?php echo $comments->id; ?>').on('submit', function (event) {
            event.preventDefault();
            if($('#comment-update-form<?php echo $comments->id; ?>').parsley().isValid()){
                $.ajax({
                    url: 'process-update-comment.php',
                    method: 'POST',
                    data: new FormData(this),
                    contentType: false,
                    cache: false,
                    processData: false,
                    beforeSend: function () {
                        $('#submit<?php echo $comments->id; ?>').attr('disabled', 'disabled');
                        $('#submit<?php echo $comments->id; ?>').val('Updating comment details, pls wait.....');
                    },
                    success: function (data) {
                        $('#comment-update-form<?php echo $comments->id; ?>').parsley().reset();
                        $('#submit<?php echo $comments->id; ?>').attr('disabled', false);
                        $('#submit<?php echo $comments->id; ?>').val('Update comment');
                        $('#success-msg<?php echo $comments->id; ?>').html(data);
                    }
                });
            }

        });
    </script>

<?php
}

?>
    <script>
        $(document).ready(function(){
            $('.select2').select2();

        })

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

    ?>
