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

//check for required fields
if ( isset($_POST['ClientID']) ) {
		
		$TableID = $_POST['TableID'];
  		$PotionID = $_POST['PotionID'];
		$TableDescription = $_POST['TableDescription'];
  		$TableStatus = $_POST['TableStatus'];
  		$ClientID = $_POST['ClientID'];
  		$ClientUserID = $_POST['ClientUserID'];
  		$NetCode = $_POST['NetCode'];
  		$SysCode = $_POST['SysCode'];
		$UpdatedDate = $_POST['UpdatedDate'];
			$SalPur1ID = $_POST['SalPur1ID'];
		$TableName = $_POST['TableName'];

      // include db connect class
      require_once __DIR__ . '/../DBConnect.php';
               $parm=array();
	           $query="select max(TableID) as maxid from Restaurant2Table where ClientID =".$ClientID;
			   
			   try {
				$stmt = $conn->prepare($query);
				$stmt->execute();
				$users = $stmt->fetchAll();
				$TableID = 0;
				foreach ($users as $user) {
					$TableID = $user['maxid'] + 1;
				}
			}
			catch (PDOException $e) {
				echo $sql . "<br>" . $e->getMessage();
			}
	                  
			    $UpdatedDate = date("Y-m-d H:i:s");
				$sql = "INSERT INTO Restaurant2Table (TableID, PotionID, TableDescription, TableStatus, ClientID, ClientUserID, UpdatedDate, NetCode, SysCode, SalPur1ID, TableName) VALUES ('$TableID', '$PotionID', '$TableDescription', '$TableStatus', '$ClientID', '$ClientUserID', '$UpdatedDate', '$NetCode', '$SysCode', '$SalPur1ID', '$TableName');";
      		
				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["TableID"] = $TableID;
						$response["UpdatedDate"] = $UpdatedDate;
						$response["message"] = "User Register";
						echo json_encode($response);
						}
					}
				catch (PDOException $e) {
					echo $sql . "<br>" . $e->getMessage();
				}
        }else{
      // success
      $response["success"] = 0;
      $response["message"] = "Send All Required Fields ";
      // echoing JSON response
      echo json_encode($response);
    }
?>
