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

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

export const createOrder = (customer_id, department_id, reference, notes) => {
  const token = localStorage.getItem('token');
  if (!token) {
    return null;
  }
  return axios.post(API_URL + '/orders', {
    customer_id,
    department_id,
    reference,
    notes
  }, {
    headers: {
      Authorization: `Bearer ${token}`
    }
  });
};

export const editOrder = (id, customer_id, department_id, reference, notes) => {
  const token = localStorage.getItem('token');
  if (!token) {
    return null;
  }
  return axios.put(API_URL + '/orders', {
    id,
    customer_id,
    department_id,
    reference,
    notes
  }, {
    headers: {
      Authorization: `Bearer ${token}`
    }
  });
};

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


export const getUserOrder = (id, include_all = false) => {
  const token = localStorage.getItem('token');
  if (!token) {
    return null;
  }
  return axios.get(API_URL + `/user/order?id=${id}&include_all=${include_all}`, {
    headers: {
      Authorization: `Bearer ${token}`
    }
  });
};

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