{#  DrCr: Web-based double-entry bookkeeping framework
    Copyright (C) 2022  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 %}Edit statement line charge{% endblock %}

{% block body %}
	<div class="container">
		<h1 class="h2 mt-4 mb-4">Statement line</h1>
		
		<table class="table">
			<thead>
				<tr>
					<th>Source account</th>
					<th>Date</th>
					<th>Description</th>
					<th>Dr</th>
					<th>Cr</th>
					<th>Balance</th>
				</tr>
			</thead>
			<tbody>
				<tr id="statement-line" data-quantity="{{ statement_line.amount().quantity_string() }}" data-commodity="{{ statement_line.commodity }}">
					<td>{{ statement_line.source_account }}</td>
					<td>{{ statement_line.dt.strftime('%Y-%m-%d') }}</td>
					<td>{{ statement_line.description }}</td>
					<td>{{ statement_line.amount().format() if statement_line.quantity >= 0 else '' }}</td>
					<td>{{ (statement_line.amount()|abs).format() if statement_line.quantity < 0 else '' }}</td>
					<td>{{ statement_line.balance or '' }}</td>
				</tr>
			</tbody>
		</table>
		
		<h1 class="h2 mt-4 mb-4">Transaction</h1>
		
		<form method="POST">
			<table class="table">
				<thead>
					<tr>
						<th>Date</th>
						<th colspan="2">Description</th>
						<th>Dr</th>
						<th>Cr</th>
					</tr>
				</thead>
				<tbody>
					{# FIXME: Customise date, etc. #}
					<tr>
						<td>{{ statement_line.dt.strftime('%Y-%m-%d') }}</td>
						<td colspan="2"><input type="text" name="description" value="{{ statement_line.description }}" class="w-100"></td>
						<td></td>
						<td></td>
					</tr>
					<tr data-side="source">
						{# Source line #}
						<td></td>
						<td></td>
						<td><i>{{ 'Dr' if statement_line.quantity >= 0 else 'Cr' }}</i> {{ statement_line.source_account }} {#<a class="text-primary" href="#" onclick="addPosting(this);return false;"><i class="bi bi-plus-circle-fill"></i></a>#}</td>
						{% if statement_line.quantity >= 0 %}<td class="has-amount">{{ statement_line.amount().format() }}</td>{% else %}<td></td>{% endif %}
						{% if statement_line.quantity < 0 %}<td class="has-amount">{{ (statement_line.amount()|abs).format() }}</td>{% else %}<td></td>{% endif %}
					</tr>
					{# Charge lines #}
					{% if transaction == None %}
						<tr data-side="charge">
							<td></td>
							<td></td>
							<td><i>{{ 'Cr' if statement_line.quantity >= 0 else 'Dr' }}</i> <input type="text" name="charge-account"> <a class="text-primary" href="#" onclick="addPosting(this);return false;"><i class="bi bi-plus-circle-fill"></i></a></td>
							{% if statement_line.quantity < 0 %}<td class="has-amount">{{ (statement_line.amount()|abs).format() }}</td>{% else %}<td></td>{% endif %}
							{% if statement_line.quantity >= 0 %}<td class="has-amount">{{ statement_line.amount().format() }}</td>{% else %}<td></td>{% endif %}
						</tr>
					{% elif transaction.postings|length == 2 %}
						<tr data-side="charge">
							<td></td>
							<td></td>
							<td><i>{{ 'Cr' if statement_line.quantity >= 0 else 'Dr' }}</i> <input type="text" name="charge-account" value="{{ transaction.charge_account(statement_line.source_account) }}"> <a class="text-primary" href="#" onclick="addPosting(this);return false;"><i class="bi bi-plus-circle-fill"></i></a></td>
							{% if statement_line.quantity < 0 %}<td class="has-amount">{{ (statement_line.amount()|abs).format() }}</td>{% else %}<td></td>{% endif %}
							{% if statement_line.quantity >= 0 %}<td class="has-amount">{{ statement_line.amount().format() }}</td>{% else %}<td></td>{% endif %}
						</tr>
					{% else %}
						{% for posting in transaction.postings if posting.account != statement_line.source_account %}
							<tr data-side="charge">
								<td></td>
								<td></td>
								<td><i>{{ 'Cr' if statement_line.quantity >= 0 else 'Dr' }}</i> <input type="text" name="charge-account" value="{{ posting.account }}"> <a class="text-primary" href="#" onclick="addPosting(this);return false;"><i class="bi bi-plus-circle-fill"></i></a></td>
								{% if statement_line.quantity < 0 %}<td class="has-amount">{{ posting.commodity }}<input type="number" name="charge-amount" step="0.01" value="{{ posting.amount().quantity_string() }}"></td>{% else %}<td></td>{% endif %}
								{% if statement_line.quantity >= 0 %}<td class="has-amount">{{ posting.commodity }}<input type="number" name="charge-amount" step="0.01" value="{{ (posting.amount()|abs).quantity_string() }}"></td>{% else %}<td></td>{% endif %}
							</tr>
						{% endfor %}
					{% endif %}
					</tr>
				</tbody>
			</table>
			
			<div class="d-flex justify-content-end">
				<button type="submit" class="btn btn-primary">Save</button>
			</div>
		</form>
	</div>
	
	<script>
		function addPosting(el) {
			let trPosting = el.parentNode.parentNode;
			let trLine = document.getElementById('statement-line');
			let qtyf = parseFloat(trLine.dataset.quantity);
			
			if (trPosting.dataset['side'] === 'source') {
				alert('NYI');
				return;
			}
			
			if (trPosting.dataset['side'] === 'charge') {
				let inputAmount = trLine.dataset.commodity + '<input type="number" name="charge-amount" step="0.01">';
				
				// Add new posting row
				let trNew = document.createElement('tr');
				trNew.dataset['side'] = 'charge';
				trNew.innerHTML = '<td></td><td></td><td><i>' + (qtyf >= 0 ? 'Cr' : 'Dr') + '</i> <input type="text" name="charge-account"> <a class="text-primary" href="#" onclick="addPosting(this);return false;"><i class="bi bi-plus-circle-fill"></i></a></td><td>' + (qtyf < 0 ? inputAmount : '') + '</td><td>' + (qtyf >= 0 ? inputAmount : '') + '</td>';
				trPosting.after(trNew);
				
				// Put input box in existing row
				trPosting.querySelector('.has-amount').innerHTML = inputAmount;
			}
		}
	</script>
{% endblock %}