<?php

/**********************************************************************
* Section 1 : pre-processing 
*********************************************************************/

  include ('include_fns.php');
  include ('StatementsHeader.php');
  include ('BenefitCheckHeader.php');

  
  session_start();
//  ob_start();
  
  $action = $_GET['action'];
  
  $buttons = array();
  
  //append to this string if anything processed before header has output 
  $status = '';
   

  // need to process log in or out requests before anything else
  if($_POST['username']&&$_POST['password'])
  {
    $_SESSION['admin_user'] = $_POST['username'];
    $_SESSION['password']   = $_POST['password'];

    $login = login($_POST['username'], $_POST['password'], 0 );
    
    if ($login) 
    {
      $status .= "<p> </p>";

      $status .= "<p></p>";
      $status .= "<p></p>";

      $status .= "<p>&nbsp&nbspLogged in as <b>".$_POST['username']."</b></p>";
                      

      $_SESSION['admin_user'] = $_POST['username'];
      $_SESSION['password']   = $_POST['password'];
      
      
      $userinfo = ExecuteQuery("SELECT PRACNAME
                                FROM PROVIDER 
                                WHERE PRACNUM = '".$_SESSION['admin_user']."';");
                              
      $_SESSION['practice'] = $userinfo[0][0];
      
      
      $status .= "<p>&nbsp&nbspPractice Name: <b>".$userinfo[0][0]."</b></p>";
      
    }
    else
    {
      unset($_SESSION['admin_user']);
      unset($_SESSION['password']);

      $status .= "<p><b>Sorry, we could not log you in with that username and password.</b></p>
                      <p><b>Ensure that your desired user is ticked as Remote User.</b></p><br />";
    }
  }
  
  if($action == 'log-out')
  {
    if (isset($_SESSION['admin_user']))
    {
      $sessionid =  session_id();   
    }
    
    unset($action);
    $_SESSION=array();
    session_destroy();
  }




/**********************************************************************
* Section 2: set up and display headers 
*********************************************************************/

  if (check_admin_user())
  {
    
      $buttons[0] = 'view-statement';
      $buttons[1] = 'benefit-check';
  }
  else
    $buttons[0] = 'log-in';
  
  display_main_menu($buttons);

  //display any text generated by functions called before header
  echo $status;





/**********************************************************************
* Section 3: perform action
*********************************************************************/

  // only these actions can be done if not logged in
  switch ( $action )
  {
    case 'log-in' :
    case '':
    {
      if(!check_logged_in())
        display_login_form($action);
        
      break;
    }
  }
  
  // The following actions may only be performed by an admin user
  if(check_admin_user())
  {
    switch ( $action )
    {
      case 'view-statement':
      {
        Show_Statements_Header(); 
        break;
      }
      case 'benefit-check':
      {
        Show_BenefitCheck_Header(); 
        break;
      }
      
      case 'capture-claim':
      {

        break;
      }

    }
    

  } 

?> 
