<?php

require_once 'auth.php';

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




include("config.php");

if (isset($_GET['edit'])) {
    $getid = $_GET['edit'];

    $sql = "select * from `action_plan` where id='$getid'";
    $query = mysqli_query($con, $sql);

    if ($query) {
        foreach ($query as $items) {
            $year = $items['year'];
            $district = $items['district'];
            $description = $items['description'];
            $constituency = $items['constituency'];
            $status = $items['status'];
            $document = $items['document'];
        }
    }
}


if (isset($_POST['submit'])) {
    $year = $_POST['year'];
    $district = $_POST['district'];
    $description = $_POST['description'];
    $constituency = $_POST['constituency'];
    $status = $_POST['status'];
    $old_pdf = $_POST['old_pdf'];

    // PDF Upload Handling
    $new_pdf = $_FILES['new_pdf']['name'];
    if ($new_pdf == "") {
        $pdf = $old_pdf; // Keep the old PDF if no new PDF is uploaded
    } else {
        $pdf = $new_pdf;
        $temp_pdf = $_FILES['new_pdf']['tmp_name'];
        $folder_pdf = 'pdfs/' . $pdf; // Store PDFs in a separate folder
        move_uploaded_file($temp_pdf, $folder_pdf);
    }

    // Update Database
    $sql = $con->prepare("UPDATE `action_plan` SET year=?, district=?, description=?,constituency=?,status=?, document=? WHERE id=?");
    $sql->bind_param("ssssssi", $year, $district, $description, $constituency, $status,$pdf, $getid);

    if ($sql->execute()) {
        echo "<script>
        alert('PDF Updated Successfully');
        window.location.href = 'action-plan-list.php'; 
      </script>";
    } else {
        echo "<script>alert('Failed to update PDF')</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">
            <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">Update Action Plan</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" value="<?php echo $year ?>"
                                            placeholder="Enter Yaer" required>
                                    </div>
                                </div>
                                <div class="col-lg-12">
                                    <div class="form-group">
                                        <label>District</label>
                                        <input type="text" name="district" value="<?php echo $district ?>" class="form-control"
                                            placeholder="Enter District" required>
                                    </div>
                                </div>
                                <div class="col-lg-12">
                                    <div class="form-group">
                                        <label>Description</label>
                                        <textarea class="form-control"  name="description" required
                                            placeholder="Enter Description"><?php echo $description ?></textarea>
                                    </div>
                                </div>

                                <div class="col-lg-12">
                                    <div class="form-group">
                                        <label>Constituency</label>
                                        <input type="text" name="constituency" value="<?php echo $constituency ?>" class="form-control"
                                            placeholder="Enter Constituency" required>
                                    </div>
                                </div>
                                <div class="col-lg-12">
                                    <div class="form-group">
                                        <label>Status</label>
                                        <input type="text" name="status" value="<?php echo $status ?>" class="form-control"
                                            placeholder="Enter Status" 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="hidden" name="old_pdf" value="<?php echo $document ?>">
                                                    <input type="file" name="new_pdf" accept="application/pdf" >
                                                    
                                                    <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>


    <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/js/feather.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>
        CKEDITOR.replace('description');
    </script>


</body>

</html>