{% extends "./template.html" %}
{% block content %}
{% load static %}
<div class="card mt-4">
    <div class="card-header">
        <h2>Bug Client List</h2>
    </div>
    <div class="card-body">
        <a href="{% url 'new_bug_client' %}">New Bug Client</a>
        {% if bug_client_results %}
            <table class="table table-striped table-hover">
                <thead>
                    <tr>
                        <td width="80%">Client Name</td>
                        <td width="20%">Type</td>
                        {% if bug_client_permission == 4 %}
                            <td>Delete</td>
                        {% endif %}
                    </tr>
                </thead>
                {% for row in bug_client_results %}
                    <tr>
                        <td><a href="{% url 'bug_client_information' row.bug_client_id %}">{{ row.bug_client_name }}</a></td>
                        <td>{{ row.list_of_bug_client }}</td>
                        {% if bug_client_permission == 4 %}
                            <td><a href="javascript:void(0)" onclick="delete_bug_client({{ row.bug_client_id }})">Delete</a></td>
                        {% endif %}
                    </tr>
                {% endfor %}
            </table>
            <script>
                function delete_bug_client(bug_client_id) {
                    //Send data to the database
                    $.ajaxSetup({
                        beforeSend: function (xhr, settings) {
                            if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
                                xhr.setRequestHeader("X-CSRFToken", csrftoken);
                            }
                        }
                    });

                    $.ajax({
                        url: '/bug_client_delete/' + bug_client_id + '/',
                        data: {},
                        type: "POST",
                        success: function(data) {
                            location.reload();
                        },
                        error: function() {
                            new_error("Could not delete the Bug Client");
                        }
                    })
                }
            </script>
        {% else %}
            <p>There are no bug clients listed. Please create a new one</p>
        {% endif %}
    </div>
</div>
{% endblock %}