{% extends 'miningtaxes/base.html' %}
{% load i18n %}
{% load static %}
{% load humanize %}
{% load evelinks %}

{% block details %}
    <div class="card card-default">
        <div class="card-header" style="display:flex;">
		<h5 class="card-title">{% translate 'MiningTaxes Settings' %} (v{{ version }})</h5>
        </div>
        <div class="card-body">
	    <form class="form" action="{{ url }}" method="POST">
		    {% csrf_token %}

  <div class="form-group">
    <label class="control-label" for="{{ form.phrase.id_for_label }}">
      {{ form.phrase.label }}
    </label>

    <div>
      {{ form.phrase }}
      {% if form.phrase.help_text %}
        <small class="form-text text-muted">{{ form.phrase.help_text }}</small>
      {% endif %}
      {% for error in form.phrase.errors %}
        <div class="text-danger">{{ error }}</div>
      {% endfor %}
    </div>
  </div>

  <div class="form-group">
    <label class="control-label" for="{{ form.interest_rate.id_for_label }}">
      {{ form.interest_rate.label }}
    </label>

    <div>
      {{ form.interest_rate }}
      {% if form.interest_rate.help_text %}
        <small class="form-text text-muted">{{ form.interest_rate.help_text }}</small>
      {% endif %}
      {% for error in form.interest_rate.errors %}
        <div class="text-danger">{{ error }}</div>
      {% endfor %}
    </div>
  </div><br/>
		    <button type="submit" class="btn btn-primary">Update Settings</button>
	    </form>
	</div>
    </div>

    <div class="card card-default">
        <div class="card-header" style="display:flex;">
		<h5 class="card-title">Tax Rates (Nothing is saved until you hit 'Save Tax Rates'!)</h5>
        </div>
        <div class="card-body">
	    <form action="{% url 'miningtaxes:admin_launcher_save_rates' %}" method="POST">
		    {% csrf_token %}
		    <input type="hidden" name='tax_data' id="tax_data"/>
		    <button type="submit" onClick="saveRates()" class="btn btn-primary">Save Tax Rates</button>
	    </form>
	    <br/>
	    <br/>
	    Bulk set: <select id='bulk_tax'></select> : <input type='text' size=10 id='bulk_rate' value=10> <button onClick="bulkSet()" class='btn btn-primary'>Set</button>
	    <br/>
	    <br/>

	    <div class="table-responsive">
		<table class="table table-striped " id="tax_rate">
		    <thead>
			<tr>
			    <th>{% translate 'Ore Group' %}</th>
			    <th>{% translate 'Ore' %}</th>
			    <th>{% translate 'Tax Rate (%)' %}</th>
			</tr>
		    </thead>
		    <tbody></tbody>
		</table>
	    </div>
	</div>
    </div>


    <div class="card card-default">
        <div class="card-header" style="display:flex;">
            <h5 class="card-title">{% translate 'Accountant Characters (add only one accountant per corp)' %}</h5>
        </div>
        <div class="card-body">
            <!-- Characters -->
            <div class="character-cards">
                <ul class="cards_container">
                    <!-- Add New Character -->
                    <li class="cards_item">
                        <div class="card">
                            <div class="card-header">{% translate 'Register Character' %}</div>

                            <div class="card-body card__content">
                                <a href="{% url 'miningtaxes:add_admin_character' %}">
                                    <img src="{{ 1|character_portrait_url:256 }}" alt="Add character">
                                </a>
                            </div>

                            <div class="card-footer">
                                <a class="btn btn-primary btn-sm" href="{% url 'miningtaxes:add_admin_character' %}">{% translate 'Register' %}</a>
                            </div>
                        </div>
                    </li>

                    {% if has_registered_characters %}
                        <!-- Registered Characters -->
                        {% for auth_character in auth_characters|dictsort:"character_name" %}
                            <li class="cards_item">
                                <div class="card">
                                    <div class="card-header">
                                        {{ auth_character.character_name }}
                                    </div>
                                    <div class="card-body card__content">
                                        <p>
					<a href="">
                                                <span class="aa-miningtaxes-launcher-character-image">
                                                    <img src="{{ auth_character.character_id|character_portrait_url:256 }}" alt="{{ auth_character.character_name }}">

                                                    {% if auth_character.alliance_id %}
                                                        <span class="aa-miningtaxes-alliance-logo">
                                                            <img class="" src="{{ auth_character.alliance_id|alliance_logo_url:64 }}" title="{{ auth_character.alliance_name }}">
                                                        </span>
                                                    {% endif %}

                                                    <span class="aa-miningtaxes-corporation-logo">
                                                        <img class="" src="{{ auth_character.corporation_id|corporation_logo_url:64 }}" title="{{ auth_character.corporation_name }}">
                                                    </span>
                                                </span>
                                            </a>
                                        </p>

                                        <div class="text-center">
                                        </div>
                                    </div>

                                    <div class="card-footer">
                                        <a class="btn btn-danger btn-sm" href="{% url 'miningtaxes:remove_admin_character' auth_character.character.pk %}"
                                            role="button" title="{% translate 'Remove this character.' %}"
                                            onclick="return confirm('Are you sure you want to REMOVE this character?')">
                                            <i class="far fa-trash-alt"></i>
                                        </a>
                                    </div>
                                </div>
                            </li>
                        {% endfor %}
                    {% endif %}
                </ul>
            </div>
        </div>
    </div>

    <div class="card card-default">
        <div class="card-header" style="display:flex;">
            <h5 class="card-title">{% translate 'Registered Characters in plugin' %}</h5>
        </div>
        <div class="card-body">
            <!-- Characters -->
            <div class="character-cards">
                        <!-- Registered Characters -->
                        {% for auth_character in auth_registered|dictsort:"character_name" %}
                                        <a class="btn btn-danger btn-sm" href="{% url 'miningtaxes:remove_admin_registered' auth_character.character.pk %}"
                                            role="button" title="{% translate 'Remove this character.' %}"
							  onclick="return confirm('Are you sure you want to REMOVE {{auth_character.character_name}}? All mining history and tax history will be erased!')">
						<i class="far fa-trash-alt"></i> {{auth_character.character_name}}
                                        </a>
                        {% endfor %}
            </div>
        </div>
    </div>

    <div class="card card-default">
        <div class="card-header" style="display:flex;">
            <h5 class="card-title">{% translate 'Admin actions' %}</h5>
        </div>
        <div class="card-body">
		<ul>
			<li>
				<a class="btn btn-danger btn-sm" href="{% url 'miningtaxes:purge_old_corphistory' %}"
				    role="button" title="{% translate 'Purge corp mining history older than 90 days.' %}"
				    onclick="return confirm('Are you sure you want to PURGE corp mining history older than 90 days?')">
				    <i class="far fa-trash-alt"></i> Purge corp history older than 90 days
				</a>
			</li>
		</ul>
        </div>
    </div>

