<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";

/**
 * Local products cache
 * @type {ref<null>}
 */
export const products_cache = ref(null);

/**
 * Get the product name
 * @param id
 * @param fallback
 * @returns {string}
 */
export const getProductName = (id, fallback = null) => {
  if (products_cache.value === null) {
    products_cache.value = [];
    SessionUser.objects.products.get.all().then((products) => {
      products_cache.value = products;
    });
  }
  const product = products_cache.value.find((product) => product.id === parseInt(id));
  return product ? product.name : fallback === null ? SessionUser.objects.global.language.no_data : fallback;
}

/**
 * The Products object
 */
export const Products = {
  meta: {
    title: "Produkter",
    icon: "fas fa-list",
    description: "Oversigt over produkter",
    endpoint: "/products",
    labels: {
      single: "produkt",
      multiple: "produkter"
    }
  },
  columns: {
    id: {
      label: "ID",
      type: "number",
      sortable: true,
      creation: {
        required: false
      }
    },
    name: {
      label: "Navn",
      type: "string",
      sortable: true,
      creation: {
        required: true
      }
    },
    description: {
      label: "Beskrivelse",
      type: "string",
      sortable: true,
      creation: {
        required: false
      }
    },
    price: {
      label: "Pris",
      type: "number",
      sortable: true,
      creation: {
        required: true
      }
    },
    subscription_allowed: {
      label: "Abonnement tilladt",
      type: "boolean",
      sortable: true,
      creation: {
        required: false
      }
    },
    category: {
      label: "Kategori",
      type: "select",
      sortable: true,
      creation: {
        required: false
      },
      options: async () => {
        return SessionUser.objects.categories.get.all();
      }
    },
    piktogram: {
      label: "Piktogram",
      type: "number",
      sortable: true,
      creation: {
        required: false
      }
    },
    economic_product_id: {
      label: "E-conomic produkt",
      type: "select",
      sortable: true,
      creation: {
        required: false
      },
      options: async () => {
        return SessionUser.objects.economic_products.get.all();
      }
    },
    apply_category_discount:
    {
      label: "Anvend kategori rabat",
      type: "boolean",
      sortable: true,
      creation: {
        required: false
      }
    },
    requires_note: {
      label: "Kræver note",
      type: "boolean",
      sortable: true,
      creation: {
        required: false
      }
    },
  },
  add: async (name, price) => {
    return ObjectsGlobal.add.object(
        Products.meta.endpoint,
        {
          name: name,
          price: price
        }
    );
  },
  set: {
    name: async (id, name) => {
      return ObjectsGlobal.set.column(
        Products.meta.endpoint,
        id,
        "name",
        name
      )
    },
    description: async (id, description) => {
      return ObjectsGlobal.set.column(
        Products.meta.endpoint,
        id,
        "description",
        description
      )
    },
    price: async (id, price) => {
      return ObjectsGlobal.set.column(
        Products.meta.endpoint,
        id,
        "price",
          ObjectsGlobal.parse.number(price)
      )
    },
    subscription_allowed: async (id, subscription_allowed) => {
      return ObjectsGlobal.set.column(
        Products.meta.endpoint,
        id,
        "subscription_allowed",
        ObjectsGlobal.parse.boolean(subscription_allowed)
      )
    },
    category: async (id, category) => {
      return ObjectsGlobal.set.column(
        Products.meta.endpoint,
        id,
        "category",
        category
      )
    },
    piktogram: async (id, piktogram) => {
      return ObjectsGlobal.set.column(
        Products.meta.endpoint,
        id,
        "piktogram",
        piktogram
      )
    },
    economic_product_id: async (id, economic_product_id) => {
      return ObjectsGlobal.set.column(
        Products.meta.endpoint,
        id,
        "economic_product_id",
          ObjectsGlobal.parse.number(economic_product_id)
      )
    },
    apply_category_discount: async (id, apply_category_discount) => {
      return ObjectsGlobal.set.column(
        Products.meta.endpoint,
        id,
        "apply_category_discount",
        ObjectsGlobal.parse.boolean(apply_category_discount)
      )
    },
    requires_note: async (id, requires_note) => {
      return ObjectsGlobal.set.column(
        Products.meta.endpoint,
        id,
        "requires_note",
        ObjectsGlobal.parse.boolean(requires_note)
      )
    },
  },
  get: {
    all: async () => {
      return ObjectsGlobal.get.objects(Products.meta.endpoint);
    },
    single: async (id) => {
      return ObjectsGlobal.get.object(Products.meta.endpoint, id);
    },
  },
  functions: {
    getProductName: getProductName,
    showCategoryProducts: async (category_id) => {
      console.log(category_id);
      return ObjectsGlobal.showObjects(Products, {
        id: true,
        name: true,
      }, {category: category_id}, false
      );
    }
  },
  /**
   * Show the create object form
   * @param onAfterSubmit
   * @returns {Promise<SweetAlertResult<Awaited<any>>>}
   */
  showCreateObjectForm: (onAfterSubmit = null) => {
    return ObjectsGlobal.showCreateObjectForm(Products, 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(
        Products,
        id,
        column,
        value,
        onAfterSubmit
    );
  },
};
</script>