<!-- Component d'un formulaire d'un banque account d'organization -->
<template>
  <main>
    <LayoutContainer>
      <v-card class="mb-5">
        <FormToolbar title="bank_account" icon="fa-euro-sign"/>

        <UiForm
          :id="id"
          :model="model"
          :query="query()"
          :submitActions="submitActions">
          <template #form.input="{entry, updateRepository}">
            <v-container fluid class="container">
              <v-row>

                <v-col cols="12" sm="6">
                  <UiInputText field="bankName" :data="entry['bankName']" @update="updateRepository" />
                </v-col>

                <v-col cols="12" sm="6">
                  <UiInputText field="bic" :data="entry['bic']" @update="updateRepository" />
                </v-col>

                <v-col cols="12" sm="6">
                  <UiInputText field="iban" :data="entry['iban']" @update="updateRepository" :mask="['AA ## ##### ##### XXXXXXXXXXX ##']" />
                </v-col>

                <v-col cols="12" sm="6">
                  <UiInputText field="debitAddress" :data="entry['debitAddress']" @update="updateRepository" />
                </v-col>

                <v-col cols="12" sm="6">
                  <UiInputText field="holder" :data="entry['holder']" @update="updateRepository" />
                </v-col>

                <v-col cols="12" sm="6">
                  <UiInputCheckbox field="principal" :data="entry['principal']" @update="updateRepository" />
                </v-col>

              </v-row>
            </v-container>

          </template>

          <template #form.button>
            <NuxtLink :to="{ path: '/organization', query: { accordion: 'bank_account' }}" class="no-decoration">
              <v-btn class="mr-4 ot_light_grey ot_grey--text">
                {{ $t('back') }}
              </v-btn>
            </NuxtLink>
          </template>

        </UiForm>
      </v-card>
    </LayoutContainer>
  </main>
</template>

<script lang="ts">
import { defineComponent, computed} from '@nuxtjs/composition-api'
import { Repository as VuexRepository } from '@vuex-orm/core/dist/src/repository/Repository'
import { Query, Model } from '@vuex-orm/core'
import {SUBMIT_TYPE} from '~/types/enums'
import { repositoryHelper } from '~/services/store/repository'
import {AnyJson} from "~/types/interfaces";
import {BankAccount} from "~/models/Core/BankAccount";

export default defineComponent({
  props: {
    id:{
      type: [Number, String],
      required: true
    }
  },
  setup () {
    const repository: VuexRepository<Model> = repositoryHelper.getRepository(BankAccount)
    const query: Query = repository.query()

    const submitActions = computed(() => {
      let actions:AnyJson = {}
      actions[SUBMIT_TYPE.SAVE_AND_BACK] = { path: `/organization`, query: { accordion: 'bank_account' } }
      actions[SUBMIT_TYPE.SAVE] = { path: `/organization/bank_account/` }
      return actions
    })

    return {
      model: BankAccount,
      query: () => query,
      submitActions
    }
  },
  beforeDestroy() {
    repositoryHelper.cleanRepository(BankAccount)
  }
})

</script>
