<?php


require_once 'auth.php';

// Check if the user is authenticated as an admin
checkAdminAuth();





include("config.php");

if (isset($_POST['submit'])) {
    $year = $_POST['year'];

    $document = $_FILES['image']['name'];
    $temp = $_FILES['image']['tmp_name'];
    $file_size = $_FILES['image']['size'];

    // Get the file extension
    $file_extension = strtolower(pathinfo($document, PATHINFO_EXTENSION));

    // Allowed extensions
    $allowed_extensions = ['pdf'];

    // Max file size (5MB)
    $maxFileSize = 5 * 1024 * 1024;

    // Check file type and size
    if (!in_array($file_extension, $allowed_extensions)) {
        echo "<script>alert('Only PDF files are allowed');</script>";
    } elseif ($file_size > $maxFileSize) {
        echo "<script>alert('File size must be less than 5MB');</script>";
    } else {
        $folder = 'image/' . $document;
        if (move_uploaded_file($temp, $folder)) {
            $sql = $con->prepare("INSERT INTO `annual_accounts` (year,document) VALUES (?,?)");
            $sql->bind_param("ss", $year, $document);

            if ($sql->execute()) {
                echo "<script>
                alert('Data Inserted Successfully');
                window.location.href = 'annual-accounts-list.php'; 
              </script>";
            } else {
                echo "<script>alert('Something went wrong');</script>";
            }
        } else {
            echo "<script>alert('Failed to upload file');</script>";
        }
    }
}




if (isset($_POST['delete'])) {
    $getid = $_POST['news'];

    $sql = "delete from `annual_accounts` where id='$getid'";
    $query = mysqli_query($con, $sql);

    if ($query) {
        echo "<script>alert ('Deleted successfully')</script>";
    } else {
        echo "<script>alert ('something wrong')</script>";
    }
}

?>






<!DOCTYPE html>
<html lang="en">


<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0">
    <title>Admin</title>

    <link rel="shortcut icon" href="assets/img/favicon.png">

    <link rel="stylesheet" href="assets/css/select2.min.css">

    <link rel="stylesheet" href="assets/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="assets/plugins/bootstrap-tagsinput/css/bootstrap-tagsinput.css">

    <link rel="stylesheet" href="assets/plugins/fontawesome/css/fontawesome.min.css">
    <link rel="stylesheet" href="assets/plugins/fontawesome/css/all.min.css">

    <link rel="stylesheet" href="assets/css/dataTables.bootstrap4.min.css">

    <link rel="stylesheet" href="assets/plugins/feather/feather.css">

    <link rel="stylesheet" href="assets/css/style.css">

    <script src="ckeditornci/ckeditor_l/ckeditor.js"></script>
</head>

<body>
    <div class="main-wrapper">

        <?php

        include("header.php");
        include("sidebar.php");

        ?>

        <div class="page-wrapper page-settings">
            <div class="content">
                <form method="post" enctype="multipart/form-data">
                    <div class="row">
                        <div class="col-lg-7 col-sm-12 m-auto">
                            <div class="content-page-header">
                                <h5 class="mb-2">Add Annual Accounts</h5>

                            </div>
                            <div class="row">

                               
                                <div class="col-lg-12">
                                    <div class="form-group">
                                        <label>Year</label>
                                        <input type="text" name="year" class="form-control"
                                            placeholder="Enter Year" required>
                                    </div>
                                </div>
                                <div class="col-lg-12">
                                    <div class="form-group mb-0">
                                        <label>Upload PDF:</label>
                                    </div>
                                    <div class="form-uploads mb-4">
                                        <div class="form-uploads-path">
                                            <img src="assets/img/icons/upload.svg" alt="img">
                                            <div class="file-browse">
                                                <h6>Drag & drop image or </h6>
                                                <div class="file-browse-path">
                                                    <input type="file" name="image" accept="application/pdf" required>
                                                    <p id="file-size-warning" style="color: red; display: none;">File size must be less than 5MB.</p>

                                                    <a href="javascript:void(0);"> Browse</a>
                                                </div>
                                            </div>

                                            <h5>Max file size: 5MB | Supported formates: PDF</h5>
                                        </div>
                                    </div>
                                </div>

                                <div class="col-lg-12">
                                    <div class="btn-path">
                                        <button type="submit" name="submit" class="btn btn-submit "> Submit</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>


                </form>



            </div>
        </div>
    </div>


    <div class="modal fade" id="delete-item" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Delete Sub Categories</h5>
                    <button type="button" class="btn-close close-modal" data-bs-dismiss="modal" aria-label="Close">
                        <i class="fe fe-x"></i>
                    </button>
                </div>
                <form method="post">
                    <input type="hidden" name="news" id="category_id" values="">
                    <div class="modal-body py-0">
                        <div class="del-modal">
                            <p>Are you sure want to Delete?</p>
                        </div>
                    </div>
                    <div class="modal-footer pt-0">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn btn-primary" name="delete">Yes</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script data-cfasync="false" src="../../cdn-cgi/scripts/5c5dd728/cloudflare-static/email-decode.min.js"></script>
    <script src="assets/js/jquery-3.6.4.min.js"></script>

    <script src="assets/js/select2.min.js"></script>

    <script src="assets/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>

    <script src="assets/plugins/bootstrap-tagsinput/js/bootstrap-tagsinput.js"></script>

    <script src="assets/js/feather.min.js"></script>

    <script src="assets/js/jquery.dataTables.min.js"></script>
    <script src="assets/js/dataTables.bootstrap4.min.js"></script>

    <script src="assets/plugins/slimscroll/jquery.slimscroll.min.js"></script>

    <script src="assets/plugins/sweetalert/sweetalert2.all.min.js"></script>
    <script src="assets/plugins/sweetalert/sweetalerts.min.js"></script>

    <script src="assets/js/admin.js"></script>

    <script>
        document.addEventListener("DOMContentLoaded", function() {
            const fileInput = document.querySelector('input[name="image"]');
            const warningText = document.getElementById("file-size-warning");

            fileInput.addEventListener("change", function() {
                if (fileInput.files.length > 0) {
                    const fileSize = fileInput.files[0].size; // File size in bytes
                    const maxFileSize = 5 * 1024 * 1024; // 5MB in bytes

                    if (fileSize > maxFileSize) {
                        warningText.style.display = "block"; // Show warning
                        fileInput.value = ""; // Clear file input
                    } else {
                        warningText.style.display = "none"; // Hide warning
                    }
                }
            });
        });
    </script>


    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // Delegate event listener to a parent container (or document)
            document.addEventListener('click', function(e) {
                if (e.target && e.target.closest('.delete-table')) {
                    // Get the clicked element
                    var deleteLink = e.target.closest('.delete-table');

                    // Get the category ID from the data attribute
                    var categoryId = deleteLink.getAttribute('data-category-id');

                    // Set the category_id input field value in the modal form
                    document.getElementById('category_id').value = categoryId;

                    // Show the modal
                    var deleteModal = new bootstrap.Modal(document.getElementById('delete-item'));
                    deleteModal.show();
                }
            });

            // Close modal properly
            var deleteModalElement = document.getElementById('delete-item');

            deleteModalElement.addEventListener('hidden.bs.modal', function() {
                // Ensure the backdrop is properly removed when modal is closed
                document.body.classList.remove('modal-open');
                document.querySelector('.modal-backdrop')?.remove();
            });
        });
    </script>


    <script>
        CKEDITOR.replace('description');
    </script>


</body>


</html>