<script>
import { ref } from 'vue'
import axios from 'axios'
import {API_URL} from "@/main.js";

export const getOrderItems = (order_id) => {
  const token = localStorage.getItem('token');
  if (!token) {
    return null;
  }
  return axios.get(API_URL + '/order/items?order_id=' + order_id, {
    headers: {
      Authorization: `Bearer ${token}`
    }
  });
};

export const createOrderItem = (order_id, product_id, quantity, related_item_id = null, notes = null, forcePrice = null) => {
  const token = localStorage.getItem('token');
  if (!token) {
    return null;
  }
  return axios.post(API_URL + '/order/items', {
    order_id,
    product_id,
    quantity,
    related_item_id,
    notes,
    price: forcePrice
  }, {
    headers: {
      Authorization: `Bearer ${token}`
    }
  });
};

export const removeOrderItem = (id) => {
  const token = localStorage.getItem('token');
  if (!token) {
    return null;
  }
  return axios.delete(API_URL + '/order/items?id=' + id, {
    headers: {
      Authorization: `Bearer ${token}`
    }
  });
};

export const editOrderItem = (id, price, notes, reference, quantity) => {
  const token = localStorage.getItem('token');
  if (!token) {
    return null;
  }
  return axios.put(API_URL + '/order/items', {
    id,
    price,
    notes,
    reference,
    quantity
  }, {
    headers: {
      Authorization: `Bearer ${token}`
    }
  });
};
</script>