<script>
import {Config} from "@/components/session/token/superUser/modules/xlvask/Config.vue";
import {SessionUser} from "@/components/session/token/SessionUser.vue";

/**
 * The XLVask object
 */
export const XLVask = {
  meta: {
    title: "XLVask",
    icon: "fas fa-car",
    description: "XLVask",
    endpoint: "/modules/xlvask",
    config_endpoint: "/configuration/xlvask",
    labels: {
      single: "xlvask",
      multiple: "xlvask"
    }
  },
  config: Config,
  functions: {
    convertDateTimeToISO: (date) => {
      if (date === null) {
        return null
      }
      return date.getFullYear() + '-' +
          String(date.getMonth() + 1).padStart(2, '0') + '-' +
          String(date.getDate()).padStart(2, '0') + 'T' +
          String(date.getHours()).padStart(2, '0') + ':' +
          String(date.getMinutes()).padStart(2, '0') + ':' +
          String(date.getSeconds()).padStart(2, '0') + '.' +
          String(date.getMilliseconds()).padStart(3, '0')
    },
    convertISODateTimeToDate: (date) => {
      if (date === null) {
        return null
      }
      const dateTime = date.split('T');
      const dateParts = dateTime[0].split('-');
      const timeParts = dateTime[1].split(':');
      return new Date(dateParts[0], dateParts[1] - 1, dateParts[2], timeParts[0], timeParts[1], timeParts[2]);
    },
    testConnection: async () => {
      return SessionUser.superUser.modules.xlvask.functions.getCustomers(
          null,
          null,
          null,
          null,
      )
    },
    getUsageLog: async (dateFrom = null, regNr = null, customerId = null, vehicleId = null) => {
      return SessionUser.request(
          "/modules/xlvask/usageLog",
          "GET",
          {
            dateFrom: dateFrom,
            regNr: regNr,
            customerId: customerId,
            vehicleId: vehicleId,
          }
      )
    },
    getVehicles: async (customerId = null, vehicleId = null, registrationNumber = null, changeDate = null) => {
      return SessionUser.request(
          "/modules/xlvask/vehicles",
          "GET",
          {
            customerId: customerId,
            vehicleId: vehicleId,
            registrationNumber: registrationNumber,
            changeDate: changeDate,
          }
      )
    },
    getCustomers: async (customerId = null, customerName = null, customerExternalId = null, vatNumber = null, changeDate = null, users = null) => {
      return SessionUser.request(
          "/modules/xlvask/customers",
          "GET",
          {
            customerId: customerId,
            customerName: customerName,
            customerExternalId: customerExternalId,
            vatNumber: vatNumber,
            changeDate: changeDate,
            users: users,
          }
      )
    },
    getVehicleTypes: async () => {
      return SessionUser.request(
          "/modules/xlvask/internal/vehicle-types",
          "GET",
          {}
      )
    },
    getRelatedOrders(washIds) {
      console.log("Getting related orders for washIds: ", washIds);
      return SessionUser.request(
          "/modules/xlvask/related-orders",
          "GET",
          {
            washIds
          }
      )
    }
  },
}
</script>