{#
The Mia! Accounting Project
form.html: The account form

 Copyright (c) 2023 imacat.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

Author: imacat@mail.imacat.idv.tw (imacat)
First written: 2023/2/1
#}
{% extends "accounting/base.html" %}

{% block accounting_scripts %}
  <script src="{{ url_for("accounting.static", filename="js/account-form.js") }}"></script>
{% endblock %}

{% block content %}

<div class="mb-3 accounting-toolbar">
  <a class="btn btn-primary" role="button" href="{% block back_url %}{% endblock %}">
    <i class="fa-solid fa-circle-chevron-left"></i>
    <span class="d-none d-md-inline">{{ A_("Back") }}</span>
  </a>
</div>

<form id="accounting-form" action="{% block action_url %}{% endblock %}" method="post">
  {{ form.csrf_token }}
  {% if request.args.next %}
    <input type="hidden" name="next" value="{{ request.args.next }}">
  {% endif %}
  <div class="form-floating mb-3">
    <input id="accounting-base-code" type="hidden" name="base_code" value="{{ form.base_code.data|accounting_default }}">
    <div id="accounting-base-control" class="form-control accounting-clickable accounting-material-text-field {% if form.base_code.data %} accounting-not-empty {% endif %} {% if form.base_code.errors %} is-invalid {% endif %}" data-bs-toggle="modal" data-bs-target="#accounting-base-selector-modal">
      <label class="form-label" for="accounting-base">{{ A_("Base account") }}</label>
      <div id="accounting-base">
        {% if form.base_code.data %}
          {% if form.base_code.errors %}
            {{ A_("(Unknown)") }}
          {% else %}
            {{ form.selected_base }}
          {% endif %}
        {% endif %}
      </div>
    </div>
    <div id="accounting-base-error" class="invalid-feedback">{% if form.base_code.errors %}{{ form.base_code.errors[0] }}{% endif %}</div>
  </div>

  <div class="form-floating mb-3">
    <input id="accounting-title" class="form-control {% if form.title.errors %} is-invalid {% endif %}" type="text" name="title" value="{{ form.title.data|accounting_default }}" placeholder=" " required="required">
    <label class="form-label" for="accounting-title">{{ A_("Title") }}</label>
    <div id="accounting-title-error" class="invalid-feedback">{% if form.title.errors %}{{ form.title.errors[0] }}{% endif %}</div>
  </div>

  <div id="accounting-is-need-offset-control" class="form-check form-switch mb-3 {% if form.base_code.data[0] not in ["1", "2", "3"] %} d-none {% endif %}">
    <input id="accounting-is-need-offset" class="form-check-input" type="checkbox" name="is_need_offset" value="1" {% if form.is_need_offset.data %} checked="checked" {% endif %}>
    <label class="form-check-label" for="accounting-is-need-offset">
      {{ A_("The line items in the account need offset.") }}
    </label>
  </div>

  <div class="d-none d-md-block">
    <button class="btn btn-primary" type="submit">
      <i class="fa-solid fa-floppy-disk"></i>
      {{ A_("Save") }}
    </button>
  </div>

  <div class="d-md-none accounting-material-fab">
    <button class="btn btn-primary" type="submit">
      <i class="fa-solid fa-floppy-disk"></i>
    </button>
  </div>
</form>

<div class="modal fade" id="accounting-base-selector-modal" tabindex="-1" aria-labelledby="accounting-base-selector-modal-label" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h1 class="modal-title fs-5" id="accounting-base-selector-modal-label">{{ A_("Select Base Account") }}</h1>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="{{ A_("Close") }}"></button>
      </div>
      <div class="modal-body">
        <div class="input-group mb-2">
          <input id="accounting-base-selector-query" class="form-control form-control-sm" type="search" placeholder=" " required="required">
          <label class="input-group-text" for="accounting-base-selector-query">
            <i class="fa-solid fa-magnifying-glass"></i>
            {{ A_("Search") }}
          </label>
        </div>

        <ul id="accounting-base-selector-option-list" class="list-group accounting-selector-list">
          {% for base in form.base_options %}
            <li class="list-group-item accounting-clickable accounting-base-selector-option" data-code="{{ base.code }}" data-text="{{ base }}" data-query-values="{{ base.query_values|tojson|forceescape }}" data-bs-dismiss="modal">
              {{ base }}
            </li>
          {% endfor %}
        </ul>
        <p id="accounting-base-selector-option-no-result" class="d-none">{{ A_("There is no data.") }}</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{{ A_("Cancel") }}</button>
        {% if form.base_code.data %}
          <button id="accounting-base-selector-clear" type="button" class="btn btn-danger" data-bs-dismiss="modal">{{ A_("Clear") }}</button>
        {% else %}
          <button id="accounting-base-selector-clear" type="button" class="btn btn-secondary" disabled="disabled" data-bs-dismiss="modal">{{ A_("Clear") }}</button>
        {% endif %}
      </div>
    </div>
  </div>
</div>

{% endblock %}
