<?php

  include ('include_fns.php');

  session_start();

  ?>

  <script>

    var JTable = function() {};

    JTable.Setup = function() {
      var table = $('.jtable');
      $('caption', table).addClass('ui-state-default');
      $('th', table).addClass('ui-state-default');
      $('td', table).toggleClass('ui-widget-content');
    };



    $(function() {
      JTable.Setup();
    });
      
  </script>

  <?php
  
  function getwizardinfo($wizardheader, $account, $patient)
  {
        if($conn=db_connect())
        {

          if ($wizardheader == 'Patient')
          {
            $query = "SELECT T.TITLE_DESC AS TITLE, P.INITIALS AS INITIALS, P.SEX, P.IDNUMBER AS ID_NO, P.BIRTHDAY,
                      P.PHONE1 AS PHONE_1,P.PHONE2 AS PHONE_2,P.CELL, P.EMAIL,
                      P.HOMELANGUAGE AS HOME_LANGUAGE, P.RELATIONSHIP, P.EMPLOYER,
                      P.OCCUPATION, P.PATIENTALERTMSG AS ALERT_MESSAGE, ATT.FIRSTNAME ||' '||ATT.SURNAME AS ATTORNEY,
                      R.FIRSTNAME ||' '|| R.SURNAME AS SECRETARY,  A.CASENO AS FILE_REF, C.COURTHOUSE_NAME AS COURT_HOUSE, A.COURTDATE AS COURT_DATE, 
                      I.INJURY_DESC AS INJURY, M.MATTERTYPE_DESC AS MATTER_TYPE
                      FROM PATIENT P
                      LEFT OUTER JOIN ACCOUNT A ON A.ACCOUNT_KEY = P.ACCOUNT_KEY
                      LEFT OUTER JOIN TITLE T ON T.TITLE_KEY = P.TITLE_KEY
                      LEFT OUTER JOIN ATTORNEY ATT ON ATT.ATTORNEY_KEY = A.ATTORNEY_KEY
                      LEFT OUTER JOIN RECEPTIONIST R ON R.RECEPTIONIST_KEY = A.RECEPTIONIST_KEY
                      LEFT OUTER JOIN COURTHOUSE C ON C.COURTHOUSE_KEY = A.COURTHOUSE_KEY
                      LEFT OUTER JOIN INJURY I ON I.INJURY_KEY = A.INJURY_KEY
                      LEFT OUTER JOIN MATTERTYPE M ON M.MATTERTYPE_KEY = A.MATTERTYPE_KEY
                      WHERE P.ACCOUNT_KEY ='$account' AND P.PATIENT_KEY = '$patient' ";
          }
          else
          if ($wizardheader == 'Address')
          {
            $query = "SELECT A.ADDRESS_TYPE, A.ADDRESS1 AS ADDRESS_1, A.ADDRESS2 AS ADDRESS_2,  A.ADDRESS3 AS ADDRESS_3, A.TOWN, A.POSTALCODE AS POSTAL_CODE
                      FROM ADDRESS A
                      WHERE A.ACCOUNT_KEY ='$account' AND A.PATIENT_KEY = '$patient' ";
          }

          $result = ibase_query($conn, $query);

          if (!$result)
          {
            if ($wizardheader == 'Appointments')
            {
              echo "No Appointments exist for this Account";
              exit;
            }
            else
            {
              echo "Patient could not be found.";
              echo $query;
              exit;
            }
          }
          else
          {
            $numcol = ibase_num_fields($result);
            $i = 0;
            $j = 0;

            while ($row=ibase_fetch_row ($result))
            {

             $wizardinfo = '{';
             $tempinfo   = '';
             
             for ($j = 0; $j < $numcol; $j++)
             {
               $colinfo = ibase_field_info($result, $j);
               
               $tempinfo = $tempinfo.'"'.$colinfo['alias'].'": "'.$row[$j].'"';

               if ($j == ($numcol -1))
                 $tempinfo = $tempinfo.'}';
               else
                 $tempinfo = $tempinfo.',';

               $i++;
             }

             $wizardinfo = $wizardinfo.$tempinfo;
            }

          }

          return $wizardinfo;

          ibase_close($conn);
        }
  }

  function formatfield($field)
  {
    $result = str_replace('IG_', '&nbsp', $field);
    $result = str_replace('_', '&nbsp', $result);
    
    return $result;
  }

  function showwizardinfo($wizardheader, $account, $patient )
  {
    if(!check_admin_user())
    {
     echo "You do not seem to be logged in.";
     exit;
    }
    
    
    if ($wizardheader == 'Notes')
    {
      $query = "SELECT A.NOTES
                FROM ACCOUNTNOTES A
                WHERE A.ACCOUNT_KEY = '$account' ";
                
      $list = ExecuteQuery($query);

      echo '<textarea name="mmoWizNotes" id = "mmoWizNotes" rows=25 style="width:100%" readonly>'.$list[0][0].'</textarea> ';
    }
    else
    if ($wizardheader == 'Appointments')
    {
      echo '<table class="jtable shadetable" width "100%"  border="0" align="middle">';
      echo '<tr>';
      echo '  <th>Date</th><th>Time</th><th>Practitioner</th><th>Region</th><th>Status</th><th>Appointment Notes</th>';
      echo '</tr>';
      
      $query = "SELECT D.DIARY_DATE, D.DIARY_FROMTIME, R.REGION_NAME, P.FIRSTNAME, P.SURNAME, S.DIARYSTATUS_DESC, D.DIARY_NOTE
                FROM DIARY D
                LEFT OUTER JOIN PRACTITIONER P ON P.PRACTITIONER_KEY = D.PRACTITIONER_KEY
                LEFT OUTER JOIN DIARYSTATUS S ON S.DIARYSTATUS_KEY = D.DIARYSTATUS_KEY
                LEFT OUTER JOIN REGION R ON R.REGION_KEY = D.REGION_KEY
                
                WHERE D.ACCOUNT_KEY = '$account' AND D.PATIENT_KEY = '$patient'
                ORDER BY D.DIARY_DATE, D.DIARY_FROMTIME";
                
      $list = ExecuteQuery($query);

      $items = sizeof($list);

      for($i = 0; $i<$items; $i++)
      {
        echo '<tr>';
          echo '<td width = 90 style = "padding-left:2px; padding-right:2px;">'.substr($list[$i][0],0,10).' </td>';
          echo '<td width = 50 style = "padding-left:2px; padding-right:2px;">'.substr($list[$i][1],10,6).'</td>';
          echo '<td width = 200 style = "padding-left:2px; padding-right:2px;">'.$list[$i][3].' '.$list[$i][4].'</td>';
          echo '<td width = 200 style = "padding-left:2px; padding-right:2px;">'.$list[$i][2].'</td>';
          echo '<td width = 200 style = "padding-left:2px; padding-right:2px;">'.$list[$i][5].'</td>';
          
          if ($list[$i][6] == null) 
            echo '<td width = 350 style = "padding-left:2px; padding-right:2px;">&nbsp</td>';
          else
            echo '<td width = 350 style = "padding-left:2px; padding-right:2px;">'.$list[$i][6].'</td>';

          
        echo '</tr>';
      }

      echo '</table>';
    }
    else
    if ($wizardheader == 'SMS')
    {
      echo '<table class="jtable shadetable" width "100%"  border="0" align="middle">';
      echo '<tr>';
      echo '  <th>Sent</th><th>User</th><th>Message</th>';
      echo '</tr>';
      
      $query = "SELECT E.WHENSENT, H.USERNAME, E.SMSMESSAGE
                FROM EMCSMS E
                LEFT OUTER JOIN HFUSERS H ON H.HFUSERS_KEY = E.HFUSERS_KEY
                WHERE E.ACCOUNT_KEY = '$account' AND E.PATIENT_KEY = '$patient'
                ORDER BY E.WHENSENT DESC";
                
      $list = ExecuteQuery($query);

      $items = sizeof($list);

      for($i = 0; $i<$items; $i++)
      {
        echo '<tr>';
          echo '<td width = 150 style = "padding-left:2px; padding-right:2px;">'.$list[$i][0].'</td>';
          echo '<td width = 90 style = "padding-left:2px; padding-right:2px;">'.$list[$i][1].'</td>';
          echo '<td width = 600 style = "padding-left:2px; padding-right:2px;">'.$list[$i][2].'</td>';
        echo '</tr>';
      }

      echo '</table>';
    }    
    else
    {
      $wizardinfo = getwizardinfo($wizardheader, $account, $patient);

      $json = json_decode($wizardinfo, true);
    
      if ($json)
      {

        echo '<table class="jtable shadetable" width "100%"  border="0" align="middle">';

        foreach($json as $field=>$value)
        {
          if ($wizardheader == 'Patient')
          {
            echo '<tr>';
	        echo ' <th width="200" align="left" scope="row">'.formatfield($field).'</td>';

            if ($field == 'SEX')
            {
              if ($value == '0')
                echo  '<td >MALE</td>';
              else
              if ($value == '1')
                echo  '<td>FEMALE</td>';
              else
              if ($value == '2')
                echo  '<td>UNKNOWN</td>';
            }
            else
            if ($field == 'EMAIL')
            {
              echo  '<td><a href="mailto:'. $value.'">'.$value.'</a></td>';
            }
            else
            if ($field == 'CELL')
            {
              echo  '<td><a href="tel:'. $value.'">'.$value.'</a></td>';
            }
            else
            if ($field == 'ALERT_MESSAGE')
            {
              echo  '<td><p style= "color:red">'.$value.'</p></td>';
            }
            else

            {
              if ($value == '')
      	        echo '<td>&nbsp</td>';
              else
      	        echo '<td>'.$value.'</td>';
            }
	        echo '</tr>';
          }
          else
          if ($wizardheader == 'Address')
          {
            if ($field == 'ADDRESS_TYPE')
            {
              if ($value == '0')
                $AddressType = 'POSTAL';
              else
              if ($value == '1')
                $AddressType = 'PHYSICAL';
              else
              if ($value == '2')
                $AddressType = 'BUSINESS';
            }

            if ($field <> 'ADDRESS_TYPE')
            {
              echo '<tr>';
	          echo ' <th width="200" align="left" scope="row">'.$AddressType.' '.formatfield($field).'</td>';

              if ($value == '')
    	        echo '<td>&nbsp</td>';
              else
      	        echo '<td>'.$value.'</td>';

	          echo '</tr>';
            }
          }

        }
        echo '</table>';
      }
      else
        echo "No data found !";
    }
  }

  $wizardheader = $_GET['header'];
  $account      = $_GET['account'];
  $patient      = $_GET['patient'];

  showwizardinfo($wizardheader, $account, $patient);
?>
