<template>
  <div>
    <DefaultHeaderPage />
    <div>
      <DefaultTable
        ref="tableRef"
        :columns="columns"
        :api-call="getCities"
        :delete-function="deleteCity"
        :mostrar-selecao-de-colunas="false"
        :mostrar-botao-fullscreen="false"
        :mostrar-toggle-inativos="false"
        open-item
        add-item
        @on-row-click="onRowClick"
        @on-add-item="onAddItem"
      />
    </div>
  </div>
</template>
<script setup>
import { defineAsyncComponent, useTemplateRef } from "vue";
import { useQuasar } from "quasar";
import { useI18n } from "vue-i18n";
import { permissionStore } from "src/stores/permission";
import { getCities, deleteCity } from "src/api/city";

import DefaultTable from "src/components/defaults/DefaultTable.vue";
import DefaultHeaderPage from "src/components/layout/DefaultHeaderPage.vue";

const AddEditCityDialog = defineAsyncComponent(
  () => import("src/pages/city/components/AddEditCityDialog.vue"),
);

const permission_store = permissionStore();
const $q = useQuasar();
const tableRef = useTemplateRef("tableRef");
const { t } = useI18n();

const columns = [
  {
    name: "id",
    label: "ID",
    field: "id",
    align: "left",
    required: true,
    sortable: true,
  },
  {
    name: "nome",
    label: t("common.terms.name"),
    field: "name",
    align: "left",
    sortable: true,
  },
  {
    name: "state",
    label: t("ui.navigation.state"),
    field: (row) => row.state.name,
    align: "left",
    sortable: true,
  },
  {
    name: "country",
    label: t("ui.navigation.country"),
    field: (row) => row.country.name,
    align: "left",
    sortable: true,
  },
  {
    name: "status",
    label: t("common.terms.status"),
    field: (row) =>
      row.status == "ACTIVE" ? t("common.status.active") : t("common.status.inactive"),
    align: "left",
    sortable: true,
  },
  {
    name: "actions",
    required: true,
  },
];

const onRowClick = ({ row }) => {
  if (permission_store.getAccess("config.city", "edit") === false) {
    $q.loading.hide();
    $q.notify({
      type: "negative",
      message: t("validation.permissions.edit"),
    });
    return;
  }
  $q.dialog({
    component: AddEditCityDialog,
    componentProps: {
      city: row,
      title: () =>
        useI18n().t("common.actions.edit") +
        " " +
        useI18n().t("ui.navigation.city"),
    },
  }).onOk(async (success) => {
    if (success) {
      tableRef.value.refresh();
    }
  });
};

const onAddItem = () => {
  if (permission_store.getAccess("config.city", "add") === false) {
    $q.loading.hide();
    $q.notify({
      type: "negative",
      message: t("validation.permissions.add"),
    });
    return;
  }
  $q.dialog({
    component: AddEditCityDialog,
    componentProps: {
      title: () =>
        useI18n().t("common.actions.add") + " " + useI18n().t("ui.navigation.city"),
    },
  }).onOk(async (success) => {
    if (success) {
      tableRef.value.refresh();
    }
  });
};
</script>
