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

//check for required fields
if (isset($_POST['MessageID']) &&
isset($_POST['ReceiverMobileNo']) &&   
isset($_POST['ReceiveTime']) &&   
isset($_POST['ReadTime']) &&
isset($_POST['SenderMobieNo']) &&   
isset($_POST['NetCode']) &&                                           
isset($_POST['SysCode']) &&                                                                                      
isset($_POST['ReceiverStatus'])) {
    
    $MessageID= $_POST['MessageID']; 
	$ReceiverMobileNo= $_POST['ReceiverMobileNo']; 
	$ReceiveTime= $_POST['ReceiveTime'];   
	$ReadTime= $_POST['ReadTime'];   
	$SenderMobieNo= $_POST['SenderMobieNo'];
	$NetCode= $_POST['NetCode'];                                           
	$SysCode= $_POST['SysCode'];        
	$ReceiverStatus= $_POST['ReceiverStatus']; 
    
    // include db connect class
    require_once __DIR__ . '/../DBConnect.php';
    $parm  = array();
    $sql = "select max(ReceiverID) as maxid from Chat2Receiver";
	try {
        $stmt = $conn->prepare($sql);
        $stmt->execute();
        $users = $stmt->fetchAll();
		$ReceiverID = 0;
        foreach ($users as $user) {
			$ReceiverID = $user['maxid'] + 1;
        }
    }
    catch (PDOException $e) {
        echo $sql . "<br>" . $e->getMessage();
    }
   ///////////////////////////////now Importin in DB
   
	$UpdatedDate = date("Y-m-d H:i:s");
    $sql = "INSERT INTO `Chat2Receiver` (`MessageID`, `ReceiverID`, `ReceiverMobileNo`, `ReceiveTime`, `ReadTime`, `SenderMobieNo`,`NetCode`, `SysCode`, `UpdatedDate`, `ReceiverStatus`) 
	VALUES ('$MessageID', '$ReceiverID', '$ReceiverMobileNo', '$ReceiveTime', '$ReadTime', '$SenderMobieNo', '$NetCode', '$SysCode', '$UpdatedDate', '$ReceiverStatus');";
	
      try {
       $result= $conn->exec($sql);	
       //require __DIR__ . '/pusher.php';
			if( $result === false ) {
			/////////////not inserted
				$response["success"] = 0;  
				$response["message"] = "User Not Register";
			echo json_encode($response);
			}else{
				/////////////inserted
			$response["success"] = 1;
            $response["ReceiverID"] = $ReceiverID;
            $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";
    // echoing JSON response
    echo json_encode($response);
}
?>