<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 Bookings object
 */
export const Bookings = {
  meta: {
    title: "Bookinger",
    icon: "fas fa-list",
    description: "Oversigt over bookinger",
    endpoint: "/bookings",
    labels: {
      single: "booking",
      multiple: "bookinger",
      fields: {
        names: {
          customer_number: {
            label: "E-conomic kundenummer",
          },
        },
      },
    },
  },
  columns: {
    id: {
      label: "ID",
      type: "number",
      sortable: true,
      creation: {
        required: false
      }
    },
    customer_number: {
      label: "Kundenummer",
      type: "number",
      sortable: true,
      creation: {
        required: true
      },
    },
    wash_type: {
      label: "Vasketype",
      type: "string",
      sortable: true,
      creation: {
        required: true
      },
    },
    contact_email: {
      label: "Kontakt E-mail",
      type: "string",
      sortable: true,
      creation: {
        required: true
      },
    },
    reference_number: {
    label: "Reference",
      type: "string",
      sortable: true,
      creation: {
        required: true
      },
    },
    regNrTraekker: {
      label: "Reg. 1",
      type: "string",
      sortable: true,
      creation: {
        required: true
      },
    },
    regNrTrailer: {
      label: "Reg. 2",
      type: "string",
      sortable: true,
      creation: {
        required: false
      },
    },
    washCertificateEmail: {
      label: "Vaskecertifikat E-mail",
      type: "string",
      sortable: true,
      creation: {
        required: false
      },
    },
    date: {
      label: "Dato",
      type: "string",
      sortable: true,
      creation: {
        required: true
      },
    },
    department: {
      label: "Afdeling",
      type: "select",
      sortable: true,
      creation: {
        required: true
      },
      options: async () => {
        return SessionUser.objects.department_notification_sms.columns.department.options()
      }
    },
    pickup_bool: {
      label: "Hentning",
      type: "boolean",
      sortable: true,
      creation: {
        required: false
      },
    },
    notes: {
      label: "Noter",
      type: "string",
      sortable: true,
      creation: {
        required: false
      },
    },
    washCertificateStatus: {
      label: "Vaskecertifikat status",
      type: "select",
      sortable: true,
      creation: {
        required: false
      },
      options: async () => {
        return [
          { id: 'pending', name: "Afventer" },
          { id: 'cancelled', name: "Annulleret" },
          { id: 'completed', name: "Færdig" },
        ];
      }
    },
    washCertificateUrl: {
      label: "Vaskecertifikat URL",
      type: "string",
      sortable: true,
      creation: {
        required: false
      },
    },
    status: {
      label: "Status",
      type: "select",
      sortable: true,
      creation: {
        required: true
      },
      options: async () => {
        return [
          { id: 'pending', name: "Afventer" },
          { id: 'cancelled', name: "Annulleret" },
          { id: 'completed', name: "Færdig" },
        ];
      }
    },
    data: {
      label: "Data",
      type: "string",
      sortable: false,
      creation: {
        required: false
      },
    },
    wash_certificate_pdf: {
      label: "Vaskecertifikat PDF",
      type: "string",
      sortable: false,
      creation: {
        required: false
      },
    },
    created_at: {
      label: "Oprettet",
      type: "string",
      sortable: true,
      creation: {
        required: false
      },
    },
    deleted_at: {
      label: "Slettet",
      type: "string",
      sortable: true,
      creation: {
        required: false
      },
    },
  },
  set: {
    reference_number: async (id, reference) => {
      return ObjectsGlobal.set.column(
          Bookings.meta.endpoint,
          id,
          "reference_number",
          reference
      )
    },
  },
  get: {
    single: async (identifier) => {
      return ObjectsGlobal.get.object(Bookings.meta.endpoint, identifier, {authenticated: SessionUser.functions.hasToken()});
    },
  },
  functions: {
    submit: async (form) => {
      console.log(form);
      return ObjectsGlobal.add.object(Bookings.meta.endpoint, form, {authenticated: SessionUser.functions.hasToken()});
    },
    download: async (id) => {
      return SessionUser.request(
          '/bookings/download_pdf',
          'GET',
          {
            id: id
          }
          );
    },
  },
  /**
   * Show the create object form
   * @param onAfterSubmit
   * @returns {Promise<SweetAlertResult<Awaited<any>>>}
   */
  showCreateObjectForm: (onAfterSubmit = null) => {
    return ObjectsGlobal.showCreateObjectForm(Bookings, 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(
        Bookings,
        id,
        column,
        value,
        onAfterSubmit
    );
  }
};
</script>