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

  //check for required fields
	if (isset($_POST['ID3']) && isset($_POST['AddStatus']) && isset($_POST['AddTitle'])
	  && isset($_POST['AddDetail']) && isset($_POST['Price']) && isset($_POST['AddGPSPostFrom']) && isset($_POST['AddGPSLocation'])
	   && isset($_POST['AddArea']) && isset($_POST['AddCategory']) && isset($_POST['ContactNo']) && isset($_POST['NameOfPerson']) 
	   && isset($_POST['ClientID']) && isset($_POST['ClientUserID']) && isset($_POST['NetCode']) && isset($_POST['SysCode']) 
	   && isset($_POST['AddPostingDate']) && isset($_POST['Country']) && isset($_POST['City'])) {

        $AddPostingDate = $_POST['AddPostingDate'];
        $CAddPostingDate = date("Y-m-d", strtotime($AddPostingDate));
    		$ID3 = $_POST['ID3'];
    		$AddStatus = $_POST['AddStatus'];
    		$AddTitle = $_POST['AddTitle'];
    		$AddDetail = $_POST['AddDetail'];
    		$Price = $_POST['Price'];
    		$AddGPSPostFrom = $_POST['AddGPSPostFrom'];
    		$AddGPSLocation = $_POST['AddGPSLocation'];
    		$AddArea = $_POST['AddArea'];
		    $AddCategory = $_POST['AddCategory'];
			$ContactNo = $_POST['ContactNo'];
			$NameOfPerson = $_POST['NameOfPerson'];
			$CountryClientID = $_POST['ClientID'];
			$CountryClientUserID = $_POST['ClientUserID'];
			$NetCode = $_POST['NetCode'];
			$SysCode = $_POST['SysCode'];
			$Country = $_POST['Country'];
			$City = $_POST['City'];
		
        // include db connect class
    		require_once __DIR__ . '/../DBConnect.php';
			//$tablename="AddCategory4";
               $parm=array();
			   $query="select max(ClientAddNo) as ClientAddNo from AddCategory4 where ClientID= '$CountryClientID' and ClientUserID = '$CountryClientUserID'";
			   $query2="select max(ID4) as ID4 from AddCategory4";
			  try {
					$stmt = $conn->prepare($query);
					$stmt->execute();
					$users = $stmt->fetchAll();
					$ClientAddNo = 0;
					foreach ($users as $user) {
						$ClientAddNo = $user['ClientAddNo'] + 1;
					}

					$stmt2 = $conn->prepare($query2);
					$stmt2->execute();
					$users2 = $stmt2->fetchAll();
					$ID4 = 0;
					foreach ($users2 as $user) {
						$ID4 = $user['ID4'] + 1;
					}
				}
				catch (PDOException $e) {
					echo $sql . "<br>" . $e->getMessage();
				}
        $UpdatedDate = date("Y-m-d H:i:s");

        $sql = "INSERT INTO AddCategory4 (ID4 ,ID3, ClientAddNo, AddStatus, AddTitle, AddDetail, Price, AddGPSPostFrom, AddGPSLocation, AddArea, AddCategory, ContactNo, NameOfPerson, ClientID, ClientUserID, NetCode, SysCode, UpdatedDate, AddPostingDate, Country, City)
		 VALUES ('$ID4','$ID3', '$ClientAddNo', '$AddStatus', '$AddTitle', '$AddDetail', '$Price', '$AddGPSPostFrom', '$AddGPSLocation', '$AddArea', '$AddCategory', '$ContactNo', '$NameOfPerson', '$CountryClientID', '$CountryClientUserID', '$NetCode', '$SysCode', '$UpdatedDate', '$CAddPostingDate', '$Country', '$City');";
		
				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["ClientAddNo"] = $ClientAddNo;
						$response["UpdatedDate"] = $UpdatedDate;
						$response["ID4"] = $ID4;
						$response["message"] = "User Register";
						echo json_encode($response);
						}
					}
				catch (PDOException $e) {
					echo $sql . "<br>" . $e->getMessage();
				}

      }else{
          // no number found
          $response["success"] = 0;
          $response["message"] = "Send All Parameter List";
		  // echo no users JSON
		  echo json_encode($response);	
        }
?>
