<script>
import Swal from "sweetalert2";
import { authenticatedRequest } from "@/components/session/authenticatedRequest.vue";
import { ObjectsGlobal } from "@/components/session/token/SessionUser/Objects/ObjectsGlobal.vue";
import { SessionUser } from "@/components/session/token/SessionUser.vue";
import {ref} from "vue";

/**
 * Local departments cache
 * @type {ref<null>}
 */
export const economic_departments_cache = ref(null);

/**
 * Get the Economic Department name
 * @param id
 * @returns {string}
 */
export const getEconomicDepartmentName = (id) => {
  if (economic_departments_cache.value === null) {
    economic_departments_cache.value = [];
    SessionUser.objects.economic_departments.get.all().then((departments) => {
      economic_departments_cache.value = departments;
    });
  }
  const department = economic_departments_cache.value.find((category) => category.departmentNumber === parseInt(id));
  return department ? department.name : SessionUser.objects.global.language.no_data;
}

/**
 * The Economic Departments object
 */
export const EconomicDepartments = {
  meta: {
    title: "E-conomic afdelinger",
    icon: "fas fa-list",
    description: "Oversigt over e-conomic afdelinger",
    endpoint: "/economic/departments",
    labels: {
      single: "e-conomic afdeling",
      multiple: "e-conomic afdelinger"
    }
  },
  columns: {
    name: {
      label: "Navn",
      type: "string",
      sortable: true,
      creation: {
        required: true
      }
    },
    departmentNumber: {
      label: "Afdelingsnummer",
      type: "string",
      sortable: true,
      creation: {
        required: true
      }
    },
  },
  add: async (name, description) => {
    return ObjectsGlobal.add.object(
        EconomicDepartments.meta.endpoint,
        {
          name: name,
          description: description
        }
    );
  },
  set: {
    name: async (id, name) => {
      return ObjectsGlobal.set.column(
        EconomicDepartments.meta.endpoint,
        id,
        "name",
        name
      )
    },
    description: async (id, description) => {
      return ObjectsGlobal.set.column(
        EconomicDepartments.meta.endpoint,
        id,
        "description",
        description
      )
    },
  },
  get: {
    all: async () => {
      return ObjectsGlobal.get.objects(EconomicDepartments.meta.endpoint);
    },
    single: async (id) => {
      return ObjectsGlobal.get.object(EconomicDepartments.meta.endpoint, id);
    }
  },
  functions: {
    getEconomicDepartmentName: getEconomicDepartmentName,
    showDeleteObjectForm: (id, onAfterSubmit = null) => {
      return ObjectsGlobal.showDeleteObjectForm(EconomicDepartments, id, onAfterSubmit);
    },
  },
  /**
   * Show the create object form
   * @param onAfterSubmit
   * @returns {Promise<SweetAlertResult<Awaited<any>>>}
   */
  showCreateObjectForm: (onAfterSubmit = null) => {
    return ObjectsGlobal.showCreateObjectForm(EconomicDepartments, onAfterSubmit);
  },
  /**
   * Show the edit object field form
   * @param id
   * @param column
   * @param value
   * @param onAfterSubmit
   * @returns {Promise<SweetAlertResult<Awaited<any>>>}
   */
  showEditObjectFieldForm: (id, column, value, onAfterSubmit = null) => {
    return ObjectsGlobal.showEditObjectFieldForm(
        EconomicDepartments,
        id,
        column,
        value,
        onAfterSubmit
    );
  }
};
</script>