<?php

$response = array();


//check for required fields
if ( isset($_POST['Item1TypeID']) && isset($_POST['Item2GroupName']) && isset($_POST['ClientID']) && isset($_POST['ClientUserID']) && isset($_POST['NetCode'])&& isset($_POST['SysCode']) ) 
	{
		$Item1TypeID = $_POST['Item1TypeID'];
  		$Item2GroupName = $_POST['Item2GroupName'];
  		$ClientID = $_POST['ClientID'];
  		$ClientUserID = $_POST['ClientUserID'];
  		$NetCode = $_POST['NetCode'];
  		$SysCode = $_POST['SysCode'];

      // include db connect class
       require_once __DIR__ . '/../DBConnect.php';
               $parm=array();
	           $query="select max(Item2GroupID) as maxid from Item2Group where ClientID =".$ClientID;
	          
			  try {
					$stmt = $conn->prepare($query);
					$stmt->execute();
					$users = $stmt->fetchAll();
					$Item2GroupID = 0;
					foreach ($users as $user) {
						$Item2GroupID = $user['maxid'] + 1;
					}
				}
				catch (PDOException $e) {
					echo $sql . "<br>" . $e->getMessage();
				}
		 $UpdatedDate = date("Y-m-d H:i:s");
          $sql = "INSERT INTO Item2Group (Item2GroupID,Item1TypeID, Item2GroupName, ClientID, ClientUserID, NetCode, SysCode, UpdatedDate) VALUES ($Item2GroupID,$Item1TypeID, '$Item2GroupName',$ClientID, $ClientUserID, $NetCode, $SysCode, '$UpdatedDate')";
        //  $params = array($Item2GroupID,$Item1TypeID, $Item2GroupName,$ClientID, $ClientUserID, $NetCode, $SysCode, $UpdatedDate);
      		
			
			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["Item2GroupID"] = $Item2GroupID;
				$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;  
							   try {
		require_once __DIR__ . '/../DBConnect.php';
			 $sql = "SELECT * FROM Item2Group where ClientID= $ClientID AND Item2GroupName='$Item2GroupName' ";
			$stmt = $conn->prepare($sql);
			$stmt->execute();
			$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
			   if($result!=null &&  $result>0){
					$response["Item2Group"] = array();
					foreach($result as $row) { 
						$Item2Group["ID"] = $row["ID"];          
						$Item2Group["Item2GroupID"] = $row["Item2GroupID"]; 
						$Item2Group["Item1TypeID"] = $row["Item1TypeID"];
						$Item2Group["Item2GroupName"] = $row["Item2GroupName"];                                     
						$Item2Group["ClientID"] = $row["ClientID"];    
						$Item2Group["ClientUserID"] = $row["ClientUserID"];
						$Item2Group["NetCode"] = $row["NetCode"];                                          
						$Item2Group["SysCode"] = $row["SysCode"];                                            
						$Item2Group["UpdatedDate"] = $row["UpdatedDate"];
						
					  // push single product into final response array
					  array_push($response["Item2Group"], $Item2Group);

					}
					 // success
				//	$response["success"] = 1;
					// echoing JSON response
				//	echo json_encode($response);
				}
				else{
					
			 // success
				$response["success"] = -99;
				$response["message"] = "No Data Availaible For ".$sql;
				
			   // echoing JSON response
			//	echo json_encode($response);
					
				}
		}
		catch(PDOException $e){
			echo "Error: " . $e->getMessage();
			}
							}
					echo json_encode($response);
				}
			
		}else{
		  // success
		  $response["success"] = 0;
		  $response["message"] = "Send All Required Fields(Item2GroupID,Item1TypeID,Item2GroupName,ClientID,ClientUserID,NetCode, SysCode)";
		  // echoing JSON response
		  echo json_encode($response);
		}
		function getFromItem2Group($parm1,$parm2,$parm3) {
   
			try {
		require_once __DIR__ . '/../DBConnect.php';
			 $sql = "SELECT * FROM Item2Group where ClientID= $parm2 AND Item2GroupName='$parm1' AND ClientUserID='$parm3'";
			$stmt = $conn->prepare($sql);
			$stmt->execute();
			$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
			   if($result!=null &&  $result>0){
					$response["Item2Group"] = array();
					foreach($result as $row) { 
						$Item2Group["ID"] = $row["ID"];          
						$Item2Group["Item2GroupID"] = $row["Item2GroupID"]; 
						$Item2Group["Item1TypeID"] = $row["Item1TypeID"];
						$Item2Group["Item2GroupName"] = $row["Item2GroupName"];                                     
						$Item2Group["ClientID"] = $row["ClientID"];    
						$Item2Group["ClientUserID"] = $row["ClientUserID"];
						$Item2Group["NetCode"] = $row["NetCode"];                                          
						$Item2Group["SysCode"] = $row["SysCode"];                                            
						$Item2Group["UpdatedDate"] = $row["UpdatedDate"];
						
					  // push single product into final response array
					  array_push($response["Item2Group"], $Item2Group);

					}
					 // success
					$response["success"] = 1;
					// echoing JSON response
				//	echo json_encode($response);
				}
				else{
					
			 // success
				$response["success"] = 0;
				$response["message"] = "No Data Availaible";
			   // echoing JSON response
			//	echo json_encode($response);
					
				}
		}
		catch(PDOException $e){
			echo "Error: " . $e->getMessage();
			}
}
		//$conn = null;
?>
