{% load static %}
{% load i18n %}
{% load crispy_forms_tags %}
{% load inventree_extras %}

{% settings_value "PART_INTERNAL_PRICE" as show_internal_price %}

<a class="anchor" id="overview"></a>
<div class='panel-heading'>
    <div class='d-flex flex-wrap'>
        <h4>{% trans "Pricing Overview" %}</h4>
        {% include "spacer.html" %}
        <div class='btn-group' role='group'>
            <button type='button' class='btn btn-success' id='part-pricing-refresh' title='{% trans "Refresh Part Pricing" %}'>
                <span class='fas fa-redo-alt'></span> {% trans "Refresh" %}
            </button>
            <button type='button' class='btn btn-success' id='part-pricing-edit' title='{% trans "Override Part Pricing" %}'>
                <span class='fas fa-edit'></span> {% trans "Edit" %}
            </button>
        </div>
    </div>
</div>
<div class='panel-content'>
    {% with part.pricing as pricing %}
    {% if pricing.is_valid %}
    <!-- Part pricing table -->
    <div class='alert alert-info alert-block'>
        {% trans "Last Updated" %}: {% render_date pricing.updated %}
    </div>
    <div class='row full-height'>
        <div class='col col-md-6'>
            <table class='table table-striped table-condensed'>
                <col width='25'>
                <thead>
                    <tr>
                        <th></th>
                        <th>{% trans "Price Category" %}</th>
                        <th>{% trans "Minimum" %}</th>
                        <th>{% trans "Maximum" %}</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>
                            {% if show_internal_price and roles.sales_order.view %}
                            <a href='#internal-cost'>
                                <span class='fas fa-dollar-sign'></span>
                            </a>
                            {% endif %}
                        </td>
                        <th>{% trans "Internal Pricing" %}</th>
                        <td>{% render_currency pricing.internal_cost_min %}</td>
                        <td>{% render_currency pricing.internal_cost_max %}</td>
                    </tr>
                    {% if part.purchaseable %}
                    <tr>
                        <td>
                            {% if roles.purchase_order.view %}
                            <a href='#purchase-price-history'>
                                <span class='fas fa-chart-line'></span>
                            </a>
                            {% endif %}
                        </td>
                        <th>{% trans "Purchase History" %}</th>
                        <td>{% render_currency pricing.purchase_cost_min %}</td>
                        <td>{% render_currency pricing.purchase_cost_max %}</td>
                    </tr>
                    <tr>
                        <td>
                            {% if roles.purchase_order.view %}
                            <a href='#supplier-prices'>
                                <span class='fas fa-building'></span>
                            </a>
                            {% endif %}
                        </td>
                        <th>{% trans "Supplier Pricing" %}</th>
                        <td>{% render_currency pricing.supplier_price_min %}</td>
                        <td>{% render_currency pricing.supplier_price_max %}</td>
                    </tr>
                    {% endif %}
                    {% if part.assembly %}
                    <tr>
                        <td>
                            {% if part.has_bom %}
                            <a href='#bom-cost'>
                                <span class='fas fa-tools'></span>
                            </a>
                            {% endif %}
                        </td>
                        <th>{% trans "BOM Pricing" %}</th>
                        <td>{% render_currency pricing.bom_cost_min %}</td>
                        <td>{% render_currency pricing.bom_cost_max %}</td>
                    </tr>
                    {% endif %}
                    {% if part.is_template %}
                    <tr>
                        <td><a href='#variant-cost'><span class='fas fa-shapes'></span></a></td>
                        <th>{% trans "Variant Pricing" %}</th>
                        <td>{% render_currency pricing.variant_cost_min %}</td>
                        <td>{% render_currency pricing.variant_cost_max %}</td>
                    </tr>
                    {% endif %}
                    {% if pricing.override_min or pricing.override_max %}
                    <tr>
                        <td><a href='#pricing-overrides'><span class='fas fa-exclamation-circle'></span></a></td>
                        <th>{% trans "Pricing Overrides" %}</th>
                        <td>{% render_currency pricing.override_min currency=pricing.override_min_currency %}</td>
                        <td>{% render_currency pricing.override_max currency=pricing.override_max_currency %}</td>
                    </tr>
                    {% endif %}
                    <tr>
                        <td></td>
                        <th>{% trans "Overall Pricing" %}</th>
                        <th>{% render_currency pricing.overall_min %}</th>
                        <th>{% render_currency pricing.overall_max %}</th>
                    </tr>
                </tbody>
            </table>
        </div>
        <div class='col col-md-6'>
            {% if part.salable and roles.sales_order.view %}
            <table class='table table-striped table-condensed'>
                <col width='25'>
                <thead>
                    <tr>
                        <th></th>
                        <th>{% trans "Price Category" %}</th>
                        <th>{% trans "Minimum" %}</th>
                        <th>{% trans "Maximum" %}</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>
                            <a href='#sale-cost'>
                                <span class='fas fa-dollar-sign'></span>
                            </a>
                        </td>
                        <th>{% trans "Sale Price" %}</th>
                        <td>{% render_currency pricing.sale_price_min %}</td>
                        <td>{% render_currency pricing.sale_price_max %}</td>
                    </tr>
                    <tr>
                        <td>
                            <a href='#sale-price-history'>
                                <span class='fas fa-chart-line'></span>
                            </a>
                        </td>
                        <th>{% trans "Sale History" %}</th>
                        <td>{% render_currency pricing.sale_history_min %}</td>
                        <td>{% render_currency pricing.sale_history_max %}</td>
                    </tr>
                </tbody>
            </table>
            {% else %}
            <div class='alert alert-block alert-info'>
                {% trans "Sale price data is not available for this part" %}
            </div>
            {% endif %}
        </div>
    </div>
    {% else %}
    <div class='alert alert-warning alert-block'>
        {% trans "Price range data is not available for this part." %}
    </div>
    {% endif %}
    {% endwith %}
