{% extends "base.html" %}

{% block title %}Invoice {{ invoice.invoice_number }} - TimeTracker{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <!-- Enhanced Header with Status Badge -->
            <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-file-invoice-dollar text-primary"></i>
                        Invoice {{ invoice.invoice_number }}
                    </h1>
                    {% set status_config = {
                        'draft': {'color': 'secondary', 'icon': 'edit', 'bg': 'bg-secondary'},
                        'sent': {'color': 'info', 'icon': 'paper-plane', 'bg': 'bg-info'},
                        'paid': {'color': 'success', 'icon': 'check-circle', 'bg': 'bg-success'},
                        'overdue': {'color': 'danger', 'icon': 'exclamation-triangle', 'bg': 'bg-danger'},
                        'cancelled': {'color': 'dark', 'icon': 'times-circle', 'bg': 'bg-dark'}
                    } %}
                    {% set config = status_config.get(invoice.status, status_config.draft) %}
                    <span class="status-badge-large {{ config.bg }} text-white">
                        <i class="fas fa-{{ config.icon }} me-2"></i>
                        {{ invoice.status|title }}
                    </span>
                </div>
                <div class="btn-group" role="group">
                    <a href="{{ url_for('invoices.edit_invoice', invoice_id=invoice.id) }}" class="btn btn-secondary">
                        <i class="fas fa-edit me-1"></i> Edit
                    </a>
                    <div class="btn-group" role="group">
                        <button type="button" class="btn btn-info dropdown-toggle dropdown-toggle-split" 
                                data-bs-toggle="dropdown" aria-expanded="false">
                            <span class="visually-hidden">Export options</span>
                        </button>
                        <ul class="dropdown-menu dropdown-menu-end">
                            <li>
                                <a class="dropdown-item" href="{{ url_for('invoices.export_invoice_csv', invoice_id=invoice.id) }}">
                                    <i class="fas fa-download me-2"></i> Export CSV
                                </a>
                            </li>
                            <li>
                                <a class="dropdown-item" href="{{ url_for('invoices.export_invoice_pdf', invoice_id=invoice.id) }}">
                                    <i class="fas fa-file-pdf me-2"></i> Export PDF
                                </a>
                            </li>
                        </ul>
                    </div>
                    <a href="{{ url_for('invoices.duplicate_invoice', invoice_id=invoice.id) }}" class="btn btn-outline-primary">
                        <i class="fas fa-copy me-1"></i> Duplicate
                    </a>
                    <a href="{{ url_for('invoices.list_invoices') }}" class="btn btn-outline-secondary">
                        <i class="fas fa-arrow-left me-1"></i> Back
                    </a>
                </div>
            </div>

            <!-- Invoice Overview Cards -->
            <div class="row mb-4">
                <div class="col-md-8">
                    <div class="card shadow-sm border-0">
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="invoice-section">
                                        <h6 class="section-title text-primary mb-3">
                                            <i class="fas fa-user me-2"></i>Bill To
                                        </h6>
                                        <div class="client-details">
                                            <div class="client-name">{{ invoice.client_name }}</div>
                                            {% if invoice.client_email %}
                                            <div class="client-email">
                                                <i class="fas fa-envelope me-1 text-muted"></i>
                                                {{ invoice.client_email }}
                                            </div>
                                            {% endif %}
                                            {% if invoice.client_address %}
                                            <div class="client-address">
                                                <i class="fas fa-map-marker-alt me-1 text-muted"></i>
                                                {{ invoice.client_address|nl2br }}
                                            </div>
                                            {% endif %}
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="invoice-section">
                                        <h6 class="section-title text-primary mb-3">
                                            <i class="fas fa-info-circle me-2"></i>Invoice Details
                                        </h6>
                                        <div class="invoice-details">
                                            <div class="detail-row">
                                                <span class="detail-label">Invoice #:</span>
                                                <span class="detail-value">{{ invoice.invoice_number }}</span>
                                            </div>
                                            <div class="detail-row">
                                                <span class="detail-label">Project:</span>
                                                <span class="detail-value">{{ invoice.project.name }}</span>
                                            </div>
                                            <div class="detail-row">
                                                <span class="detail-label">Issue Date:</span>
                                                <span class="detail-value">{{ invoice.issue_date.strftime('%B %d, %Y') }}</span>
                                            </div>
                                            <div class="detail-row">
                                                <span class="detail-label">Due Date:</span>
                                                <span class="detail-value {% if invoice.is_overdue %}text-danger{% endif %}">
                                                    {{ invoice.due_date.strftime('%B %d, %Y') }}
                                                    {% if invoice.is_overdue %}
                                                        <span class="badge bg-danger ms-2">
                                                            <i class="fas fa-exclamation-triangle me-1"></i>
                                                            {{ invoice.days_overdue }} days overdue
                                                        </span>
                                                    {% endif %}
                                                </span>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="col-md-4">
                    <div class="card shadow-sm border-0">
                        <div class="card-header bg-light 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">
                            <div class="status-actions">
                                {% if invoice.status == 'draft' %}
                                <div class="mb-3">
                                    <a href="{{ url_for('invoices.generate_from_time', invoice_id=invoice.id) }}" 
                                       class="btn btn-primary w-100">
                                        <i class="fas fa-clock me-2"></i> Generate from Time
                                    </a>
                                </div>
                                {% endif %}
                                
                                <div class="mb-3">
                                    <button type="button" class="btn btn-outline-secondary w-100" 
                                            data-bs-toggle="modal" data-bs-target="#statusModal">
                                        <i class="fas fa-edit me-2"></i> Change Status
                                    </button>
                                </div>
                                
                                <div class="info-section">
                                    <div class="info-item">
                                        <div class="info-label">Created by</div>
                                        <div class="info-value">{{ invoice.creator.display_name }}</div>
                                    </div>
                                    <div class="info-item">
                                        <div class="info-label">Created</div>
                                        <div class="info-value">{{ invoice.created_at.strftime('%B %d, %Y') }}</div>
                                    </div>
                                    <div class="info-item">
                                        <div class="info-label">Last Modified</div>
                                        <div class="info-value">{{ invoice.updated_at.strftime('%B %d, %Y') }}</div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            {% set tax_present = invoice.tax_rate > 0 %}
            <!-- Totals Summary -->
            <div class="row mb-4">
                <div class="{{ 'col-md-4' if tax_present else 'col-md-6' }}">
                    <div class="card summary-card border-0 shadow-sm">
                        <div class="card-body d-flex align-items-center">
                            <div class="summary-icon bg-primary text-white me-3"><i class="fas fa-coins"></i></div>
                            <div>
                                <div class="summary-label">Subtotal</div>
                                <div class="summary-value">{{ "%.2f"|format(invoice.subtotal) }} {{ currency }}</div>
                            </div>
                        </div>
                    </div>
                </div>
                {% if tax_present %}
                <div class="col-md-4">
                    <div class="card summary-card border-0 shadow-sm">
                        <div class="card-body d-flex align-items-center">
                            <div class="summary-icon bg-warning text-white me-3"><i class="fas fa-percent"></i></div>
                            <div>
                                <div class="summary-label">Tax ({{ "%.2f"|format(invoice.tax_rate) }}%)</div>
                                <div class="summary-value">{{ "%.2f"|format(invoice.tax_amount) }} {{ currency }}</div>
                            </div>
                        </div>
                    </div>
                </div>
                {% endif %}
                <div class="{{ 'col-md-4' if tax_present else 'col-md-6' }}">
                    <div class="card summary-card border-0 shadow-sm">
                        <div class="card-body d-flex align-items-center">
                            <div class="summary-icon bg-success text-white me-3"><i class="fas fa-sack-dollar"></i></div>
                            <div>
                                <div class="summary-label">Total Amount</div>
                                <div class="summary-value text-success">{{ "%.2f"|format(invoice.total_amount) }} {{ currency }}</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Enhanced Invoice Items -->
            <div class="row mb-4">
                <div class="col-12">
                    <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 d-flex align-items-center">
                                <i class="fas fa-list me-2"></i>Invoice Items
                                <span class="badge badge-soft-secondary ms-2">{{ invoice.items.count() }} items</span>
                            </h6>
                            {% if invoice.status == 'draft' %}
                            <a href="{{ url_for('invoices.edit_invoice', invoice_id=invoice.id) }}" class="btn btn-sm btn-outline-primary">
                                <i class="fas fa-edit me-2"></i> Edit Items
                            </a>
                            {% endif %}
                        </div>
                        <div class="card-body p-0">
                            {% if invoice.items.count() > 0 %}
                                <div class="table-responsive">
                                    <table class="table table-hover mb-0">
                                        <thead class="table-light">
                                            <tr>
                                                <th class="border-0 ps-3">Description</th>
                                                <th class="border-0 text-center">Quantity (Hours)</th>
                                                <th class="border-0 text-end">Unit Price</th>
                                                <th class="border-0 text-end pe-3">Total Amount</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            {% for item in invoice.items %}
                                            <tr class="item-row">
                                                <td class="ps-3">
                                                    <div class="item-description">
                                                        <div class="item-text">{{ item.description }}</div>
                                                        {% if item.time_entry_ids %}
                                                        <div class="item-meta">
                                                            <i class="fas fa-clock text-info me-1"></i> 
                                                            Generated from {{ item.time_entry_ids.split(',')|length }} time entries
                                                        </div>
                                                        {% endif %}
                                                    </div>
                                                </td>
                                                <td class="text-center">
                                                    <span class="quantity-badge">{{ "%.2f"|format(item.quantity) }}h</span>
                                                </td>
                                                <td class="text-end">
                                                    <span class="unit-price">{{ "%.2f"|format(item.unit_price) }} {{ currency }}</span>
                                                </td>
                                                <td class="text-end pe-3">
                                                    <span class="item-total">{{ "%.2f"|format(item.total_amount) }} {{ currency }}</span>
                                                </td>
                                            </tr>
                                            {% endfor %}
                                        </tbody>
                                        <tfoot class="table-light">
                                            <tr>
                                                <td colspan="3" class="text-end ps-3">
                                                    <strong>Subtotal:</strong>
                                                </td>
                                                <td class="text-end pe-3">
                                                    <strong>{{ "%.2f"|format(invoice.subtotal) }} {{ currency }}</strong>
                                                </td>
                                            </tr>
                                            {% if invoice.tax_rate > 0 %}
                                            <tr>
                                                <td colspan="3" class="text-end ps-3">
                                                    <strong>Tax ({{ "%.2f"|format(invoice.tax_rate) }}%):</strong>
                                                </td>
                                                <td class="text-end pe-3">
                                                    <strong>{{ "%.2f"|format(invoice.tax_amount) }} {{ currency }}</strong>
                                                </td>
                                            </tr>
                                            {% endif %}
                                            <tr class="table-primary">
                                                <td colspan="3" class="text-end ps-3">
                                                    <strong class="fs-5">Total Amount:</strong>
                                                </td>
                                                <td class="text-end pe-3">
                                                    <strong class="fs-5">{{ "%.2f"|format(invoice.total_amount) }} {{ currency }}</strong>
                                                </td>
                                            </tr>
                                        </tfoot>
                                    </table>
                                </div>
                            {% else %}
                                <div class="text-center py-5">
                                    <div class="empty-state">
                                        <i class="fas fa-list fa-3x text-muted mb-3"></i>
                                        <h5 class="text-muted">No invoice items</h5>
                                        <p class="text-muted mb-4">Add items to this invoice to generate totals.</p>
                                        {% if invoice.status == 'draft' %}
                                        <a href="{{ url_for('invoices.edit_invoice', invoice_id=invoice.id) }}" class="btn btn-primary">
                                            <i class="fas fa-plus me-2"></i> Add Items
                                        </a>
                                        {% endif %}
                                    </div>
                                </div>
                            {% endif %}
                        </div>
                    </div>
                </div>
            </div>

            <!-- Additional Information -->
            {% if invoice.notes or invoice.terms %}
            <div class="row mb-4">
                <div class="col-12">
                    <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-file-text me-2"></i>Additional Information
                            </h6>
                        </div>
                        <div class="card-body">
                            <div class="row">
                                {% if invoice.notes %}
                                <div class="col-md-6">
                                    <h6 class="text-primary mb-3">Notes</h6>
                                    <div class="content-box">
                                        {{ invoice.notes|nl2br }}
                                    </div>
                                </div>
                                {% endif %}
                                {% if invoice.terms %}
                                <div class="col-md-6">
                                    <h6 class="text-primary mb-3">Terms & Conditions</h6>
                                    <div class="content-box">
                                        {{ invoice.terms|nl2br }}
                                    </div>
                                </div>
                                {% endif %}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            {% endif %}
        </div>
    </div>
</div>

<!-- Status Change Modal -->
<div class="modal fade" id="statusModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Change Invoice Status</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <form method="POST" action="{{ url_for('invoices.update_invoice_status', invoice_id=invoice.id) }}">
                    <div class="mb-3">
                        <label for="new_status" class="form-label">New Status</label>
                        <select class="form-select" id="new_status" name="new_status" required>
                            <option value="draft" {% if invoice.status == 'draft' %}selected{% endif %}>Draft</option>
                            <option value="sent" {% if invoice.status == 'sent' %}selected{% endif %}>Sent</option>
                            <option value="paid" {% if invoice.status == 'paid' %}selected{% endif %}>Paid</option>
                            <option value="cancelled" {% if invoice.status == 'cancelled' %}selected{% endif %}>Cancelled</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label for="status_notes" class="form-label">Notes (Optional)</label>
                        <textarea class="form-control" id="status_notes" name="status_notes" rows="3" 
                                  placeholder="Add any notes about this status change..."></textarea>
                    </div>
                    <div class="d-flex justify-content-end gap-2">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn btn-primary">Update Status</button>
                    </div>
                </form>
            </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;
}

