{% extends 'base.html' %}

{% block title %}Dashboard - RepairsHub{% endblock %}

{% block head %}
<style>
    .dashboard-container {
        display: flex;
        min-height: calc(100vh - 70px);
    }
    .sidebar {
        width: 260px;
        background: var(--navy-dark);
        color: white;
        padding: 2rem 1.5rem;
    }
    .main-content {
        flex: 1;
        padding: 2rem 3rem;
        background: #f1f5f9;
    }
    .nav-link-custom {
        color: #94a3b8;
        padding: 0.75rem 1rem;
        border-radius: 8px;
        display: flex;
        align-items: center;
        gap: 12px;
        text-decoration: none;
        transition: all 0.2s;
        margin-bottom: 0.5rem;
    }
    .nav-link-custom:hover, .nav-link-custom.active {
        background: var(--navy-light);
        color: white;
    }
    .stat-card {
        background: white;
        padding: 1.5rem;
        border-radius: 12px;
        border: none;
        box-shadow: 0 1px 3px rgba(0,0=0,0.1);
    }
    .stat-value {
        font-size: 2rem;
        font-weight: 700;
        margin-bottom: 0.25rem;
    }
    .stat-label {
        color: var(--text-muted);
        font-size: 0.875rem;
        font-weight: 500;
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }
</style>
{% endblock %}

