{% extends "django_eveonline_connector/adminlte/characters/view_character.html" %}

{% block header %}
Contracts
<button id="generate-report-button" onclick="createReport()" class="btn btn-warning pull-right">Refresh Audit</button>
{% endblock %}

{% block description %}
{{character.name}}
<p id="{{character.external_id}}" class="external-id"></p>

{% endblock %}

{% block tab_content %}
{% if 'django_eveonline_doctrine_manager' in INSTALLED_APPS %}
<!-- Doctrine Audit -->
<div class="box box-warning box-solid">
    <div class="box-header">
        <h3 class="box-title">
            Doctrine Audit
        </h3>
    </div>
    <div class="box-body">
        <table id="doctrine-audit-table" class="datatable table table-bordered table-hover text-center">
            <thead>
                <th class="col-lg-2 col-md-2 col-xs-1">Doctrine</th>
                <th class="col-lg-2 col-md-2 col-xs-1">Status</th>
            </thead>
            <tbody>
                <tr>
                    <td>
                        <i class="fa fa-1x fa-spinner fa-spin" aria-hidden="true"></i>
                    </td>
                    <td>
                        <i class="fa fa-1x fa-spinner fa-spin" aria-hidden="true"></i>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</div>
<!-- End Doctrine Audit -->
<!-- Fitting Audit -->
<div class="box box-warning box-solid">
    <div class="box-header">
        <h3 class="box-title">
            Fitting Audit
        </h3>
    </div>
    <div class="box-body">
        <table id="fitting-audit-table" class="datatable table table-bordered table-hover text-center">
            <thead>
                <th class="col-lg-2 col-md-2 col-xs-1">Fitting</th>
                <th class="col-lg-2 col-md-2 col-xs-1">Status</th>
            </thead>
            <tbody>
                <tr>
                    <td>
                        <i class="fa fa-1x fa-spinner fa-spin" aria-hidden="true"></i>
                    </td>
                    <td>
                        <i class="fa fa-1x fa-spinner fa-spin" aria-hidden="true"></i>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</div>
<!-- End Fitting Audit -->

{% endif %}
{% endblock %}

{% block scripts %}
<!-- Datatable Settings -->
<script>
    $(function () {
        $('table').DataTable({
            'paging': false,
            'searching': false,
            'ordering': true,
            'order': [[1, 'asc']],
            "autoWidth": false,
        })
    })
</script>

<!-- Update Menu Tab -->
<script>
    $(document).ready(function () {
        $('#audit').addClass('active');
    });
</script>

<!-- Fitting Audit Scripts -->
<script>
async function clearTableRows() {
    const fittingTable = document.getElementById('fitting-audit-table').getElementsByTagName('tbody')[0];
    while (fittingTable.hasChildNodes()) {
        fittingTable.removeChild(fittingTable.firstChild);
    }
    const doctrineTable = document.getElementById('doctrine-audit-table').getElementsByTagName('tbody')[0];
    while (doctrineTable.hasChildNodes()) {
        doctrineTable.removeChild(doctrineTable.firstChild);
    }
}

async function updateTable() {
    var externalId = document.querySelectorAll('.external-id')[0].id
    const fittingTable = document.getElementById('fitting-audit-table').getElementsByTagName('tbody')[0];
    let res = await fetch('/eveonline/api/fittings?external_id=' + externalId)
    if (res.status !== 200) {
        try {
            fittingTable.deleteRow(0);
        } catch (e) {
            // already cleared
        }
        var row = fittingTable.insertRow(0);
        var cell1 = row.insertCell(0);
        var cell2 = row.insertCell(1);
        cell1.innerHTML = '<i class="fa fa-warning text-warning"></i>'
        cell2.innerHTML = '<i class="fa fa-warning text-warning"></i>'
    }
    try {
        let resJSON = await res.json()
        try {
            fittingTable.deleteRow(0);
        } catch (e) {
            // already cleared
        }
        for (var i = 0; i < resJSON['fittings'].length; i++) {
            fitting = resJSON['fittings'][i]
            var row = fittingTable.insertRow(0);
            var cell1 = row.insertCell(0);
            var cell2 = row.insertCell(1);
            cell1.innerHTML = '<img class="img-circle pull-left" src="https://images.evetech.net/types/' + fitting['type_id'] + '/icon?size=32"title="' + fitting["name"] + '" width="32px">' + fitting['name']
            if (fitting['missing_skills'].length > 0) {
                cell2.innerHTML = '<i class="fa fa-times text-danger"></i>'
            }
            else {
                cell2.innerHTML = '<i class="fa fa-check text-success"></i>';
            }

        }
    } catch (e) {
        console.log("Failed to pull fittings: " + e);
    }


    const doctrineTable = document.getElementById('doctrine-audit-table').getElementsByTagName('tbody')[0];
    res = await fetch('/eveonline/api/doctrines?external_id=' + externalId)
    if (res.status !== 200) {
        try {
            doctrineTable.deleteRow(0);
        } catch(e) {
            // already cleared
        }
        
        var row = doctrineTable.insertRow(0);
        var cell1 = row.insertCell(0);
        var cell2 = row.insertCell(1);
        cell1.innerHTML = '<i class="fa fa-warning text-warning"></i>'
        cell2.innerHTML = '<i class="fa fa-warning text-warning"></i>'
    }
    else {
        resJSON = await res.json()
        try {
            doctrineTable.deleteRow(0);
        } catch (e) {
            // already cleared
        }
        for (var i = 0; i < resJSON['doctrines'].length; i++) {
            doctrine = resJSON['doctrines'][i]
            var row = doctrineTable.insertRow(0);
            var cell1 = row.insertCell(0);
            var cell2 = row.insertCell(1);
            cell1.innerHTML = doctrine['name']
            if (doctrine['has_skills']) {
                cell2.innerHTML = '<i class="fa fa-check text-success"></i>';
            }
            else {
                cell2.innerHTML = '<i class="fa fa-times text-danger"></i>'
            }
        }
    }
}
async function createReport() {
    var externalId = document.querySelectorAll('.external-id')[0].id
    const button = document.getElementById("generate-report-button")
    button.classList = "btn btn-warning pull-right"
    button.innerHTML = '<i class="fa fa-1x fa-spinner fa-spin" aria-hidden="true"></i>'
    const res = await fetch('/eveonline/api/report/generate?external_id=' + externalId)

    if (res.status == 204) {
        button.classList = "btn btn-success pull-right"
        button.innerText = "Refresh Success"
        clearTableRows()
        updateTable()
    }
    else {
        button.classList = "btn btn-danger pull-right"
        button.innerText = "Refresh Failed"
    }
   
}
document.addEventListener("DOMContentLoaded", async function () {
    updateTable()
})
</script>
<!-- End Fitting Audit Scripts -->

{% endblock %}