import React, { useCallback, useEffect, useState, useRef } from 'react';
import { useDispatch } from 'react-redux';
import { Messages } from '../../Components/Notifications/Messages.jsx';
import { Tables } from '../../Components/Tables/Table.jsx';
import { Search } from '../../Components/Forms/Search.jsx';
import FormHeader from '../PageComponents/FormHeader.jsx';
import { Checkbox, Form, Space } from 'antd';
import { InputField } from '../../Components/Forms/InputField.jsx';
import Buttons from '../../Components/Forms/Buttons.jsx';
import {
changeBreadCrumb,
getEmpAccess,
} from '../../Features/AppPage/CenterPage.js';
import {
getExtTabledata,
putExtTabledata,
postExtTabledata,
deleteExtTabledata,
} from '../../Features/ExteraCharges/ExtraCharges.js';
import {
EditFilled,
DeleteFilled,
PlusOutlined,
ReloadOutlined,
ArrowRightOutlined,
} from '@ant-design/icons';
import { DefaultModal } from '../../Components/Modal/DefaultModal.jsx';
import { getSession, validateSafeInput } from '../../Services/Others.js';
import { DropDowns } from '../../Components/Forms/DropDown.jsx';
import { getAdmin } from '../../Features/ProductPage/ProductPage.js';
import { useAuth } from '../../AuthContext.jsx';
import { getPreferenceData } from '../../Features/BookingScreen/BookingData/BookingData.js';
import Imageupload from '../../Components/Forms/Upload.jsx';
// src\Styles\OverAllStyle\OverAllStyle.scss
import '../../Styles/OverAllStyle/OverAllStyle.scss';
const subDirectory = import.meta.env.ENV_BASE_URL;
const items = [
{
name: 'Home',
link: `${subDirectory}app-page/home`,
},
];
function ExtraChargesList() {
const { SadminuserAccess } = useAuth();
let SAAccessCommonMaster = SadminuserAccess?.find(
(e) => e?.MenuName === 'Extra Charges'
);
console.log(SAAccessCommonMaster, 'SAAccessCommonMaster', SadminuserAccess);
const formRef = useRef();
const dispatch = useDispatch();
const [messageType, setMessageType] = useState(null);
const [messageData, setMessageData] = useState(null);
const [searchedText, setSearchedText] = useState('');
const [uniqueId, setUniqueId] = useState('');
const [editState, setEditState] = useState(false);
const [tabledata, setTabledata] = useState();
const [empData, setEmpData] = useState();
const [addnewAccess, setaddnewAccess] = useState(true);
const [filteredInfo, setFilteredInfo] = useState({});
const [sortedInfo, setSortedInfo] = useState({});
const [open, setOpen] = useState(false);
const [page, setpage] = useState(1);
const [SelectedTaxId, setSelectedTaxId] = useState(null);
const [SelectedTaxamount, setSelectedTaxamount] = useState(null);
const [SelectedPrice, setSelectedPrice] = useState(null);
const [TaxData, setTaxData] = useState([]);
const CompId = getSession('CompId');
const BranchId = getSession('BranchId');
const AppId = getSession('AppId');
const UserId = getSession('UserId');
const UserType = getSession('UserType');
const [allowDecimal, setAllowDecimal] = useState(false);
const [ImageUrl, setImageUrl] = useState(null);
useEffect(() => {
fetchData();
Table();
dispatch(changeBreadCrumb({ items: items }));
getPreference();
// if (UserType === "Employee") {
// fetchApi()
// }
}, []);
useEffect(() => {
if (UserType === 'Employee') {
fetchApi();
}
}, [UserType]);
useEffect(() => {
let hasAccess = false;
if (UserType === 'Admin' || UserType === 'Super Admin') {
hasAccess = true;
} else if (UserType === 'Employee') {
hasAccess = empData?.AddAccess === 'Y';
} else if (UserType === 'Super Admin User') {
hasAccess = SAAccessCommonMaster?.AddAccess === 'Y';
}
setaddnewAccess(!hasAccess);
}, [empData, SAAccessCommonMaster, UserType]);
const fetchApi = async () => {
let data = {
CompId: CompId,
BranchId: BranchId,
AppId: AppId,
EmpId: UserId,
};
let response = await dispatch(getEmpAccess(data)).unwrap();
let datas = response?.data?.data?.[0]?.EmpAccessDetails?.filter(
(item) => item.ConfigName === 'Extra Charges'
);
setEmpData(datas?.[0]);
};
async function fetchData() {
const gettingAdminTax = await dispatch(
getAdmin({ CompId: CompId, AppId: AppId })
).unwrap();
if (gettingAdminTax.data?.statusCode === 1) {
await setTaxData(gettingAdminTax.data?.data);
}
}
const Table = async () => {
const data = {
CompId: CompId,
BranchId: BranchId,
AppId: AppId,
};
let response = await dispatch(getExtTabledata(data)).unwrap();
if (response?.data?.statusCode == 1) {
setTabledata(response?.data?.data);
}
};
const actionsFormatter = async (row, rowIndex) => {
if (row.ActiveStatus !== 'D') {
fetchData();
setOpen(true);
setEditState(true);
await setSelectedTaxId(row.TaxId);
setUniqueId(row.UniqueId);
formRef?.current?.setFieldsValue(row);
let taxAmount = TaxData.filter((value) => value.TaxId === row.TaxId);
const finalTaxAmount = formRef.current?.getFieldsValue()?.Price
? Math.round(
(formRef.current?.getFieldsValue()?.Price *
taxAmount[0]?.TaxPercentage) /
(taxAmount[0]?.TaxPercentage + 100),
2
)
: 0;
const netPrice =
parseInt(formRef.current?.getFieldsValue()?.Price) -
parseInt(finalTaxAmount);
formRef.current?.setFieldsValue({
TaxAmount: finalTaxAmount,
PriceWithoutTax: netPrice ? netPrice : 0,
});
setSelectedPrice(netPrice ? netPrice : 0);
setSelectedTaxamount(finalTaxAmount);
}
};
const statusFormatter = async (row) => {
let deleteData = {
UniqueId: row.UniqueId,
ActiveStatus: row.ActiveStatus == 'A' ? 'D' : 'A',
UpdatedBy: getSession('UserId'),
};
let response = await dispatch(deleteExtTabledata(deleteData)).unwrap();
if (response?.data?.statusCode == 1) {
Table();
setMessageType('success');
setMessageData(
row.ActiveStatus == 'A'
? 'In-Activated Successfully'
: 'Activated Successfully'
);
}
};
const columns = [
{
title: 'Sl.No',
key: 'sno',
align: 'center',
width: '60px',
render: (text, object, index) => (
{(page - 1) * 10 + index + 1}
),
},
{
title: 'Extra Charges Type',
dataIndex: 'ExtraChargeType',
key: 'ExtraChargeType',
width: '100px',
filteredValue: [searchedText],
render: (text) => {text},
onFilter: (value, record) => {
return (
String(record.ExtraChargeType)
?.toLowerCase()
?.includes(value?.toLowerCase()) ||
String(record.Price)?.toLowerCase()?.includes(value?.toLowerCase())
);
},
},
{
title: 'Price',
dataIndex: 'Price',
key: 'Price',
align: 'right',
width: '100px',
render: (text) => {safeRound(text)},
},
{
title: 'Action',
key: 'Action',
dataIndex: 'Action',
width: '100px',
align: 'center',
render: (_, record, index) =>
tabledata.length >= 1 ? (