<?php
include('includes/header.php');
include('includes/navbar.php');
?>


<?php
if (isset($_POST['update_website'])) {
    $website_name = $_POST['website_name'];
    $website_url = $_POST['website_url'];
    $website_email = $_POST['website_email'];
    $admin_mail = $_POST['admin_mail'];
    $company_address = $_POST['company_address'];
    $live_chat_link = $_POST['live_chat_link'];

    if (empty($website_name) || empty($website_url) || empty($website_email)) {
      $error = "empty";
    }else {
      $sql_userupdate = "UPDATE settings SET website_name  = '$website_name', website_url = '$website_url', website_email = '$website_email',
      admin_mail = '$admin_mail', company_address = '$company_address', live_chat = '$live_chat_link' WHERE id = 1";
      if ($connection->query($sql_userupdate)===TRUE) {
          $error = "update_success";
          }else {
            $error = "error";
          }
    }
}
 ?>

<div class="container-fluid">

    <!-- DataTales Example -->
    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">Edit Website Details </h6>
        </div>
        <div class="card-body">
          <form action="" method="POST">
              <div class="form-group">
                  <label> Website Name </label>
                  <input type="text" name="website_name" value="<?php echo $website_name ?>" class="form-control" required>
              </div>
              <div class="form-group">
                  <label> Website URL </label>
                  <input type="text" name="website_url" value="<?php echo $website_url ?>" class="form-control" required onkeypress="return ignoreSpaces(event)">
              </div>
              <div class="form-group">
                  <label>Website Email</label>
                  <input type="email" name="website_email" value="<?php echo $website_email ?>" class="form-control" required>
              </div>
              <div class="form-group">
                  <label>Admin Email</label>
                  <input type="text" name="admin_mail" value="<?php echo $admin_mail ?>" class="form-control">
              </div>

              <div class="form-group">
                  <label>Company Address</label>
                  <textarea name="company_address" rows="8" cols="80" class="form-control"><?php echo $company_address ?></textarea>
              </div>

              <div class="form-group">
                  <label>Live Chat Link</label>
                  <textarea name="live_chat_link" rows="8" cols="80" class="form-control"><?php echo $live_chat_link ?></textarea>
              </div>

              <div class="form-group" style="display:none">
                  <label>Website Currency</label>
                  <input type="text" name="currency" value="<?php echo $currency ?>" class="form-control">
              </div>

              <button type="submit" name="update_website" class="btn btn-success"> Update Website </button>
          </form>

          <br>
          <?php
          if (isset($_POST['update_logo'])) {
              $extension = ['jpeg', 'JPEG', 'jpg', 'png', 'PNG'];
              $img4 = $_FILES['img4']['name'];
              $img_temp4 = $_FILES['img4']['tmp_name'];
              $folder4 = "../images/$img4";
              $ext4 = pathinfo($img4, PATHINFO_EXTENSION);
              $ext_name4 = str_replace('.'.$ext4, '', $img4);
              $replace_name4 = time().'.'.$ext4;
                if (in_array($ext4, $extension)) {
                  $x4 = '.'.$ext4;
                   $new_folder4 = str_replace($x4, $replace_name4, $folder4);
                   move_uploaded_file($img_temp4, $new_folder4);
                   $x_replace_name4 = $ext_name4.$replace_name4;
              }else {
                      $x_replace_name4 = $logo_img;
                      $error = "logo_error";
                    }

              $sql  = "UPDATE settings SET logo_img = '$x_replace_name4' WHERE id = 1";
              if ($connection->query($sql)===TRUE) {
                  $error = "logo_success";
              }else {
                $error = "error";
              }
          }

           ?>
          <form class="" action="" method="post" enctype="multipart/form-data">
            <div class="form-group">
                <label>Website Logo</label>
                <br>
                <a href="../images/<?php echo $logo_img ?>"><img src="../images/<?php echo $logo_img ?>" alt="logo" style="max-width:60%"></a>

                <br><br>
                <label>Upload Website Logo</label>
                <input type="file" name="img4" value="<?php echo $card_price ?>" class="form-control" required>
                <br>
                <button type="submit" name="update_logo" class="btn btn-success"> Update Logo </button>
            </div>
          </form>




          <br><br><br>
          <?php
          if (isset($_POST['update_favicon'])) {
              $extension = ['jpeg', 'JPEG', 'jpg', 'png', 'PNG'];
              $img1 = $_FILES['img1']['name'];
              $img_temp4 = $_FILES['img1']['tmp_name'];
              $folder4 = "../images/$img1";
              $ext4 = pathinfo($img1, PATHINFO_EXTENSION);
              $ext_name4 = str_replace('.'.$ext4, '', $img1);
              $replace_name4 = time().'.'.$ext4;
                if (in_array($ext4, $extension)) {
                  $x4 = '.'.$ext4;
                   $new_folder4 = str_replace($x4, $replace_name4, $folder4);
                   move_uploaded_file($img_temp4, $new_folder4);
                   $x_replace_name4 = $ext_name4.$replace_name4;
              }else {
                      $x_replace_name4 = $favicon;
                      $error = "logo_error";
                    }

              $sql  = "UPDATE settings SET favicon = '$x_replace_name4' WHERE id = 1";
              if ($connection->query($sql)===TRUE) {
                  $error = "favicon_success";
              }else {
                $error = "error";
              }
          }

           ?>
          <form class="" action="" method="post" enctype="multipart/form-data">
            <div class="form-group">
                <label>Website Favicon</label>
                <br>
                <a href="../images/<?php echo $favicon ?>"><img src="../images/<?php echo $favicon ?>" alt="logo" style="max-width:60%"></a>

                <br><br>
                <label>Upload Website Favicon</label>
                <input type="file" name="img1" value="<?php echo $card_price ?>" class="form-control" required>
                <br>
                <button type="submit" name="update_favicon" class="btn btn-success"> Update Favicon </button>
            </div>
          </form>
        </div>
    </div>
</div>

</div>

<p id="error" style='display: none'><?php echo $error?></p>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script>
    var error = document.getElementById('error');

    if (error.textContent == 'empty') {
         swal("ERROR!", "Input's cannot be empty!", "warning");

    }else if (error.textContent == "update_success") {
        swal("SUCCESS!", "Website Updated Successfully", "success");
        setTimeout(() => {
            window.location.href = 'site_setting.php'
        }, 2000);

    }else if (error.textContent == "logo_success") {
        swal("SUCCESS!", "Website Logo Updated Successfully", "success");
        setTimeout(() => {
            window.location.href = 'site_setting.php'
        }, 2000);

    }else if (error.textContent == "favicon_success") {
        swal("SUCCESS!", "Website Favicon Updated Successfully", "success");
        setTimeout(() => {
            window.location.href = 'site_setting.php'
        }, 2000);

    }else if (error.textContent == "error") {
        swal("ERROR!", "Sorry an error occurred. Please try again later", "warning");

    }else if (error.textContent == "logo_error") {
        swal("ERROR!", "The image format is not supported", "warning");
    }

</script>
<?php
include('includes/scripts.php');
include('includes/footer.php');
?>
