<?php
include "pin/header.php";
include "pin/sidenav.php";

$user_id = $_SESSION['user_id'];
$emp_id = $_SESSION['emp_id'];
$user_role = $_SESSION['user_role'];


if(isset($_GET['delete'])){
    $curr = mysqli_fetch_assoc(mysqli_query($db, "SELECT * FROM download_files WHERE id='$_GET[delete]'"));
    $q = mysqli_query($db, "DELETE FROM download_files WHERE id='$_GET[delete]'");
    if($q){
        unlink("files_download/$curr[file_name]");
        echo '<script>alert("File deleted successfully!");</script>';
    }else{
        echo '<script>alert("Failed to delete file!");</script>';
    }
    echo '<script>window.location.href = "download_files.php";</script>';
}


?>


<!-- ============================================================== -->
<!-- Start Page Content here -->
<!-- ============================================================== -->

<div class="content-page">
    <div class="content">
        <div class="container-fluid">

            <div class="mt-5"></div>

            <div class="row">
                <div class="col-12">
                    <div class="card">
                        <div class="card-body p-4">
                            <h4 class="header-title mt-0 mb-1 mb-4">
                                Download Files
                            </h4>

                            <table id="basic-datatable" class="table dt-responsive nowrap custom-responsive-datatable">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>File</th>
                                        <th>Time</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>

                            </table>
                            <!-- </div> -->

                        </div> <!-- end card body-->
                    </div> <!-- end card -->
                </div><!-- end col-->
            </div>
            <!-- end row-->

        </div>
    </div> <!-- content -->

</div>

<!-- ============================================================== -->
<!-- End Page content -->
<!-- ============================================================== -->



<?php
include "pin/footer.php";
?>

<script>
    $(document).ready(function() {

        // ############################### columns
            let columns = [{data: 'id'}, {data: 'tag'}, {data: 'created_at'}, {data: 'file_name'}];
        // ############################### columns

        function show_hierarchy_data() {

            // ############################### setArray
            // ############################### setArray

            $("#filter_options").html('');
            form_fields.each(function() {
                search_filter(this);
            });

            if (setArray.length > 0) {
                loadTable('#basic-datatable', 'get_from_server/download_files_get', columns, [0, 'desc'], setArray);
            } else {
                loadTable('#basic-datatable', 'get_from_server/download_files_get', columns, [0, 'desc']);
            }
            // console.log(setArray);
            setArray = [];
            // console.log(setArray);
        }

        show_hierarchy_data();
        $("#search_filter").click(show_hierarchy_data);
    });
</script>