{#  DrCr: Web-based double-entry bookkeeping framework
    Copyright (C) 2022–2024  Lee Yingtong Li (RunasSudo)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
#}

{% extends 'base.html' %}
{% block title %}Chart of accounts{% endblock %}

{% block content %}
	<h1 class="page-heading">
		Chart of accounts
	</h1>
	
	<form method="POST">
		<div class="my-2 py-2 flex gap-x-2 items-baseline bg-white sticky top-0">
			<div class="relative dropdownbox w-[450px]">  {# FIXME: Width hardcoded #}
				<button type="button" class="relative w-full cursor-default bg-white bordered-field pl-3 pr-10 text-left">
					<span class="dropdownbox-text block truncate">Asset</span>  {# FIXME: Hardcoded default #}
					<span class="pointer-events-none absolute inset-y-0 right-0 flex items-center pr-2">
						<svg class="h-5 w-5 text-gray-400" viewBox="0 0 20 20" fill="currentColor">
						<path fill-rule="evenodd" d="M10 3a.75.75 0 01.55.24l3.25 3.5a.75.75 0 11-1.1 1.02L10 4.852 7.3 7.76a.75.75 0 01-1.1-1.02l3.25-3.5A.75.75 0 0110 3zm-3.76 9.2a.75.75 0 011.06.04l2.7 2.908 2.7-2.908a.75.75 0 111.1 1.02l-3.25 3.5a.75.75 0 01-1.1 0l-3.25-3.5a.75.75 0 01.04-1.06z" clip-rule="evenodd" />
						</svg>
					</span>
				</button>
				<input type="hidden" name="kind" value="drcr.asset">  {# FIXME: Hardcoded default #}
				<ul class="hidden absolute z-20 mt-1 max-h-60 w-full overflow-auto bg-white py-1 text-sm shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none">
					{% for plugin_name, plugin_account_kinds in account_kinds_by_plugin.items() %}
						<li class="relative cursor-default select-none py-1 pl-3 pr-9 text-gray-500 border-b border-gray-300{% if loop.index0 > 0 %} pt-4{% endif %}">
							<span class="block truncate text-xs font-bold uppercase">{{ plugin_name }}</span>
						</li>
						{% for account_kind in plugin_account_kinds %}
							<li class="group relative cursor-default select-none py-1 pl-3 pr-9 text-gray-900 hover:text-white hover:bg-emerald-600" data-value="{{ account_kind[0] }}"{% if account_kind[0] == 'drcr.asset' %} data-selected="selected"{% endif %}>
								<span class="combobox-text block truncate group-data-[selected=selected]:font-semibold">{{ account_kind[1] }}</span>
								<span class="hidden group-data-[selected=selected]:flex absolute inset-y-0 right-0 items-center pr-4 text-emerald-600 group-hover:text-white">
									<svg class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
										<path fill-rule="evenodd" d="M16.704 4.153a.75.75 0 01.143 1.052l-8 10.5a.75.75 0 01-1.127.075l-4.5-4.5a.75.75 0 011.06-1.06l3.894 3.893 7.48-9.817a.75.75 0 011.05-.143z" clip-rule="evenodd" />
									</svg>
								</span>
							</li>
						{% endfor %}
					{% endfor %}
				</ul>
			</div>
			<div>
				<button formaction="{{ url_for('account_add_kind') }}" class="btn-primary" type="submit">Add type</button>
			</div>
		</div>
		
		<table class="min-w-full">
			<thead>
				<tr>
					<th></th>
					<th class="py-0.5 px-1 text-gray-900 font-semibold text-start">Account</th>
					<th class="py-0.5 pl-1 text-gray-900 font-semibold text-start">Associated types</th>
				</tr>
			</thead>
			<tbody>
				{% for account in accounts %}
					<tr class="border-t border-gray-300">
						<td class="py-0.5 pr-1 text-gray-900 align-baseline"><input class="checkbox-primary" type="checkbox" name="sel-account" value="{{ account }}"></td>
						<td class="py-0.5 px-1 text-gray-900 align-baseline">{{ account }}</td>
						<td class="py-0.5 pl-1 text-gray-900 align-baseline">
							{% if account in account_configurations %}
								<ul class="list-disc ml-5">
									{% for account_configuration in account_configurations[account] %}
										<li>{{ account_kinds_map[account_configuration.kind] }}</li>
									{% endfor %}
								</ul>
							{% endif %}
						</td>
					</tr>
				{% endfor %}
			</tbody>
		</table>
	</form>
{% endblock %}

{% block scripts %}
	<script>
		document.querySelectorAll('.dropdownbox').forEach((elDropdownbox) => {
			const elButton = elDropdownbox.querySelector('button');
			const elInput = elDropdownbox.querySelector('input');
			const elUl = elDropdownbox.querySelector('ul');
			
			// Open and close dropdown box
			elButton.addEventListener('click', (evt) => {
				if (elUl.classList.contains('hidden')) {
					elUl.classList.remove('hidden');
					elUl.classList.add('block');
				} else {
					elUl.classList.remove('block');
					elUl.classList.add('hidden');
				}
			});
			
			// Update value on select
			elUl.querySelectorAll('li').forEach((elLi) => {
				if (!elLi.dataset.value) {
					return;
				}
				
				elLi.addEventListener('click', (evt) => {
					const liText = elLi.querySelector('.combobox-text').innerText;
					const liValue = elLi.dataset.value;
					
					elButton.querySelector('.dropdownbox-text').innerText = liText;
					elInput.value = liValue;
					
					elUl.querySelectorAll('li').forEach((elLi2) => {
						elLi2.dataset.selected = '';
					});
					elLi.dataset.selected = 'selected';
					
					elUl.classList.remove('block');
					elUl.classList.add('hidden');
				});
			});
		});
	</script>
{% endblock %}