<?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) {

    $sqlc = "SELECT * FROM details_of_users where user='$log'";
    $resultc = mysqli_query($conn, $sqlc);
    while ($rowc = mysqli_fetch_array($resultc)) {
      $name = $rowc['id'];
    }
  } else {
    $redirectUrl = "index.php";
    echo "<script type=\"text/javascript\">";
    echo "window.location.href = '$redirectUrl'";
    echo "</script>";
  }
}

if (array_key_exists('load_subcat', $_POST)) {
  $id = htmlspecialchars(trim($_POST['id']), ENT_QUOTES, 'UTF-8');
  $sql = "SELECT id,sub_cat FROM sub_cat WHERE cat_id = $id";
  //echo $sql;
  $result = mysqli_query($conn, $sql);
  echo '<option value="empty">Select Sub Category...</option>';
  while ($row = mysqli_fetch_assoc($result)) {
    $data[] = $row;
  }

  if (!empty($data)) {
    foreach ($data as $data) {
      echo '<option value="' . $data['id'] . '">' . $data['sub_cat'] . '</option>';
    }
  } else {
    echo '<option>No data found</option>';
  }
  echo json_encode($data);
}

//strip_tags();
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif', 'bmp');


if (isset($_FILES['itm_img'])) {
  $itm_name = htmlspecialchars(trim($_POST['item_name']), ENT_QUOTES, 'UTF-8');
  $itm_code = htmlspecialchars(trim($_POST['item_code']), ENT_QUOTES, 'UTF-8');
  $itm_price = htmlspecialchars(trim($_POST['price']), ENT_QUOTES, 'UTF-8');
  $itm_discrip = htmlspecialchars(trim($_POST['discription']), ENT_QUOTES, 'UTF-8');
  $tag_text = htmlspecialchars(trim($_POST['tag_text']), ENT_QUOTES, 'UTF-8');
  $main_cat = htmlspecialchars(trim($_POST['cat_main']), ENT_QUOTES, 'UTF-8');
  $sub_cat = '0';

  foreach ($_POST['sub_cat'] as $selectedOption) {
    $sql_sub_check = "SELECT * FROM sub_cat_mapping WHERE itm_code = '$itm_code' AND sub_cat = '$selectedOption'";
    $result_sub_check = mysqli_query($conn, $sql_sub_check);

    if (mysqli_num_rows($result_sub_check) == 0) {

      $sql_sub = "INSERT INTO sub_cat_mapping SET itm_code = '$itm_code', sub_cat = '$selectedOption'";
      $result_sub = mysqli_query($conn, $sql_sub);
    }
  }

  $des = $_POST['des'];
  mkdir('../upload/itm_dis/' . $itm_code);
  $file_name = '../upload/itm_dis/' . $itm_code . '/' . $itm_code . '.txt';
  $myfile = fopen($file_name, "w") or die("Unable to open file!");
  //$txt = "John Doe\n";
  fwrite($myfile, $des);
  fclose($myfile);

  //$des = mysqli_real_escape_string($conn, $des);
  //echo $des;
  //echo "IN An Working";
  if (isset($_POST['featue_pro'])) {
    $featue_pro = 1;
  } else {
    $featue_pro = 0;
  }

  if (isset($_POST['new_pro'])) {
    $new_pro = 1;
  } else {
    $new_pro = 0;
  }

  if (isset($_POST['ex_delivery'])) {
    $ex_delivery = 1;
  } else {
    $ex_delivery = 0;
  }

  if (isset($_POST['soled_out'])) {
    $soled_out = 1;
  } else {
    $soled_out = 0;
  }

  if (isset($_POST['home_page'])) {
    $home_page = 1;
  } else {
    $home_page = 0;
  }

  if (isset($_POST['int_delivery'])) {
    $int_delivery = 1;
  } else {
    $int_delivery = 0;
  }

  if (isset($_POST['same_delivery'])) {
    $same_delivery = 1;
  } else {
    $same_delivery = 0;
  }

  //echo $main_cat;
  mkdir('../upload/item/' . $itm_code);
  $path = '../upload/item/' . $itm_code . '/'; // upload directory
  $img = htmlspecialchars(trim($_FILES['itm_img']['name']), ENT_QUOTES, 'UTF-8');
  $tmp = htmlspecialchars(trim($_FILES['itm_img']['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) . strtolower($img);

  // check's valid format
  if (in_array($ext, $valid_extensions)) {
    $path = $path . $final_image;
    //echo "img logo name".$final_image;
    if (move_uploaded_file($tmp, $path)) {
      $sql = "INSERT INTO items VALUES (NULL, '$main_cat', '$sub_cat' , '$itm_code' , '$itm_name' , '$itm_price' , '0.00' , '$itm_discrip' , '$tag_text' , '' , '$final_image' , '$name' , '$featue_pro' , '$new_pro', '$ex_delivery', '$soled_out', '$home_page', '$int_delivery','$same_delivery')";
      $result = mysqli_query($conn, $sql);
      if ($result) {

        $sql_id = "SELECT id FROM items WHERE item_code = '$itm_code'";
        $result_id = mysqli_query($conn, $sql_id);
        $row_id = mysqli_fetch_assoc($result_id);
        $stock_itm_id = $row_id['id'];

        mysqli_query($conn, "INSERT INTO stock SET stock_card='$stock_itm_id',ref='', dr_qty='20', date='" . date("Y-m-d") . "'");

        echo "inserted";
      } else {
        echo "sqlerror";
      }
    } else {
      echo "uploaderror";
    }
  } else {
    echo 'invalid';
  }
}

if (array_key_exists('del_cat', $_POST)) {
  /*echo $_POST['uname'];*/
  $id = htmlspecialchars(trim($_POST['id']), ENT_QUOTES, 'UTF-8');

  $sql = mysqli_query($conn, "DELETE FROM cat WHERE id=$id");

  if ($sql) {
    $response['status'] = 'ok';
  } else {
    $response['status'] = 'error';
  }

  echo json_encode($response);
  //echo "In";
}

if (isset($_FILES['up_itm_img'])) {

  $itm_name = htmlspecialchars(trim($_POST['up_item_name']), ENT_QUOTES, 'UTF-8');
  $itm_code = htmlspecialchars(trim($_POST['up_item_code']), ENT_QUOTES, 'UTF-8');
  $itm_price = htmlspecialchars(trim($_POST['up_price']), ENT_QUOTES, 'UTF-8');
  $itm_discrip = htmlspecialchars(trim($_POST['up_discription']), ENT_QUOTES, 'UTF-8');
  $main_cat = htmlspecialchars(trim($_POST['up_cat_main']), ENT_QUOTES, 'UTF-8');
  $sub_cat = '';
  $id_holder = htmlspecialchars(trim($_POST['id_holder']), ENT_QUOTES, 'UTF-8');
  $tag_text = htmlspecialchars(trim($_POST['up_tag_text']), ENT_QUOTES, 'UTF-8');
  $des_up = $_POST['des2'];

  $sub_cat = '0';

  $itm_ori_price = '';

  if (isset($_POST['itm_ori_price'])) {
    $itm_ori_price = htmlspecialchars(trim($_POST['itm_ori_price']), ENT_QUOTES, 'UTF-8');
  }


/*   $sql_sub_check = "SELECT * FROM sub_cat_mapping WHERE itm_code = '$itm_code'";
  $result_sub_check = mysqli_query($conn, $sql_sub_check);

  if (mysqli_num_rows($result_sub_check) > 0) {

    $sql_del = "DELETE FROM sub_cat_mapping WHERE itm_code = '$itm_code'";
    $result_del = mysqli_query($conn, $sql_del);

    if ($result_del) {
      foreach ($_POST['up_sub_cat'] as $selectedOption) {
        $sql_sub = "INSERT INTO sub_cat_mapping SET itm_code = '$itm_code', sub_cat = '$selectedOption'";
        $result_sub = mysqli_query($conn, $sql_sub);
      }
    }
  } else {
    foreach ($_POST['up_sub_cat'] as $selectedOption) {
      $sql_sub = "INSERT INTO sub_cat_mapping SET itm_code = '$itm_code', sub_cat = '$selectedOption'";
      $result_sub = mysqli_query($conn, $sql_sub);
    }
  } */



  $file_name = '../upload/itm_dis/' . $itm_code . '/' . $itm_code . '.txt';

  if (!file_exists($file_name)) {
    mkdir('../upload/itm_dis/' . $itm_code);
  }
  $myfile = fopen($file_name, "w") or die("Unable to open file!");
  //$txt = "John Doe\n";
  fwrite($myfile, $des_up);
  fclose($myfile);

  //$des_up = mysqli_real_escape_string($conn, $des_up);
  if (isset($_POST['up_featue_pro'])) {
    $featue_pro = 1;
  } else {
    $featue_pro = 0;
  }

  if (isset($_POST['up_new_pro'])) {
    $new_pro = 1;
  } else {
    $new_pro = 0;
  }

  if (isset($_POST['up_ex_delivery'])) {
    $ex_delivery = 1;
  } else {
    $ex_delivery = 0;
  }

  if (isset($_POST['up_soled_out'])) {
    $soled_out = 1;
  } else {
    $soled_out = 0;
  }

  if (isset($_POST['up_home_page'])) {
    $home_page = 1;
  } else {
    $home_page = 0;
  }

  if (isset($_POST['up_int_delivery'])) {
    $int_delivery = 1;
  } else {
    $int_delivery = 0;
  }

  if (isset($_POST['up_same_delivery'])) {
    $same_delivery = 1;
  } else {
    $same_delivery = 0;
  }

  //echo $new_pro;

  //echo $main_cat;

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

  if (!file_exists($path)) {
    mkdir($path);
  }
  // get uploaded file's extension
  $ext = strtolower(pathinfo($img, PATHINFO_EXTENSION));

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

  // check's valid format
  if (in_array($ext, $valid_extensions)) {
    $path = $path . $final_image;
    //echo "img logo name".$final_image;
    if (move_uploaded_file($tmp, $path)) {
      $sql = "UPDATE items SET item_code = '$itm_code' , item_name = '$itm_name' , price = '$itm_price', ori_price='$itm_ori_price' , discription = '$itm_discrip' , description_more = '' , f_img = '$final_image' , featue_pro = '$featue_pro' , new_pro = '$new_pro' , tag_text = '$tag_text', ex_delivery = '$ex_delivery', soled_out='$soled_out', home_page='$home_page', int_delivery='$int_delivery', same_delivery='$same_delivery' WHERE id= '$id_holder'";
      $result = mysqli_query($conn, $sql);
      if ($result) {
        echo "inserted";
      } else {
        echo "sqlerror";
      }
    } else {
      echo "uploaderror";
    }
  } else {
    $sql2 = "UPDATE items SET item_code = '$itm_code' , item_name = '$itm_name' , price = '$itm_price', ori_price='$itm_ori_price' , discription = '$itm_discrip' , description_more = '$des_up' , featue_pro = '$featue_pro' , new_pro = '$new_pro' , tag_text = '$tag_text', ex_delivery = '$ex_delivery', soled_out='$soled_out', home_page='$home_page', int_delivery='$int_delivery', same_delivery='$same_delivery' WHERE id= '$id_holder'";
    $result2 = mysqli_query($conn, $sql2);
    if ($result2) {
      echo "inserted";
    } else {
      echo "sqlerror";
    }
  }
}
