{% extends "base.html" %}

{% import "bootstrap/wtf.html" as wtf %}
{% import "bootstrap/fixes.html" as fixes %}
{% import "bootstrap/utils.html" as util %}
{% import "macros.html" as pbw %}

{% block title %}User {{ user.email }}{% endblock %}

{% block scripts %}
    {{ super() }}

    <script>
        $(function () {
            $('[data-toggle="tooltip"]').tooltip()
        })
    </script>

    <script type="text/javascript" src="https://cdn.datatables.net/v/bs/dt-1.10.16/datatables.min.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/buttons/1.5.1/js/dataTables.buttons.min.js"></script>

    <script>
        $(document).ready(function () {
            $("#network-table").DataTable();
            $("#query-table").DataTable();

            {% if 'analysis' in blueprints %}
                $("#experiment-table").DataTable();
                $("#omic-table").DataTable();
            {% endif %}
        });
    </script>
{% endblock %}

{% block content %}
    <div class="container">
        {{ util.flashed_messages(dismissible=True, container=False) }}

        {% if current_user.is_authenticated and current_user.is_admin %}
            <ol class="breadcrumb">
                <li><a href="{{ url_for('ui.home') }}">Catalog</a></li>
                <li><a href="{{ url_for('ui.view_users') }}">Users</a></li>
                <li class="active">{{ user }}</li>
            </ol>
        {% endif %}

        <div class="page-header">
            <h1>
                {% if user == current_user %}
                    My Activity
                {% else %}
                    {{ user }}
                {% endif %}
            </h1>
        </div>

        <!-- Reports panel -->
        {% if pending_reports|length > 0 %}
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">{% if user == current_user %}My {% endif %}Pending Jobs</h3>
                </div>
                <ul class="list-group">
                    {% for report in pending_reports %}
                        {% if not report.failed and not report.stalled %}
                            <li class="list-group-item">
                                <code>#{{ report.id }}</code> {{ report.source_name }}
                                {#
                                Not sure why this isn't working
                                <a href="{{ url_for('task.check', uuid=report.task_uuid) }}">{{ report.task_uuid }}</a>
                                #}
                            </li>
                        {% endif %}
                    {% endfor %}
                </ul>
                <a href="{{ url_for('parser.view_parser') }}" class="btn btn-primary btn-block">
                    Upload Another BEL Script
                </a>
            </div>
        {% endif %}


        <!-- Projects panel -->
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">{% if user == current_user %}My {% endif %}Projects</h3>
            </div>
            <div class="list-group">
                {% for project in user.projects %}
                    <div class="list-group-item">
                        <h4 class="list-group-item-heading">{{ project.name }}</h4>
                        <div class="list-group-item-text">
                            <p>
                                {% if project.description %}
                                    {{ project.description }}
                                {% else %}
                                    <code>No description has been given.</code>
                                {% endif %}
                            </p>

                            <ul class="list-inline">
                                {% if current_user.is_authenticated and current_user.is_admin %}
                                    <li>
                                        <a target="_blank" class="btn btn-info"
                                           href="{{ url_for('dbs.get_project_metadata', project_id=project.id) }}">
                                            {{ project.id }}
                                        </a>
                                    </li>
                                {% endif %}
                                {% if project.networks %}
                                    {# Make sure that there's networks or don't show download buttons #}
                                    <li>
                                        <a href="{{ url_for('dbs.summarize_project', project_id=project.id) }}"
                                           class="btn btn-default" data-toggle="tooltip" data-placement="top"
                                           title="Export an Excel sheet with a summary of all networks in this project as well as a summary of the merged network">
                                            <span class="glyphicon glyphicon-signal" aria-hidden="true"></span>
                                            Summarize
                                        </a>
                                    </li>
                                    <li>
                                        <a href="{{ url_for('ui.view_explore_project', project_id=project.id) }}"
                                           class="btn btn-default" data-toggle="tooltip"
                                           data-placement="top" title="Merge all networks and explore">
                                            <span class="glyphicon glyphicon-search" aria-hidden="true"></span>
                                            Explore
                                        </a>
                                    </li>
                                    <li>
                                        <a href="{{ url_for('ui.send_async_project_merge', user_id=current_user.id, project_id=project.id) }}"
                                           class="btn btn-default" data-toggle="tooltip" data-placement="top"
                                           title="Merge all networks into a single BEL document and email as a single BEL script">
                                            <span class="glyphicon glyphicon-download" aria-hidden="true"></span>
                                            Merge and Email BEL
                                        </a>
                                    </li>
                                {% endif %}
                                <li>
                                    <a href="{{ url_for('project.edit_view', id=project.id, url=url_for('ui.view_current_user_activity')) }}"
                                       title="Edit" class="btn btn-default">
                                        <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
                                        Edit
                                    </a>
                                </li>
                                <li>
                                    <a href="{{ url_for('dbs.drop_project_by_id', project_id=project.id, next=url_for('ui.view_current_user_activity')) }}"
                                       title="Drop" class="btn btn-default">
                                        <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
                                        Drop
                                    </a>
                                </li>
                            </ul>

                            <div class="row">
                                <div class="col-md-6">
                                    <h5>Networks</h5>
                                    <ul>
                                        {% for project_network in project.networks %}
                                            <li>
                                                <a href="{{ url_for('ui.view_network', network_id=project_network.id) }}">
                                                    {{ project_network }}
                                                </a>
                                            </li>
                                        {% endfor %}
                                    </ul>
                                </div>
                                <div class="col-md-6">
                                    <h5>Members</h5>
                                    <ul>
                                        {% for user in project.users %}
                                            <li>
                                                {{ user }}
                                            </li>
                                        {% endfor %}
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>
                {% endfor %}
            </div>
        </div>

        <!-- Networks panel -->
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">{% if user == current_user %}My {% endif %}Uploaded Networks</h3>
            </div>

            <div class="panel-body">
                <table class="table table-striped table-responsive" id="network-table">
                    <thead>
                    <tr>
                        <th>Network</th>
                        <th>Date of Upload</th>
                        <th></th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for report in user.reports | reverse %}
                        {% if report.network is not none %}
                            <tr>
                                <td>
                                    <a href="{{ url_for('ui.view_network', network_id=report.network.id) }}">
                                        {{ report.network }}
                                    </a>
                                </td>
                                <td>{{ report.created }}</td>
                                <td>
                                    <div class="pull-right">
                                        <a class="btn btn-default btn-xs"
                                           href="{{ url_for('ui.view_network', network_id=report.network.id) }}">
                                            <span class="glyphicon glyphicon-signal" aria-hidden="true"></span>
                                        </a>
                                        <a class="btn btn-default btn-xs"
                                           href="{{ url_for('ui.view_explore_network', network_id=report.network.id) }}">
                                            <span class="glyphicon glyphicon-search" aria-hidden="true"></span>
                                        </a>
                                        <a class="btn btn-default btn-xs"
                                           href="{{ url_for('dbs.drop_network', network_id=report.network.id, next=url_for('ui.view_current_user_activity')) }}">
                                            <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
                                        </a>
                                    </div>
                                </td>
                            </tr>
                        {% endif %}
                    {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>

        {% if 'analysis' in blueprints %}
            <!-- Omics panel -->
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">{% if user == current_user %}My {% endif %}Omics</h3>
                </div>
                <div class="panel-body">
                    <table class="table table-striped table-responsive" id="omic-table">
                        <thead>
                        <tr>
                            <th>Identifier</th>
                            <th>Name</th>
                            <th>Description</th>
                            <th>Public</th>
                            <th></th>
                        </tr>
                        </thead>
                        <tbody>
                        {% for omic in user.omics %}
                            <tr>
                                <td>{{ omic.id }}</td>
                                <td>{{ omic.source_name }}</td>
                                <td>{{ omic.description }}</td>
                                <td>
                                    {% if omic.public %}
                                        <span class="glyphicon glyphicon-ok-circle" aria-hidden="true"></span>
                                    {% else %}
                                        <span class="glyphicon glyphicon glyphicon-remove-circle"
                                              aria-hidden="true"></span>
                                    {% endif %}
                                </td>
                            </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>

            <!-- Experiments panel -->
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">{% if user == current_user %}My {% endif %}Experiments</h3>
                </div>
                <div class="panel-body">
                    <p>
                        Differential gene expression data can be created easily with GEO2R by following <a
                            href="{{ url_for('help.dgx') }}">this
                        tutorial</a>. As an example pipeline, we recommend using the data from GEO Experiment GSE28146
                        with the available NeuroMMSig subgraphs.
                    </p>
                </div>
                <div class="panel-body">
                    {{ pbw.render_experiment_table(user.experiments) }}
                </div>
            </div>
        {% endif %}

        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">{% if user == current_user %}My {% endif %}Queries</h3>
            </div>
            <div class="panel-body">
                {{ pbw.render_query_table(queries) }}
            </div>
        </div>
    </div>

    {% include "footer.html" %}
{% endblock %}
