<?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';
        global $school_name;
        ?>
        <title>Add marks setting | <?php echo $school_name; ?> </title>
        <?php
        $module_name = 'Mark Setting';
        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 Marks Setting</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 Marks Setting</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-8">
                        <div class="callout callout-info" style="margin-top: 30px;">
                            <p class="text-teal">Note: Please make sure 1st CA, 2nd CA, 3rd CA and exam total add up to a total of 100 marks. Also note that any added mark setting here applies to the current year <?php echo date('Y'); ?> </p>
                        </div>
                        <div class="card card-secondary">
                            <div class="card-header">
                                <h3 class="card-title">Enter Marks Setting details below</h3>
                            </div>
                            <!-- /.card-header -->
                            <!-- form start -->
                            <form class="form-horizontal" method="post" id="marks-add">
                                <div class="card-body">
                                    <div class="form-group row">
                                        <label for="class" class="col-sm-3 col-form-label">Select Class <span style="color: #f00;">*</span></label>
                                        <div class="col-sm-9">
                                            <?php
                                            //fetch all classes
                                            $sql = "SELECT * FROM classes";
                                            $conn->query($sql);
                                            $details = $conn->fetchMultiple();

                                            ?>
                                            <select name="class" id="class" class="form-control select2" required>
                                                <?php
                                                if ($conn->rowCount() > 0) {
                                                    foreach ($details as $class) {
                                                        ?>
                                                    <option value="<?php echo $class->name; ?>">
                                                        <?php echo $class->name; ?> </option><?php
                                                    }
                                                } else {
                                                    ?>
                                                    <option value="">No classes found</option><?php
                                                }
                                                ?>
                                            </select>
                                        </div>
                                    </div>

                                    <div class="form-group row">
                                        <label for="term" class="col-sm-3 col-form-label">Select Term <span style="color: #f00;">*</span></label>
                                        <div class="col-sm-9">
                                            <?php
                                            //fetch all classes
                                            $sql = "SELECT * FROM terms";
                                            $conn->query($sql);
                                            $details = $conn->fetchMultiple();

                                            ?>
                                            <select name="term" id="term" class="form-control select2" required>
                                                <?php
                                                if ($conn->rowCount() > 0) {
                                                    foreach ($details as $terms) {
                                                        ?>
                                                    <option value="<?php echo $terms->name; ?>">
                                                        <?php echo $terms->name; ?> </option><?php
                                                    }
                                                } else {
                                                    ?>
                                                    <option value="">No terms found</option><?php
                                                }
                                                ?>
                                            </select>
                                        </div>
                                    </div>

                                    <div class="form-group row">
                                        <label for="1st_ca" class="col-sm-3 col-form-label">1st CA Total</label>
                                        <div class="col-sm-9">
                                            <input type="number" class="form-control" id="1st_ca" name="1st_ca" required>
                                        </div>
                                    </div>

                                    <div class="form-group row">
                                        <label for="2nd_ca" class="col-sm-3 col-form-label">2nd CA Total</label>
                                        <div class="col-sm-9">
                                            <input type="number" class="form-control" id="2nd_ca" name="2nd_ca" required>
                                        </div>
                                    </div>

                                    <div class="form-group row">
                                        <label for="3rd_ca" class="col-sm-3 col-form-label">3rd CA Total</label>
                                        <div class="col-sm-9">
                                            <input type="number" class="form-control" id="3rd_ca" name="3rd_ca" required>
                                        </div>
                                    </div>

                                    <div class="form-group row">
                                        <label for="exam" class="col-sm-3 col-form-label">Exam Total</label>
                                        <div class="col-sm-9">
                                            <input type="number" class="form-control" id="exam" name="exam" required>
                                        </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_mark_setting" value="Add Marks Settings" class="btn btn-info" id="submit">
                                        </div>
                                    </div>

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

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



                    </div>


                </div>


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

        });
    </script>

    <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">
        <?php include 'includes/styles.php'; ?>
        <title>Login to continue | <?php echo $school_name; ?> </title>

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

?>