<template>
  <div class="row justify-content-center">
    <div class="col-8">
      <form name="editForm" novalidate @submit.prevent="save()">
        <h2 id="sasiedziApp.transaction.home.createOrEditLabel" data-cy="TransactionCreateUpdateHeading">Create or edit a Transaction</h2>
        <div>
          <div class="form-group" v-if="transaction.id">
            <label for="id">ID</label>
            <input type="text" class="form-control" id="id" name="id" v-model="transaction.id" readonly />
          </div>
          <div class="form-group">
            <label class="form-control-label" for="transaction-type">Type</label>
            <select
              class="form-control"
              name="type"
              :class="{ valid: !v$.type.$invalid, invalid: v$.type.$invalid }"
              v-model="v$.type.$model"
              id="transaction-type"
              data-cy="type"
            >
              <option v-for="transactionType in transactionTypeValues" :key="transactionType" :value="transactionType">
                {{ transactionType }}
              </option>
            </select>
          </div>
          <div class="form-group">
            <label class="form-control-label" for="transaction-date">Date</label>
            <b-input-group class="mb-3">
              <b-input-group-prepend>
                <b-form-datepicker
                  aria-controls="transaction-date"
                  v-model="v$.date.$model"
                  name="date"
                  class="form-control"
                  :locale="currentLanguage"
                  button-only
                  today-button
                  reset-button
                  close-button
                >
                </b-form-datepicker>
              </b-input-group-prepend>
              <b-form-input
                id="transaction-date"
                data-cy="date"
                type="text"
                class="form-control"
                name="date"
                :class="{ valid: !v$.date.$invalid, invalid: v$.date.$invalid }"
                v-model="v$.date.$model"
              />
            </b-input-group>
          </div>
          <div class="form-group">
            <label class="form-control-label" for="transaction-comment">Comment</label>
            <input
              type="text"
              class="form-control"
              name="comment"
              id="transaction-comment"
              data-cy="comment"
              :class="{ valid: !v$.comment.$invalid, invalid: v$.comment.$invalid }"
              v-model="v$.comment.$model"
            />
          </div>
          <div class="form-group">
            <label class="form-control-label" for="transaction-event">Event</label>
            <select class="form-control" id="transaction-event" data-cy="event" name="event" v-model="transaction.event">
              <option :value="null"></option>
              <option
                :value="transaction.event && eventOption.id === transaction.event.id ? transaction.event : eventOption"
                v-for="eventOption in events"
                :key="eventOption.id"
              >
                {{ eventOption.name }}
              </option>
            </select>
          </div>
          <div class="form-group">
            <label>Destination account</label>
            <select v-model="transaction.beneficiary" class="form-control" v-if="dictUserAccounts.length">
              <option v-for="account in dictUserAccounts" :key="account.id" :value="account">{{ account.name }}</option>
            </select>
            <label>Unsettled Balance:</label>
            <input type="text" class="form-control" :value="calculateUnsettledBalance()" readonly />
          </div>
          <div class="form-group">
            <button type="button" @click="addTransactionItem" class="btn btn-success">Add Transaction Item</button>
            <div class="table-responsive">
              <table class="table table-striped" aria-describedby="event.registrations">
                <thead>
                  <tr>
                    <th scope="col"><span></span></th>
                    <!--                    <th scope="col"><span>Locked</span></th>-->
                    <th scope="col"><span>Account</span></th>
                    <th scope="col"><span>Amount</span></th>
                    <th scope="col"><span>Comment</span></th>
                    <th scope="col"></th>
                  </tr>
                </thead>
                <tbody>
                  <tr v-for="(item, index) in transaction.transactionItems" :key="index" class="transaction-item">
                    <td>
                      {{ item.id }}
                    </td>
                    <!--                    <td>-->
                    <!--                      <input type="checkbox" v-model="item.locked" class="form-check-input" />-->
                    <!--                    </td>-->
                    <td>
                      <select v-model="item.userAccount" class="form-control" v-if="dictUserAccounts.length">
                        <option v-for="account in dictUserAccounts" :key="account.id" :value="account">{{ account.name }}</option>
                      </select>
                    </td>
                    <td>
                      <input type="number" v-model="item.amount" step="0.01" class="form-control" />
                    </td>
                    <td>
                      <input type="text" v-model="item.comment" class="form-control" placeholder="Comment" />
                    </td>
                    <td>
                      <button type="button" @click="removeTransactionItem(index)" class="btn btn-danger">Remove</button>
                    </td>
                  </tr>
                </tbody>
              </table>
            </div>
          </div>
        </div>
        <div>
          <button type="button" id="cancel-save" data-cy="entityCreateCancelButton" class="btn btn-secondary" @click="previousState()">
            <font-awesome-icon icon="ban"></font-awesome-icon>&nbsp;<span>Cancel</span>
          </button>
          <button
            type="submit"
            id="save-entity"
            data-cy="entityCreateSaveButton"
            :disabled="v$.$invalid || isSaving"
            class="btn btn-primary"
          >
            <font-awesome-icon icon="save"></font-awesome-icon>&nbsp;<span>Save</span>
          </button>
        </div>
      </form>
    </div>
  </div>
</template>
<script lang="ts" src="./transaction-update.component.ts"></script>
