<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";



    /**
     * The Department Categories object
     */
    export const DepartmentCategories = {
      meta: {
        title: "Afdelings varekataloger",
        icon: "fas fa-list",
        description: "Oversigt over afdelingens varekataloger",
        endpoint: "/departments/categories",
        labels: {
          single: "varekatalog adgang",
          multiple: "varekataloger"
        }
      },
      columns: {
        id: {
          label: "ID",
          type: "number",
          sortable: true,
          creation: {
            required: false
          }
        },
        department_id: {
          label: "Afdelings ID",
          type: "select",
          sortable: true,
          creation: {
            required: true
          },
          options: async () => {
            console.log("Getting departments");
            return SessionUser.objects.departments.get.all();
          }
        },
        category_id: {
          label: "Kategori ID",
          type: "select",
          sortable: true,
          creation: {
            required: true
          },
          options: async () => {
            console.log("Getting categories");
            return SessionUser.objects.categories.get.all();
          }
        },
      },
      add: async (department_id, category_id) => {
        return ObjectsGlobal.add.object(
            DepartmentCategories.meta.endpoint,
            {
              department_id: parseInt(department_id),
              category_id: parseInt(category_id)
            }
        );
      },
      set: {
        department_id: async (id, department_id) => {
          return ObjectsGlobal.set.column(
              DepartmentCategories.meta.endpoint,
              id,
              "department_id",
              parseInt(department_id)
          )
        },
        category_id: async (id, category_id) => {
          return ObjectsGlobal.set.column(
              DepartmentCategories.meta.endpoint,
              id,
              "category_id",
              parseInt(category_id)
          )
        },
      },
      get: {
        all: async () => {
          return ObjectsGlobal.get.objects(DepartmentCategories.meta.endpoint);
        },
        single: async (id) => {
          return ObjectsGlobal.get.object(DepartmentCategories.meta.endpoint, parseInt(id));
        }
      },
      delete: async (id) => {
        return ObjectsGlobal.delete.object(DepartmentCategories.meta.endpoint, parseInt(id));
      },
      functions: {
        showCreateObjectFormDepartment: async (department_id, onAfterSubmit = null) => {
          return ObjectsGlobal.showCreateObjectForm(DepartmentCategories, onAfterSubmit, {department_id: department_id});
        },
        showDeleteObjectForm: (id, onAfterSubmit = null) => {
          return ObjectsGlobal.showDeleteObjectForm(DepartmentCategories, id, onAfterSubmit);
        },
      },
      /**
       * Show the create object form
       * @param onAfterSubmit
       * @returns {Promise<SweetAlertResult<Awaited<any>>>}
       */
      showCreateObjectForm: (onAfterSubmit = null) => {
        return ObjectsGlobal.showCreateObjectForm(DepartmentCategories, 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(
            DepartmentCategories,
            id,
            column,
            value,
            onAfterSubmit
        );
      }
    };
</script>