{% block content %}
<div class="dashboard-container">
    <aside class="sidebar d-none d-lg-block">
        <div class="mb-5">
            <h6 class="text-uppercase small fw-bold text-white mb-3 px-3">Main Menu</h6>
            <a href="{% url 'dashboard' %}" class="nav-link-custom active">
                <i class="bi bi-grid-1x2-fill"></i> Dashboard
            </a>
            <a href="{% url 'job_list' %}" class="nav-link-custom">
                <i class="bi bi-tools"></i> Jobs
            </a>
            <a href="{% url 'user_list' %}" class="nav-link-custom">
                <i class="bi bi-people"></i> Users
            </a>
        </div>
        <div class="mb-5">
            <h6 class="text-uppercase small fw-bold text-white mb-3 px-3">Configuration</h6>
            <a href="{% url 'settings' %}" class="nav-link-custom">
                <i class="bi bi-gear"></i> Settings
            </a>
        </div>
    </aside>

    <main class="main-content">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <div>
                <h2 class="mb-1">Welcome back, {{ user.username }}</h2>
                <p class="text-muted mb-0">Managing <strong>{{ company.name }}</strong></p>
            </div>
            <a href="{% url 'job_create' %}" class="btn btn-primary"><i class="bi bi-plus-lg me-2"></i>New Repair Job</a>
        </div>

        {% if messages %}
        {% for message in messages %}
        <div class="alert alert-{{ message.tags }} alert-dismissible fade show mb-4" role="alert">
            {{ message }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
        {% endfor %}
        {% endif %}

        <div class="row g-4 mb-5">
            <div class="col-md-3">
                <div class="stat-card">
                    <div class="stat-value text-primary">{{ total_jobs }}</div>
                    <div class="stat-label">Total Jobs</div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="stat-card">
                    <div class="stat-value text-warning">{{ jobs_with_incomplete_folders }}</div>
                    <div class="stat-label">Jobs with Incomplete Folders</div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="stat-card">
                    <h6 class="mb-3">Jobs by Status</h6>
                    <ul class="list-group list-group-flush">
                        {% for status in jobs_by_status %}
                            <li class="list-group-item d-flex justify-content-between align-items-center px-0 py-1">
                                {{ status.status__name }}
                                <span class="badge bg-secondary rounded-pill">{{ status.count }}</span>
                            </li>
                        {% empty %}
                            <li class="list-group-item px-0 py-1">No jobs found.</li>
                        {% endfor %}
                    </ul>
                </div>
            </div>
        </div>

        <div class="row g-4 mb-5">
            <div class="col-md-12">
                <div class="stat-card">
                    <h6 class="mb-3">Incomplete Folder Breakdown (Overall)</h6>
                    {% if incomplete_folder_breakdown %}
                        <ul class="list-group list-group-flush">
                            {% for item in incomplete_folder_breakdown %}
                                <li class="list-group-item d-flex justify-content-between align-items-center px-0 py-1">
                                    <a href="{% url 'job_list' %}?missing_folder_id={{ item.folder.id }}" class="text-decoration-none text-dark">
                                        {{ item.folder.name }}
                                    </a>
                                    <span class="badge bg-danger rounded-pill">{{ item.missing_count }}</span>
                                </li>
                            {% endfor %}
                        </ul>
                    {% else %}
                        <p class="text-muted">All required folders are complete for all jobs, or no required folders are defined.</p>
                    {% endif %}
                </div>
            </div>
        </div>

        <div class="card border-0 shadow-sm">
            <div class="card-header bg-white py-3 border-0 d-flex justify-content-between align-items-center">
                <h5 class="mb-0">Recent Repair Jobs</h5>
                <a href="{% url 'job_list' %}" class="small text-decoration-none">View All</a>
            </div>
            <div class="table-responsive">
                <table class="table align-middle mb-0">
                    <thead class="bg-light">
                        <tr>
                            <th class="border-0 px-4">Job Ref</th>
                            <th class="border-0">Address</th>
                            <th class="border-0">Status</th>
                            <th class="border-0 text-end px-4">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for job in jobs %}
                            <tr>
                                <td class="px-4 fw-bold text-primary">{{ job.job_ref }}</td>
                                <td>{{ job.address_line_1 }}, {{ job.postcode }}</td>
                                <td><span class="badge bg-light text-dark border">{{ job.status.name }}</span></td>
                                <td class="text-end px-4">
                                    <a href="{% url 'job_detail' job.pk %}" class="btn btn-sm btn-outline-secondary">View</a>
                                </td>
                            </tr>
                        {% empty %}
                            <tr>
                                <td colspan="4" class="text-center py-5 text-muted">
                                    <i class="bi bi-clipboard2-x d-block mb-3 fs-1"></i>
                                    No repair jobs found. Create your first job to get started!
                                </td>
                            </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>

        <div class="d-flex justify-content-between align-items-center mb-0 mt-5">
            <div>
                <h2 class="">Client Breakdown</h2>
            </div>
        </div>

        {# Client-specific breakdown #}
        {% if client_stats %}
            {% for client_stat in client_stats %}

            <div class="row g-4 mt-3">
                <div class="card border-0 shadow-none mb-4">
                <div class="card-header bg-white py-3 border-0">
                    <h3 class="mb-0">{{ client_stat.client_name }}</h3>
                </div>
                </div>
            </div>

            <div class="row g-4 mb-5">
            <div class="col-md-3">
                <div class="stat-card">
                    <div class="stat-value text-primary">{{ client_stat.total_jobs }}</div>
                    <div class="stat-label">Total Jobs</div>
                </div>
            </div>

            <div class="col-md-3">
                <div class="stat-card">
                    <div class="stat-value text-warning">{{ client_stat.incomplete_jobs }}</div>
                    <div class="stat-label">Jobs with Incomplete Folders</div>
                </div>
            </div>

            <div class="col-md-6">
                <div class="stat-card">
                    <h6 class="mb-3">Jobs by Status</h6>
                    <ul class="list-group list-group-flush">
                        {% for status in jobs_by_status %}
                            <li class="list-group-item d-flex justify-content-between align-items-center px-0 py-1">
                                <span class="badge bg-secondary rounded-pill"></span>
                            </li>
                        {% empty %}
                            <li class="list-group-item px-0 py-1">No jobs found.</li>
                        {% endfor %}
                    </ul>
                </div>
            </div>
            </div>

            
                    <div class="row g-4 mb-5">
                        <div class="col-md-12">
                            <div class="stat-card">
                                <h6 class="mb-3">Incomplete Folders</h6>
                                {% if client_stat.incomplete_folder_breakdown %}
                                    <ul class="list-group list-group-flush">
                                        {% for folder_item in client_stat.incomplete_folder_breakdown %}
                                            <li class="list-group-item d-flex justify-content-between align-items-center px-0 py-1">
                                                <a href="{% url 'job_list' %}?missing_folder_id={{ folder_item.folder_id }}" class="text-decoration-none text-dark">
                                                    {{ folder_item.folder_name }}
                                                </a>
                                                <span class="badge bg-danger rounded-pill">{{ folder_item.missing_count }}</span>
                                            </li>
                                        {% endfor %}
                                    </ul>
                                {% else %}
                                    <p class="text-muted">All folders complete.</p>
                                {% endif %}
                            </div>
                        </div>
                    </div>
            {% endfor %}
        {% else %}
            <div class="card border-0 shadow-sm mb-5">
                <div class="card-body">
                    <p class="text-muted">No client-specific job data available.</p>
                </div>
            </div>
        {% endif %}

        
    </main>
</div>
{% endblock %}