<?php

// array for JSON response
$response = array();

//check for required fields
if (isset($_POST['AcTypeID']) && isset($_POST['AcGruopName']) && isset($_POST['ClientID']) && isset($_POST['ClientUserID']) 
      && isset($_POST['NetCode']) && isset($_POST['SysCode'])) {

    $AcTypeID = $_POST['AcTypeID'];
    $AcGruopName = $_POST['AcGruopName'];
    $ClientID = $_POST['ClientID'];
	$ClientUserID = $_POST['ClientUserID'];
	$NetCode = $_POST['NetCode'];
	$SysCode = $_POST['SysCode'];
	
    // include db connect class
       require_once __DIR__ . '/../DBConnect.php';
    
        $sql = "SELECT * FROM Account2Group WHERE ClientID = '$ClientID' AND AcGruopName = '$AcGruopName'";
        $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["Account2Group"] = array();

            foreach($result as $row) { 
                $Account2Group["AcGroupID"]      = $row["AcGroupID"];
                $Account2Group["AcTypeID"]       = $row["AcTypeID"];
                $Account2Group["AcGruopName"]    = $row["AcGruopName"];
                $Account2Group["ClientID"]       = $row["ClientID"];
                $Account2Group["ClientUserID"]   = $row["ClientUserID"];
                $Account2Group["SysCode"]        = $row["SysCode"];
                $Account2Group["NetCode"]        = $row["NetCode"];
                $Account2Group["UpdatedDate"]    = $row["UpdatedDate"];

                $current_date = date("Y-m-d H:i:s");
                $Account2Group["SessionDate"] = $current_date;
                // push single product into final response array
                array_push($response["Account2Group"], $Account2Group);
            }
            // 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(AcGroupID) as maxid from Account2Group 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 Account2Group (AcGroupID,AcTypeID, AcGruopName, ClientID, UpdatedDate,ClientUserID,SysCode,NetCode) 
                                        VALUES ('$MAX_ID','$AcTypeID','$AcGruopName','$ClientID','$UpdatedDate','$ClientUserID','$SysCode','$NetCode');";

            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["AcGroupID"] = $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);
    }

?>