</div>

{% if show_internal_price and roles.sales_order.view %}
<a class="anchor" id="internal-cost"></a>
<div class='panel-heading'>
    <div class='d-flex flex-wrap'>
        <h4>{% trans "Internal Pricing" %}
            <a href="#overview" title='{% trans "Jump to overview" %}'><span class="fas fa-level-up-alt"></span></a>
        </h4>
        {% include "spacer.html" %}
        <div class='btn-group' role='group'>
            <button class='btn btn-success' id='new-internal-price-break' type='button'>
                <span class='fas fa-plus-circle'></span> {% trans "Add Internal Price Break" %}
            </button>
        </div>
    </div>
</div>

<div class='panel-content'>
    <div class="row full-height">
        <div class="col col-md-6">
            <div style="max-width: 99%; height: 100%;">
                <canvas id="InternalPriceBreakChart"></canvas>
            </div>
        </div>
        <div class="col col-md-6">

            <table class='table table-striped table-condensed' id='internal-price-break-table'>
            </table>
        </div>
    </div>
</div>
{% endif %}

{% if part.purchaseable and roles.purchase_order.view %}
<a class="anchor" id="purchase-price-history"></a>
<div class='panel-heading'>
    <h4>
        {% trans "Purchase History" %}
        <a href="#overview" title='{% trans "Jump to overview" %}'><span class="fas fa-level-up-alt"></span></a>
    </h4>
</div>
<div class='panel-content'>
    <div class="row full-height">
        <div class="col col-md-6">
            <div style="max-width: 99%; height: 100%;">
                <canvas id="part-purchase-history-chart"></canvas>
            </div>
        </div>
        <div class="col col-md-6">
            <table class='table table-striped table-condensed' id='part-purchase-history-table'>
            </table>
        </div>
    </div>
</div>

