{% extends "base.html" %}
{% block head %}
    <script>
        var call_url = "{{ url_for('call_view', call_id='') }}";
        $(function () {
            $('.call-row').each(function (i, row) {
                $(row).find('td').wrapInner("<a href='" + call_url + row.dataset.id + (
                    i > 0 ? '?not-newest=1' : ''
                ) + "'></a>");
            })
        });
    </script>
{% endblock %}
{% block body %}

    {% set typ = func.type %}

    <h2>
        {% if typ == 'module' %}
            Executions of module:
        {% else %}
            Function:
        {% endif %}

        <span class="code-font">
            {% if typ == 'module' %}
                {{ short_path }}
            {% else %}
                {{ func.name }}
            {% endif %}
        </span>
    </h2>

    <p><strong>Full file path: </strong><a href="{{ url_for('file_view', path=func.file) }}">{{ func.file }}</a></p>

    {% if typ == 'function' %}
        {# if there are no calls then this may not be the most recent function object #}
        {% if calls %}
            <p><strong>Line: </strong>{{ func.lineno }}</p>
        {% endif %}

        <h3>Calls:</h3>
    {% endif %}

    {% if calls %}
        <table class="table table-hover table-bordered table-striped">
            <thead>
            <tr>
                <th></th>
                <th>Start time</th>
                {% if typ == 'function' %}
                    <th>Arguments</th>
                {% endif %}
                <th>Result</th>
            </tr>
            </thead>
            <tbody>
            {% for call in calls %}
                <tr data-id="{{ call.id }}" class="call-row">
                    <td>
                        {{ call.state_icon }}
                    </td>
                    <td>
                        {{ call.pretty_start_time }}
                    </td>

                    {% if typ == 'function' %}
                        <td>
                            {% if call.arguments_list %}
                                <ul>
                                    {% for k, v in call.arguments_list %}
                                        <li class="code-font">
                                            {{ k }} = {{ v }}
                                        </li>
                                    {% endfor %}
                                </ul>
                            {% else %}
                                -
                            {% endif %}
                        </td>
                    {% endif %}

                    <td class="code-font">
                        {{ call.result }}
                    </td>
                </tr>
            {% endfor %}

            </tbody>
        </table>
    {% else %}
        <p>No calls yet. Is the function still running?</p>
    {% endif %}
{% endblock %}