<?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 `board_administration` where id='$getid'";
    $query = mysqli_query($con, $sql);

    if ($query) {
        foreach ($query as $items) {
            $name = $items['name'];
            $image = $items['image'];
        }
    }
}

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

    $old_image = $_POST['old_image'];
    $new_image = $_FILES['new_image']['name'];

    if ($new_image == "") {
        $img = $old_image;
    } else {
        $img = $new_image;
        $temp = $_FILES['new_image']['tmp_name'];
        $folder = 'image/' . $img;
        move_uploaded_file($temp, $folder);
    }

    $sql = $con->prepare("update `board_administration` set id=?,image=?,name=? where id=?");
    $sql->bind_param("sssi", $getid, $img,$name, $getid);

    if ($sql->execute()) {
        echo "<script>
        alert('Data updated Successfully');
        window.location.href = 'board-administration-list.php'; 
      </script>";
    } else {
        echo "<script>alert('Failed to update data')</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 Board Administration</h5>

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

                                <div class="col-lg-12">
                                    <div class="form-group mb-0">
                                        <label>Image</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_image" value="<?php echo $image ?>">
                                                    <input type="file" name="new_image">
                                                    <a href="javascript:void(0);"> Browse</a>
                                                </div>
                                            </div>
                                            <h5>Supported formates: JPEG, PNG</h5>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-lg-12">
                                    <div class="form-group">
                                        <label>Name</label>
                                        <input type="text" name="name" class="form-control" value="<?php echo $name ?>"
                                            placeholder="Enter NAme" required>
                                    </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>