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

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