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

{% block admin_title %}Cost Factor History{% endblock %}

{% block admin_header %}Change History: {{ factor.name }}{% endblock %}

{% block admin_actions %}
<a href="{{ url_for('admin.cost_factors') }}" class="btn btn-secondary">
    <i class="fas fa-arrow-left"></i> Back to Cost Factors
</a>
<a href="{{ url_for('admin.edit_cost_factor', factor_id=factor.id) }}" class="btn btn-primary">
    <i class="fas fa-edit"></i> Edit Factor
</a>
{% endblock %}

{% block admin_content %}
<div class="row">
    <div class="col-md-4">
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">Factor Information</h5>
            </div>
            <div class="card-body">
                <dl class="row">
                    <dt class="col-sm-4">Name</dt>
                    <dd class="col-sm-8">{{ factor.name }}</dd>
                    
                    <dt class="col-sm-4">Category</dt>
                    <dd class="col-sm-8">
                        <span class="badge bg-primary">{{ factor.category }}</span>
                    </dd>
                    
                    <dt class="col-sm-4">Current Value</dt>
                    <dd class="col-sm-8">
                        <span class="badge bg-success">{{ "%.4f"|format(factor.factor_value) }}</span>
                    </dd>
                    
                    <dt class="col-sm-4">Status</dt>
                    <dd class="col-sm-8">
                        {% if factor.is_active %}
                            <span class="badge bg-success">Active</span>
                        {% else %}
                            <span class="badge bg-danger">Inactive</span>
                        {% endif %}
                    </dd>
                    
                    <dt class="col-sm-4">Created</dt>
                    <dd class="col-sm-8">{{ factor.created_at.strftime('%Y-%m-%d %H:%M') if factor.created_at else 'Unknown' }}</dd>
                    
                    <dt class="col-sm-4">Last Updated</dt>
                    <dd class="col-sm-8">{{ factor.updated_at.strftime('%Y-%m-%d %H:%M') if factor.updated_at else 'Never' }}</dd>
                </dl>
            </div>
        </div>
    </div>

    <div class="col-md-8">
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">Change History</h5>
            </div>
            <div class="card-body">
                {% if history %}
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>Date & Time</th>
                                <th>Changed By</th>
                                <th>Old Value</th>
                                <th>New Value</th>
                                <th>Change</th>
                                <th>Reason</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for change in history %}
                            <tr>
                                <td>{{ change.changed_at.strftime('%Y-%m-%d %H:%M') }}</td>
                                <td>
                                    {% if change.user %}
                                        <span class="badge bg-info">{{ change.user.username }}</span>
                                    {% else %}
                                        <span class="text-muted">Unknown</span>
                                    {% endif %}
                                </td>
                                <td>
                                    {% if change.old_value is not none %}
                                        <span class="badge bg-secondary">{{ "%.4f"|format(change.old_value) }}</span>
                                    {% else %}
                                        <span class="text-muted">N/A</span>
                                    {% endif %}
                                </td>
                                <td>
                                    <span class="badge bg-success">{{ "%.4f"|format(change.new_value) }}</span>
                                </td>
                                <td>
                                    {% if change.old_value is not none %}
                                        {% set diff = change.new_value - change.old_value %}
                                        {% if diff > 0 %}
                                            <span class="badge bg-danger">+{{ "%.4f"|format(diff) }}</span>
                                        {% elif diff < 0 %}
                                            <span class="badge bg-warning">{{ "%.4f"|format(diff) }}</span>
                                        {% else %}
                                            <span class="badge bg-secondary">No change</span>
                                        {% endif %}
                                    {% else %}
                                        <span class="badge bg-info">Initial</span>
                                    {% endif %}
                                </td>
                                <td>
                                    {% if change.change_reason %}
                                        <small class="text-muted">{{ change.change_reason }}</small>
                                    {% else %}
                                        <span class="text-muted">No reason provided</span>
                                    {% endif %}
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
                {% else %}
                <div class="text-center py-4">
                    <i class="fas fa-history fa-3x text-muted mb-3"></i>
                    <h5 class="text-muted">No Changes Yet</h5>
                    <p class="text-muted">This factor hasn't been modified since it was created.</p>
                </div>
                {% endif %}
            </div>
        </div>

        {% if history %}
        <div class="card mt-3">
            <div class="card-header">
                <h5 class="mb-0">Summary</h5>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-3">
                        <div class="text-center">
                            <h4 class="text-primary">{{ history|length }}</h4>
                            <small class="text-muted">Total Changes</small>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="text-center">
                            <h4 class="text-success">{{ history|selectattr('change_reason')|list|length }}</h4>
                            <small class="text-muted">With Reasons</small>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="text-center">
                            <h4 class="text-info">{{ history|selectattr('user')|list|length }}</h4>
                            <small class="text-muted">By Known Users</small>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="text-center">
                            <h4 class="text-warning">{{ (history|last).changed_at.strftime('%Y-%m-%d') if history else 'N/A' }}</h4>
                            <small class="text-muted">Last Modified</small>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        {% endif %}
    </div>
</div>
{% endblock %}

{% block styles %}
{{ super() }}
<style>
.badge {
    font-size: 0.875em;
}

dl.row dt {
    font-weight: 600;
    color: #6c757d;
}

dl.row dd {
    margin-bottom: 0.5rem;
}

.table th {
    background-color: #f8f9fa;
    border-top: none;
    font-weight: 600;
}

.text-center h4 {
    margin-bottom: 0.25rem;
}
</style>
{% endblock %} 