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


/**
 * The Vehicles object
 */
export const Vehicles = {
  meta: {
    title: "Køretøjer",
    icon: "fas fa-list",
    description: "Oversigt over køretøjer",
    endpoint: "/vehicles",
    labels: {
      single: "køretøj",
      multiple: "køretøjer"
    }
  },
  columns: {
    id: {
      label: "ID",
      type: "number",
      sortable: true,
      creation: {
        required: false,
      }
    },
    type: {
      label: "Type",
      type: "select",
      sortable: true,
      creation: {
        required: true,
      },
      options: async (only_that_allows_subscription = true) => {
        // Get all the products, that has 'subscription_allowed' set to true
        let products = await SessionUser.objects.products.get.all();
        // Filter the products based on the subscription_allowed flag and category
        if (only_that_allows_subscription) {
          products = products.filter((product) => product.subscription_allowed === true);
        }
        products = products.filter((product) => product.category !== 4);
        // 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,
            addons: product.addons,
            price: product.price,
          }
        });
      },
    },
    reg: {
      label: "Reg. 1.",
      type: "string",
      sortable: true,
      creation: {
        required: true,
      }
    },
    wash_subscription: {
      label: "Vaske abonnement",
      type: "boolean",
      sortable: true,
      creation: {
        required: true,
      }
    },
    customer_id: {
      label: "Kunde ID",
      type: "number",
      sortable: true,
      creation: {
        required: true,
      }
    },
    reference: {
      label: "Reference / Intern nummer",
      type: "string",
      sortable: true,
      creation: {
        required: false,
      }
    },
  },
  add: async (type, reg, wash_subscription, customer_id, reference = null) => {
      return ObjectsGlobal.add.object(
        Vehicles.meta.endpoint,
        {
          type: parseInt(type),
          reg: reg,
          wash_subscription: ObjectsGlobal.parse.boolean(wash_subscription),
          customer_id: parseInt(customer_id),
          ...(reference ? { reference: reference } : {}),
        }
      );
    },
  set: {
    type: async (id, type) => {
      return ObjectsGlobal.set.column(
        Vehicles.meta.endpoint,
        id,
        "type",
        parseInt(type)
      )
    },
    reg: async (id, reg) => {
      return ObjectsGlobal.set.column(
        Vehicles.meta.endpoint,
        id,
        "reg",
        reg
      )
    },
    wash_subscription: async (id, wash_subscription) => {
      return ObjectsGlobal.set.column(
        Vehicles.meta.endpoint,
        id,
        "wash_subscription",
        ObjectsGlobal.parse.boolean(wash_subscription)
      ).catch((error) => {
        Swal.fire({
          title: "Fejl",
          text: error.response.data.data.message,
          icon: "error",
        });
        throw error;
      });
    },
    reference: async (id, reference) => {
      return ObjectsGlobal.set.column(
        Vehicles.meta.endpoint,
        id,
        "reference",
        reference
      )
    },
  },
  get: {
    all: async () => {
      return ObjectsGlobal.get.objects(Vehicles.meta.endpoint);
    },
    single: async (id) => {
      return ObjectsGlobal.get.object(Vehicles.meta.endpoint, id);
    },
  },
  delete: async (id) => {
    // Delete the object
    return ObjectsGlobal.delete.object(
        Vehicles.meta.endpoint,
        id
    );
  },
  functions: {
    showDeleteObjectForm: async (id, onAfterSubmit = null) => {
      return ObjectsGlobal.showDeleteObjectForm(
          Vehicles,
          parseInt(id),
          onAfterSubmit
      );
    },
    showCreateObjectForCustomerForm: async (customerId, onAfterSubmit = null) => {
      console.log('Create vehicle for customer', customerId);
      return ObjectsGlobal.showCreateObjectForm(
          Vehicles,
          onAfterSubmit,
          {
            customer_id: parseInt(customerId),
          },
      );
    },
  },
  /**
   * Show the create object form
   * @param onAfterSubmit
   * @returns {Promise<SweetAlertResult<Awaited<any>>>}
   */
  showCreateObjectForm: (onAfterSubmit = null) => {
    return ObjectsGlobal.showCreateObjectForm(Vehicles, 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(
        Vehicles,
        id,
        column,
        value,
        onAfterSubmit
    );
  },
};
</script>