import { createAsyncThunk } from "@reduxjs/toolkit";
import axios from "axios";
import { LOADING_END, LOADING_START } from "../../../lib/redux/slices/appSlice";
import { getRoleFromUrl } from "../../../utils/getRoleFromUrl";

export const vetFarmGetBarsOverview = createAsyncThunk(
  "VET_FARM_GET_OVERVIEW_BARS_SERVICE",
  async (d, { dispatch }) => {
    dispatch(LOADING_START());
    const { data, status } = await axios.get(`dashboard_detail_of_killing`, {
      params: {
        date1: d.selectedDate1,
        date2: d.selectedDate2,
        role: getRoleFromUrl(),
        search: "filter",
        value: d.textValue,
        without_quarantine_code_state: d.hasDocumentState,
      },
    });
    dispatch(LOADING_END());
    return { data, status };
  }
);

export const parentCompanyGetBarsOverview = createAsyncThunk(
  "PARENT_COMPANY_GET_OVERVIEW_BARS_SERVICE",
  async (d, { dispatch }) => {
    dispatch(LOADING_START());
    const { data, status } = await axios.get(
      `${d.province}parent_company_dashboard_detail_of_killing`,
      {
        params: {
          date1: d.selectedDate1,
          date2: d.selectedDate2,
          role: getRoleFromUrl(),
          search: "filter",
          value: d.textValue,
          without_quarantine_code_state: d.hasDocumentState,
        },
      }
    );
    dispatch(LOADING_END());
    return { data, status };
  }
);

export const vetFarmGetFinishedBarsOverview = createAsyncThunk(
  "VET_FARM_GET_OVERVIEW_BARS_SERVICE",
  async (d, { dispatch }) => {
    dispatch(LOADING_START());
    const { data, status } = await axios.get(`dashboard_detail_of_killing`, {
      params: {
        date1: d.selectedDate1,
        date2: d.selectedDate2,
        role: getRoleFromUrl(),
        search: "filter",
        value: d.textValue,
        type: "assignment",
        without_bar_document: d.hasDocumentState,
        role_key: d?.roleKey || "",
      },
    });
    dispatch(LOADING_END());
    return { data, status };
  }
);
