<%
def fk_action_color action
	case action
	when 'CASCADE'
		'negative'
	when 'SET NULL'
		'warning'
	end
end
%>

<h1>
	Table - <code><%= params["schema"] %>.<%= params["name"] %></code>
</h1>

<% if object[:comment] %>
	<h2>Description</h2>
	<p><%= object[:comment] %></p>
<% end %>

<%= render_markdown "schema/#{params["schema"]}/#{params["name"]}.md" %>

<h2>Columns</h2>

<table class="ui compact fixed celled table">
	<thead>
		<th>Column Name</th>
		<th>Data Type</th>
		<th class="three wide">Required Field?</th>
		<th>Default Value</th>
	</thead>
	<tbody>
		<% object[:columns].each do |row| %>
		<tr>
			<td>
				<% if fk = object[:foreign_keys].fetch(row["column_name"], nil) %>
					<i class="icon book"></i>
					<a href="<%= url "/schemas/#{fk["references_schema"]}/tables/#{fk["references_table"]}" %>" title="<%= fk["references_schema"] %>.<%= fk["references_table"] %>"><%= row["column_name"] %></a>
				<% else %>
					<%= row["column_name"] %>
				<% end %>
			</td>
			<td><%= row["data_type"] %></td>
			<td class="<%= row["is_nullable"] == 'NO' && row["column_default"].to_s.empty? ? 'warning' : '' %>"><%= row["is_nullable"] == 'NO' ? '<i class="icon check"></i>' : ''%></td>
			<td><code><%= row["column_default"] %></code></td>
		</tr>
		<% end %>
	</tbody>
</table>


<% unless object[:foreign_keys].empty? %>
	<h2>Foreign Keys</h2>
	<table class="ui compact fixed celled table">
		<thead>
			<th>Column Name</th>
			<th>References</th>
			<th class="three wide">On Delete</th>
			<th>Constraint Name</th>
		</thead>
		<tbody>
			<% object[:foreign_keys].each do |column_name, row| %>
			<tr>
				<td><%= row["column_name"] %></td>
				<td><i class="icon book"></i> <a href="<%= url "/schemas/#{row["references_schema"]}/tables/#{row["references_table"]}" %>" title="<%= row["references_schema"] %>.<%= row["references_table"] %>"><%= "#{row["references_schema"]}.#{row["references_table"]}" %></a></td>
				<td class="<%= fk_action_color row["on_delete"] %>"><%= row["on_delete"] %></td>
				<td><%= row["constraint_name"] %></td>
			</tr>
			<% end %>
		</tbody>
	</table>
<% end %>

<h2>Indexes</h2>
<table class="ui compact fixed celled table">
	<thead>
		<th>Index Name</th>
		<th class="two wide">Primary?</th>
		<th class="two wide">Unique?</th>
		<th class="two wide">Type</th>
		<th>Columns</th>
	</thead>
	<tbody>
		<% object[:indexes].each do |index_name, row| %>
		<tr>
			<td><%= row["index_name"] %></td>
			<td><% if row["is_primary"] == "t" %><i class="icon check"></i><% end %></td>
			<td><% if row["is_unique"] == "t" %><i class="icon check"></i><% end %></td>
			<td><%= row["index_type"] %></td>
			<td><%= row["index_keys"][1..-2].split(",").join ", " %></td>
		</tr>
		<% end %>
	</tbody>
</table>