{% extends "modern/base.html" %}

{% block title %}Dashboard - BLOOM LIMS{% endblock %}

{% block content %}
<div class="page-header">
  <h1>Dashboard</h1>
  <p class="text-muted">Welcome{% if udat.email %}, {{ udat.email.split('@')[0] }}{% endif %}</p>
</div>

{% if db_unavailable %}
<div class="alert alert-warning mb-lg">
  <i class="fas fa-triangle-exclamation"></i>
  Database is currently unreachable. You are logged in, but live counts and recent items are temporarily unavailable.
</div>
{% endif %}

<!-- Quick Stats -->
<div class="grid grid-4 mb-lg">
  <a href="/queue_details" class="stat-card">
    <div class="card-icon success">
      <i class="fas fa-list-check"></i>
    </div>
    <div class="stat-content">
      <div class="stat-value">{{ stats.queue_runtime_total | default(0) }}</div>
      <div class="stat-label">Queue Runtime Objects</div>
    </div>
  </a>
  
  <a href="/search?record_types=instance" class="stat-card">
    <div class="card-icon highlight">
      <i class="fas fa-cubes"></i>
    </div>
    <div class="stat-content">
      <div class="stat-value">{{ stats.objects_total | default(0) }}</div>
      <div class="stat-label">Objects</div>
    </div>
  </a>
  
  <a href="/equipment_overview" class="stat-card">
    <div class="card-icon info">
      <i class="fas fa-microscope"></i>
    </div>
    <div class="stat-content">
      <div class="stat-value">{{ stats.equipment_total | default(0) }}</div>
      <div class="stat-label">Equipment</div>
    </div>
  </a>
  
  <a href="/reagent_overview" class="stat-card">
    <div class="card-icon warning">
      <i class="fas fa-vial"></i>
    </div>
    <div class="stat-content">
      <div class="stat-value">{{ stats.reagents_total | default(0) }}</div>
      <div class="stat-label">Reagents</div>
    </div>
  </a>
</div>

<!-- Two Column Layout -->
<div class="grid grid-2 mb-lg">
  <!-- Recent Queue Runtime -->
  <div class="card">
    <div class="card-header">
      <h3 class="card-title"><i class="fas fa-list-check"></i> Recent Queue Runtime</h3>
      <a href="/queue_details" class="btn btn-outline btn-sm">View All</a>
    </div>
    <div class="card-body">
      {% if recent_queue_runtime %}
        <div class="list-group">
          {% for queue_item in recent_queue_runtime[:5] %}
          <a href="/euid_details?euid={{ queue_item.euid }}" class="list-item">
            <div class="list-item-content">
              <span class="badge badge-euid">{{ queue_item.euid }}</span>
              <span class="list-item-title">{{ queue_item.name | default(queue_item.euid) }}</span>
            </div>
            <span class="badge badge-{% if queue_item.status == 'complete' %}success{% elif queue_item.status == 'exception' %}error{% else %}info{% endif %}">
              {{ queue_item.status | default('active') }}
            </span>
          </a>
          {% endfor %}
        </div>
      {% else %}
        <div class="empty-state">
          <div class="empty-state-icon"><i class="fas fa-list-check"></i></div>
          <p>No recent queue runtime objects</p>
          <a href="/queue_details" class="btn btn-primary btn-sm">Open Queue Runtime</a>
        </div>
      {% endif %}
    </div>
  </div>
  
  <!-- Recent Objects -->
  <div class="card">
    <div class="card-header">
      <h3 class="card-title"><i class="fas fa-cubes"></i> Recent Objects</h3>
      <a href="/search?record_types=instance" class="btn btn-outline btn-sm">View All</a>
    </div>
    <div class="card-body">
      {% if recent_objects %}
        <div class="list-group">
          {% for item in recent_objects[:5] %}
          <a href="/euid_details?euid={{ item.euid }}" class="list-item">
            <div class="list-item-content">
              <span class="badge badge-euid">{{ item.euid }}</span>
              <span class="list-item-title">{{ item.name | default(item.subtype) }}</span>
            </div>
            <span class="badge badge-info">
              {{ item.category | default('object') }}
            </span>
          </a>
          {% endfor %}
        </div>
      {% else %}
        <div class="empty-state">
          <div class="empty-state-icon"><i class="fas fa-cubes"></i></div>
          <p>No recent objects</p>
        </div>
      {% endif %}
    </div>
  </div>
</div>

<!-- Quick Actions -->
<div class="card">
  <div class="card-header">
    <h3 class="card-title"><i class="fas fa-bolt"></i> Quick Actions</h3>
  </div>
  <div class="card-body">
    <div class="quick-actions">
      <a href="/queue_details" class="btn btn-outline"><i class="fas fa-list-check"></i> Queue Runtime</a>
      <a href="/equipment_overview" class="btn btn-outline"><i class="fas fa-microscope"></i> Equipment</a>
      <a href="/reagent_overview" class="btn btn-outline"><i class="fas fa-vial"></i> Reagents</a>
      <a href="/object_templates_summary" class="btn btn-outline"><i class="fas fa-cubes"></i> Templates</a>
      <a href="/database_statistics" class="btn btn-outline"><i class="fas fa-chart-bar"></i> Statistics</a>
      {% if udat.role | default('user') | lower == 'admin' %}
      <a href="/admin" class="btn btn-outline"><i class="fas fa-cog"></i> Admin</a>
      {% endif %}
    </div>
  </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
// Auto-refresh dashboard data every 60 seconds
if (typeof BloomConfig !== 'undefined' && BloomConfig.refreshInterval) {
  setInterval(() => {
    location.reload();
  }, BloomConfig.refreshInterval);
}
</script>
{% endblock %}