.invoice-section {
    padding: 16px 0;
}

.section-title {
    font-size: 16px;
    font-weight: 600;
    border-bottom: 2px solid var(--primary-color);
    padding-bottom: 8px;
}

.client-details .client-name {
    font-size: 18px;
    font-weight: 700;
    color: var(--text-primary);
    margin-bottom: 8px;
}

.client-email, .client-address {
    color: var(--text-secondary);
    margin-bottom: 4px;
    font-size: 14px;
}

.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);
}

.status-actions .btn {
    margin-bottom: 12px;
}

.info-section {
    border-top: 1px solid var(--border-color);
    padding-top: 16px;
}

.info-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
}

.info-label {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.info-value {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-primary);
}

.item-row {
    transition: background-color 0.2s ease;
}

.item-row:hover {
    background-color: var(--light-color);
}

.item-description .item-text {
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 4px;
}

.item-description .item-meta {
    font-size: 12px;
    color: var(--text-muted);
}

.quantity-badge {
    background: var(--primary-color);
    color: white;
    padding: 4px 8px;
    border-radius: 12px;
    font-size: 12px;
    font-weight: 600;
}

.unit-price {
    font-weight: 600;
    color: var(--text-secondary);
}

.item-total {
    font-weight: 700;
    color: var(--text-primary);
    font-size: 16px;
}

