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

//check for required fields
if isset(($_POST['AcGroupID']) && (isset($_POST['AcNameID']) && isset($_POST['AcName']) && isset($_POST['AcAddress']) && isset($_POST['AcContactNo']) && isset($_POST['AcEmailAddress']) && isset($_POST['AcMobileNo']) && isset($_POST['ClientID']) && isset($_POST['UserRights'])) && isset($_POST['ClientUserID']) && ($_POST['Remarks'])  ) && ($_POST['NameOfPerson'])  {

      $AcNameID = $_POST['AcNameID'];
      $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'];
		$UserRights = $_POST['UserRights'];
		$NameOfPerson = $_POST['NameOfPerson'];
		$Remarks = $_POST['Remarks'];
    // include db connect class
       require_once __DIR__ . '/../DBConnect.php';

			$sql = "SELECT AcMobileNo FROM Account3Name WHERE AcMobileNo = '$AcMobileNo' AND ClientID = '$ClientID' AND AcNameID != '$AcNameID'";
			$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 AcName FROM Account3Name WHERE ClientID = '$ClientID' AND AcName = '$AcName' AND AcNameID != '$AcNameID'";
			$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");
				$sql = "UPDATE Account3Name SET UserRights = '$UserRights',AcName = '$AcName', AcAddress = '$AcAddress', AcContactNo = '$AcContactNo', AcEmailAddress = '$AcEmailAddress', AcMobileNo = '$AcMobileNo',
				    ClientID = '$ClientID', AcGroupID = '$AcGroupID', UpdatedDate = '$UpdatedDate',Remarks = '$Remarks' , NameOfPerson = '$NameOfPerson' WHERE AcNameID = $AcNameID";
				$params = array();
				
				
				$result=0;
				try{	
					$stmt = $conn->prepare($sql);
					$stmt->execute();
					$result= $stmt->rowCount();
				}
				catch(PDOException $e){
					echo $sql . "<br>" . $e->getMessage();
				}
				if ( $result!= 0) {
						$rows_affected= "No information available.";
						$response["success"] = 1;
						$response["roweffected"] = $result;
						$response["UpdatedDate"] = $UpdatedDate;
						$response["message"] = "Update Successfully";
						// echoing JSON response
						echo json_encode($response);
				}else{
						$response["success"] = 0;
						$response["roweffected"] = $result;      
						$response["message"] = "Not updated row ".$result;
						// echoing JSON response
						echo json_encode($response);
				}
			}
		}
    }else{
      // success
      $response["success"] = 0;
      $response["message"] = "Send All Required Fields";
      // echoing JSON response
      echo json_encode($response);
    }
?>
