{% extends "base.html" %}

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

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h1 class="h3 mb-0">
                    <i class="fas fa-building text-primary"></i> {{ client.name }}
                </h1>
                <div>
                    {% if current_user.is_admin %}
                    <a href="{{ url_for('clients.edit_client', client_id=client.id) }}" class="btn btn-secondary me-2">
                        <i class="fas fa-edit"></i> Edit Client
                    </a>
                    {% endif %}
                    <a href="{{ url_for('clients.list_clients') }}" class="btn btn-outline-secondary">
                        <i class="fas fa-arrow-left"></i> Back to Clients
                    </a>
                </div>
            </div>
            <div class="d-flex justify-content-between align-items-center mb-4">
                <div class="d-flex align-items-center">
                    <h1 class="h3 mb-0 me-3">
                        <i class="fas fa-building text-primary"></i>
                        {{ client.name }}
                    </h1>
                    {% set status_map = {
                        'active':   {'bg': 'bg-success',   'label': 'Active'},
                        'inactive': {'bg': 'bg-secondary', 'label': 'Inactive'}
                    } %}
                    {% set sc = status_map.get(client.status, status_map['inactive']) %}
                    <span class="status-badge-large {{ sc.bg }} text-white">{{ sc.label }}</span>
                </div>
                <div class="btn-group" role="group">
                    {% if current_user.is_admin %}
                    <a href="{{ url_for('clients.edit_client', client_id=client.id) }}" class="btn btn-secondary">
                        <i class="fas fa-edit me-1"></i> Edit
                    </a>
                    {% endif %}
                    <a href="{{ url_for('clients.list_clients') }}" class="btn btn-outline-secondary">
                        <i class="fas fa-arrow-left me-1"></i> Back
                    </a>
                </div>
            </div>
        </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"><span class="status-badge-large {{ sc.bg }} text-white">{{ sc.label }}</span></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" onsubmit="return 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) }}" onsubmit="return 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>
<style>
.status-badge-large { padding: 8px 16px; border-radius: 25px; font-size: 14px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px; }
.section-title { font-size: 16px; font-weight: 600; border-bottom: 2px solid var(--primary-color); padding-bottom: 8px; }
.detail-row { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; padding: 8px 0; border-bottom: 1px solid var(--border-color); }
.detail-label { font-weight: 600; color: var(--text-secondary); }
.detail-value { font-weight: 600; color: var(--text-primary); }
.content-box { background: var(--light-color); padding: 16px; border-radius: 8px; border-left: 4px solid var(--primary-color); line-height: 1.6; }
.summary-card { transition: all 0.3s ease; border-radius: 12px; }
.summary-card:hover { transform: translateY(-2px); box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1) !important; }
.summary-icon { width: 44px; height: 44px; border-radius: 10px; display: inline-flex; align-items: center; justify-content: center; }
.summary-label { font-size: 12px; color: var(--text-secondary); font-weight: 600; letter-spacing: 0.4px; text-transform: uppercase; }
.summary-value { font-size: 20px; font-weight: 800; color: var(--text-primary); }
.empty-state { padding: 2rem; }
@media (max-width: 768px) {
  .btn-group { flex-direction: column; width: 100%; }
  .btn-group .btn { margin-bottom: 8px; border-radius: 6px !important; }
  .detail-row { flex-direction: column; align-items: flex-start; gap: 4px; }
  .status-badge-large { font-size: 12px; padding: 6px 12px; }
}
</style>
{% endblock %}
