<?php

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

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

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

            foreach($result as $row) { 
                $SalePurLocation["LocationID"]      = $row["LocationID"];
                $SalePurLocation["LocationName"]    = $row["LocationName"];
                $SalePurLocation["ClientID"]       = $row["ClientID"];
                $SalePurLocation["ClientUserID"]   = $row["ClientUserID"];
                $SalePurLocation["SysCode"]        = $row["SysCode"];
                $SalePurLocation["NetCode"]        = $row["NetCode"];
                $SalePurLocation["UpdatedDate"]    = $row["UpdatedDate"];

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

            try {
                $result= $conn->exec($sql);		   
                if( $result === false ) {
                    /////////////not inserted
                    $response["success"] = 0;  
                    $response["message"] = "Record Not Inserted";
                    echo json_encode($response);
                }else{
                    /////////////inserted//////////////
                    $response["success"] = 1;
                    $response["LocationID"] = $MAX_ID;
                    $response["UpdatedDate"] = $UpdatedDate;
                    $response["message"] = "Record Inserted";
                    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 (LocationID, LocationName, ClientID, UpdatedDate,ClientUserID,SysCode,NetCode)";
      // echoing JSON response
      echo json_encode($response);
    }

?>

