import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
import { axiosRetailInstanceData } from '../../AuthenicationToken/AuthenticationToken';

export const getAddCustomerDetails = createAsyncThunk(
  'addCustomer/getAddCustomerDetails',
  async (data) => {
    if (
      data?.MobileNo != null &&
      data?.MobileNo != undefined &&
      data?.AppId != null &&
      data?.AppId != undefined &&
      data?.CompId != null &&
      data?.CompId != undefined &&
      data?.BranchId != null &&
      data?.BranchId != undefined
    ) {
      return await axiosRetailInstanceData.get(
        `/customer?MobileNo=${data?.MobileNo}&AppId=${data?.AppId}&CompId=${data?.CompId}&BranchId=${data?.BranchId}`
      );
    }
  }
);

export const postAddCustomerDetails = createAsyncThunk(
  'addCustomer/postAddCustomerDetails',
  async (postData) => {
    return await axiosRetailInstanceData.post(`/customer`, postData);
  }
);

export const postRePrintReason = createAsyncThunk(
  'rePrint/postRePrintReason',
  async (postData) => {
    return await axiosRetailInstanceData.post(`/rePrint`, postData);
  }
);
export const postDefaultReprintReason = createAsyncThunk(
  'rePrint/postDefaultReprintReason',
  async (postData) => {
    return await axiosRetailInstanceData.post(`/DefaultStatusReprint`, postData);
  }
);
export const getRePrintDefault = createAsyncThunk(
  'rePrint/getRePrintDefault',
  async (data) => {
    return await axiosRetailInstanceData.get(`/ReprintHistoryStatus?compId=${data.CompId}&branchId=${data.BranchId}&appId=${data.AppId}&Type=${data?.Type ? data?.Type : 'Reprint'}`);
  }
);

export const getDefaultPaymentOptions = createAsyncThunk(
  'rePrint/getDefaultPaymentOptions',
  async (data) => {
    return await axiosRetailInstanceData.get(`/ReprintHistoryStatus?compId=${data.CompId}&branchId=${data.BranchId}&appId=${data.AppId}&Type=PaymentOptions`);
  }
);

export const getRePrintReason = createAsyncThunk(
  'rePrint/getRePrintReason',
  async (data) => {
    if (
      data?.orderFromDate != null &&
      data?.orderFromDate != undefined &&
      data?.orderToDate != null &&
      data?.orderToDate != undefined &&
      data?.appId != null &&
      data?.appId != undefined &&
      data?.compId != null &&
      data?.compId != undefined &&
      data?.branchId != null &&
      data?.branchId != undefined
    ) {
      return await axiosRetailInstanceData.get(
        `/ReprintHistory?compId=${data.compId}&branchId=${data.branchId}&appId=${data.appId}&fromDate=${data.orderFromDate}&toDate=${data.orderToDate}`
      );
    }
  }
);

export const ReprintDetails = createAsyncThunk(
  "addCustomer/ReprintDetails",
  async (data) => {
    const { AppId, CompId, BranchId, OrderFromDate, OrderToDate, OrderId } = data || {};
    if (OrderId && !OrderFromDate && !OrderToDate) {
      return await axiosRetailInstanceData.get(
        `/reprint?CompId=${CompId}&BranchId=${BranchId}&AppId=${AppId}&OrderId=${OrderId}&pageNumber=${data.PageNumber}`
      );
    }
    if (OrderFromDate && OrderToDate) {
      return await axiosRetailInstanceData.get(
        `/reprint?CompId=${CompId}&BranchId=${BranchId}&AppId=${AppId}&orderFromDate=${OrderFromDate}&orderToDate=${OrderToDate} &pageNumber=${data.PageNumber}`
      );
    } else {
      return await axiosRetailInstanceData.get(
        `/reprint?CompId=${CompId}&BranchId=${BranchId}&AppId=${AppId}&pageNumber=${data.PageNumber}`
      );
    }
  }
);



const initialState = {
  AddCustomerDetails: [],
  refreshTrigger: 0,
};

const addCustomer = createSlice({
  name: 'addCustomer',
  initialState,
  reducers: {
    changeCustomerdetails: (state, action) => {
      state.AddCustomerDetails = action?.payload;
    },
    triggerCustomerRefresh: (state) => {
      state.refreshTrigger += 1;
    },
  },
  extraReducers: (builder) => {
    builder.addCase(getAddCustomerDetails.fulfilled, (state, action) => {
      if (action?.payload?.status) {
        state.AddCustomerDetails = action?.payload?.data?.data;
      } else {
        state.AddCustomerDetails = [];
      }
    });
  },
});
export const { changeCustomerdetails, triggerCustomerRefresh } = addCustomer.actions;

export const GlobalAddCustomerDetails = (state) =>
  state.addCustomer?.AddCustomerDetails;

export const GlobalCustomerRefreshTrigger = (state) =>
  state.addCustomer?.refreshTrigger;

export default addCustomer.reducer;
