<?php

    session_start();

    include "../db.php";

    date_default_timezone_set('Asia/Kolkata');

    $currentTime = date('M j, Y g:i a');



    if ( !isset($_SESSION['uid']) && !isset($_SESSION['uref']) && !isset($_SESSION['utype']) && !isset($_SESSION['uname']) )

        header("Location: logout.php");

?>

<!DOCTYPE html>

<html lang="en">

<head>

 <?php include "includes/meta.php"; ?>        

    <?php include "includes/css.php"; ?>    

  



</head>

<body id="page-top" class="mt-55">



    <div style="margin-top: 130px;"></div>



      <?php include "includes/header2.php"; ?>

<div class="container">

                    <div class="row justify-content-center">

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

                        <div class="card shadow mt-3">

                        <h5 class="card-header text-dark  text-center font-weight-bold"> Add subject</h5>

                            <div class="card-body">

                                <form method="POST">

                                    <div class="row mt-4">

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

                                            <label class="badge badge-warning text-dark">Class</label>

                                            <select class="form-control" name="sc_class_id" id="sc_class_id">

                                                <option value="" selected disabled>Select Class..</option>

                                                <?php

                                                    $sql = "SELECT * FROM class WHERE uploadedby = '$_SESSION[uref]' AND cdisplay != 'disable'";

                                                    $run = mysqli_query($con, $sql); 

                                                    while($row = mysqli_fetch_assoc($run)){

                                                        echo "

                                                            <option value='$row[sid]'>$row[sclass]</option>

                                                        ";

                                                  

                                                    }

                                                ?>

                                            </select>

                                        </div>

                                    </div>

                                    <div class="row mt-4">

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

                                            <label class="badge badge-warning text-dark">subject</label>

                                            <input type="text" class="form-control" id="sc_name" placeholder="Enter subject" name="sc_name" required>

                                        </div>

                                    </div>

                                    <div class=" row justify-content-center">

                                        <button type="submit" class="btn btn-success text-sm-center mt-4" name="submit">

                                            <i class="fa fa-upload"></i> Upload

                                        </button>

                                    </div>

                                </form>

                            </div>

                        </div>

                    </div>

                </div>

            </div>

                <?php



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

    $sc_name = ucwords(mysqli_real_escape_string($con, strip_tags($_POST['sc_name'])));

    $sc_class_id = ucwords(mysqli_real_escape_string($con, strip_tags($_POST['sc_class_id'])));



    $ins_sql = "INSERT INTO subject (sc_name, sc_class_id, uploadedby, sc_date) VALUES ('$sc_name', '$sc_class_id', '$_SESSION[uref]', '$currentTime')";

        //  echo "<pre>";print_r($ins_sql);exit();





        if(mysqli_query($con, $ins_sql)){

            ?>

            <script>window.location = 'view-subject.php';</script>

            <?php

        }

        else{

            ?>

            <script>

                alert('OOPS !!!!');

                setTimeout(() => {

                    window.location = 'add-subject.php';

                }, 1200);

            </script>

            <?php

        }

    } 



?>

<div style="margin-top: 270px;"></div>

            <?php include "includes/ft.php"; ?>

            <?php include "includes/js.php"; ?>



    <script src="js/jquery.min.js"></script>

    <script src="js/bootstrap.min.js"></script>

    <script src="js/sb-admin-2.min.js"></script>

    <script src="js/sweetalert2.min.js"></script>

    <script src="js/custom.js"></script>



   

</body>

</html>