{% extends "base.html" %}

{% set active_page = "portfolio" %}

{% block title %} Portfolio overview {% endblock %}

{% block divs %}

<div class="container-fluid">
    <div class="row">
        <div class="col-sm-4 top-bottom">
            <h3>About</h3>
            <div class="justify">
                <p>
                    This page shows you how your portfolio of assets is doing in terms of energy (production &amp;
                    consumption) and flexibility.
                    Highlighted time slots require your attention, as upcoming balancing opportunities have been
                    identified.
                </p>
                {% if documentation_exists %}
                <p>
                    Need help understanding this page? Check out the
                    <a href="{{ url_for('flexmeasures_ui.static', filename='documentation/html/views/portfolio.html') }}"
                        target="_blank">documentation</a>.
                </p>
                {% endif %}
            </div>

            <h3>Controls</h3>
            {% block datetimepicker %} {{ super() }} {% endblock %}

            {% if asset_types | length > 0 %}
            <h3>Energy summary</h3>
            <table class="table text-right" style="font-size: 100%;">
                <thead>
                    <tr>
                        <th rowspan="2"><i class="icon-calculator icon-center"></i></th>
                        <th class="text-right"><span title="All production by assets of this type.">Production</span>
                        </th>
                        <th class="text-right"><span title="All consumption by assets of this type.">Consumption</span>
                        </th>
                        <!--th><span title="The revenue achieved by assets of this type (on the day-ahead market).">Revenue</span></th-->
                    </tr>
                    <tr>
                        <th class="text-right">MWh</th>
                        <th class="text-right">MWh</th>
                        <!--<th class="text-right">KRW</th>-->
                    </tr>
                </thead>
                <tbody>
                    {% for asset_type_name in asset_types %}
                    <tr>
                        <td class="text-left" {% if asset_types[asset_type_name].hover_label %}
                            title="{{ asset_types[asset_type_name].hover_label | capitalize }}" {% endif %}>
                            {{ asset_type_name | capitalize }}
                        </td>
                        <td>{% if asset_types[asset_type_name].is_producer and production_per_asset_type[asset_type_name] > 0  %}
                            {{ "{:,.3f}".format( production_per_asset_type[asset_type_name] ) }}
                            {% endif %}
                        </td>
                        <td>{% if asset_types[asset_type_name].is_consumer and consumption_per_asset_type[asset_type_name] > 0 %}
                            {{ "{:,.3f}".format( consumption_per_asset_type[asset_type_name] ) }}
                            {% endif %}
                        </td>
                    </tr>
                    {% endfor %}
                    {% if asset_types | length > 1 %}
                    <tr style="font-weight: bold;">
                        <td class="text-left">
                            Total
                        </td>
                        <td>{{ "{:,.3f}".format( sum_production ) }}</td>
                        <td>{{ "{:,.3f}".format( sum_consumption ) }}</td>
                    </tr>
                    {% endif %}
                </tbody>

            </table>

            {% if fm_mode == "demo" %}
            <h3>Flexibility summary</h3>
            <table class="table text-right" style="font-size: 100%;">
                <thead>
                    <tr>
                        <th rowspan="2"><i class="icon-calculator icon-center"></i></th>
                        <th class="text-right"><span
                                title="All curtailment actions by assets of this type.">Curtailment</span></th>
                        <th class="text-right"><span
                                title="All shifting actions by assets of this type.">Shifting</span></th>
                        <th class="text-right"><span
                                title="The revenue achieved by assets of this type (by payment of supplier).">Revenue</span>
                        </th>
                    </tr>
                    <tr>
                        <th class="text-right">MWh</th>
                        <th class="text-right">MWh * h</th>
                        <th class="text-right">KRW</th>
                    </tr>
                </thead>
                <tbody>
                    {% for asset_type_name in asset_types %}
                    <tr>
                        <td class="text-right" {% if asset_types[asset_type_name].hover_label %}
                            title="{{ asset_types[asset_type_name].hover_label | capitalize }}" {% endif %}>
                            {{ asset_type_name | capitalize }}
                        </td>
                        <td>{% if asset_types[asset_type_name].is_producer and flex_info["curtailment_per_asset_type"][asset_type_name] > 0  %}
                            {{ "{:,.3f}".format( flex_info["curtailment_per_asset_type"][asset_type_name] ) }}
                            {% endif %}
                        </td>
                        <td>{% if asset_types[asset_type_name].is_consumer and flex_info["shifting_per_asset_type"][asset_type_name] > 0  %}
                            {{ "{:,.3f}".format( flex_info["shifting_per_asset_type"][asset_type_name] ) }}
                            {% endif %}
                        </td>
                        <td>{{ "{:,.2f}".format( flex_info["profit_loss_flexibility_per_asset_type"][asset_type_name] ) }}</td>
                    </tr>
                    {% endfor %}
                    {% if asset_types | length > 1 %}
                    <tr style="font-weight: bold;">
                        <td class="text-left">
                            Total
                        </td>
                        <td>{{ "{:,.3f}".format( flex_info["sum_curtailment"] ) }}</td>
                        <td>{{ "{:,.3f}".format( flex_info["sum_shifting"] ) }}</td>
                        <td>{{ "{:,.2f}".format( flex_info["sum_profit_loss_flexibility"] ) }}</td>
                    </tr>
                    {% endif %}
                </tbody>
            </table>
            {% endif %}
            {% endif %}
            
            <h3>Market status</h3>
            <table
                class="table table-striped table-responsive paginate-5 {% if markets | length > 5 %}paginate{% else %}paginate-without-paging{% endif %}"
                style="font-size: 100%;">
                <thead>
                    <tr>
                        <th>Market</th>
                        <th>Currency</th>
                        <th>Average price</th>
                    </tr>
                </thead>
                <tbody>
                    {% for market in markets: %}
                    <tr>
                        <td>{{ market.display_name }}</td>
                        <td>{{ market.unit[:3] }}</td>
                        <td>
                            {% if not average_prices[market.name] | isnull %}
                            {{ "{:,.2f}".format( average_prices[market.name] ) }} {{ market.unit }}
                            {% else %}
                            Unknown for the requested period
                            {% endif %}
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>

        </div>
        <div class="col-sm-8">
            <div class="row">
                <div class="col-sm-12">
                    <h3>Portfolio status</h3>
                    <div class="alert alert-info">
                        {% if fm_mode == "demo" %}
                        <p>For onshore and vehicles, an opportunity is mocked for the following 4am timeslot (select
                            time window "today" or "tomorrow").
                            <b>Double-click the opportunity</b> to see the order book. </p>
                        {% endif%}
                        <p> You can also
                            <button onclick="window.location.href='?show_stacked={{alt_stacking}}'">
                                Switch to showing stacked {{ alt_stacking }}
                            </button>
                        </p>
                    </div>
                    {% if fm_mode == "demo" %}
                    <div class="row">
                        <div class="col-sm-12">
                            {{ portfolio_plots_divs[0] | safe }}
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-sm-12">
                            {{ portfolio_plots_divs[1] | safe }}
                        </div>
                    </div>
                    {% else %}
                    <div class="row">
                        <div class="col-sm-12">
                            {{ portfolio_plots_divs | safe }}
                        </div>
                    </div>
                    {% endif %}
                    {{ portfolio_plots_script | safe }}
                </div>
            </div>

            <div class="row">
                <div class="col-sm-12">
                    <h3>Asset status</h3>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-12">
                    <table class="table table-striped table-responsive paginate paginate-10">
                        <thead>
                            <tr>
                                <th>Asset</th>
                                <!--<th>Energy Service Company</th>-->
                                <th class="text-right">Production</th>
                                <th class="text-right">Consumption</th>
                                {% if fm_mode == "demo" %}
                                <th class="text-right">Curtailment</th>
                                <th class="text-right">Shifting</th>
                                <th class="text-right">Balancing revenue</th>
                                {% endif %}
                                <th class="no-sort"></th>
                            </tr>
                        </thead>

                        <tbody>
                            {% for asset in assets: %}
                            <tr>
                                <td>
                                    <i class="{{ asset.asset_type_name | asset_icon }} left-icon"><a
                                            href="/assets/{{ asset.id }}"
                                            title="Show asset meta data and location on map">{{ asset.display_name }}</a></i>
                                </td>
                                <td class="text-right">
                                    {% if production_per_asset[asset.name] > 0 %}
                                    {{ "{:,.2f}".format( production_per_asset[asset.name] ) }} MWh
                                    {% endif %}
                                </td>
                                <td class="text-right">
                                    {% if consumption_per_asset[asset.name] < 0 %}
                                    {{ "{:,.2f}".format( consumption_per_asset[asset.name] ) }} MWh
                                    {% endif %}
                                </td>
                                {% if fm_mode == "demo" %}
                                <td class="text-right">
                                    {% if asset.asset_type.can_curtail and flex_info["curtailment_per_asset"][asset.name] > 0 %}
                                    {{ "{:,.2f}".format( flex_info["curtailment_per_asset"][asset.name] ) }} MWh
                                    {% endif %}
                                </td>
                                <td class="text-right">
                                    {% if asset.asset_type.can_shift and flex_info["shifting_per_asset"][asset.name] > 0 %}
                                    {{ "{:,.2f}".format( flex_info["shifting_per_asset"][asset.name] ) }} MWh
                                    {% endif %}
                                </td>
                                <td class="text-right">
                                    {{ "{:,.2f}".format( flex_info["profit_loss_flexibility_per_asset"][asset.name] ) }} KRW
                                </td>
                                {% endif %}
                                <td class="text-right">
                                    <form action="analytics" method="get">
                                        <input type="hidden" name="resource" value="{{ asset.name }}">
                                        <button class="btn btn-sm btn-responsive btn-info" type="submit">Analyse this
                                            asset</button>
                                    </form>
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>

            <div class="row">
                <div class="col-sm-12">

                </div>
            </div>
        </div>
    </div>
</div>

{% block paginate_tables_script %} {{ super() }} {% endblock %}

{% endblock %}