<?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");
        exit();
    }

    $page_type = 'feesStructure';
    $page_url = 'fees-structure.php?sec='.$page_type ;
    $pageHeaderName = "Fees Structure" ;
?>

<script>
var page_url = '<?=$page_url?>';
</script>


<!DOCTYPE html>

<html lang="en">

<head>

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

    <link href="css/all.min.css" rel="stylesheet" type="text/css">
    <link href="css/alltable.css" rel="stylesheet">
    <link rel="stylesheet" href="vendor/croppie/croppie.css" />

</head>



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

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

    <div class="container">
        <div class="row mx-0 just ify-content-center">
            <div class="col-12 col-sm-12 col-md-12">
                <div class="card-body">
                    <h1 class="h2 mb-2 text-gray-800 text-sm-center" style="margin-top: 70px">
                        <?php echo $pageHeaderName ; ?></h1>
                    <hr>
                    <?php
                    if (isset($_GET["edit_id"])) {
                        // Fetch record to edit
                        $edit_run = mysqli_query($con, "SELECT * FROM fees_structure WHERE id = '$_GET[edit_id]'");
                        $edit_row = mysqli_fetch_assoc($edit_run);
                    ?>
                    <form class="user" method="post" enctype="multipart/form-data">
                        <h4 class="mb-3 badge badge-secondary">Edit <?php echo $pageHeaderName; ?></h4>
                        <div class="form-group row">

                            <div class="col-md-6">
                                <label color="black">Class</label>
                                <input type="text" name="class_name" class="form-control mb-3"
                                    placeholder="Enter Class Name *" value="<?php echo $edit_row['class_name']; ?>"
                                    required>
                            </div>

                            <div class="col-md-6">
                                <label color="black">Admission Fee</label>
                                <input type="number" name="admission_fee" class="form-control mb-3"
                                    placeholder="Enter Admission Fee *"
                                    value="<?php echo $edit_row['admission_fee']; ?>" required>
                            </div>

                            <div class="col-md-6">
                                <label color="black">Re-Admission Fee</label>
                                <input type="number" name="re_admission_fee" class="form-control mb-3"
                                    placeholder="Enter Re-Admission Fee *"
                                    value="<?php echo $edit_row['readmission_fee']; ?>" required>
                            </div>

                            <div class="col-md-6">
                                <label color="black">Monthly Fee</label>
                                <input type="number" name="monthly_fee" class="form-control mb-3"
                                    placeholder="Enter Monthly Fee *" value="<?php echo $edit_row['monthly_fee']; ?>"
                                    required>
                            </div>

                            <div class="col-md-6">
                                <label color="black">Hostel Fee</label>
                                <input type="number" name="hostel_fee" class="form-control mb-3"
                                    placeholder="Enter Hostel Fee" value="<?php echo $edit_row['hostel_fee']; ?>">
                            </div>

                        </div>

                        <button type="submit" class="btn btn-success btn-user m-2" name="edit_submit">
                            <i class="fas fa-check"></i> Update <?php echo $pageHeaderName; ?>
                        </button>
                        <a href="<?php echo $page_url; ?>" class="btn btn-warning btn-user m-2">
                            <i class="fas fa-ban"></i> Cancel
                        </a>
                    </form>

                    <?php
                    } else {
                    ?>
                    <form class="user" method="post" enctype="multipart/form-data">

                        <h4 class="mb-1 badge badge-secondary" data-toggle="collapse" data-target="#target"
                            style="cursor:pointer;">
                            Add New <?php echo $pageHeaderName; ?>
                        </h4>

                        <p class="small">
                            <i class="fas fa-hand-point-up"></i> Tap Here to add New <?php echo $pageHeaderName; ?>
                        </p>

                        <div class="collapse" id="target">

                            <div class="form-group row">

                                <!-- Class Name -->
                                <div class="col-md-6">
                                    <label color="black">Class Name</label>
                                    <input type="text" name="class_name" class="form-control mb-3"
                                        placeholder="e.g. Class XI Science" required>
                                </div>

                                <!-- Admission Fee -->
                                <div class="col-md-6">
                                    <label color="black">Admission Fee (₹)</label>
                                    <input type="number" name="admission_fee" class="form-control mb-3"
                                        placeholder="Enter Admission Fee" required>
                                </div>

                                <!-- Re-Admission Fee -->
                                <div class="col-md-6">
                                    <label color="black">Re-Admission Fee (₹)</label>
                                    <input type="number" name="readmission_fee" class="form-control mb-3"
                                        placeholder="Enter Re-Admission Fee">
                                </div>

                                <!-- Monthly Fee -->
                                <div class="col-md-6">
                                    <label color="black">Monthly Fee (₹)</label>
                                    <input type="number" name="monthly_fee" class="form-control mb-3"
                                        placeholder="Enter Monthly Fee" required>
                                </div>

                                <!-- Hostel Fee -->
                                <div class="col-md-6">
                                    <label color="black">Hostel Fee (₹)</label>
                                    <input type="number" name="hostel_fee" class="form-control mb-3"
                                        placeholder="Enter Hostel Fee">
                                </div>

                            </div>

                            <button type="submit" class="btn btn-primary btn-user m-1" name="add_submit">
                                <i class="fas fa-plus-circle"></i> Add New <?php echo $pageHeaderName; ?>
                            </button>

                            <a href="#target" data-toggle="collapse" class="btn btn-warning btn-user m-1">
                                <i class="fas fa-ban"></i> Cancel
                            </a>
                        </div>

                    </form>


                    <?php } ?>

                    <hr>

                    <!-- Active & Inactive Listing Table ../start -->
                    <?php 
                    if (!empty($_GET['data']) && $_GET['data'] == 'inactive') {
                    ?>
                    <div class="row m-0">
                        <div class="col-md-12 p-0">
                            <div class="well well-sm">
                                <b class="text-center">Inactive <?php echo $pageHeaderName; ?> List</b>
                                <a href="<?php echo $page_url; ?>" class="btn btn-primary btn-sm float-right"
                                    style="margin-top:-5px;">
                                    View Active <?php echo $pageHeaderName; ?> List
                                </a>
                            </div>
                        </div>

                        <div class="col-md-12 p-0">
                            <div class="table-responsive">
                                <table class="table table-hover table-bordered table-sm" id="thisismytable2">
                                    <thead>
                                        <tr>
                                            <th>Sl. No.</th>
                                            <th>All Details</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $sql = "SELECT * FROM fees_structure WHERE up_by = '$_SESSION[uid]' AND recycle = '0' ORDER BY id DESC";
                                        $run = mysqli_query($con, $sql);
                                        $c = 1;
                                        while ($rows = mysqli_fetch_assoc($run)) {
                                            echo "
                                            <tr id='$rows[id]'>
                                                <td><span class='badge badge-info badge-pill'>$c</span></td>
                                                <td class='text-left align-middle'>
                                                    <b>Class:</b> $rows[class_name] <br>
                                                    <b>Admission Fee:</b> ₹$rows[admission_fee] <br>
                                                    <b>Re-Admission Fee:</b> ₹$rows[readmission_fee] <br>
                                                    <b>Monthly Fee:</b> ₹$rows[monthly_fee] <br>
                                                    <b>Hostel Fee:</b> ₹$rows[hostel_fee]
                                                </td>
                                                <td class='text-center align-middle'>
                                                    <a href='$page_url&data=inactive&restore_id=$rows[id]' 
                                                        class='m-2 btn btn-warning btn-sm rounded-circle' 
                                                        title='Click to Restore'>
                                                        <i class='fas fa-recycle'></i>
                                                    </a>
                                                    <a href='$page_url&data=inactive&del_id=$rows[id]' 
                                                        class='m-2 btn btn-danger btn-sm rounded-circle' 
                                                        title='Click to Delete'>
                                                        <i class='fas fa-trash'></i>
                                                    </a>
                                                </td>
                                            </tr>";
                                            $c++;
                                        }
                                        ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>

                    <?php 
                    } else { 
                    ?>
                    <div class="row m-0">
                        <div class="col-md-12 p-0">
                            <div class="well well-sm justify-content-center">
                                <b class="text-center"><?php echo $pageHeaderName; ?> List</b>
                                <a href="<?php echo $page_url.'&data=inactive'; ?>"
                                    class="btn btn-primary btn-sm float-right" style="margin-top:-5px;">
                                    View Inactive <?php echo $pageHeaderName; ?> List
                                </a>
                            </div>
                        </div>

                        <div class="col-md-12 p-0">
                            <div class="table-responsive">
                                <table class="table table-hover table-bordered table-sm" id="thisismytable">
                                    <thead>
                                        <tr>
                                            <th>Sl. No.</th>
                                            <th>All Details</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $sql = "SELECT * FROM fees_structure WHERE up_by = '$_SESSION[uid]' AND recycle = '1' ORDER BY id DESC";
                                        $run = mysqli_query($con, $sql);
                                        $c = 1;
                                        while ($rows = mysqli_fetch_assoc($run)) {
                                            echo "
                                            <tr id='$rows[id]'>
                                                <td><span class='badge badge-info badge-pill'>$c</span></td>
                                                <td class='text-left align-middle'>
                                                    <b>Class:</b> $rows[class_name] <br>
                                                    <b>Admission Fee:</b> ₹$rows[admission_fee] <br>
                                                    <b>Re-Admission Fee:</b> ₹$rows[readmission_fee] <br>
                                                    <b>Monthly Fee:</b> ₹$rows[monthly_fee] <br>
                                                    <b>Hostel Fee:</b> ₹$rows[hostel_fee]
                                                </td>
                                                <td class='text-center align-middle'>
                                                    <a href='$page_url&edit_id=$rows[id]' 
                                                        class='m-2 btn btn-success btn-sm rounded-circle' 
                                                        title='Click to Edit'>
                                                        <i class='fas fa-pencil-alt'></i>
                                                    </a>
                                                    <a href='$page_url&recycle_id=$rows[id]' 
                                                        class='m-2 btn btn-warning btn-sm rounded-circle' 
                                                        title='Move to Recycle'>
                                                        <i class='fas fa-recycle'></i>
                                                    </a>
                                                </td>
                                            </tr>";
                                            $c++;
                                        }
                                        ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    <?php 
                    } 
                    ?>
                    <!-- Active & Inactive Listing Table --/end -->
                </div>
            </div>
        </div>
    </div>





    <!--============modal================== -->



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

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


    <script>
    $(document).ready(function() {
        $('#thisismytable2').DataTable();
    });
    $(document).ready(function() {
        $('#thisismytable').DataTable();
    });
    </script>



