{% extends "base.html" %}

{% block title %}{{ client.name }} - {{ app_name }}{% endblock %}

{% block content %}
<div class="container-fluid">
    {% from "_components.html" import page_header %}
    <div class="row">
        <div class="col-12">
            {% set actions %}
            {% if current_user.is_admin %}
            <a href="{{ url_for('clients.edit_client', client_id=client.id) }}" class="btn btn-outline-light btn-sm me-2">
                <i class="fas fa-edit"></i> {{ _('Edit Client') }}
            </a>
            {% endif %}
            <a href="{{ url_for('clients.list_clients') }}" class="btn btn-outline-light btn-sm">
                <i class="fas fa-arrow-left"></i> {{ _('Back to Clients') }}
            </a>
            {% endset %}
            {{ page_header('fas fa-building', client.name, _('Client details and project overview'), actions) }}
        </div>
    </div>

    <div class="row mb-4">
        <div class="col-md-3 col-sm-6 mb-3">
            <div class="card border-0 shadow-sm h-100 summary-card">
                <div class="card-body p-3 d-flex align-items-center">
                    <div class="summary-icon bg-primary bg-opacity-10 text-primary me-3">
                        <i class="fas fa-project-diagram"></i>
                    </div>
                    <div>
                        <div class="summary-label">{{ _('Total Projects') }}</div>
                        <div class="summary-value">{{ client.total_projects }}</div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3 col-sm-6 mb-3">
            <div class="card border-0 shadow-sm h-100 summary-card">
                <div class="card-body p-3 d-flex align-items-center">
                    <div class="summary-icon bg-success bg-opacity-10 text-success me-3">
                        <i class="fas fa-toggle-on"></i>
                    </div>
                    <div>
                        <div class="summary-label">{{ _('Active Projects') }}</div>
                        <div class="summary-value">{{ client.active_projects }}</div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3 col-sm-6 mb-3">
            <div class="card border-0 shadow-sm h-100 summary-card">
                <div class="card-body p-3 d-flex align-items-center">
                    <div class="summary-icon bg-info bg-opacity-10 text-info me-3">
                        <i class="fas fa-clock"></i>
                    </div>
                    <div>
                        <div class="summary-label">{{ _('Total Hours') }}</div>
                        <div class="summary-value">{{ "%.1f"|format(client.total_hours) }}</div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3 col-sm-6 mb-3">
            <div class="card border-0 shadow-sm h-100 summary-card">
                <div class="card-body p-3 d-flex align-items-center">
                    <div class="summary-icon bg-warning bg-opacity-10 text-warning me-3">
                        <i class="fas fa-sack-dollar"></i>
                    </div>
                    <div>
                        <div class="summary-label">{{ _('Est. Total Cost') }}</div>
                        <div class="summary-value">{{ "%.2f"|format(client.estimated_total_cost) }} {{ currency }}</div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <!-- Client Information -->
        <div class="col-lg-4">
            <div class="card mb-4 shadow-sm border-0">
                <div class="card-header bg-white py-3">
                    <h6 class="m-0 font-weight-bold text-primary">
                        <i class="fas fa-info-circle me-2"></i>{{ _('Client Information') }}
                    </h6>
                </div>
                <div class="card-body">
                    <div class="mb-3">
                        <div class="detail-row">
                            <span class="detail-label">{{ _('Status') }}</span>
                            <span class="detail-value">
                                {% if client.status == 'active' %}
                                <span class="status-badge-large bg-success text-white">{{ _('Active') }}</span>
                                {% else %}
                                <span class="status-badge-large bg-secondary text-white">{{ _('Archived') }}</span>
                                {% endif %}
                            </span>
                        </div>
                    </div>
                    {% if client.description %}
                    <div class="mb-3">
                        <div class="section-title text-primary mb-2">{{ _('Description') }}</div>
                        <div class="content-box">{{ client.description }}</div>
                    </div>
                    {% endif %}
                    {% if client.contact_person %}
                    <div class="mb-3">
                        <div class="detail-row">
                            <span class="detail-label">{{ _('Contact Person') }}</span>
                            <span class="detail-value">{{ client.contact_person }}</span>
                        </div>
                    </div>
                    {% endif %}
                    {% if client.email %}
                    <div class="mb-3">
                        <div class="detail-row">
                            <span class="detail-label">{{ _('Email') }}</span>
                            <span class="detail-value"><a href="mailto:{{ client.email }}">{{ client.email }}</a></span>
                        </div>
                    </div>
                    {% endif %}
                    {% if client.phone %}
                    <div class="mb-3">
                        <div class="detail-row">
                            <span class="detail-label">{{ _('Phone') }}</span>
                            <span class="detail-value">{{ client.phone }}</span>
                        </div>
                    </div>
                    {% endif %}
                    {% if client.address %}
                    <div class="mb-3">
                        <div class="section-title text-primary mb-2">{{ _('Address') }}</div>
                        <div class="content-box">{{ client.address }}</div>
                    </div>
                    {% endif %}
                    {% if client.default_hourly_rate %}
                    <div class="mb-3">
                        <div class="detail-row">
                            <span class="detail-label">{{ _('Default Hourly Rate') }}</span>
                            <span class="detail-value">{{ "%.2f"|format(client.default_hourly_rate) }} {{ currency }}</span>
                        </div>
                    </div>
                    {% endif %}
                </div>
            </div>

            <!-- Client Statistics -->
            <div class="card mb-4 shadow-sm border-0">
                <div class="card-header bg-white py-3">
                    <h6 class="m-0 font-weight-bold text-primary">
                        <i class="fas fa-chart-bar me-2"></i>{{ _('Statistics') }}
                    </h6>
                </div>
                <div class="card-body">
                    <div class="row text-center">
                        <div class="col-6">
                            <h4 class="text-primary">{{ client.total_projects }}</h4>
                            <small class="text-muted">{{ _('Total Projects') }}</small>
                        </div>
                        <div class="col-6">
                            <h4 class="text-success">{{ client.active_projects }}</h4>
                            <small class="text-muted">{{ _('Active Projects') }}</small>
                        </div>
                    </div>
                    <hr>
                    <div class="row text-center">
                        <div class="col-6">
                            <h4 class="text-info">{{ "%.1f"|format(client.total_hours) }}</h4>
                            <small class="text-muted">{{ _('Total Hours') }}</small>
                        </div>
                        <div class="col-6">
                            <h4 class="text-warning">{{ "%.2f"|format(client.estimated_total_cost) }}</h4>
                            <small class="text-muted">{{ _('Est. Total Cost') }}</small>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Client Actions -->
            {% if current_user.is_admin %}
            <div class="card shadow-sm border-0">
                <div class="card-header bg-white py-3">
                    <h6 class="m-0 font-weight-bold text-primary">
                        <i class="fas fa-cog me-2"></i>{{ _('Status & Actions') }}
                    </h6>
                </div>
                <div class="card-body">
                    {% if client.status == 'active' %}
                    <form method="POST" action="{{ url_for('clients.archive_client', client_id=client.id) }}" class="mb-2" data-confirm="{{ _('Are you sure you want to archive this client?') }}">
                        <button type="submit" class="btn btn-outline-secondary w-100">
                            <i class="fas fa-archive me-2"></i>{{ _('Archive Client') }}
                        </button>
                    </form>
                    {% else %}
                    <form method="POST" action="{{ url_for('clients.activate_client', client_id=client.id) }}" class="mb-2">
                        <button type="submit" class="btn btn-primary w-100">
                            <i class="fas fa-check me-2"></i>{{ _('Activate Client') }}
                        </button>
                    </form>
                    {% endif %}
                    
                    {% if client.total_projects == 0 %}
                    <form method="POST" action="{{ url_for('clients.delete_client', client_id=client.id) }}" data-confirm="{{ _('Are you sure you want to delete this client? This action cannot be undone.') }}">
                        <button type="submit" class="btn btn-danger w-100">
                            <i class="fas fa-trash me-2"></i>{{ _('Delete Client') }}
                        </button>
                    </form>
                    {% endif %}
                </div>
            </div>
            {% endif %}
        </div>

        <!-- Projects List -->
        <div class="col-lg-8">
            <div class="card shadow-sm border-0">
                <div class="card-header bg-white py-3 d-flex justify-content-between align-items-center">
                    <h6 class="m-0 font-weight-bold text-primary">
                        <i class="fas fa-project-diagram me-2"></i>{{ _('Projects') }}
                        <span class="badge badge-soft-secondary ms-2">{{ projects|length }} {{ _('total') }}</span>
                    </h6>
                    {% if current_user.is_admin %}
                    <a href="{{ url_for('projects.create_project') }}" class="btn btn-sm btn-outline-primary">
                        <i class="fas fa-plus me-1"></i> {{ _('New Project') }}
                    </a>
                    {% endif %}
                </div>
                <div class="card-body p-0">
                    {% if projects %}
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th>{{ _('Project Name') }}</th>
                                    <th>{{ _('Status') }}</th>
                                    <th>{{ _('Billable') }}</th>
                                    <th>{{ _('Hourly Rate') }}</th>
                                    <th>{{ _('Total Hours') }}</th>
                                    <th>{{ _('Est. Cost') }}</th>
                                    <th>{{ _('Actions') }}</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for project in projects %}
                                <tr>
                                    <td>
                                        <a href="{{ url_for('projects.view_project', project_id=project.id) }}" class="text-decoration-none">
                                            <strong>{{ project.name }}</strong>
                                        </a>
                                        {% if project.description %}
                                            <br><small class="text-muted">{{ project.description[:50] }}{% if project.description|length > 50 %}...{% endif %}</small>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if project.status == 'active' %}
                                        <span class="badge badge-soft-success">{{ _('Active') }}</span>
                                        {% else %}
                                        <span class="badge badge-soft-secondary">{{ _('Archived') }}</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if project.billable %}
                                        <span class="badge badge-soft-primary">{{ _('Yes') }}</span>
                                        {% else %}
                                        <span class="badge badge-soft-secondary">{{ _('No') }}</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if project.hourly_rate %}
                                        {{ "%.2f"|format(project.hourly_rate) }} {{ currency }}
                                        {% else %}
                                        -
                                        {% endif %}
                                    </td>
                                    <td>{{ "%.1f"|format(project.total_hours) }}</td>
                                    <td>
                                        {% if project.billable and project.hourly_rate %}
                                        {{ "%.2f"|format(project.estimated_cost) }} {{ currency }}
                                        {% else %}
                                        -
                                        {% endif %}
                                    </td>
                                    <td>
                                        <div class="btn-group" role="group">
                                            <a href="{{ url_for('projects.view_project', project_id=project.id) }}" 
                                               class="btn btn-sm btn-action btn-action--view" title="{{ _('View') }}">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            {% if current_user.is_admin %}
                                            <a href="{{ url_for('projects.edit_project', project_id=project.id) }}" 
                                               class="btn btn-sm btn-action btn-action--edit" title="{{ _('Edit') }}">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                            {% endif %}
                                        </div>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    {% else %}
                    <div class="text-center py-5">
                        <div class="empty-state">
                            <i class="fas fa-project-diagram fa-3x text-muted mb-3"></i>
                            <h5 class="text-muted">{{ _('No projects found') }}</h5>
                            <p class="text-muted mb-3">{{ _("This client doesn't have any projects yet.") }}</p>
                            {% if current_user.is_admin %}
                            <a href="{{ url_for('projects.create_project') }}" class="btn btn-primary">
                                <i class="fas fa-plus me-2"></i> {{ _('Create First Project') }}
                            </a>
                            {% endif %}
                        </div>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
<!-- Modern styling now handled by global CSS in base.css -->
<script>
// Generic data-confirm handler
document.addEventListener('DOMContentLoaded', function(){
  document.querySelectorAll('form[data-confirm]').forEach(function(f){
    f.addEventListener('submit', function(e){
      var msg = f.getAttribute('data-confirm') || '';
      if (msg && !confirm(msg)) { e.preventDefault(); return false; }
    });
  });
});
</script>
{% endblock %}
