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

{% block title %}Edit Cost Factor: {{ factor.name }}{% endblock %}

{% block content %}
<div class="container-fluid">
    <!-- Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h1 class="h3 mb-0">
                <i class="bi bi-pencil-square me-2 text-primary"></i>Edit Cost Factor
            </h1>
            <p class="text-muted mb-0">{{ factor.name }} ({{ factor.category }})</p>
        </div>
        <div class="d-flex gap-2">
            <a href="{{ url_for('admin.cost_factors') }}" class="btn btn-outline-secondary">
                <i class="bi bi-arrow-left me-1"></i> Back to Cost Factors
            </a>
        </div>
    </div>

    <!-- Flash Messages -->
    {% with messages = get_flashed_messages(with_categories=true) %}
        {% if messages %}
            {% for category, message in messages %}
                <div class="alert alert-{{ category }} alert-dismissible fade show" role="alert">
                    <i class="bi bi-info-circle me-2"></i>{{ message }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            {% endfor %}
        {% endif %}
    {% endwith %}

    <div class="row">
        <div class="col-lg-8">
            <div class="card border-0 shadow-sm">
                <div class="card-header bg-light border-0">
                    <h5 class="card-title mb-0">
                        <i class="bi bi-gear me-2"></i>Edit Factor Details
                    </h5>
                </div>
                <div class="card-body">
                    <form method="POST">
                        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
                        
                        <div class="row">
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="category" class="form-label fw-semibold">Category</label>
                                    <input type="text" class="form-control bg-light" id="category" value="{{ factor.category }}" readonly>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="name" class="form-label fw-semibold">Name</label>
                                    <input type="text" class="form-control bg-light" id="name" value="{{ factor.name }}" readonly>
                                </div>
                            </div>
                        </div>

                        <div class="mb-3">
                            <label for="factor_value" class="form-label fw-semibold">
                                Factor Value <span class="text-danger">*</span>
                            </label>
                            <input type="number" class="form-control form-control-lg" id="factor_value" name="factor_value" 
                                   value="{{ "%.4f"|format(factor.factor_value) }}" step="0.0001" min="0" required>
                            <div class="form-text">
                                {% if factor.category == 'tax' %}
                                    <i class="bi bi-info-circle me-1"></i>Enter as decimal (e.g., 0.06 for 6%)
                                {% elif factor.category == 'base' %}
                                    <i class="bi bi-info-circle me-1"></i>Enter the base price value
                                {% else %}
                                    <i class="bi bi-info-circle me-1"></i>Enter the multiplier factor
                                {% endif %}
                            </div>
                        </div>

                        {% if factor.calculation_formula %}
                        <div class="mb-3">
                            <label for="calculation_formula" class="form-label fw-semibold">Calculation Formula</label>
                            <input type="text" class="form-control bg-light" id="calculation_formula" 
                                   value="{{ factor.calculation_formula }}" readonly>
                        </div>
                        {% endif %}

                        {% if factor.percentage %}
                        <div class="mb-3">
                            <label for="percentage" class="form-label fw-semibold">Percentage</label>
                            <input type="text" class="form-control bg-light" id="percentage" 
                                   value="{{ factor.percentage }}" readonly>
                        </div>
                        {% endif %}

                        <div class="mb-3">
                            <label for="description" class="form-label fw-semibold">Description</label>
                            <textarea class="form-control bg-light" id="description" rows="3" readonly>{{ factor.description }}</textarea>
                        </div>

                        <div class="mb-4">
                            <label for="change_reason" class="form-label fw-semibold">
                                <i class="bi bi-chat-text me-1"></i>Reason for Change
                            </label>
                            <textarea class="form-control" id="change_reason" name="change_reason" rows="3" 
                                      placeholder="Please provide a reason for this change (optional but recommended)"></textarea>
                            <div class="form-text">
                                <i class="bi bi-clock-history me-1"></i>This will be recorded in the change history for audit purposes.
                            </div>
                        </div>

                        <div class="d-flex justify-content-between">
                            <a href="{{ url_for('admin.cost_factors') }}" class="btn btn-outline-secondary">
                                <i class="bi bi-x-circle me-1"></i> Cancel
                            </a>
                            <button type="submit" class="btn btn-primary">
                                <i class="bi bi-check-circle me-1"></i> Save Changes
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <div class="col-lg-4">
            <div class="card border-0 shadow-sm mb-3">
                <div class="card-header bg-light border-0">
                    <h5 class="card-title mb-0">
                        <i class="bi bi-info-circle me-2"></i>Current Information
                    </h5>
                </div>
                <div class="card-body">
                    <dl class="row mb-0">
                        <dt class="col-sm-4 fw-semibold">Category</dt>
                        <dd class="col-sm-8">
                            <span class="badge bg-primary">{{ factor.category }}</span>
                        </dd>
                        
                        <dt class="col-sm-4 fw-semibold">Name</dt>
                        <dd class="col-sm-8">{{ factor.name }}</dd>
                        
                        <dt class="col-sm-4 fw-semibold">Current Value</dt>
                        <dd class="col-sm-8">
                            <span class="badge bg-success fs-6">{{ "%.4f"|format(factor.factor_value) }}</span>
                        </dd>
                        
                        <dt class="col-sm-4 fw-semibold">Status</dt>
                        <dd class="col-sm-8">
                            {% if factor.is_active %}
                                <span class="badge bg-success">
                                    <i class="bi bi-check-circle me-1"></i>Active
                                </span>
                            {% else %}
                                <span class="badge bg-danger">
                                    <i class="bi bi-x-circle me-1"></i>Inactive
                                </span>
                            {% endif %}
                        </dd>
                        
                        <dt class="col-sm-4 fw-semibold">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 fw-semibold">Last Updated</dt>
                        <dd class="col-sm-8">{{ factor.updated_at.strftime('%Y-%m-%d %H:%M') if factor.updated_at else 'Never' }}</dd>
                        
                        {% if factor.updater %}
                        <dt class="col-sm-4 fw-semibold">Updated By</dt>
                        <dd class="col-sm-8">{{ factor.updater.username }}</dd>
                        {% endif %}
                    </dl>
                </div>
            </div>

            <div class="card border-0 shadow-sm">
                <div class="card-header bg-light border-0">
                    <h5 class="card-title mb-0">
                        <i class="bi bi-lightning me-2"></i>Quick Actions
                    </h5>
                </div>
                <div class="card-body">
                    <a href="{{ url_for('admin.cost_factor_history', factor_id=factor.id) }}" 
                       class="btn btn-outline-info btn-sm w-100 mb-2">
                        <i class="bi bi-clock-history me-1"></i> View Change History
                    </a>
                    
                    <a href="{{ url_for('admin.cost_factors') }}" 
                       class="btn btn-outline-secondary btn-sm w-100">
                        <i class="bi bi-list me-1"></i> Back to All Factors
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
.form-control[readonly] {
    background-color: #f8f9fa !important;
    border-color: #dee2e6;
    color: #6c757d;
}

.form-control:focus {
    border-color: #0d6efd;
    box-shadow: 0 0 0 0.2rem rgba(13, 110, 253, 0.25);
}

.badge {
    font-size: 0.875em;
}

dl.row dt {
    color: #6c757d;
    font-size: 0.9rem;
}

dl.row dd {
    margin-bottom: 0.75rem;
    font-size: 0.9rem;
}

.card {
    transition: transform 0.2s, box-shadow 0.2s;
}

.card:hover {
    transform: translateY(-1px);
    box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15) !important;
}

.btn {
    border-radius: 0.375rem;
}

.form-control-lg {
    font-size: 1.1rem;
    padding: 0.75rem 1rem;
}
</style>
{% endblock %} 