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

/**
 * The DepartmentTimeBookingsTypes object
 */
export const DepartmentTimeBookingsTypes = {
  meta: {
    title: "Tidsbookings typer",
    icon: "fas fa-list",
    description: "Oversigt over afdelings tidsbookings typer.",
    endpoint: "/department/timebookings/types",
    labels: {
      single: "vasketype",
      multiple: "vasketyper",
    }
  },
  columns: {
    id: {
      label: "ID",
      type: "number",
      sortable: true,
      creation: {
        required: false
      }
    },
    department: {
      label: "Afdeling",
      type: "select",
      sortable: true,
      creation: {
        required: true
      },
      options: async () => {
        return SessionUser.objects.departments.get.all();
      }
    },
    name: {
      label: "Navn",
      type: "string",
      sortable: true,
      creation: {
        required: false
      }
    },
    product: {
      label: "Produkt",
      type: "select",
      sortable: true,
      creation: {
        required: true
      },
      options: async () => {
        // Get all the products, that has 'subscription_allowed' set to true
        let products = await SessionUser.objects.products.get.all();
        products = products.filter((product) => product.id <= 20);
        // Add a default option (0, unknown)
        products.unshift({
          id: 0,
          name: "Ukendt"
        });
        // Map the products to the options
        return products.map((product) => {
          return {
            id: parseInt(product.id),
            name: product.name
          }
        });
      }
    },
    description: {
      label: "Beskrivelse",
      type: "string",
      sortable: true,
      creation: {
        required: false
      }
    },
    duration: {
      label: "Varighed (minutter)",
      type: "number",
      sortable: true,
      creation: {
        required: true
      }
    },
  },
  add: async (department, product, duration, name = null, description = null) => {
    return ObjectsGlobal.add.object(
        DepartmentTimeBookingsTypes.meta.endpoint,
        {
          department: parseInt(department),
          product: parseInt(product),
          name: name,
          description: description,
          duration: parseInt(duration),
        }
    );
  },
  set: {
    name: async (id, name) => {
      return ObjectsGlobal.set.column(
        DepartmentTimeBookingsTypes.meta.endpoint,
        id,
        "name",
        name
      )
    },
    product: async (id, product) => {
      return ObjectsGlobal.set.column(
        DepartmentTimeBookingsTypes.meta.endpoint,
        id,
        "product",
        parseInt(product)
      )
    },
    description: async (id, description) => {
      return ObjectsGlobal.set.column(
        DepartmentTimeBookingsTypes.meta.endpoint,
        id,
        "description",
        description
      )
    },
    duration: async (id, duration) => {
      return ObjectsGlobal.set.column(
        DepartmentTimeBookingsTypes.meta.endpoint,
        id,
        "duration",
        parseInt(duration)
      )
    },
  },
  get: {
    all: async () => {
      return ObjectsGlobal.get.objects(DepartmentTimeBookingsTypes.meta.endpoint);
    },
    single: async (id) => {
      return ObjectsGlobal.get.object(DepartmentTimeBookingsTypes.meta.endpoint, id);
    }
  },
  delete: async (id) => {
    return ObjectsGlobal.delete.object(DepartmentTimeBookingsTypes.meta.endpoint, id);
  },
  functions: {
    showDeleteObjectForm: (id, onAfterSubmit = null) => {
      return ObjectsGlobal.showDeleteObjectForm(DepartmentTimeBookingsTypes, id, onAfterSubmit);
    },
  },
  /**
   * Show the create object form
   * @param onAfterSubmit
   * @param lockedValues
   * @returns {Promise<SweetAlertResult<Awaited<any>>>}
   */
  showCreateObjectForm: (onAfterSubmit = null, lockedValues = {}) => {
    return ObjectsGlobal.showCreateObjectForm(DepartmentTimeBookingsTypes, onAfterSubmit, lockedValues);
  },
  /**
   * 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(
        DepartmentTimeBookingsTypes,
        id,
        column,
        value,
        onAfterSubmit
    );
  }
};
</script>