<?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']) ) {

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

<?php

    if ( isset($_GET["edit_id"]) ) {

        $edit_run = mysqli_query($con, "SELECT f.*, u.uid, u.uname FROM fees  AS f INNER JOIN users AS u ON u.uphone = f.sid WHERE fref = '$_GET[edit_id]'");

        $edit_row = mysqli_fetch_assoc($edit_run);

    }

    else{

        header('location:view-fees.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"> Edit Fees <div style="color: blue;"><b><i class="fa fa-user-o" aria-hidden="true"></i> <?php echo $edit_row["uname"].' ( '.showClass($con,$edit_row["sclass"]).' )';?></b></div>

            <div style="color: blue;"><b><i class="fa fa-calendar" aria-hidden="true"></i> <?php echo $edit_row["fddate"].' / '.$edit_row["fmonth"].' / '.$edit_row["fyear"];?></b></div>

            </h5>

                <div class="card-body">

                    <form method="POST">

                        <div class="row">

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

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

                            <input type="text" class="form-control"name="amount" placeholder="Enter Amount" value="<?php echo $edit_row['amount']; ?>" required>

                            </div>

                        </div>

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

                                <a href="view-fees.php" class="btn btn-warning btn-user m-2"> <i class="fa fa-ban"></i> Cancel </a>

                                <button type="submit" class="btn btn-success btn-user m-2" name="edit_fees"><i class="fa fa-edit"></i> UPDATE DETAILS </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['edit_fees'])){

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

       



            $up_sql = "UPDATE fees SET amount = '$amount', edate = '$currentTime' ";



            $up_sql .= " WHERE fref = '$_GET[edit_id]' ";

    

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

                ?>

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

                <?php

            }

            else{

                ?>

                <script>

                    alert('OOPS !!!!');

                    setTimeout(() => {

                        window.location = 'edit-fees.php?edit_id=<?php echo $_GET["edit_id"]; ?>';

                    }, 1200);

                </script>

                <?php

            }

        

    }



?>