<?php
include 'classes/database.class.php';
include 'classes/admin.class.php';




if (isset($_POST['submit'])) {

    $product_id = $_POST['product_id'];
    $name = $_POST['name'];
    $description = $_POST['description'];
    $second_description = $_POST['second_description'];

    if ($second_description == "") {
        $second_description = "empty";
    }
    $id = uniqid();
    $filename = $_FILES["image1"]["name"];
    if ($filename != "") {
        $tempname = $_FILES["image1"]["tmp_name"];
        $folder = "product_images/" . $id . $filename;
        $image1 = $id . $filename;
        $uploaded = move_uploaded_file($tempname, $folder);
    } else {
        $result = $db->setQuery("SELECT * FROM products WHERE product_id='$product_id';");
        $row = mysqli_fetch_assoc($result);
        $image1 = $row['image1'];
    }


    $result = $db->setQuery("UPDATE products SET name='$name', description='$description', image1='$image1' WHERE product_id='$product_id';");
    $result1 = $db->setQuery("UPDATE products_second_descriptions SET description='$second_description' WHERE product_id='$product_id';");

    if ($result and $result1) {
        $admin->goTo("edit-product", "success&product_id=$product_id");
    } else {
        $admin->goTo("edit-product", "error&product_id=$product_id");
    }
}


if (!isset($_GET['product_id'])) {
    $admin->goTo("./", "invalid_product");
}

if (!$admin->productExists($_GET['product_id'])) {
    $admin->goTo("./", "invalid_product");
}

$product_id = $_GET['product_id'];
$result = $db->setQuery("SELECT * FROM products WHERE product_id='$product_id';");
$row = mysqli_fetch_assoc($result);

$result1 = $db->setQuery("SELECT * FROM products_second_descriptions WHERE product_id='$product_id';");
$row1 = mysqli_fetch_assoc($result1);


?>
<!doctype html>
<html lang="zxx">


<meta http-equiv="content-type" content="text/html;charset=UTF-8" />

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="keywords" content="" />
    <title>Edit Product</title>
    <link href="http://fonts.googleapis.com/css2?family=Rubik:wght@300;400;500;600&amp;display=swap" rel="stylesheet">
    <!-- Template CSS -->
    <link rel="stylesheet" href="assets/css/style-liberty.css">

    <!-- Favicon -->
    <link href="assets/images/buralogo.png" rel="shortcut icon" />
</head>

