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

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

    $data = array();

    $main = htmlspecialchars(trim($_POST['main']), ENT_QUOTES, 'UTF-8');
    $sub_cat = htmlspecialchars(trim($_POST['sub_cat']), ENT_QUOTES, 'UTF-8');
    $sql = "SELECT id,item_name FROM items WHERE cat_id = $main AND sub_cat_id = $sub_cat";
      //echo $sql;
    $result = mysqli_query($conn, $sql);
    echo'<option value="empty">Select Item...</option>';
    while ($row = mysqli_fetch_assoc($result)) {
      $data[] = $row;
    }

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


    if (array_key_exists('load_options', $_POST)) {//data table

      $sql = "SELECT * FROM options" ;

      //echo $sql;
      $data = array();
      $result = mysqli_query($conn,$sql);
      while ($row = mysqli_fetch_assoc($result)) {
        $data[] = $row;
      }
      echo json_encode($data);
    }

//////////////////////////////// * Option Mapping * ///////////////////////////////////////////////
    if (array_key_exists('option_map', $_POST)) {
      
      $item = htmlspecialchars(trim($_POST['item']), ENT_QUOTES, 'UTF-8');

      if (!isset($_POST['option'])) {
        $data = mysqli_query($conn, "DELETE FROM item_option_table WHERE item_id='$item'");

      }else{

        mysqli_query($conn, "DELETE FROM item_option_table WHERE item_id='$item'");

        foreach ($_POST['option'] as $value) {
          $sql1 = "INSERT INTO item_option_table SET `item_id`='$item', `option_id`='$value'";
          $data=mysqli_query($conn, $sql1); 
        }
      }
      if ($data) {
            echo json_encode(array("msgType" => 1, "msg" => "Successfully Add informations")); //if sql result true pass magType1
          } else {
            echo json_encode(array("msgType" => 2, "msg" => "Sorry ! Could not be Save your Data")); //if sql result true pass magType2
          }
        }

        if (array_key_exists('select_options', $_POST)) {
          $item_id = htmlspecialchars(trim($_POST['item_id']), ENT_QUOTES, 'UTF-8');
          $sql = "SELECT option_id FROM item_option_table WHERE item_id = $item_id";
      //echo $sql;
          $result = mysqli_query($conn, $sql);
          while ($row = mysqli_fetch_assoc($result)) {
            $data[] = $row;
          }

          echo json_encode($data);
        }

////////////////////////////// * Add data to options * ////////////////////////////////////////////////////////

        if (array_key_exists('add_data_options', $_POST)) {
          
          $itm_id = htmlspecialchars(trim($_POST['itm_id']), ENT_QUOTES, 'UTF-8');
          $op_id = htmlspecialchars(trim($_POST['op_id']), ENT_QUOTES, 'UTF-8');
          $op_data = htmlspecialchars(trim($_POST['op_data']), ENT_QUOTES, 'UTF-8');

          $sql1 = "INSERT INTO option_data_table SET `item_id`='$itm_id', `option_id`='$op_id' , `data`='$op_data'";
      //echo $sql1;
          $data=mysqli_query($conn, $sql1);

          if ($data) {
          echo json_encode(array("msgType" => 1, "msg" => "Successfully Add informations")); //if sql result true pass magType1
        } else {
          echo json_encode(array("msgType" => 2, "msg" => "Sorry ! Could not be Save your Data")); //if sql result true pass magType2
        }
      }

    if (array_key_exists('load_option_data', $_POST)) {//data table

      $item_id = htmlspecialchars(trim($_POST['item_id']), ENT_QUOTES, 'UTF-8');
      $op_id = htmlspecialchars(trim($_POST['op_id']), ENT_QUOTES, 'UTF-8');

      $sql = "SELECT * FROM option_data_table WHERE item_id = '$item_id' AND option_id = '$op_id'";

      //echo $sql;
      $data = array();
      $result = mysqli_query($conn,$sql);
      while ($row = mysqli_fetch_assoc($result)) {
        $data[] = $row;
      }
      echo json_encode($data);
    }

    if (array_key_exists('del_op_data', $_POST)) {
      /*echo $_POST['uname'];*/
      $id = htmlspecialchars(trim($_POST['id']), ENT_QUOTES, 'UTF-8');
      
      $sql2 = mysqli_query($conn, "DELETE FROM option_data_table WHERE id=$id");  
      
      if ($sql2) {
        $response['status'] = 'ok';
      }else{
        $response['status'] = 'error';
      }
      
      echo json_encode($response);
    //echo "In";
    }

    ?>