<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 DepartmentTimeBookingsEntries object
 */
export const DepartmentTimeBookingsEntries = {
  meta: {
    title: "Tidsbookinger",
    icon: "fas fa-list",
    description: "Oversigt over afdelings tidsbookinger.",
    endpoint: "/department/timebookings/entries",
    labels: {
      single: "tidsbooking",
      multiple: "tidsbookinger",
    }
  },
  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();
      }
    },
    type: {
      label: "Type",
      type: "select",
      sortable: true,
      creation: {
        required: true
      },
      options: async () => {
        return SessionUser.objects.department_time_bookings_types.get.all();
      }
    },
    start: {
      label: "Start",
      type: "datetime",
      sortable: true,
      creation: {
        required: true
      }
    },
    end: {
      label: "Slut",
      type: "datetime",
      sortable: true,
      creation: {
        required: true
      }
    },
    phone_country_code: {
      label: "Landekode",
      type: "select",
      sortable: true,
      creation: {
        required: true
      },
      options: async () => {
        return [
          {
            name: "Danmark (+45)",
            id: 45
          },
          {
            name: "Sverige (+46)",
            id: 46
          },
        ];
      }
    },
    phone: {
      label: "Telefonnummer",
      type: "number",
      sortable: true,
      creation: {
        required: true
      }
    },
    reg: {
      label: "Reg. 1.",
      type: "datetime",
      sortable: true,
      creation: {
        required: false
      }
    },
    note: {
      label: "Note",
      type: "string",
      sortable: true,
      creation: {
        required: false
      }
    },
  },
  add: async (department, type, start, end, phone_country_code, phone, reg = null, note = null) => {
    return ObjectsGlobal.add.object(
        DepartmentTimeBookingsEntries.meta.endpoint,
        {
          department: parseInt(department),
          type: parseInt(type),
          start,
          end,
          phone_country_code: parseInt(phone_country_code),
          phone: parseInt(phone),
          reg,
          note
        }
    );
  },
  set: {
    department: async (id, department) => {
      return ObjectsGlobal.set.column(
          DepartmentTimeBookingsEntries.meta.endpoint,
        id,
        "department",
        parseInt(department)
      )
    },
    type: async (id, type) => {
      return ObjectsGlobal.set.column(
          DepartmentTimeBookingsEntries.meta.endpoint,
        id,
        "type",
        parseInt(type)
      )
    },
    start: async (id, start) => {
      return ObjectsGlobal.set.column(
          DepartmentTimeBookingsEntries.meta.endpoint,
        id,
        "start",
        start
      )
    },
    end: async (id, end) => {
      return ObjectsGlobal.set.column(
          DepartmentTimeBookingsEntries.meta.endpoint,
        id,
        "end",
        end
      )
    },
    phone_country_code: async (id, phone_country_code) => {
      return ObjectsGlobal.set.column(
          DepartmentTimeBookingsEntries.meta.endpoint,
        id,
        "phone_country_code",
        parseInt(phone_country_code)
      )
    },
    phone: async (id, phone) => {
      return ObjectsGlobal.set.column(
          DepartmentTimeBookingsEntries.meta.endpoint,
        id,
        "phone",
        parseInt(phone)
      )
    },
    reg: async (id, reg) => {
      return ObjectsGlobal.set.column(
          DepartmentTimeBookingsEntries.meta.endpoint,
        id,
        "reg",
        reg
      )
    },
    note: async (id, note) => {
      return ObjectsGlobal.set.column(
          DepartmentTimeBookingsEntries.meta.endpoint,
        id,
        "note",
        note
      )
    },
  },
  get: {
    all: async () => {
      return ObjectsGlobal.get.objects(DepartmentTimeBookingsEntries.meta.endpoint);
    },
    single: async (id) => {
      return ObjectsGlobal.get.object(DepartmentTimeBookingsEntries.meta.endpoint, id);
    }
  },
  delete: async (id) => {
    return ObjectsGlobal.delete.object(DepartmentTimeBookingsEntries.meta.endpoint, id);
  },
  functions: {
    showDeleteObjectForm: (id, onAfterSubmit = null) => {
      return ObjectsGlobal.showDeleteObjectForm(DepartmentTimeBookingsEntries, id, onAfterSubmit);
    },
  },
  /**
   * Show the create object form
   * @param onAfterSubmit
   * @param lockedValues
   * @returns {Promise<SweetAlertResult<Awaited<any>>>}
   */
  showCreateObjectForm: (onAfterSubmit = null, lockedValues = {}) => {
    return ObjectsGlobal.showCreateObjectForm(DepartmentTimeBookingsEntries, 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(
        DepartmentTimeBookingsEntries,
        id,
        column,
        value,
        onAfterSubmit
    );
  }
};
</script>