<?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 Fees <span id="showUserNameDetails"></span></h5>

                <div class="card-body">

                    <form method="POST">

                        

                        <div class="row" id="Show_InsertUserName">

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

                                <label class="badge badge-warning">Student User Name</label>

                                <input type="text" name="sname" id="userName" class="form-control">

                            </div>

                            <div class="col-md-6 mt-2">

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

                                <select class="form-control" name="sclass">

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

                                    <?php

                                    $result = mysqli_query($con,"SELECT * FROM class WHERE uploadedby = '$_SESSION[uref]'");

                                    while($row = mysqli_fetch_array($result)) {

                                    ?>

                                    <option value="<?php echo $row['sid'];?>"><?php echo $row["sclass"];?></option>

                                    <?php

                                    }

                                    ?>

                                </select>

                            </div>

                            

                        </div>



                        <div class="row">

                            <div class="col-md-6 mt-2">

                                <label class="badge badge-warning">Fees Date</label>

                                    

                                    <input type="date" name="s_date" class="form-control" required>

                                        

                            </div>

                            <div class="col-md-6 mt-2">

                            <label class="badge badge-warning">Total Amount</label>

                            <input type="text" class="form-control"name="amount" placeholder="Enter Amount" required>

                            </div>

                        </div>

                        <div class="row">

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

                                <label class="badge badge-warning">Receipt</label>

                                <input type="text" class="form-control" value="<?php echo mt_rand().'_'.time(); ?>" name="fref">

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

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

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

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



<script>

$(document).ready(function() {



    $('#userName').on('keyup', function() {

        var user_name = this.value;

        $("#classMsg").hide();

        $.ajax({

            url: "fees-add-ShowDetails-Ajax.php",

            type: "POST",

            data: {

            user_name: user_name

            },

            cache: false,

            success: function(result){

            $("#showUserNameDetails").html(result);

            }

        });

    });



});

</script>

</body>

</html>





<?php



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

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

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

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

        $sddate = showDate($s_date);

        $smonth = showMonth($s_date);

        $syear = showYear($s_date);

        

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

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

        $admin_ref = $_SESSION['uref'];

        // $sref = rand().'_'.time();



        $ins_sql = "INSERT INTO fees (sid, sclass, fddate, fmonth, fyear, amount, fref, uploadedby, fdate) VALUES ('$sname', '$sclass','$sddate', '$smonth', '$syear', '$amount', '$fref', '$admin_ref', '$currentTime')";





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





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

                ?>

                <script>

                    alert('Submit Successfully');

                    window.location = 'view-fees.php';

                </script>

                <?php

            }

            else{

                ?>

                <script>

                    alert('OOPS !!!!');

                    setTimeout(() => {

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

                    }, 1200);

                </script>

                <?php

            }

        

    }



        

    ?>











