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

{% block title %}Cost Factors Management{% 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-gear-fill me-2 text-primary"></i>Cost Factors Management
            </h1>
            <p class="text-muted mb-0">Configure calculation parameters and factors</p>
        </div>
        <div class="d-flex gap-2">
            <button onclick="resetFactors()" class="btn btn-warning">
                <i class="bi bi-arrow-clockwise me-1"></i>Reset to Defaults
            </button>
            <a href="{{ url_for('admin.dashboard') }}" class="btn btn-outline-secondary">
                <i class="bi bi-arrow-left me-1"></i>Back to Dashboard
            </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 %}

    <!-- Calculation Logic Diagram -->
    <div class="card border-0 shadow-sm mb-4">
        <div class="card-header bg-light border-0">
            <h3 class="card-title mb-0">
                <i class="bi bi-diagram-3 me-2 text-dark"></i>Calculation Logic
            </h3>
        </div>
        <div class="card-body">
            <div class="mermaid">
                graph TD
                    A[Base Cost] --> B[Base Factors]
                    B --> C[Mold Factors]
                    C --> D[Complexity Factors]
                    D --> E[Texture Factors]
                    E --> F[Tax Factors]
                    F --> G[Final Cost]
                    
                    B --> B1[Base Factor 1]
                    B --> B2[Base Factor 2]
                    C --> C1[Mold Factor 1]
                    C --> C2[Mold Factor 2]
                    D --> D1[Complexity Factor 1]
                    D --> D2[Complexity Factor 2]
                    E --> E1[Texture Factor 1]
                    E --> E2[Texture Factor 2]
                    F --> F1[Tax Rate]
            </div>
        </div>
    </div>

    <!-- Statistics Cards -->
    <div class="row mb-4">
        <div class="col-xl-2 col-md-4 col-sm-6 mb-3">
            <div class="card statistics-card border-0 shadow-sm h-100">
                <div class="card-body text-center">
                    <div class="icon-wrapper bg-primary bg-opacity-10 mx-auto mb-3">
                        <i class="bi bi-calculator text-primary fs-1"></i>
                    </div>
                    <h4 class="mb-1">{{ base_factors|length if base_factors else 0 }}</h4>
                    <p class="text-muted mb-0 small">Base Factors</p>
                </div>
            </div>
        </div>
        <div class="col-xl-2 col-md-4 col-sm-6 mb-3">
            <div class="card statistics-card border-0 shadow-sm h-100">
                <div class="card-body text-center">
                    <div class="icon-wrapper bg-success bg-opacity-10 mx-auto mb-3">
                        <i class="bi bi-building text-success fs-1"></i>
                    </div>
                    <h4 class="mb-1">{{ mold_factors|length if mold_factors else 0 }}</h4>
                    <p class="text-muted mb-0 small">Mold Factors</p>
                </div>
            </div>
        </div>
        <div class="col-xl-2 col-md-4 col-sm-6 mb-3">
            <div class="card statistics-card border-0 shadow-sm h-100">
                <div class="card-body text-center">
                    <div class="icon-wrapper bg-warning bg-opacity-10 mx-auto mb-3">
                        <i class="bi bi-puzzle text-warning fs-1"></i>
                    </div>
                    <h4 class="mb-1">{{ complexity_factors|length if complexity_factors else 0 }}</h4>
                    <p class="text-muted mb-0 small">Complexity</p>
                </div>
            </div>
        </div>
        <div class="col-xl-2 col-md-4 col-sm-6 mb-3">
            <div class="card statistics-card border-0 shadow-sm h-100">
                <div class="card-body text-center">
                    <div class="icon-wrapper bg-info bg-opacity-10 mx-auto mb-3">
                        <i class="bi bi-palette text-info fs-1"></i>
                    </div>
                    <h4 class="mb-1">{{ texture_factors|length if texture_factors else 0 }}</h4>
                    <p class="text-muted mb-0 small">Texture</p>
                </div>
            </div>
        </div>
        <div class="col-xl-2 col-md-4 col-sm-6 mb-3">
            <div class="card statistics-card border-0 shadow-sm h-100">
                <div class="card-body text-center">
                    <div class="icon-wrapper bg-danger bg-opacity-10 mx-auto mb-3">
                        <i class="bi bi-percent text-danger fs-1"></i>
                    </div>
                    <h4 class="mb-1">{{ tax_factors|length if tax_factors else 0 }}</h4>
                    <p class="text-muted mb-0 small">Tax Factors</p>
                </div>
            </div>
        </div>
    </div>

    <!-- Cost Factors Sections -->
    <div class="row">
        <div class="col-12">
            <!-- Base Factors -->
            <div class="card border-0 shadow-sm mb-4">
                <div class="card-header bg-primary bg-opacity-10 border-0">
                    <h3 class="card-title mb-0">
                        <i class="bi bi-layers me-2 text-primary"></i>Base Factors
                        <span class="badge bg-primary ms-2">{{ base_factors|length if base_factors else 0 }}</span>
                    </h3>
                </div>
                <div class="card-body">
                    {% if base_factors %}
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th class="border-0">Factor Name</th>
                                    <th class="border-0">Value</th>
                                    <th class="border-0">Description</th>
                                    <th class="border-0 text-end">Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for factor in base_factors %}
                                <tr>
                                    <td>
                                        <div class="d-flex align-items-center">
                                            <div class="avatar-sm bg-primary bg-opacity-10 rounded-circle d-flex align-items-center justify-content-center me-3" style="width: 40px; height: 40px;">
                                                <i class="bi bi-calculator text-primary"></i>
                                            </div>
                                            <div>
                                                <div class="fw-medium">{{ factor.name }}</div>
                                                <small class="text-muted">{{ factor.category }}</small>
                                            </div>
                                        </div>
                                    </td>
                                    <td>
                                        <span class="badge bg-primary fs-6">{{ "%.2f"|format(factor.factor_value) }}</span>
                                    </td>
                                    <td>{{ factor.description or 'Base calculation factor' }}</td>
                                    <td class="text-end">
                                        <a href="{{ url_for('admin.edit_cost_factor', factor_id=factor.id) }}" class="btn btn-sm btn-outline-primary">
                                            <i class="bi bi-pencil me-1"></i>Edit
                                        </a>
                                        <a href="{{ url_for('admin.cost_factor_history', factor_id=factor.id) }}" class="btn btn-sm btn-outline-info">
                                            <i class="bi bi-clock-history me-1"></i>History
                                        </a>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    {% else %}
                    <div class="text-center py-4">
                        <i class="bi bi-exclamation-triangle fs-1 text-warning mb-3"></i>
                        <p class="text-muted mb-0">No base factors found.</p>
                    </div>
                    {% endif %}
                </div>
            </div>

            <!-- Mold Factors -->
            <div class="card border-0 shadow-sm mb-4">
                <div class="card-header bg-success bg-opacity-10 border-0">
                    <h3 class="card-title mb-0">
                        <i class="bi bi-building me-2 text-success"></i>Mold Factors
                        <span class="badge bg-success ms-2">{{ mold_factors|length if mold_factors else 0 }}</span>
                    </h3>
                </div>
                <div class="card-body">
                    {% if mold_factors %}
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th class="border-0">Factor Name</th>
                                    <th class="border-0">Value</th>
                                    <th class="border-0">Description</th>
                                    <th class="border-0 text-end">Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for factor in mold_factors %}
                                <tr>
                                    <td>
                                        <div class="d-flex align-items-center">
                                            <div class="avatar-sm bg-success bg-opacity-10 rounded-circle d-flex align-items-center justify-content-center me-3" style="width: 40px; height: 40px;">
                                                <i class="bi bi-box text-success"></i>
                                            </div>
                                            <div>
                                                <div class="fw-medium">{{ factor.name }}</div>
                                                <small class="text-muted">{{ factor.category }}</small>
                                            </div>
                                        </div>
                                    </td>
                                    <td>
                                        <span class="badge bg-success fs-6">{{ "%.2f"|format(factor.factor_value) }}</span>
                                    </td>
                                    <td>{{ factor.description or 'Mold-related factor' }}</td>
                                    <td class="text-end">
                                        <a href="{{ url_for('admin.edit_cost_factor', factor_id=factor.id) }}" class="btn btn-sm btn-outline-success">
                                            <i class="bi bi-pencil me-1"></i>Edit
                                        </a>
                                        <a href="{{ url_for('admin.cost_factor_history', factor_id=factor.id) }}" class="btn btn-sm btn-outline-info">
                                            <i class="bi bi-clock-history me-1"></i>History
                                        </a>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    {% else %}
                    <div class="text-center py-4">
                        <i class="bi bi-exclamation-triangle fs-1 text-warning mb-3"></i>
                        <p class="text-muted mb-0">No mold factors found.</p>
                    </div>
                    {% endif %}
                </div>
            </div>

            <!-- Complexity Factors -->
            <div class="card border-0 shadow-sm mb-4">
                <div class="card-header bg-warning bg-opacity-10 border-0">
                    <h3 class="card-title mb-0">
                        <i class="bi bi-puzzle me-2 text-warning"></i>Complexity Factors
                        <span class="badge bg-warning ms-2">{{ complexity_factors|length if complexity_factors else 0 }}</span>
                    </h3>
                </div>
                <div class="card-body">
                    {% if complexity_factors %}
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th class="border-0">Factor Name</th>
                                    <th class="border-0">Value</th>
                                    <th class="border-0">Description</th>
                                    <th class="border-0 text-end">Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for factor in complexity_factors %}
                                <tr>
                                    <td>
                                        <div class="d-flex align-items-center">
                                            <div class="avatar-sm bg-warning bg-opacity-10 rounded-circle d-flex align-items-center justify-content-center me-3" style="width: 40px; height: 40px;">
                                                <i class="bi bi-gear text-warning"></i>
                                            </div>
                                            <div>
                                                <div class="fw-medium">{{ factor.name }}</div>
                                                <small class="text-muted">{{ factor.category }}</small>
                                            </div>
                                        </div>
                                    </td>
                                    <td>
                                        <span class="badge bg-warning fs-6">{{ "%.2f"|format(factor.factor_value) }}</span>
                                    </td>
                                    <td>{{ factor.description or 'Complexity-related factor' }}</td>
                                    <td class="text-end">
                                        <a href="{{ url_for('admin.edit_cost_factor', factor_id=factor.id) }}" class="btn btn-sm btn-outline-warning">
                                            <i class="bi bi-pencil me-1"></i>Edit
                                        </a>
                                        <a href="{{ url_for('admin.cost_factor_history', factor_id=factor.id) }}" class="btn btn-sm btn-outline-info">
                                            <i class="bi bi-clock-history me-1"></i>History
                                        </a>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    {% else %}
                    <div class="text-center py-4">
                        <i class="bi bi-exclamation-triangle fs-1 text-warning mb-3"></i>
                        <p class="text-muted mb-0">No complexity factors found.</p>
                    </div>
                    {% endif %}
                </div>
            </div>

            <!-- Texture Factors -->
            <div class="card border-0 shadow-sm mb-4">
                <div class="card-header bg-info bg-opacity-10 border-0">
                    <h3 class="card-title mb-0">
                        <i class="bi bi-brush me-2 text-info"></i>Texture Factors
                        <span class="badge bg-info ms-2">{{ texture_factors|length if texture_factors else 0 }}</span>
                    </h3>
                </div>
                <div class="card-body">
                    {% if texture_factors %}
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th class="border-0">Factor Name</th>
                                    <th class="border-0">Value</th>
                                    <th class="border-0">Description</th>
                                    <th class="border-0 text-end">Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for factor in texture_factors %}
                                <tr>
                                    <td>
                                        <div class="d-flex align-items-center">
                                            <div class="avatar-sm bg-info bg-opacity-10 rounded-circle d-flex align-items-center justify-content-center me-3" style="width: 40px; height: 40px;">
                                                <i class="bi bi-palette text-info"></i>
                                            </div>
                                            <div>
                                                <div class="fw-medium">{{ factor.name }}</div>
                                                <small class="text-muted">{{ factor.category }}</small>
                                            </div>
                                        </div>
                                    </td>
                                    <td>
                                        <span class="badge bg-info fs-6">{{ "%.2f"|format(factor.factor_value) }}</span>
                                    </td>
                                    <td>{{ factor.description or 'Texture-related factor' }}</td>
                                    <td class="text-end">
                                        <a href="{{ url_for('admin.edit_cost_factor', factor_id=factor.id) }}" class="btn btn-sm btn-outline-info">
                                            <i class="bi bi-pencil me-1"></i>Edit
                                        </a>
                                        <a href="{{ url_for('admin.cost_factor_history', factor_id=factor.id) }}" class="btn btn-sm btn-outline-info">
                                            <i class="bi bi-clock-history me-1"></i>History
                                        </a>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    {% else %}
                    <div class="text-center py-4">
                        <i class="bi bi-exclamation-triangle fs-1 text-warning mb-3"></i>
                        <p class="text-muted mb-0">No texture factors found.</p>
                    </div>
                    {% endif %}
                </div>
            </div>

            <!-- Tax Factors -->
            <div class="card border-0 shadow-sm mb-4">
                <div class="card-header bg-danger bg-opacity-10 border-0">
                    <h3 class="card-title mb-0">
                        <i class="bi bi-percent me-2 text-danger"></i>Tax Factors
                        <span class="badge bg-danger ms-2">{{ tax_factors|length if tax_factors else 0 }}</span>
                    </h3>
                </div>
                <div class="card-body">
                    {% if tax_factors %}
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th class="border-0">Country/Region</th>
                                    <th class="border-0">Tax Rate</th>
                                    <th class="border-0 text-end">Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for factor in tax_factors %}
                                <tr>
                                    <td>
                                        <div class="d-flex align-items-center">
                                            <div class="avatar-sm bg-danger bg-opacity-10 rounded-circle d-flex align-items-center justify-content-center me-3" style="width: 40px; height: 40px;">
                                                <i class="bi bi-flag text-danger"></i>
                                            </div>
                                            <div>
                                                <div class="fw-medium">{{ factor.name }}</div>
                                                <small class="text-muted">{{ factor.description or 'Tax rate' }}</small>
                                            </div>
                                        </div>
                                    </td>
                                    <td>
                                        <span class="badge bg-danger fs-6">{{ "%.1f"|format(factor.factor_value * 100) }}%</span>
                                    </td>
                                    <td class="text-end">
                                        <a href="{{ url_for('admin.edit_cost_factor', factor_id=factor.id) }}" class="btn btn-sm btn-outline-danger">
                                            <i class="bi bi-pencil me-1"></i>Edit
                                        </a>
                                        <a href="{{ url_for('admin.cost_factor_history', factor_id=factor.id) }}" class="btn btn-sm btn-outline-info">
                                            <i class="bi bi-clock-history me-1"></i>History
                                        </a>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    {% else %}
                    <div class="text-center py-4">
                        <i class="bi bi-exclamation-triangle fs-1 text-warning mb-3"></i>
                        <p class="text-muted mb-0">No tax factors found.</p>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Add Mermaid.js for diagram rendering -->
