<?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
// Get the ID from the query string
$id = $_GET['update_id'];

// Fetch the settings details
$sql = "SELECT * FROM settings WHERE id='$id'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);

// Assign the current values to variables
$site_name = $row['site_name'];
$logo = $row['logo'];
$logo_text = $row['logo_text'];
$email = $row['email'];
$phone = $row['phone'];
$address = $row['address'];
$footer_text = $row['footer_text'];
?>

<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 Header/Footer</strong>
                    </div>
                    <div class="card-body">
    <form action="" method="POST" enctype="multipart/form-data">
        <div class="form-group">
            <label for="site_name">Site Name</label>
            <input type="text" class="form-control" name="site_name" id="site_name" value="<?php echo $site_name; ?>" placeholder="Enter Site Name">
        </div>

        <div class="form-group">
            <label for="logo">Logo</label>
            <input type="file" class="form-control" name="logo" id="logo">
            <small>Current Logo: <img src="<?php echo $logo; ?>" alt="Logo" style="width: 50px; height: 50px;"></small>
        </div>

        <div class="form-group">
            <label for="logo_text">Logo Text</label>
            <textarea class="form-control"name="logo_text" id="logo_text" placeholder="Enter Logo Text"><?php echo $logo_text; ?></textarea>
        </div>

        <div class="form-group">
            <label for="email">Email</label>
            <input type="email" class="form-control" name="email" id="email" value="<?php echo $email; ?>" placeholder="Enter Email">
        </div>

        <div class="form-group">
            <label for="phone">Phone</label>
            <input type="text" class="form-control" name="phone" id="phone" value="<?php echo $phone; ?>" placeholder="Enter Phone Number">
        </div>

        <div class="form-group">
            <label for="address">Address</label>
            <textarea class="form-control" name="address" id="address" placeholder="Enter Address"><?php echo $address; ?></textarea>
        </div>

        <div class="form-group">
            <label for="footer_text">Footer Text</label>
            <textarea class="form-control" name="footer_text" id="footer_text" placeholder="Enter Footer Text"><?php echo $footer_text; ?></textarea>
        </div>

        <input type="submit" value="Update" name="update" class="btn btn-primary">
    </form>
</div>
                </div>
            </div>
        </div>

    </div>

    <?php
if (isset($_POST['update'])) {
    $site_name = mysqli_real_escape_string($conn, $_POST['site_name']);
    $logo_text = mysqli_real_escape_string($conn, $_POST['logo_text']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $phone = mysqli_real_escape_string($conn, $_POST['phone']);
    $address = mysqli_real_escape_string($conn, $_POST['address']);
    $footer_text = mysqli_real_escape_string($conn, $_POST['footer_text']);

    // Handle logo upload
    if (!empty($_FILES['logo']['name'])) {
        $logo_tmp = $_FILES['logo']['tmp_name'];
        $logo_name = "upload/" . basename($_FILES['logo']['name']);
        move_uploaded_file($logo_tmp, $logo_name);
    } else {
        $logo_name = $logo; // Keep the current logo if no new file is uploaded
    }

    // Update the settings table
    $update_sql = "UPDATE settings SET 
        site_name='$site_name', 
        logo='$logo_name', 
        logo_text='$logo_text', 
        email='$email', 
        phone='$phone', 
        address='$address', 
        footer_text='$footer_text', 
        updated_at=NOW() 
        WHERE id='$id'";

    if (mysqli_query($conn, $update_sql)) {
        echo "<script>alert('Settings updated successfully!'); window.location.href='view_header_footer.php';</script>";
    } else {
        echo "<script>alert('Failed to update settings.');</script>";
    }
}
?>

</div>

<?php
include("includes/footer.php");
ob_end_flush(); // Send the buffered output to the client
?>