{% load guardian_tags %}
{% load widget_tweaks %}
{% load url_link %}
{% load dict %}
{% load is_publisher %}
{% load crispy_forms_tags %}
{% load plurals_type %}
{% load has_access_url %}
{% block content %}
{% get_obj_perms request.user for project as "project_perms" %}
{% if "change_project" in project_perms %}
<a style="margin-bottom: 5px;" class="btn btn-primary"
    href="{% url 'core:choose_tool_project' project.id data_type item.id %}">Add Link</a>
{% else %}
<div class="disabled-button-wrapper" data-toggle="popover" data-trigger="hover" data-placement="right"
    title="Action not available" data-container="body" data-html="true"
    data-content="You don't have sufficient permissions to  <br/> <strong>Add Link</strong>.">
    <button class="add-link btn btn-primary " type="button" disabled>Add link</button>
</div>
{% endif %}
</div>
</div>
</br>
<section>

    <table id="table" class="table table-striped table-bordered table-hover">

        <thead>
            <tr>
                <th>Name</th>
                <th>Tool</th>
                <th>Actions</th>
            </tr>
        </thead>

        <tbody>
            {% for mapping in mappings %}
            {% if not mapping.0.tool_id|is_publisher %}
            <tr>
                <td><i class="fa fa-laptop fa-1x laptop ic-w mx-1"></i><a href="{{mapping.0.link_url}}"
                        target="_blank">{{mapping.0.name}}</a></td>
                <td>{{mapping.0.link_tool}}</td>
                <td>
                    <div class="btn-group">
                        <div class="btn-group">
                            {% if mapping.0.tool_id|has_access_url %}
                            <a type="button" style="margin-right:0; margin-left: 10px;"
                                class="btn-sm btn rounded btn-secondary" role=button href="{{mapping.0.link_url}}"
                                target="_blank">View on {{mapping.0.link_tool}}</a>
                            {% endif %}
                        </div>

                        <button type="button" style="margin-left: 10px;border:0px"
                            class="btn-sm dropdown-toggle dropdown-toggle-split btn-secondary" data-toggle="dropdown"
                            aria-haspopup="true" aria-expanded="false">Add & Link
                        </button>


                        <div class="dropdown-menu {{key|lower}}" aria-labelledby="dropdownMenuLink">
                            {% for key, values in mapping.1.items %}
                            <a class="dropdown-item "
                                href='{% url 'core:'|add:key  project_id=project.id investigation_id=investigation.id tool_id=mapping.0.tool_id.id map_id=mapping.0.object_id %}'> {{mapping.0.link_tool|lower}}
                                {% for value in values %}{{value}}{% if not forloop.last %} &{% endif %}{% endfor %}</a>
                            {% endfor %}
                        </div>
                        <a type="button" style="margin-right:0; margin-left: 10px;"
                            class="btn-sm btn rounded btn-danger" role=button
                            href='{% url 'core:delete-mapping' project_id=project.id investigation_id=investigation.id data_type="investigation" pk=mapping.0.id  %}'>
                            <i class="fa fa-trash-alt fa-1x"></i></a>
                    </div>
                    </div>
            </tr>
            {% endif %}
            {% endfor %}
        </tbody>
    </table>

</section>

<br><br>
<div class="row">
    <div class="col-sm-6">
        <span class="badge badge-pill badge-secondary">{{ nb_publisher }}</span>
        <span class="texte-badge"> publication found</span>
    </div>
    <div class="col-sm-6 text-right">
        <a style="margin-bottom: 5px;" class="btn btn-info"
            href="{% url 'core:choose_tool_for_publish_project' project.id data_type item.id %}">Publish</a>
    </div>
</div>
<section>

    <table id="table" class="table table-striped table-bordered table-hover">

        <thead>
            <tr>
                <th>Name</th>
                <th>DOI</th>
                <th>Tool</th>
                <th>Actions</th>
            </tr>
        </thead>

        <tbody>
            {% for mapping in mappings %}
            {% if mapping.0.tool_id|is_publisher %}
            <tr>
                <td><i class="fa fa-laptop fa-1x laptop ic-w mx-1"></i><a href="{{mapping.0.link_url}}"
                        target="_blank">{{mapping.0.name}}</a></td>
                <td>{{mapping.0.object_id}}</td>
                <td>{{mapping.0.link_tool}}</td>

                <td>
                    <div class="btn-group">
                        <div class="btn-group">
                            <a type="button" style="margin-right:0; margin-left: 10px;"
                                class="btn-sm btn rounded btn-secondary" role=button href="{{mapping.0.link_url}}"
                                target="_blank">View on {{mapping.0.link_tool}}</a>
                        </div>
                        <a type="button" style="margin-right:0; margin-left: 10px;"
                            class="btn-sm btn rounded btn-secondary" role=button target="_blank"
                            href='{% url 'core:choose_object_to_publish' project.id data_type investigation.id mapping.0.link_tool mapping.0.tool_id.id mapping.0.object_id %}'>
                            Update depository</a>
                    </div>
                    <a type="button" style="margin-right:0; margin-left: 10px;" class="btn-sm btn rounded btn-danger"
                        role=button
                        href='{% url 'core:delete-mapping' project_id=project.id investigation_id=investigation.id data_type="investigation" pk=mapping.0.id  %}'>
                        <i class="fa fa-trash-alt fa-1x"></i></a>
                    </div>
                    </div>
            </tr>
            {% endif %}
            {% endfor %}
        </tbody>
    </table>

</section>
<button type="button" class="btn btn-secondary"><a href="{% url 'core:projects-details' project.id %}"> Back
    </a></button>


{% endblock %}