<?php
session_start();
if (!isset($_SESSION['user']) || $_SESSION['user']!=='pacific') {
    header('Location:login.php');
}

include 'include/database.php'; ?>

<?php include 'include/header.php'; ?>

<?php include 'include/sidebar.php'; ?>

<?php
if (isset($_GET['up'])) {
    $pid = $_GET['up'];
    $sql = "select * from `product` where id=$pid";
    $result = mysqli_query($con, $sql);
    if ($result) {
        foreach ($result as $key) {
            $image = $key['image'];
            $title = $key['product_name'];
             $description = $key['description'];

        }
    }
}

?>


<?php
if (isset($_POST['submit'])) {
    $title = $_POST['title'];
       $description = $_POST['b'];


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

    if ($new_image == "") {
        $floder = $old_image;
    } else {
        $floder = $new_image;

        $temp_name =$_FILES ['new_image']['tmp_name'];
        $update_image = "img_uploads/" . $floder;

        // $update_image1=$update_image;
        move_uploaded_file($temp_name, $update_image);
    }
    $sql = "update `product` set id='$pid', product_name='$title', image='$floder', description='$description' where id=$pid";
    $result = mysqli_query($con, $sql);

    if ($result) {
        echo '<script>alert("Successfully Updated product.")</script>';

        echo '<script>window.location.href = "product.php";</script>';

        include 'include/reload.php';
    } else {
        echo '<script>alert("Failed To Update Product")</script>';

    }

}


?>






<div class="page-wrapper" style="background-color: #aad1e0;">
    <div class="content">
        <div class="row">
            <div class="col-lg-7 col-sm-12 ">
                <div class="content-page-header">
                    <h5>Product Update</h5>
                </div>
                <form method="post" enctype="multipart/form-data">

                    <div class="row">

                        <div class="col-lg-12">
                            <div class="form-group">
                                <label>Product Image </label>
                                <input type="hidden" name="old_image" class="form-control"
                                    placeholder="Select News image" value="<?php echo $image ?>">
                                <input type="file" name="new_image" class="form-control"
                                    placeholder="Select News image">

                            </div>
                        </div>

                        <div class="col-lg-12">
                            <div class="form-group">
                                <label>Product Name </label>


                                <input type="text" class="form-control" placeholder="Enter Product Name....."
                                    value="<?php echo $title ?>" name="title" required>



                            </div>
                        </div>

                        <div class="col-lg-12">
                            <div class="form-group">
                                <label> Product Description </label>

                                <textarea id="editor" class="form-control" style="height: 500px;"
                                    placeholder="Enter Product Details....."
                                    name="b"><?php echo $description ?></textarea>


                            </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>

                </form>
            </div>


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






<?php
include 'include/footer.php';
?>



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

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