<h1>Listing door_logs</h1>

<%= link_to 'Download Door Logs', download_path %>
<a href="#" onclick="$('#log-guide').toggle();">Show Log Guide</a>
<div id="log-guide" style="display: none;">
<pre>
Guide to Card Number storage:
cardnum = (R+(r*32767)), convert to hex

Guide to log keys and data:
 * A=alarm armed (# level)
 * a=added user (# usernum)
 * C=keypad command (# command)
 * c=second half
 * c=checked user (0=failed, #=found usernum)
 * D=denied access (# card num)
 * d=second half
 * d=deleted user (# usernum)
 * E=second (#=second)
 * F=priv fail (0=wrong pw, 1=too many attempts, 2=not logged in)
 * f=second half
 * f=card fail (#=usermask)
 * G=granted access (# card num)
 * g=second half of card
 * H=hour (#=hour)
 * i=attempt to write to invalid eeprom address (# usernum)
 * I=attempt to delete from invalid eeprom address (# usernum)
 * L=locked (1=door1, 2=door2, 3=bedtime)
 * M=minute (#=minute)
 * m=alarm state (# level)
 * R=read tag (# card num)
 * r=second half of tag
 * Q=superuser authed (#=superuser)
 * S=auth (0=privileged mode enabled)
 * s=alarm sensor (# zone)
 * t=alarm trained (#=sensor value)
 * T=alarm triggered (0)
 * U=unlocked door (1=door1, 2=door2, # card num)
 * u=second half of card
 * Z=user db cleared (0)
 * z=log cleared (0)
</pre>
</div>
<table>
  <tr>
    <th>Date</th>
    <th>Key</th>
    <th>Data</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @divided_tmp = nil %>
<% @door_logs.each do |door_log| %>
  <tr>
    <td><%= door_log.created_at %></td>
    <td><%= door_log.key %></td>
    <td><%= door_log.data %></td>
    <% 
      if door_log.key == 'r' || door_log.key == 'd' ||door_log.key == 'g'
        @divided_tmp = door_log.data.to_i
      elsif (door_log.key == 'R' || door_log.key == 'D' || door_log.key == 'G') && !@divided_tmp.nil? %>
      <td>
        <%= case door_log.key
              when 'R'
                "Read"
              when 'D'
                "Denied"
              when 'G'
                "Granted"
              end %>
        <% @cardnum = (door_log.data.to_i+(@divided_tmp*32767)).to_s(16) %>
        <%= "Card: "+@cardnum %>
	<% @card_user = User.find(:first,:conditions=>['card_number LIKE ?', @cardnum]) %>
	<%= "("+@card_user.name+")" unless @card_user.nil? %>
      </td>
   <% else 
        @divided_tmp = nil
      end %>
  </tr>
<% end %>
</table>

<br />