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

//check for required fields
if (isset($_POST['AcName']) && isset($_POST['AcAddress']) && isset($_POST['AcContactNo']) && isset($_POST['AcEmailAddress']) && isset($_POST['Salary'])
    && isset($_POST['AcMobileNo']) && isset($_POST['DisplayImage']) && isset($_POST['AcPassward']) && isset($_POST['SecurityRights']) && isset($_POST['ClientID']) && isset($_POST['AcGroupID']) && isset($_POST['SerialNo'])  &&  isset($_POST['AcDebitBal']) && isset($_POST['AcCreditBal']) && isset($_POST['ClientUserID']) && isset($_POST['SysCode']) && isset($_POST['NetCode']) && isset($_POST['UpdatedDate']) && isset($_POST['UserRights']) && isset($_POST['AccountPhoto'])) {

        $AcName = $_POST['AcName'];
        $NameOfPerson = $AcName;
  		$AcAddress = $_POST['AcAddress'];
  		$DisplayImage = $_POST['DisplayImage'];
  		$AcContactNo = $_POST['AcContactNo'];
  		$AcEmailAddress = $_POST['AcEmailAddress'];
  		$Salary = $_POST['Salary'];
  		$AcMobileNo = $_POST['AcMobileNo'];
  		$AcPassward = $_POST['AcPassward'];
        $SecurityRights = $_POST['SecurityRights'];
        $ClientID = $_POST['ClientID']; /////////////////////here ContryClientID is Coming from CountryDropDown liste which Selected
		$AcGroupID = $_POST['AcGroupID'];
		$SerialNo = $_POST['SerialNo'];
	
		
	////////////////New Added By Bilal
	////$AcNameID = $_POST['AcNameID'];
	$AcDebitBal = $_POST['AcDebitBal'];
	$AcCreditBal = $_POST['AcCreditBal'];
	$ClientUserID = $_POST['ClientUserID'];
	$SysCode = $_POST['SysCode'];
	$NetCode = $_POST['NetCode'];
	///$UpdatedDate = $_POST['UpdatedDate'];
	$UserRights = $_POST['UserRights'];
	$AccountPhoto = $_POST['AccountPhoto'];
	
	
   // include db connect class
       require_once __DIR__ . '/../DBConnect.php';
//  $CountryName = $_POST['ClientID'];
	  /////////////////////////////////////Settting Client ID
      $flag=false;
		
	//$ClientID = $_POST['ClientID'];
   // include db connect class
   
   ////////////////////////////////////////////////Mobile No is Checking
      $sql = "SELECT AcMobileNo FROM Account3Name WHERE AcMobileNo = '$AcMobileNo' AND ClientID = '$ClientID'";
		$params = array();
		$stmt = $conn->prepare($sql);
		$stmt->execute();
		$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
		
		/////////////////////////////////////////////// Email es checking
		 $sql1 = "SELECT * FROM Account3Name WHERE AcEmailAddress = '$AcEmailAddress' AND ClientID = '$ClientID'";
		$params1 = array();
		$stmt1 = $conn->prepare($sql1);
		$stmt1->execute();
		$result1 = $stmt1->fetchAll(PDO::FETCH_ASSOC);
		if($result1!=null &&  $result1>0){
				// success
			$response["success"] = -3;
			$response["message"] = "Email Already Registered";
			echo json_encode($response);
		}else if($result!=null &&  $result>0 && $AcMobileNo!=''){
				
			// success
			$response["success"] = -3;
			$response["message"] = "Login Number Already Register";
			// echoing JSON response
			echo json_encode($response);
			
		}else{
		    
		    ///////////////////////////////////////////////////// Account Name is Checking
				$sql = "SELECT AcName FROM Account3Name WHERE ClientID = '$ClientID' AND AcName = '$AcName'";
				$params = array();
				$stmt = $conn->prepare($sql);
				$stmt->execute();
				$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
				if($result!=null &&  $result>0){
				  // success
				  $response["success"] = 0;
				  $response["message"] = "Name Already Register";
				  // echoing JSON response
					echo json_encode($response);
				}else{
					$UpdatedDate = date("Y-m-d H:i:s");
					//////////////////////Getting Maximun AcNameID from DataBase
					$MAX_ID=0;
					$sqll="Select max(AcNameID) as maxid from Account3Name where ClientID = '$ClientID'";
						try {
								$stmt = $conn->prepare($sqll);
								$stmt->execute();
								$users = $stmt->fetchAll();
								foreach ($users as $user) {
									$MAX_ID = $user['maxid'] + 1;
								}
							}
							catch (PDOException $e) {
								echo $sql . "<br>" . $e->getMessage();
							}
						   
					////////////////////////////////////////////////////////////////
					$sql = "INSERT INTO Account3Name (AcNameID,AcName,NameOfPerson, AcGroupID, AcAddress, AcContactNo, AcEmailAddress, Salary, AcMobileNo, AcPassward, SecurityRights, ClientID, UpdatedDate, SerialNo,AcDebitBal,AcCreditBal,ClientUserID,SysCode,NetCode,UserRights,AccountPhoto) VALUES ('$MAX_ID','$MAX_ID','$NameOfPerson', '$AcGroupID', '$AcAddress', '$AcContactNo', '$AcEmailAddress', '$Salary', '$AcMobileNo', '$AcPassward', '$SecurityRights', '$ClientID', '$UpdatedDate','$SerialNo','$AcDebitBal','$AcCreditBal','$ClientUserID','$SysCode','$NetCode','$UserRights','$AccountPhoto');";
					try {
						   $result= $conn->exec($sql);		   
								if( $result === false ) {
								/////////////not inserted
									$response["success"] = 0;  
									$response["message"] = "User Not Register";
								echo json_encode($response);
								}else{
		
	
							     $response["success"] = 1;
								$response["AcNameID"] = $MAX_ID;
								$response["ClientID"] = $ClientID;
								$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 Param List Should Include (AcName, AcGroupID, AcAddress, AcContactNo, AcEmailAddress, Salary, AcMobileNo, AcPassward, SecurityRights, ClientID, UpdatedDate, SerialNo,AcDebitBal,AcCreditBal,ClientUserID,SysCode,NetCode,UserRights,AccountPhoto)";
      // echoing JSON response
      echo json_encode($response);
    }
?>
