<?php
include '../db/db.php';
session_start();
$log=htmlspecialchars(trim($_SESSION['user']), ENT_QUOTES, 'UTF-8');
if (empty($log)) {

  $redirectUrl = "../../index.php";
  echo "<script type=\"text/javascript\">";
  echo "window.location.href = '$redirectUrl'";
  echo "</script>";

}else{

  $sqlu="SELECT * FROM details_of_users WHERE user='$log' and val='1'";
  $resultu=mysqli_query($conn, $sqlu);
  $countu=mysqli_num_rows($resultu);
  if($countu==1){

    $valid_extensions = array('jpeg', 'jpg', 'png', 'gif', 'bmp');
    if(isset($_FILES['itm_img']))
    {


      $sub_title = htmlspecialchars(trim($_POST['sub_title']), ENT_QUOTES, 'UTF-8');
      $main_cat = htmlspecialchars(trim($_POST['main_cat']), ENT_QUOTES, 'UTF-8');
      $description = htmlspecialchars(trim($_POST['description']), ENT_QUOTES, 'UTF-8');


      $path = '../upload/international_delivery_banner/';
      $img = htmlspecialchars(trim($_FILES['itm_img']['name']), ENT_QUOTES, 'UTF-8');
      $tmp = htmlspecialchars(trim($_FILES['itm_img']['tmp_name']), ENT_QUOTES, 'UTF-8');

      $ext = strtolower(pathinfo($img, PATHINFO_EXTENSION));

      $final_image = rand(1000,1000000).strtolower($img);

      if(in_array($ext, $valid_extensions)){

        $path = $path.strtolower($final_image); 
        if(move_uploaded_file($tmp,$path))
        {
          $sql = "INSERT INTO international_delivery_banner VALUES (NULL, '$final_image', '$sub_title', '$main_cat', '$description')";
          $result = mysqli_query($conn, $sql);
          if ($result) {
            echo "Data inserted";
          }
        }
      }
      else 
      {
        echo 'invalid';
      }
    }   


    if (array_key_exists('load_banner', $_POST)) {
      $id = htmlspecialchars(trim($_POST['id']), ENT_QUOTES, 'UTF-8');

      $sql = mysqli_query($conn, "SELECT * FROM international_delivery_banner WHERE id=$id"); 
      $row = mysqli_fetch_assoc($sql);

      echo json_encode($row);
    }

    if(isset($_POST['sub_title_up'])){

      if (!empty($_POST['itm_img_up'])) {

        $id = htmlspecialchars(trim($_POST['id']), ENT_QUOTES, 'UTF-8');
        $sub_title = htmlspecialchars(trim($_POST['sub_title_up']), ENT_QUOTES, 'UTF-8');
        $main_cat = htmlspecialchars(trim($_POST['main_cat_up']), ENT_QUOTES, 'UTF-8');
        $description = htmlspecialchars(trim($_POST['description_up']), ENT_QUOTES, 'UTF-8');

        $valid_extensions = array('jpeg', 'jpg', 'png', 'gif', 'bmp');
        $path = '../upload/international_delivery_banner/'; // upload directory
        $img = htmlspecialchars(trim($_FILES['itm_img_up']['name']), ENT_QUOTES, 'UTF-8');
        $tmp = htmlspecialchars(trim($_FILES['itm_img_up']['tmp_name']), ENT_QUOTES, 'UTF-8');

        // get uploaded file's extension
        $ext = strtolower(pathinfo($img, PATHINFO_EXTENSION));

        // can upload same image using rand function
        $final_image = rand(1000,1000000).$img;

        // check's valid format
        if(in_array($ext, $valid_extensions))
        {
          $path = $path.strtolower($final_image); 
        //echo "img logo name".$final_image;
          if(move_uploaded_file($tmp,$path))
          {
            $result = mysqli_query($conn, "UPDATE international_delivery_banner SET img_name='$final_image', sub_title='$sub_title', main_title='$main_cat', description='$description' WHERE id='$id'");
            if ($result) {
              echo "Data inserted";
            }
          }
        }
        else 
        {
          echo 'invalid';
        }
      }else{

        $id = htmlspecialchars(trim($_POST['id']), ENT_QUOTES, 'UTF-8');
        $sub_title = htmlspecialchars(trim($_POST['sub_title_up']), ENT_QUOTES, 'UTF-8');
        $main_cat = htmlspecialchars(trim($_POST['main_cat_up']), ENT_QUOTES, 'UTF-8');
        $description = htmlspecialchars(trim($_POST['description_up']), ENT_QUOTES, 'UTF-8');
        $result = mysqli_query($conn, "UPDATE international_delivery_banner SET sub_title='$sub_title', main_title='$main_cat', description='$description' WHERE id='$id'");
        if ($result) {
          echo "Data inserted without image";
        }else{
          echo 'invalid without image';
        }
      }
    }
    if (array_key_exists('del_export_banner', $_POST)) {
      $id=htmlspecialchars(trim($_POST['id']), ENT_QUOTES, 'UTF-8');

      $sqlc="SELECT img_name FROM international_delivery_banner WHERE id = '$id'";
      $resultc=mysqli_query($conn, $sqlc);
      while ($rowc = mysqli_fetch_array($resultc)){
        $img=$rowc['img_name'];
        unlink('../upload/international_delivery_banner/'.$img);
      }

      $sql2=mysqli_query($conn,"DELETE FROM international_delivery_banner WHERE id = '$id'");

      if ($sql2) {
        echo "Data Deleted";
      }else{
        echo "Invalid";
      }
    }

  } else{
    $redirectUrl = "index.php";
    echo "<script type=\"text/javascript\">";
    echo "window.location.href = '$redirectUrl'";
    echo "</script>";

  }
}
?>	
