<?php
include("connection.php");

$id = $_GET['id'];

// Fetch existing data
$sql = "SELECT * FROM `header` WHERE `id`='$id'";
$query = mysqli_query($con, $sql);
$row = mysqli_fetch_assoc($query);

if (isset($_POST['update'])) {
    $email = mysqli_real_escape_string($con, $_POST['email']);
    $phone = mysqli_real_escape_string($con, $_POST['phone']);
    $address = mysqli_real_escape_string($con, $_POST['address']);
    $old_logo = $_POST['old_logo'];

    // Initialize logo variable
    $logo = $old_logo;

    // Check if a new image is uploaded
    if (!empty($_FILES['logo']['name'])) {
        $originalName = $_FILES['logo']['name'];
        $extension = pathinfo($originalName, PATHINFO_EXTENSION);
        $uniqueName = time() . '_' . uniqid() . '.' . $extension;

        $img_path = "./assets/images/slider/" . $uniqueName;

        if (move_uploaded_file($_FILES['logo']['tmp_name'], $img_path)) {
            // Delete old image if exists
            if (!empty($old_logo) && file_exists("./assets/images/slider/" . $old_logo)) {
                unlink("./assets/images/slider/" . $old_logo);
            }
            $logo = $uniqueName; // set new logo name to update
        } else {
            echo "Image upload failed.";
        }
    }

    // Update query
    $sql1 = "UPDATE `header` SET `logo`='$logo', `email`='$email', `phone`='$phone', `address`='$address' WHERE `id`='$id'";
    $query1 = mysqli_query($con, $sql1);

    if ($query1) {
        header("location: viewheader.php");
        exit();
    } else {
        echo "Update failed: " . mysqli_error($con);
    }
}
?>



<?php
include("header.php")
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <title>Header Update</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta content="Premium Multipurpose Admin & Dashboard Template" name="description" />
    <meta content="Themesbrand" name="author" />
    <!-- App favicon -->
    <!-- <link rel="shortcut icon" href="assets/images/favicon.ico" /> -->

    <!-- Plugins css -->
    <link href="assets/libs/dropzone/dropzone.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" type="text/css" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css">
    <link rel="shortcut icon" href="./assets/logo/fav.png" />

    <!-- Bootstrap Css -->
    <link href="assets/css/bootstrap.min.css" id="bootstrap-style" rel="stylesheet" type="text/css" />
    <!-- Icons Css -->
    <link href="assets/css/icons.min.css" rel="stylesheet" type="text/css" />
    <!-- App Css-->
    <link href="assets/css/app.min.css" id="app-style" rel="stylesheet" type="text/css" />
    <!-- App js -->
    <!-- <script src="assets/js/plugin.js"></script> -->
    <style>
        .form .input {
            width: 100%;
            background: white;
            border: none;
            padding: 15px 20px;
            border-radius: 20px;
            margin-bottom: 9px;
            margin-top: 15px;
            box-shadow: rgba(0, 0, 0, 0.24) 0px 3px 8px;
            border-inline: 2px solid transparent;
        }

        input[name='logo'] {
            display: none;

        }


        div#upload {
            cursor: pointer;

        }

        ::-webkit-file-upload-button {
            display: none;
        }

        /* 
        .form .input::-moz-placeholder {
            color: rgb(170, 170, 170);
        }

        .form .input::placeholder {
            color: rgb(170, 170, 170);
        }

        .form .input:focus {
            outline: none;
            border-inline: 2px solid #12b1d1;
        } */

        .filepath {
            display: none;
        }

        img[alt="img"] {
            width: clamp(50px, 15%, 100px);
            aspect-ratio: 1/0.70;
        }
    </style>
</head>

