<template>
  <q-dialog ref="dialogRef" @hide="onDialogHide">
    <q-card class="q-dialog-plugin overflow-hidden treasury-dialog-card">
      <DefaultDialogHeader :title="title" @close="onDialogCancel" />

      <q-form ref="formRef" @submit="onOKClick">
        <q-card-section class="row q-col-gutter-md q-px-lg q-pt-md q-pb-sm">
          <DefaultInput
            v-model="form.name"
            label="Nome (ex.: Banco do Brasil, Caixa Loja)"
            class="col-12"
            outlined
            :rules="[(v) => !!v || 'Informe o nome']"
          />

          <DefaultInput
            v-model="form.bank_name"
            label="Banco"
            class="col-12 col-sm-6"
            outlined
          />
          <DefaultInput
            v-model="form.bank_agency"
            label="Agência"
            class="col-12 col-sm-6"
            outlined
          />
          <DefaultInput
            v-model="form.bank_account"
            label="Conta"
            class="col-12 col-sm-6"
            outlined
          />
          <DefaultSelect
            v-model="form.bank_type_account"
            label="Tipo"
            :options="accountTypeOptions"
            class="col-12 col-sm-6"
            outlined
            emit-value
            map-options
          />
        </q-card-section>

        <q-card-actions align="right" class="q-px-lg q-pt-md q-pb-lg q-gutter-sm">
          <q-btn flat label="Cancelar" color="grey-7" @click="onDialogCancel" />
          <q-btn color="primary" label="Salvar" type="submit" :loading="loading" />
        </q-card-actions>
      </q-form>
    </q-card>
  </q-dialog>
</template>

<script setup>
import { computed, ref } from "vue";
import { useDialogPluginComponent } from "quasar";
import {
  createTreasuryAccountMe,
  updateTreasuryAccountMe,
} from "src/api/treasury";

import DefaultDialogHeader from "src/components/defaults/DefaultDialogHeader.vue";
import DefaultInput from "src/components/defaults/DefaultInput.vue";
import DefaultSelect from "src/components/defaults/DefaultSelect.vue";

defineEmits([...useDialogPluginComponent.emits]);

const props = defineProps({
  account: {
    type: Object,
    default: null,
  },
});

const { dialogRef, onDialogHide, onDialogOK, onDialogCancel } =
  useDialogPluginComponent();

const formRef = ref(null);
const loading = ref(false);
const isEdit = computed(() => !!props.account?.id);
const title = computed(() => (isEdit.value ? "Editar conta" : "Nova conta"));

const accountTypeOptions = [
  { label: "Corrente", value: "corrente" },
  { label: "Poupança", value: "poupanca" },
];

const form = ref({
  name: props.account?.name ?? "",
  treasury_type: "bank",
  bank_name: props.account?.bank_name ?? "",
  bank_agency: props.account?.bank_agency ?? "",
  bank_account: props.account?.bank_account ?? "",
  bank_type_account: props.account?.bank_type_account ?? null,
});

async function onOKClick() {
  const valid = await formRef.value?.validate();
  if (!valid) return;

  loading.value = true;

  try {
    if (isEdit.value) {
      await updateTreasuryAccountMe(props.account.id, form.value);
    } else {
      await createTreasuryAccountMe(form.value);
    }

    onDialogOK(true);
  } catch (e) {
    console.error(e);
  } finally {
    loading.value = false;
  }
}
</script>

<style scoped>
.treasury-dialog-card {
  width: 520px;
  max-width: calc(100vw - 32px);
}
</style>