<script src="{{ url_for('static', filename='js/mermaid.min.js') }}"></script>
<script>
mermaid.initialize({
    startOnLoad: true,
    theme: 'default',
    securityLevel: 'loose',
    fontFamily: '"Helvetica Neue", Arial, sans-serif'
});

document.addEventListener('DOMContentLoaded', function() {
    console.log('Cost Factors Template Loaded');
    try {
        const debugInfo = {
            baseFactors: {{ base_factors|length if base_factors else 0 }},
            moldFactors: {{ mold_factors|length if mold_factors else 0 }},
            complexityFactors: {{ complexity_factors|length if complexity_factors else 0 }},
            textureFactors: {{ texture_factors|length if texture_factors else 0 }},
            taxFactors: {{ tax_factors|length if tax_factors else 0 }}
        };
        console.log('Debug Info:', debugInfo);
    } catch (error) {
        console.error('Error in debug logging:', error);
    }
});

function resetFactors() {
    if (confirm('Are you sure you want to reset all cost factors to their default values? This action cannot be undone.')) {
        fetch('{{ url_for("admin.reset_cost_factors") }}', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            }
        }).then(response => {
            if (response.ok) {
                window.location.reload();
            } else {
                alert('Error resetting factors. Please try again.');
            }
        }).catch(error => {
            console.error('Error:', error);
            alert('Error resetting factors. Please try again.');
        });
    }
}
</script>

{% block styles %}
{{ super() }}
<style>
/* Card Styles */
.card {
    transition: transform 0.2s, box-shadow 0.2s;
}

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

/* Table Styles */
.table th {
    font-weight: 600;
    color: #495057;
}

.table td {
    vertical-align: middle;
}

/* Badge Styles */
.badge {
    padding: 0.5em 0.8em;
}

.badge.fs-6 {
    font-size: 0.875rem !important;
}

/* Icon Styles */
.avatar-sm i {
    font-size: 1.25rem;
}

/* Animation */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

.card {
    animation: fadeIn 0.3s ease-out;
}

/* Statistics Cards */
.statistics-card {
    border-radius: 1rem;
    overflow: hidden;
}

.statistics-card .icon-wrapper {
    width: 48px;
    height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
}

/* Mermaid Diagram */
.mermaid {
    background: white;
    padding: 1rem;
    border-radius: 0.5rem;
}

/* Responsive Design */
@media (max-width: 768px) {
    .statistics-card {
        margin-bottom: 1rem;
    }
    
    .table-responsive {
        margin: 0 -1rem;
    }
}
</style>
{% endblock %}
{% endblock %} 