{% endblock details %}

{% block extra_css %}
    <link rel="stylesheet" href="{% static 'miningtaxes/css/global.css' %}" type="text/css">
    <link rel="stylesheet" href="{% static 'miningtaxes/css/miningtaxes.css' %}" type="text/css">
    <link rel="stylesheet" href="{% static 'miningtaxes/css/launcher.css' %}" type="text/css">
{% endblock %}

{% block extra_script %}
var tax_data;
var tax_table;
var groups = {};
var lookup = {};

function saveRates() {
	$("#tax_data").val(JSON.stringify(tax_data));
}

function render() {
	tax_table.clear();
	tax_data.forEach(function(d) { tax_table.row.add(d); });
	tax_table.draw();
}

function bulkSet() {
	saveRates();
	const g = $("#bulk_tax").val();
	const r = parseFloat($("#bulk_rate").val());
	tax_data.forEach(function(d) {
		if (d.group == g) { d.tax_rate = r; }
	})
	render();
}

function saveTaxRate(tid) {
	const d = tax_data[lookup[tid]];
	d.tax_rate = parseFloat($("#row-" + tid).val());
}

$(document).ready(function () {
        tax_table = $('#tax_rate').DataTable({
            columns: [
                { data: 'group' },
                { data: 'name' },
		{ data: 'tax_rate', render: function(d,j, i,k) {
		  return '<input type="text" id="row-' + i.tid + '" value=' + d + ' onKeyUp="saveTaxRate(' + i.tid + ')" />';
		} },
            ],
            order: [[0, "asc"]],
	    pageLength: 10
        });
	$.getJSON("{% url 'miningtaxes:admin_launcher_tax_table' %}", function(d) {
		tax_data = d["data"];
		for (var i = 0; i < tax_data.length; i++) {
			groups[tax_data[i].group] = 1;
			lookup[tax_data[i].tid] = i;
		}
		render();
		var html = "";
		Object.keys(groups).forEach(function(d) {
		html += "<option value='" + d + "'>" + d + "</option>";
		});
		$("#bulk_tax").html(html);
	});
});
{% endblock %}
