
        <?php include 'include/navbar.php';?>
        <?php include 'include/sidebar.php';?>
     
            <div id="layoutSidenav_content">
                <main>
                    <div class="container-fluid px-4">
                        <h1 class="mt-4">Dashboard</h1>
                        <ol class="breadcrumb mb-4">
                            <li class="breadcrumb-item active">Update Website</li>
                        </ol>

                        <div class=" mt-4">

<?php

$result = mysqli_query($conn, "SELECT * FROM website_info LIMIT 1");
$data = mysqli_fetch_assoc($result);

if (isset($_POST['submit'])) {
    // Escape all inputs
    $phone = mysqli_real_escape_string($conn, $_POST['phone']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $address = mysqli_real_escape_string($conn, $_POST['address']);
    $whatsapp = mysqli_real_escape_string($conn, $_POST['whatsapp']);
    $about_content = mysqli_real_escape_string($conn, $_POST['about_content']);
    $home_banner_content = mysqli_real_escape_string($conn, $_POST['home_banner_content']);

    $facebook = mysqli_real_escape_string($conn, $_POST['facebook']);
    $instagram = mysqli_real_escape_string($conn, $_POST['instagram']);
    $twitter = mysqli_real_escape_string($conn, $_POST['twitter']);
    $linkedin = mysqli_real_escape_string($conn, $_POST['linkedin']);
    $youtube = mysqli_real_escape_string($conn, $_POST['youtube']);
    $tiktok = mysqli_real_escape_string($conn, $_POST['tiktok']);
    $pinterest = mysqli_real_escape_string($conn, $_POST['pinterest']);
    $website_name = mysqli_real_escape_string($conn, $_POST['website_name']);
    $title = mysqli_real_escape_string($conn, $_POST['title']);
    $description = mysqli_real_escape_string($conn, $_POST['description']);

    $logoName = $data['logo'] ?? '';
    $aboutImageName = $data['about_image'] ?? '';
    $bannerImageName = $data['home_banner_image'] ?? '';

    if (!empty($_FILES['logo']['name'])) {
        $logoName = time() . '_logo_' . basename($_FILES['logo']['name']);
        move_uploaded_file($_FILES['logo']['tmp_name'], '../uploads/' . $logoName);
    }

    if (!empty($_FILES['about_image']['name'])) {
        $aboutImageName = time() . '_about_' . basename($_FILES['about_image']['name']);
        move_uploaded_file($_FILES['about_image']['tmp_name'], '../uploads/' . $aboutImageName);
    }

    if (!empty($_FILES['home_banner_image']['name'])) {
        $bannerImageName = time() . '_banner_' . basename($_FILES['home_banner_image']['name']);
        move_uploaded_file($_FILES['home_banner_image']['tmp_name'], '../uploads/' . $bannerImageName);
    }

    $check = mysqli_query($conn, "SELECT * FROM website_info LIMIT 1");

    if (mysqli_num_rows($check) > 0) {
        $existing = mysqli_fetch_assoc($check);
        $id = (int)$existing['id'];

        $update = "UPDATE website_info SET 
            phone='$phone',
            email='$email',
            address='$address',
            whatsapp='$whatsapp',
            logo='$logoName',
            about_image='$aboutImageName',
            about_content='$about_content',
            home_banner_image='$bannerImageName',
            home_banner_content='$home_banner_content',
            facebook='$facebook',
            instagram='$instagram',
            twitter='$twitter',
            linkedin='$linkedin',
            youtube='$youtube',
            tiktok='$tiktok',
            pinterest='$pinterest',
            website_name='$website_name',
            title='$title',
            description='$description'
            WHERE id=$id";

        if (mysqli_query($conn, $update)) {
            echo "<p class='p-2 bg-success text-white text-center'>Data updated successfully.</p>";
        } else {
            echo "<p class='p-2 bg-danger text-white text-center'>Update failed: " . mysqli_error($conn) . "</p>";
        }
    } else {
        $insert = "INSERT INTO website_info 
            (logo, phone, email, address, whatsapp, about_image, about_content, home_banner_image, home_banner_content,
             facebook, instagram, twitter, linkedin, youtube, tiktok, pinterest, website_name, description, title)
            VALUES 
            ('$logoName', '$phone', '$email', '$address', '$whatsapp', '$aboutImageName', '$about_content', '$bannerImageName', '$home_banner_content',
             '$facebook', '$instagram', '$twitter', '$linkedin', '$youtube', '$tiktok', '$pinterest','$website_name', $description , $title)";

        if (mysqli_query($conn, $insert)) {
            echo "<p class='p-2 bg-success text-white text-center'>Data saved successfully.</p>";
        } else {
            echo "<p class='p-2 bg-danger text-white text-center'>Insert failed: " . mysqli_error($conn) . "</p>";
        }
    }
}
?>

<form action="" method="POST" enctype="multipart/form-data">
  <!-- Logo -->
  <?php if (!empty($data['logo'])): ?>
    <div class="mb-3">
      <label>Current Logo:</label><br>
      <img src="../uploads/<?php echo $data['logo']; ?>" width="150">
    </div>
  <?php endif; ?>
  <div class="mb-3">
    <label>Logo</label>
    <input type="file" name="logo" class="form-control">
  </div>

  <!-- Basic Info -->

  <input type="text" name="website_name" class="form-control mb-2" placeholder="Website Name" value="<?php echo $data['website_name'] ?? ''; ?>">



 <textarea name="title" class="form-control mb-2" placeholder="Webste title"><?php echo $data['title'] ?? ''; ?></textarea>

  <textarea name="description" class="form-control mb-2" placeholder="Description"><?php echo $data['description'] ?? ''; ?></textarea>



  
  <input type="text" name="phone" class="form-control mb-2" placeholder="Phone" value="<?php echo $data['phone'] ?? ''; ?>">
  <input type="email" name="email" class="form-control mb-2" placeholder="Email" value="<?php echo $data['email'] ?? ''; ?>">
  <input type="text" name="whatsapp" class="form-control mb-2" placeholder="WhatsApp" value="<?php echo $data['whatsapp'] ?? ''; ?>">
  <textarea name="address" class="form-control mb-2" placeholder="Address"><?php echo $data['address'] ?? ''; ?></textarea>

  <!-- About Section -->
  <?php if (!empty($data['about_image'])): ?>
    <div class="mb-3">
      <label>Current About Us Image:</label><br>
      <img src="../uploads/<?php echo $data['about_image']; ?>" width="150">
    </div>
  <?php endif; ?>
 

 <textarea name="about_content" class="form-control mb-2" placeholder="About Us Content"><?php echo $data['about_content'] ?? ''; ?></textarea>

  <!-- Home Banner -->
  <?php if (!empty($data['home_banner_image'])): ?>
    <div class="mb-3">
      <label>Current Home Banner Image:</label><br>
      <img src="../uploads/<?php echo $data['home_banner_image']; ?>" width="150">
    </div>
  <?php endif; ?>
  <input type="file" name="home_banner_image" class="form-control mb-2">
  <textarea name="home_banner_content" class="form-control mb-2" placeholder="Home Banner Content"><?php echo $data['home_banner_content'] ?? ''; ?></textarea>

  <!-- Social Media -->
  <input type="url" name="facebook" class="form-control mb-2" placeholder="Facebook Link" value="<?php echo $data['facebook'] ?? ''; ?>">
  <input type="url" name="instagram" class="form-control mb-2" placeholder="Instagram Link" value="<?php echo $data['instagram'] ?? ''; ?>">
  <input type="url" name="twitter" class="form-control mb-2" placeholder="Twitter Link" value="<?php echo $data['twitter'] ?? ''; ?>">
  <input type="url" name="linkedin" class="form-control mb-2" placeholder="LinkedIn Link" value="<?php echo $data['linkedin'] ?? ''; ?>">
  <input type="url" name="youtube" class="form-control mb-2" placeholder="YouTube Link" value="<?php echo $data['youtube'] ?? ''; ?>">
  <input type="url" name="tiktok" class="form-control mb-2" placeholder="TikTok Link" value="<?php echo $data['tiktok'] ?? ''; ?>">
  <input type="url" name="pinterest" class="form-control mb-2" placeholder="Pinterest Link" value="<?php echo $data['pinterest'] ?? ''; ?>">

  <button type="submit" name="submit" class="btn btn-primary mt-2"><?php echo $data ? 'Update' : 'Submit'; ?></button>
</form>




                    </div>
                </main>
                <?php include "include/footer.php";?>
            </div>
        </div>
<!-- JS TextEditor -->
<script src="//cdn.ckeditor.com/4.13.1/standard/ckeditor.js"></script>
         <script>
        CKEDITOR.replace('arContent');
    </script>
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
        <script src="js/scripts.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js" crossorigin="anonymous"></script>
        <script src="assets/demo/chart-area-demo.js"></script>
        <script src="assets/demo/chart-bar-demo.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/simple-datatables@latest" crossorigin="anonymous"></script>
        <script src="js/datatables-simple-demo.js"></script>
    </body>
</html>
