<?php
session_start();
ob_start(); // Start output buffering at the beginning
include("connection.php");
include("includes/header.php");
include("includes/sidebar.php");

$user_profile = $_SESSION['user_name'];
if ($user_profile == true) {
} else {
    // header('location:index.php');
}
?>

<?php
$id = $_GET['update_id']; // Get the ID from the URL

// Select specific fields from the `hostel` table
$sql = "SELECT * FROM product_details WHERE id='$id'";

$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result); // Fetch the record

$product = $row['product_id'];
$details = $row['product_details'];
// $value = $row['value'];
// $attribute = $row['attribute'];

?>

<div class="app-main__outer">
    <div class="app-main__inner">

        <div class="app-page-title">
            <div class="page-title-wrapper">
                <div class="page-title-heading">
                    <div class="page-title-icon">
                        <i class="pe-7s-car icon-gradient bg-mean-fruit">
                        </i>
                    </div>
                    <div>Admin Dashboard

                    </div>
                </div>
                <div class="page-title-actions">
                    <button type="button" data-toggle="tooltip" title="Example Tooltip" data-placement="bottom" class="btn-shadow mr-3 btn btn-dark">
                        <i class="fa fa-star"></i>
                    </button>

                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-12">
                <div class="card mb-3">
                    <div class="card-header">
                        <strong>Update product-details </strong>
                    </div>
                    <div class="card-body">
                        <form action="" method="POST" enctype="multipart/form-data">

                            <!-- <div class="form-group">
                                <label for="attribute">Attribute</label>
                                <input type="text" class="form-control" name="attribute" value="<?php echo $attribute; ?>" placeholder="Enter Subtitle">
                            </div>

                            <div class="form-group">
                                <label for="value">Value</label>
                                <input type="text" class="form-control" name="value" value="<?php echo $value; ?>" placeholder="Enter title">
                            </div> -->

                            <div class="form-group">
    <label for="details">Product-Details</label>
    <textarea class="form-control" id="details" name="details"  rows="5" placeholder="Enter Product-Details"><?php echo $details; ?></textarea>
</div>

                            <input type="submit" value="Update" name="update" class="btn btn-primary">
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
    CKEDITOR.replace('details');
</script>


    <?php
    if (isset($_POST['update'])) {

        // $value = mysqli_real_escape_string($conn, $_POST['value']);
        // $attribute = mysqli_real_escape_string($conn, $_POST['attribute']);

        $details = mysqli_real_escape_string($conn, $_POST['details']);

        // Update the record in the database
        $sql = "UPDATE product_details SET 
           product_details='$details', 
                updated_at=NOW() 
            WHERE id='$id'";

    $result = mysqli_query($conn, $sql);

        $result = mysqli_query($conn, $sql);

        if ($result) {
            header('location:view_product_details.php'); // Redirect after successful update
        } else {
            echo "Error updating record: " . mysqli_error($conn);
        }
    }
    ?>

</div>

<?php
include("includes/footer.php");
ob_end_flush(); // Send the buffered output to the client
?>