
<?php
   // session_start();
    include_once('../config/conn/db-connection.php');
    include_once('../config/class/sap_model.php');
    $SAP = new SAP_infosytem();

        if(ISSET($_POST)){

               $purpose = trim($_POST['purpose']);
               $beneficiaries_id = trim($_POST['beneficiaries_id']);

                $sql = 'SELECT * FROM tbl_beneficiaries WHERE beneficiaries_id = :beneficiaries_id';
                $statement = $db->prepare($sql);
                $statement->bindParam(':beneficiaries_id', $beneficiaries_id, PDO::PARAM_INT);
                $statement->execute();
                $bene = $statement->fetch(PDO::FETCH_ASSOC);
                if ($bene) {
                        $full_name =  ucfirst(htmlentities($bene['lastname'])) . ', ' . htmlentities(ucfirst($bene['firstname'])) .' ' . htmlentities(ucfirst($bene['middlename']));
                      } else {
                        echo "Error";
                  }

               $status ="New";

             // ==========audit trail==================
              // $user = trim($_POST['user']);
               if(!empty($_SERVER["HTTP_CLIENT_IP"])){
                     $ip = $_SERVER["HTTP_CLIENT_IP"];
                  }elseif(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){ 
                     $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
                  }else{
                     $ip = $_SERVER["REMOTE_ADDR"];
                }
               $host = gethostbyaddr($_SERVER['REMOTE_ADDR']);
               date_default_timezone_set("asia/manila");
               $logdate =  date('M-d-Y h:i A', strtotime("+0 HOURS"));
               $action = "The Beneficiarias member requested the SAP Form his/her name is '".$full_name."'";    
            // ==========end audit trail==============


               $SAP->request_sapform($purpose, $beneficiaries_id, $status, $full_name, $ip, $host,  $logdate, $action);
                  echo '<script type="text/javascript">';
                  echo 'setTimeout(function () { swal("Request SAP Form Successfully!","Message!","success");';
                  echo '}, 1000);</script>';
                  echo '<script type="text/javascript">';
                  echo 'setTimeout(function(){';
                  echo 'window.location = "/SAP_Information_System/Members/sap-form.php"';
                  echo '},1000)';
                  echo '</script>';
             
        }
?>