<template>
  <div>
    <DefaultHeaderPage />
    <div>
      <DefaultTable
        ref="tableRef"
        :columns="columns"
        :api-call="getProviders"
        :delete-function="deleteProvider"
        :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"
      >
        <template #body-cell-approval_status="slotProps">
          <q-td :props="slotProps">
            <q-badge
              :color="{ pending: 'warning', accepted: 'positive', rejected: 'negative' }[slotProps.row.approval_status] ?? 'grey'"
              :label="$t(`provider.approval_status.${slotProps.row.approval_status}`)"
            />
          </q-td>
        </template>
      </DefaultTable>
    </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 { getProviders, deleteProvider } from "src/api/provider";

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

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

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

const columns = [
  {
    name: "document",
    label: t("provider.fields.document"),
    field: "document",
    align: "left",
    sortable: true,
  },
  {
    name: "user",
    label: t("common.terms.user"),
    field: (row) => row.user?.name || "-",
    align: "left",
    sortable: true,
  },
  {
    name: "birth_date",
    label: t("provider.fields.birth_date"),
    field: "birth_date",
    align: "left",
    sortable: true,
  },
  {
    name: "approval_status",
    label: t("provider.fields.approval_status"),
    field: "approval_status",
    align: "left",
    sortable: true,
  },
  {
    name: "average_rating",
    label: t("provider.fields.average_rating"),
    field: (row) => row.average_rating || "-",
    align: "left",
    sortable: true,
  },
  {
    name: "total_services",
    label: t("provider.fields.total_services"),
    field: "total_services",
    align: "left",
    sortable: true,
  },
  {
    name: "daily_price_8h",
    label: t("provider.fields.daily_price_8h"),
    field: (row) => row.daily_price_8h ? `R$ ${row.daily_price_8h}` : "-",
    align: "left",
    sortable: true,
  },
  {
    name: "actions",
    required: true,
  },
];

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

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