<?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="">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);
  }


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

    $query = "SELECT id, sub_cat FROM sub_cat WHERE cat_id = $id";

    $result = mysqli_query($conn, $query);

    $str = '<option><option>';

    foreach($result as $result)
    {
        $str .= "<option value=". $result['id'] . ">" . $result['sub_cat'];       
    }
    echo json_encode($str);
  }



  if (array_key_exists('delete_item', $_POST)) {
    //echo "in";
    $id = htmlspecialchars(trim($_POST['id']), ENT_QUOTES, 'UTF-8');
    //echo $id;
    $sql = mysqli_query($conn, "DELETE FROM items WHERE item_code='$id'");
    $sql2 = mysqli_query($conn, "DELETE FROM sub_cat_mapping WHERE item_code='$id'"); 
    if ($sql) {
      $response = 'ok';
    }else{
      $response = 'error';
    }
    
    echo json_encode($response);
    //echo "In";
  }

  if (array_key_exists('load_item_up', $_POST)) {

    $data = array();
    $row = array();

    $sql = "SELECT * FROM items WHERE id = '".htmlspecialchars(trim($_POST['id']), ENT_QUOTES, 'UTF-8')."'";
    $result = mysqli_query($conn, $sql);
    $row2 = mysqli_fetch_assoc($result);

    foreach ($row2 as $key => $value) {
      $row = array_merge($row, array($key => htmlspecialchars_decode($value), ENT_QUOTES));
    }


    //echo htmlspecialchars_decode($row['item_name'], ENT_QUOTES);

    $itm_code = $row['item_code'];
    $file_name = '../upload/itm_dis/'.$itm_code.'/'.$itm_code.'.txt';
    
    if (file_exists($file_name)) {
      $myfile = fopen($file_name, "r");

      $itm_dis = fgets($myfile);

      $dis_arr = array();

      $dis_arr['description_m'] = $itm_dis;

      $data = array_merge($row, $dis_arr);
    }else{
      $data = array_merge($row, array('description_m' => ''));
    }
    

    echo json_encode($data);
  }
  
  if (array_key_exists('load_up_main_cat', $_POST)) {

    $id = htmlspecialchars(trim($_POST['id']), ENT_QUOTES, 'UTF-8');
    $sql = "SELECT id,cat FROM cat WHERE id = $id";

    $result = mysqli_query($conn, $sql);
    $row = mysqli_fetch_assoc($result);

    echo'<option value="' . $row['id'] . '">' . $row['cat'] . '</option>';

    $sql2 = " SELECT id,cat FROM cat ";

    $result2 = mysqli_query($conn, $sql2);
    
    while ($row2 = mysqli_fetch_assoc($result2)) {
      $data[] = $row2;
    }

    if (!empty($data)) {
    //echo'<option>Select Yout Province</option>';
      foreach ($data AS $data) {
        if($data['id'] != $row['id']) {
          echo'<option value="' . $data['id'] . '">' . $data['cat'] . '</option>';
        }
      }
    } else {
      echo'<option>No data found</option>';
    }
    echo json_encode($data);
  }

  if (array_key_exists('load_up_sub_cat', $_POST)) {

    $data = array();
    $data2 = array();
    $id = htmlspecialchars(trim($_POST['id']), ENT_QUOTES, 'UTF-8');
    $mid = htmlspecialchars(trim($_POST['mid']), ENT_QUOTES, 'UTF-8');

    $sql2 = " SELECT id,sub_cat FROM sub_cat WHERE cat_id = $mid";

    $result2 = mysqli_query($conn, $sql2);
    

    while ($row2 = mysqli_fetch_assoc($result2)) {
     $sub = $row2['id'].', ';

     $sql3 = "SELECT
     sub_cat_mapping.sub_cat
     FROM
     sub_cat_mapping
     WHERE sub_cat_mapping.itm_code =  '$id' AND sub_cat_mapping.sub_cat='$sub'";

     $result3 = mysqli_query($conn, $sql3);

     if(mysqli_num_rows($result3) == 0) {
       echo'<option value="' . $row2['id'] . '">' . $row2['sub_cat'] . '</option>';
     } else {
      //echo $row2['id'];
      echo'<option selected="selected" value="' . $row2['id'] . '">' . $row2['sub_cat'] . '</option>';
    }


  }
    //} else {
      //echo'<option>No data found</option>';
    //}
      //echo json_encode($data);
}
?>