<a class="anchor" id="supplier-prices"></a>
<div class='panel-heading'>
    <h4>
        {% trans "Supplier Pricing" %}
        <a href="#overview" title='{% trans "Jump to overview" %}'><span class="fas fa-level-up-alt"></span></a>
    </h4>
</div>

<div class='panel-content'>
    <div class='row full-height'>
        <div class="col col-md-6">
            <div style="max-width: 99%; height: 100%;">
                <canvas id="part-supplier-pricing-chart"></canvas>
            </div>
        </div>
        <div class="col col-md-6">
            <table class='table table-striped table-condensed' id='part-supplier-pricing-table'>
            </table>
        </div>
    </div>
</div>
{% endif %}

{% if part.assembly and part.has_bom %}
<a class="anchor" id="bom-cost"></a>
<div class='panel-heading'>
    <h4>{% trans "BOM Pricing" %}
        <a href="#overview" title='{% trans "Jump to overview" %}'><span class="fas fa-level-up-alt"></span></a>
    </h4>
</div>

<div class='panel-content'>
    <div class='row full-height'>
        <div class="col col-md-6">
            <div style="max-width: 99%; height: 100%;">
                <canvas id="bom-pricing-chart"></canvas>
            </div>
        </div>
        <div class="col col-md-6">
            <table class='table table-striped table-condensed' id='bom-pricing-table'>
            </table>
        </div>
    </div>
</div>
{% endif %}

{% if part.is_template %}
<a class='anchor' id='variant-cost'></a>
<div class='panel-heading'>
    <h4>
        {% trans "Variant Pricing" %}
        <a href="#overview" title='{% trans "Jump to overview" %}'><span class="fas fa-level-up-alt"></span></a>
    </h4>
</div>
<div class='panel-content'>
    <div class="row full-height">
        <div class="col col-md-6">
            <div style="max-width: 99%; height: 100%;">
                <canvas id="variant-pricing-chart"></canvas>
            </div>
        </div>
        <div class="col col-md-6">
            <table class='table table-striped table-condensed' id='variant-pricing-table'>
            </table>
        </div>
    </div>
</div>
{% endif %}

{% if part.salable and roles.sales_order.view %}
<a class="anchor" id="sale-cost"></a>
<div class='panel-heading'>
    <div class='d-flex flex-wrap'>
        <h4>{% trans "Sale Pricing" %}
            <a href="#overview" title='{% trans "Jump to overview" %}'><span class="fas fa-level-up-alt"></span></a>
        </h4>
        {% include "spacer.html" %}
        <div class='btn-group' role='group'>
            <button class='btn btn-success' id='new-price-break' type='button'>
                <span class='fas fa-plus-circle'></span> {% trans "Add Sell Price Break" %}
            </button>
        </div>
    </div>
</div>

<div class='panel-content'>
    <div class="row full-height">
        <div class="col col-md-6">
            <div style="max-width: 99%; height: 100%;">
                <canvas id="SalePriceBreakChart"></canvas>
            </div>
        </div>
        <div class="col col-md-6">
            <table class='table table-striped table-condensed' id='price-break-table'>
            </table>
        </div>
    </div>
</div>

<a class="anchor" id="sale-price-history"></a>
<div class='panel-heading'>
    <div class='d-flex flex-wrap'></div>
    <h4>{% trans "Sale History" %}
        <a href="#overview" title='{% trans "Jump to overview" %}'><span class="fas fa-level-up-alt"></span></a>
    </h4>
</div>

<div class='panel-content'>
    <div class="row full-height">
        <div class="col col-md-6">
            <div style="max-width: 99%; height: 100%;">
                <canvas id="part-sales-history-chart"></canvas>
            </div>
        </div>
        <div class="col col-md-6">
            <table class='table table-striped table-condensed' id='part-sales-history-table'>
            </table>
        </div>
    </div>
</div>
{% endif %}