</body>

</html>



<?php
if (isset($_POST['add_submit'])) {
    $class_name      = mysqli_real_escape_string($con, $_POST['class_name']);
    $admission_fee   = mysqli_real_escape_string($con, $_POST['admission_fee']);
    $readmission_fee = mysqli_real_escape_string($con, $_POST['readmission_fee']);
    $monthly_fee     = mysqli_real_escape_string($con, $_POST['monthly_fee']);
    $hostel_fee      = mysqli_real_escape_string($con, $_POST['hostel_fee']);
    $up_by           = $_SESSION['uid'];
    $recycle           = '1';

    $insert_sql = "INSERT INTO fees_structure 
                   (class_name, admission_fee, readmission_fee, monthly_fee, hostel_fee, up_by, recycle)
                   VALUES ('$class_name', '$admission_fee', '$readmission_fee', '$monthly_fee', '$hostel_fee', '$up_by', '$recycle')";
    
    if (mysqli_query($con, $insert_sql)) {
         ?>

<script>
alert('success');

window.location = page_url;
</script>

<?php
    } else {
         ?>

<script>
alert('Oops!!');

window.location = page_url;
</script>

<?php
    }
}

// === Move record to inactive (Recycle) ===
if (isset($_GET["recycle_id"])) {
    $recycle_sql = "UPDATE fees_structure SET recycle = '0' WHERE id = '$_GET[recycle_id]'";
    if (mysqli_query($con, $recycle_sql)) {
        ?>
<script>
alert('Successfully moved to inactive list');
window.location = page_url;
</script>
<?php
    } else {
        ?>
<script>
alert('OOPS! Something went wrong.');
setTimeout(() => {
    window.location = page_url;
}, 1200);
</script>
<?php
    }
}

