<?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['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'];
  		$AcAddress = $_POST['AcAddress'];
  		$AcContactNo = $_POST['AcContactNo'];
  		$AcEmailAddress = $_POST['AcEmailAddress'];
  		$Salary = $_POST['Salary'];
  		$AcMobileNo = $_POST['AcMobileNo'];
  		$AcPassward = $_POST['AcPassward'];
      $SecurityRights = $_POST['SecurityRights'];
  		$ClientID = $_POST['ClientID'];
		$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';

      $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);
		if($result!=null &&  $result>0 && $AcMobileNo!=''){
				
			// success
			$response["success"] = 0;
			$response["message"] = "Login Number Already Register";
			// echoing JSON response
			echo json_encode($response);
		}else{
				$sql = "SELECT * 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"] = $uniqnum;
				  $response["message"] = "Name Already Register";
				  $response["Account3Name"] = array();
      foreach($result as $row) { 
        $Account3Name["AcNameID"] = $row["AcNameID"];
        $Account3Name["AcName"] = $row["AcName"];
        $Account3Name["AcGroupID"] = $row["AcGroupID"];
        $Account3Name["AcAddress"] = $row["AcAddress"];
        $Account3Name["AcMobileNo"] = $row["AcMobileNo"];
        $Account3Name["AcContactNo"] = $row["AcContactNo"];
        $Account3Name["AcEmailAddress"] = $row["AcEmailAddress"];
        $Account3Name["AcDebitBal"] = $row["AcDebitBal"];
        $Account3Name["AcCreditBal"] = $row["AcCreditBal"];
        $Account3Name["AcPassward"] = $row["AcPassward"];
        $Account3Name["ClientID"] = $row["ClientID"];
        $Account3Name["ClientUserID"] = $row["ClientUserID"];
        $Account3Name["SysCode"] = $row["SysCode"];
        $Account3Name["NetCode"] = $row["NetCode"];
        $Account3Name["UpdatedDate"] = $row["UpdatedDate"];
        $Account3Name["SerialNo"] = $row["SerialNo"];
        $Account3Name["UserRights"] = $row["UserRights"];
        $Account3Name["SecurityRights"] = $row["SecurityRights"];
        $Account3Name["Salary"] = $row["Salary"];

        $current_date = date("Y-m-d H:i:s");
        $Account3Name["SessionDate"] = $current_date;

        // push single product into final response array
        array_push($response["Account3Name"], $Account3Name);
      }
				  // 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, AcGroupID, AcAddress, AcContactNo, AcEmailAddress, Salary, AcMobileNo, AcPassward, SecurityRights, ClientID, UpdatedDate, SerialNo,AcDebitBal,AcCreditBal,ClientUserID,SysCode,NetCode,UserRights,AccountPhoto) VALUES ('$MAX_ID','$AcName', '$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{
									/////////////inserted
								$response["success"] = 1;
								$response["AcNameID"] = $MAX_ID;
								$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);
    }
?>
