<?php
// array for JSON response
$response = array();

//check for required fields
if (isset($_POST['Item2GroupID']) &&
    isset($_POST['ItemName']) &&
    isset($_POST['ClientID']) && 
    isset($_POST['ClientUserID']) &&
    isset($_POST['NetCode']) &&
    isset($_POST['SysCode']) &&
    isset($_POST['SalePrice'])
    && isset($_POST['ItemCode'])
    && isset($_POST['Stock']) &&
    isset($_POST['WebUrl']) &&
    isset($_POST['MinimumStock']) &&
    isset($_POST['ItemDescription'])
)
{

    $Item2GroupID = $_POST['Item2GroupID'];
    $ItemName = $_POST['ItemName'];
    $ClientID = $_POST['ClientID'];
    $ClientUserID = $_POST['ClientUserID'];
    $NetCode = $_POST['NetCode'];
    $SysCode = $_POST['SysCode'];
    $SalePrice = $_POST['SalePrice'];
    $ItemCode = $_POST['ItemCode'];
    $Stock = $_POST['Stock'];
    $WebUrl = $_POST['WebUrl'];
    $MinimumStock = $_POST['MinimumStock'];
    $ItemDescription = $_POST['ItemDescription'];

    // include db connect class
    require_once __DIR__ . '/../DBConnect.php';
    $tablename = "Item3Name";
    $parm = array();
    $query = "select max(Item3NameID) as maxid from " . $tablename . " where ClientID=$ClientID";

    try
    {
        $stmt = $conn->prepare($query);
        $stmt->execute();
        $users = $stmt->fetchAll();
        $Item3NameID = 0;
        foreach ($users as $user)
        {
            $Item3NameID = $user['maxid'] + 1;
        }
    }
    catch(PDOException $e)
    {
        echo $sql . "<br>" . $e->getMessage();

    }

    $UpdatedDate = date("Y-m-d H:i:s");
    $sql = "INSERT INTO $tablename (
			  Item3NameID,
			  UpdatedDate, 
			  Item2GroupID, 
			  ItemName,
			  ClientID,    
           		ClientUserID, 
          	NetCode,                                            
              SysCode,                                                 
			SalePrice,                               
			ItemCode,                                           
			Stock,
			WebUrl,
			MinimumStock,
			ItemDescription
			) 
		  VALUES ( '$Item3NameID',
			  '$UpdatedDate', 
			  '$Item2GroupID', 
			  '$ItemName',
			  '$ClientID',    
           		'$ClientUserID', 
          	'$NetCode',                                            
              '$SysCode',                                                 
			'$SalePrice',                               
			'$ItemCode',                                           
			'$Stock',
			'$WebUrl',
			'$MinimumStock',
			'$ItemDescription'
			);";

    try
    {
        $result = $conn->exec($sql);
        if ($result === false)
        {
            /////////////not inserted
            $response["success"] = 0;
            $response["message"] = "User Not Register";
            echo json_encode($response);
        }
        else
        {
            /////////////inserted
            $response["success"] = 1;
            $response["Item3NameID"] = $Item3NameID;
            $response["UpdatedDate"] = $UpdatedDate;
            $response["message"] = "User Register";
            echo json_encode($response);
        }

    }
    catch(PDOException $e)
    {
        
				    	$response["success"] = 0;  
					$response["message"] = $e->getMessage();
						$response["sql"] =$sql;
							$response["Code"] = $e->getCode();
							if($e->getCode()==23000){
	$response["success"] = $uniqnum; 
      $sql = "SELECT * FROM Item3Name where ClientID= ".$ClientID." AND ItemName='$ItemName'";
      $params = array();
      $stmt = $conn->prepare($sql);
			$stmt->execute();
			$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
	if($result!=null &&  $result>0){
        $response["Item3Name"] = array();
       
		foreach($result as $row) { 
$Item3Name["ID"] = $row["ID"];          
$Item3Name["Item3NameID"] = $row["Item3NameID"]; 
$Item3Name["Item2GroupID"] = $row["Item2GroupID"]; 
$Item3Name["ItemName"] = $row["ItemName"];                                           
$Item3Name["ClientID"] = $row["ClientID"];    
$Item3Name["ClientUserID"] = $row["ClientUserID"];  
$Item3Name["NetCode"] = $row["NetCode"];                                            
$Item3Name["SysCode"] = $row["SysCode"];                                            
$Item3Name["UpdatedDate"] = $row["UpdatedDate"];             
$Item3Name["SalePrice"] = $row["SalePrice"];                               
$Item3Name["ItemCode"] = $row["ItemCode"];
		 $Item3Name["Stock"] = $row["Stock"];
		  $Item3Name["WebUrl"] = $row["WebUrl"];
		   $Item3Name["MinimumStock"] = $row["MinimumStock"];
		    $Item3Name["ItemDescription"] = $row["ItemDescription"];

          // push single product into final response array
          array_push($response["Item3Name"], $Item3Name);
        }
	
        // success
         //$response["success"] = 1;
      
        // echoing JSON response
       // echo json_encode($response);
      }else{
		  // success
         $response["success"] = 0;
        $response["message"] = "ClientID Not Exist";
        // echoing JSON response
        echo json_encode($response);
	 }
							echo json_encode($response);
    
	
}
	}
}else
{
    // success
    $response["success"] = 0;
    $response["message"] = "Send All Required Fields( Item2GroupID, ItemName,ClientID,
         ClientUserID,
         NetCode,
         SysCode,
         SalePrice,
         ItemCode,                                           
		 Stock,
		 WebUrl,
		 MinimumStock,
		 ItemDescription,
		 )";
    // echoing JSON response
    echo json_encode($response);
}
?>
