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

export const currentDepartment = ref(null);
export const selectDepartment = (department) => {
  currentDepartment.value = department;
};

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

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

  localStorage.removeItem('token');
  return result;
};

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

export const createUser = (customer_number, password, role) => {
  const token = localStorage.getItem('token');
  if (!token) {
    return null;
  }
  // If the customer number is empty, set it to 0
  if (!customer_number) {
    customer_number = 0;
  }
  return axios.post(API_URL + '/users', {
    customer_number,
    password,
    role
  }, {
    headers: {
      Authorization: `Bearer ${token}`
    }
  });
};

export const editUser = (id, customer_number, display_name, role, password) => {
  const token = localStorage.getItem('token');
  if (!token) {
    return null;
  }
  return axios.put(API_URL + '/users', {
    id,
    customer_number,
    display_name,
    role,
    password
  }, {
    headers: {
      Authorization: `Bearer ${token}`
    }
  });
};

export const createDepartment = (name, description) => {
  const token = localStorage.getItem('token');
  if (!token) {
    return null;
  }
  return axios.post(API_URL + '/departments', {
    name,
    description
  }, {
    headers: {
      Authorization: `Bearer ${token}`
    }
  });
};

export const editDepartment = (id, name, description, economic_department_id) => {
  const token = localStorage.getItem('token');
  if (!token) {
    return null;
  }
  return axios.put(API_URL + '/departments', {
    id,
    name,
    description,
    economic_department_id
  }, {
    headers: {
      Authorization: `Bearer ${token}`
    }
  });
};

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

export const createUserDiscount = (userId, objectId, discount, isCategory) => {
  const token = localStorage.getItem('token');
  if (!token) {
    return null;
  }
  return axios.post(API_URL + '/superuser/user/discounts', {
    user_id: userId,
    object_id: objectId,
    discount: discount,
    is_category: isCategory
  }, {
    headers: {
      Authorization: `Bearer ${token}`
    }
  });
};
</script>