<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 departments_cache = ref(null);

/**
 * Get the department name
 * @param id {number}
 * @returns {string}
 */
export const getDepartmentName = async (id) => {
  if (departments_cache.value === null) {
    departments_cache.value = [];
    await SessionUser.objects.departments.get.all().then((departments) => {
      departments_cache.value = departments;
    });
  }
  const department = departments_cache.value.find((department) => department.id === parseInt(id));
  return department ? department.name : SessionUser.objects.global.language.no_data;
};



    /**
     * The Departments object
     */
    export const Departments = {
      meta: {
        title: "Afdelinger",
        icon: "fas fa-list",
        description: "Oversigt over afdelinger",
        endpoint: "/departments",
        labels: {
          single: "afdeling",
          multiple: "afdelinger"
        }
      },
      columns: {
        id: {
          label: "ID",
          type: "number",
          sortable: true,
          creation: {
            required: false
          }
        },
        name: {
          label: "Navn",
          type: "string",
          sortable: true,
          creation: {
            required: true
          }
        },
        description: {
          label: "Beskrivelse",
          type: "string",
          sortable: true,
          creation: {
            required: false
          }
        },
        economic_department_id: {
          label: "E-conomic afdeling",
          type: "select",
          sortable: true,
          creation: {
            required: true
          },
          options: async () => {
            return SessionUser.objects.economic_departments.get.all();
          }
        },
        slack_webhook: {
          label: "Slack webhook",
          type: "string",
          sortable: true,
          creation: {
            required: false
          }
        },
        dimension: {
          label: "Dimension",
          type: "number",
          sortable: true,
          creation: {
            required: false
          }
        },
      },
      add: async (name, economic_department_id) => {
        return ObjectsGlobal.add.object(
            Departments.meta.endpoint,
            {
              name: name,
              economic_department_id: economic_department_id
            }
        );
      },
      set: {
        name: async (id, name) => {
          return ObjectsGlobal.set.column(
              Departments.meta.endpoint,
              id,
              "name",
              name
          )
        },
        description: async (id, description) => {
          return ObjectsGlobal.set.column(
              Departments.meta.endpoint,
              id,
              "description",
              description
          )
        },
        economic_department_id: async (id, economic_department_id) => {
          return ObjectsGlobal.set.column(
              Departments.meta.endpoint,
              id,
              "economic_department_id",
              parseInt(economic_department_id),
          )
        },
        slack_webhook: async (id, slack_webhook) => {
          return ObjectsGlobal.set.column(
              Departments.meta.endpoint,
              id,
              "slack_webhook",
              slack_webhook
          )
        },
        dimension: async (id, dimension) => {
          return ObjectsGlobal.set.column(
              Departments.meta.endpoint,
              id,
              "dimension",
              parseInt(dimension)
          )
        }
      },
      get: {
        all: async () => {
          return ObjectsGlobal.get.objects(Departments.meta.endpoint);
        },
        single: async (id) => {
          return ObjectsGlobal.get.object(Departments.meta.endpoint, id);
        }
      },
      functions: {},
      /**
       * Show the create object form
       * @param onAfterSubmit
       * @returns {Promise<SweetAlertResult<Awaited<any>>>}
       */
      showCreateObjectForm: (onAfterSubmit = null) => {
        return ObjectsGlobal.showCreateObjectForm(Departments, 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(
            Departments,
            id,
            column,
            value,
            onAfterSubmit
        );
      }
    };
</script>