<body data-sidebar="dark">
    <!-- <body data-layout="horizontal" data-topbar="dark"> -->

    <!-- Begin page -->
    <div id="layout-wrapper">

        <div class="w-80 ">
            <div class="page-content" style="width: 80%;margin-left: 260px;">
                <div class="container">
                    <!-- start page title -->
                    <div class="row">
                        <div class="col-12">
                            <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                                <h4 class="mb-sm-0 font-size-18">Header Update</h4>


                            </div>
                        </div>
                    </div>
                    <!-- end page title -->

                    <div class="row">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-body">

                                    <div>
                                        <form class="form" action="" method="POST" enctype="multipart/form-data">
                                            <!-- Hidden for old image -->
                                            <input type="hidden" name="old_logo" value="<?php echo $row['logo']; ?>">

                                            <div class="dropzone">
                                                <div class="fallback">
                                                    <input type="file" name="logo" id="imageInput" />
                                                </div>
                                                <div class="dz-message needsclick" id="upload">
                                                    <div class="mb-3">
                                                        <i class="display-4 text-muted bx bxs-cloud-upload"></i>
                                                    </div>
                                                    <h4>Drop files here or click to upload.</h4>
                                                </div>
                                            </div>

                                            <ul class="list-unstyled mb-0" id="dropzone-preview"></ul>

                                            <!-- Preview -->
                                            <div class="filepath" style="display:block; margin-top:10px;">
                                                <img id="previewImg" src="./assets/images/slider/<?php echo $row['logo']; ?>" alt="img" style="max-height:150px;">
                                                <span id="filename"><?php echo $row['logo']; ?></span>
                                            </div>
                                            <br>
                                            <div class="row">
                                                <div class="col-lg-6">
                                                    <label for="">Email</label>
                                                    <input type="text" class="form-control mb-3" name="email" value="<?php echo $row['email']; ?>" placeholder="Sub Heading">
                                                </div>
                                                <div class="col-lg-6">
                                                    <label for="">Phone</label>
                                                    <input type="text" class="form-control mb-3" name="phone" value="<?php echo $row['phone']; ?>" placeholder="Heading">
                                                </div>
                                                <div class="col-lg-12">
                                                    <label for="">Address</label>
                                                    <input type="text" class="form-control mb-3" name="address" value="<?php echo $row['address']; ?>" placeholder="Heading">
                                                </div>
                                            </div>


                                            <br>



                                            <div class="text-center mt-4">
                                                <button type="submit" name="update" class="btn btn-primary" value="submit">
                                                    Update
                                                </button>
                                                <a href="viewheader.php"><button type="button" class="btn btn-danger">View Data</button></a>
                                            </div>
                                        </form>
                                    </div>

                                </div>
                            </div>
                        </div>
                        <!-- end col -->
                    </div>
                    <!-- end row -->
                </div>
                <!-- container-fluid -->
            </div>
            <!-- End Page-content -->


        </div>
        <!-- end main content-->
    </div>
    <!-- END layout-wrapper -->




    <!-- JAVASCRIPT -->
    <script src="assets/libs/jquery/jquery.min.js"></script>
    <script src="assets/libs/bootstrap/js/bootstrap.bundle.min.js"></script>
    <script src="assets/libs/metismenu/metisMenu.min.js"></script>
    <script src="assets/libs/simplebar/simplebar.min.js"></script>
    <script src="assets/libs/node-waves/waves.min.js"></script>

    <!-- Plugins js -->
    <script src="assets/libs/dropzone/dropzone-min.js"></script>

    <!-- Form file upload init js -->
    <script src="assets/js/pages/form-file-upload.init.js"></script>

    <script src="assets/js/app.js"></script>
    <script>
        const imgInput = document.querySelector("input[name='logo']");
        const uploadBox = document.getElementById('upload');
        const previewImg = document.getElementById('previewImg');
        const filenameSpan = document.getElementById('filename');

        uploadBox.onclick = () => imgInput.click();

        imgInput.onchange = () => {
            if (imgInput.files.length > 0) {
                const file = imgInput.files[0];
                const fileURL = URL.createObjectURL(file);
                previewImg.src = fileURL;
                filenameSpan.textContent = file.name;
                document.querySelector(".filepath").style.display = 'block';
            }
        };
    </script>
</body>

</html>