// === Restore record from inactive ===
if (isset($_GET["restore_id"])) {
    $restore_sql = "UPDATE fees_structure SET recycle = '1' WHERE id = '$_GET[restore_id]'";
    if (mysqli_query($con, $restore_sql)) {
        ?>
<script>
alert('Successfully Restored');
window.location = page_url + '&data=inactive';
</script>
<?php
    } else {
        ?>
<script>
alert('OOPS! Something went wrong.');
setTimeout(() => {
    window.location = page_url + '&data=inactive';
}, 1200);
</script>
<?php
    }
}

// === Permanently Delete Record ===
if (isset($_GET["del_id"])) {
    $delete_sql = "DELETE FROM fees_structure WHERE id = '$_GET[del_id]'";
    if (mysqli_query($con, $delete_sql)) {
        ?>
<script>
alert('Successfully Deleted');
window.location = page_url + '&data=inactive';
</script>
<?php
    } else {
        ?>
<script>
alert('OOPS! Something went wrong.');
setTimeout(() => {
    window.location = page_url + '&data=inactive';
}, 1200);
</script>
<?php
    }
}

// === Handle Edit Submit ===
if (isset($_POST['edit_submit'])) {
    $edit_id = mysqli_real_escape_string($con, $_GET['edit_id']);

    // User Inputs
    $class_name       = mysqli_real_escape_string($con, $_POST['class_name']);
    $admission_fee    = mysqli_real_escape_string($con, $_POST['admission_fee']);
    $re_admission_fee = mysqli_real_escape_string($con, $_POST['re_admission_fee']);
    $monthly_fee      = mysqli_real_escape_string($con, $_POST['monthly_fee']);
    $hostel_fee       = mysqli_real_escape_string($con, $_POST['hostel_fee']);

    // Update Query (removed trailing comma)
    $up_sql = "
        UPDATE fees_structure 
        SET 
            class_name = '$class_name',
            admission_fee = '$admission_fee',
            readmission_fee = '$re_admission_fee',
            monthly_fee = '$monthly_fee',
            hostel_fee = '$hostel_fee'
        WHERE id = '$edit_id'
    ";

    if (mysqli_query($con, $up_sql)) {
        ?>
<script>
alert('Update Successful');
window.location = page_url;
</script>
<?php
    } else {
        ?>
<script>
alert('OOPS! Update failed.');
setTimeout(() => {
    window.location = page_url;
}, 1000);
</script>
<?php
    }
}
?>