<body>


    <body>
        <!-- New toolbar-->
        <style>
            * {
                box-sizing: border-box;
                font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
            }
        </style>

        <!--header-->
        <?php include 'header.php'; ?>
        <!--/header-->
        <!--/inner-page-->

        <div class="inner-banner">
        </div>
        <section class="w3l-breadcrumb">
            <div class="container">
                <ul class="breadcrumbs-custom-path">
                    <li><a href="./">Home</a></li>
                    <li class="active"><span class="fa fa-arrow-right mx-2" aria-hidden="true"></span> Contact Us</li>
                </ul>
            </div>
        </section>
        <!--//inner-page-->
        <!-- /contact-section -->
        <section class="w3l-contact-2 py-5" id="contact">
            <div class="container py-lg-5 py-md-4 py-2">

                <div style="margin: 8px auto; display: block; text-align:center;">

                    <!---728x90--->


                </div>
                <div class="header-section text-center mx-0 mb-md-5 mb-4">
                    <h6 class="title-subhny">Edit</h6>
                    <h3 class="title-w3l mt-2">Edit Product</h3>
                </div>


                <div style="margin: 8px auto; display: block; text-align:center;">

                    <!---728x90--->

                </div>
                <?php
                if (isset($_GET['message_not_sent'])) {
                    echo "<div class='alert alert-danger' style='text-align:center'>An error occured. try again!</div>";
                }

                if (isset($_GET['message_sent'])) {
                    echo "<div class='alert alert-success' style='text-align:center'>Message sent successfully!</div>";
                }
                ?>
                <div class="contact-grids d-grid">
                    <div class="contact-left-img">
                        <img src="product_images/<?php echo $row['image1']; ?>" class="img-fluid radius-image" alt="">
                    </div>
                    <div class="contact-right">
                        <form action="" method="POST" class="signin-form" enctype="multipart/form-data">
                            <?php
                            if (isset($_GET['success'])) {
                                echo "<div class='alert alert-success'>Product edited successfully!</div>";
                            }

                            if (isset($_GET['error'])) {
                                echo "<div class='alert alert-danger'>Upload error!</div>";
                            }
                            ?>
                            <div class="input-grids">
                                <input type="hidden" name="product_id" value="<?php echo $product_id; ?>">

                                <input type="text" name="name" id="w3lName" placeholder="Name*" value="<?php echo $row['name']; ?>" class="contact-input" />
                                <textarea type="text" id="editor" name="description" id="w3lSender" placeholder="description" class="contact-input" style="height:300px;margin-bottom:20px;" required>
                                    <?php echo $row['description']; ?>
                                </textarea>
                                <textarea type="text" id="second_editor" name="second_description" id="w3lSender" placeholder="Second description" class="contact-input" style="height:300px;margin-bottom:20px;" required>
                                 <?php echo $row1['description']; ?>
                                </textarea>
                                <input type="file" name="image1" id="w3lSubect" placeholder="Image" class="contact-input" />
                                <!-- <input type="text" name="w3lWebsite" id="w3lWebsite" placeholder="Website URL*" class="contact-input" required="" /> -->
                            </div>
                            <!-- <div class="form-input">
                                <textarea name="message" id="w3lMessage" placeholder="Type your message here*" required=""></textarea>
                            </div> -->
                            <div class="form-buttonhny text-lg-right">
                                <button class="btn btn-style btn-primary" name="submit">Save Changes</button>
                            </div>
                        </form>
                    </div>
                </div>
                <div class="map-iframe mt-5 mb-lg-5 pb-lg-4">
                    <!-- <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d317718.69319292053!2d-0.3817765050863085!3d51.528307984912544!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x47d8a00baf21de75%3A0x52963a5addd52a99!2sLondon%2C+UK!5e0!3m2!1sen!2spl!4v1562654563739!5m2!1sen!2spl" width="100%" height="400" frameborder="0" style="border: 0px;" allowfullscreen=""></iframe> -->
                </div>
            </div>
        </section>

        <!-- //contact-section -->

        <div style="margin: 8px auto; display: block; text-align:center;">

            <!---728x90--->

        </div>
        <!-- footer-28 block -->
        <?php include 'footer.php'; ?>

        <!-- Template JavaScript -->
        <script src="assets/js/jquery-3.3.1.min.js"></script>
        <script src="assets/js/theme-change.js"></script>
        <!--/MENU-JS-->
        <script>
            $(window).on("scroll", function() {
                var scroll = $(window).scrollTop();

                if (scroll >= 80) {
                    $("#site-header").addClass("nav-fixed");
                } else {
                    $("#site-header").removeClass("nav-fixed");
                }
            });

            //Main navigation Active Class Add Remove
            $(".navbar-toggler").on("click", function() {
                $("header").toggleClass("active");
            });
            $(document).on("ready", function() {
                if ($(window).width() > 991) {
                    $("header").removeClass("active");
                }
                $(window).on("resize", function() {
                    if ($(window).width() > 991) {
                        $("header").removeClass("active");
                    }
                });
            });
        </script>
        <!--//MENU-JS-->

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



        <div id="v-w3layouts"></div>
        <script src="assets/js/ckeditor.js"></script>

        <script>
            ClassicEditor.create(document.getElementById("editor"))
            ClassicEditor.create(document.getElementById("second_editor"))
        </script>

    </body>



</html>