.content-box {
    background: var(--light-color);
    padding: 16px;
    border-radius: 8px;
    border-left: 4px solid var(--primary-color);
    line-height: 1.6;
}

.empty-state {
    padding: 2rem;
}

.empty-state i {
    opacity: 0.5;
}

.summary-card .summary-label {
    font-size: 12px;
    color: var(--text-secondary);
    font-weight: 600;
    letter-spacing: 0.4px;
}

.summary-card .summary-value {
    font-size: 20px;
    font-weight: 800;
    color: var(--text-primary);
}

.summary-card .summary-icon {
    width: 44px;
    height: 44px;
    border-radius: 10px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 6px 20px rgba(0,0,0,0.08);
}

@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 %}

{% block scripts %}
<script>
$(document).ready(function() {
    // Add hover effects to item rows
    $('.item-row').hover(
        function() {
            $(this).addClass('table-active');
        },
        function() {
            $(this).removeClass('table-active');
        }
    );
    
    // Status change confirmation
    $('#statusModal form').on('submit', function(e) {
        const newStatus = $('#new_status').val();
        const currentStatus = '{{ invoice.status }}';
        
        if (newStatus === currentStatus) {
            e.preventDefault();
            alert('The selected status is the same as the current status.');
            return false;
        }
        
        if (newStatus === 'cancelled') {
            if (!confirm('Are you sure you want to cancel this invoice? This action cannot be undone.')) {
                e.preventDefault();
                return false;
            }
        }
    });
});
</script>
{% endblock %}
