<?php require_once('Connections/courier.php');
require_once("function/function.php");


// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  session_start();
}

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
  $_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset($_POST['username'])) {
  $loginUsername= validateUserInputs($_POST['username']);
  $password1 = validateUserInputs($_POST['password']);
  $password=md5($password1);
//  $shift_period=$_POST['shift_time'];
  $MM_fldUserAuthorization = "";
  $MM_redirectLoginSuccess = "index.php";
  $MM_redirectLoginFailed = "index.php?error";
  $MM_redirecttoReferrer = false;
  //mysql_select_db($database_courier, $courier);

/////////////// benefit api key ////////////////////////
  $_SESSION['benefit_api_key'] = '5146fc1e5b01711fd2db12297beca4ea';
  $_SESSION['provider_idd'] = '115';

  /////////////////  Pass record to John ////////////////

  $fields = array(
    'METHOD' => urlencode('GET_MEMBER_DETAILS'),
    'APIKEY' => urlencode('abaa6ba8663295bd60df9b8941484d31'),
    'USERNAME' => urlencode('GHIC'),
    'PASSWORD' => urlencode('PAR9DT'),
    'PROVIDERID' => urlencode('115'),
    'EMAIL' => $loginUsername,
    'MEMBER_NO' => $password1,

  );
  //print_r(json_encode($fields));exit();
  $resultSearch = onlineJohnAPI($fields);
  $decodeResultSearch = json_decode($resultSearch,"true");
  $data = $decodeResultSearch['RESULT'];
  $status = $decodeResultSearch['STATCODE'];
  if ($status == "200") {
    foreach ($data as $dataAll) {
      $member_no = 'GABG000525-090';//$dataAll['member_no'];
      $surname = 'Doe';//$dataAll['surname'];
      $firstname = 'John';//$dataAll['firstname'];
      $othernames = 'Manu';//$dataAll['othernames'];
      $dob = $dataAll['date_of_birth'];
      $sex = $dataAll['sex'];
      $telephone = $dataAll['telephone_no'];
      $email = $dataAll['email'];
      $member_plan1 = $dataAll['member_plan'];
      $employer = $dataAll['employer'];
      $principal_id = $dataAll['principal_id'];
      $ins_company = $dataAll['ins_company'];
      $member_id = $dataAll['member_id'];
      $member_type = $dataAll['member_type'];
      $provider_id = '115';
      $picpath = "https://rxhealthbeta.com/prosper/GAB_NEW/assets/images/avatar-placeholder.png";
      $picpath123 = "http://pics.rxhealthclaim.com/".$ins_company."/".$member_id.".jpg";
    }
  }else if ($status == "404") {
    $LoginRS__query = "SELECT * FROM users WHERE username=? AND password=? and status='1'";
    $LoginRS = $conn->prepare($LoginRS__query);
    $LoginRS->execute(array($loginUsername, $password));
    $row_LoginRS = $LoginRS->fetch(PDO::FETCH_ASSOC);
    //print_r($row_LoginRS);exit;
    $loginFoundUser = $LoginRS->rowCount();

    if ($loginFoundUser > 0) {
      $loginStrGroup = "";

      $_SESSION['myMM_Username'] = $loginUsername;
      $_SESSION['myMM_UserGroup'] = $loginStrGroup;
      $_SESSION['myMM_Fullname'] = $row_LoginRS['firstnames'] . ' ' . $row_LoginRS['surname'];
      $fullname = $row_LoginRS['firstnames'] . ' ' . $row_LoginRS['surname'];
      $_SESSION['myMM_Userid'] = $row_LoginRS['id'];
      $user_id = $row_LoginRS['id'];
      $_SESSION['security'] = $row_LoginRS['security'];
      $_SESSION['creditID'] = $row_LoginRS['provider_id'];
      $_SESSION['currentBranch'] = $row_LoginRS['branch'];

      //$_SESSION['securitystaff'] = $row_LoginRS['securitystaff'];

      $security = $_SESSION['security'];


    }
  }

  //exit();
  // $LoginRS__query="SELECT * FROM users WHERE username=? AND password=? and status='1'";
  // $LoginRS = $conn->prepare($LoginRS__query);
  // $LoginRS -> execute(array($loginUsername,$password));
  // $row_LoginRS = $LoginRS->fetch(PDO::FETCH_ASSOC);
  // //print_r($row_LoginRS);exit;
  // $loginFoundUser = $LoginRS->rowCount();

  if ($status == "200") {
    $provider_id=$provider_id;
    $query_rscarmodel11 = "SELECT * FROM provider_details where provider_id=?";
    $rscarmodel11=$conn->prepare($query_rscarmodel11);
    $rscarmodel11->execute(array($provider_id));
    $row_rscarmodel11 = $rscarmodel11->fetch(PDO::FETCH_ASSOC);

    $_SESSION['provider_id'] = $row_rscarmodel11['provider_id'];
    $_SESSION['provider_id_master'] = $row_rscarmodel11['provider_id_master'];
    $_SESSION['facility_name'] = $row_rscarmodel11['facility_name'];
    $_SESSION['facility_tel_number'] = $row_rscarmodel11['facility_tel_number'];
    $_SESSION['password'] = $row_rscarmodel11['password'];
    $_SESSION['apikey'] = $row_rscarmodel11['apikey'];
    $_SESSION['username'] = $row_rscarmodel11['username'];


    $loginStrGroup = "";
    $_SESSION['member_no'] = $member_no;
    $_SESSION['myMM_Username'] = $surname.' '.$firstname;
    $_SESSION['dob'] = $dob;
    $_SESSION['myMM_Fullname'] = $surname.' '.$firstname.' '.$othernames;
    $_SESSION['sex'] = $sex;
    $_SESSION['telephone'] = $telephone;
    $_SESSION['email'] = $email;
    $_SESSION['employer'] = $employer;
    $_SESSION['provider_id'] = $provider_id;
    $_SESSION['principal_id'] = $principal_id;
    $_SESSION['ins_company'] = $ins_company;
    $_SESSION['member_id'] = $member_id;
    $_SESSION['member_type'] = $member_type;
    $_SESSION['picpath'] = $picpath;


    $GET_PLAN = array(
      'METHOD' => urlencode('GET_PLAN'),
      'APIKEY' => $_SESSION['apikey'],
      'USERNAME' => $_SESSION['username'],
      'PASSWORD' => $_SESSION['password'],
      'PROVIDERID' => $_SESSION['provider_id'],
      'MEMBER_PLAN' => $member_plan1,

    );
    $get_saved_record = APICall($GET_PLAN);
    $get_saved_record_json = json_decode($get_saved_record,"true");
    $savedRecordPlan = $get_saved_record_json['RESULT'];
    foreach ($savedRecordPlan as $savedRecordPlans) {
    $member_plan = $savedRecordPlans['plan_group'];
    }
    
    $_SESSION['member_plan'] = $member_plan;


    $machineip = getenv("REMOTE_ADDR");


    if (isset($_SESSION['PrevUrl']) && false) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];	
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}
?>
