import { useCallback, useEffect, useRef, useState } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import { Form, Checkbox, Radio, Divider, InputNumber } from 'antd'; import { ArrowRightOutlined } from '@ant-design/icons'; import FormHeader from '../PageComponents/FormHeader.jsx'; import Buttons from '../../Components/Forms/Buttons.jsx'; import { Messages } from '../../Components/Notifications/Messages'; import { changeBreadCrumb, getEmpAccess, } from '../../Features/AppPage/CenterPage.js'; import { getApplicationDtlsByAppId, getPreferenceAppNames02, gettableData, postPreferenceAppNames, settingDataSelector, } from '../../Features/PreferenceMaster/PreferenceMaster.js'; import { getSession } from '../../Services/Others.js'; import { useAuth } from '../../AuthContext.jsx'; import { ApplicationPreferences } from '../../Features/BrachLogin/BranchLogin.js'; import './PreferenceList.scss'; import { InputField } from '../../Components/Forms/InputField.jsx'; import { changePreferenceData } from '../../Features/BookingScreen/BookingData/BookingData.js'; const PreferenceList = ({ setting = false }) => { const { SadminuserAccess, featureaddDetails } = useAuth(); const dispatch = useDispatch(); const formRef = useRef(); const CompId = getSession('CompId'); const BranchId = getSession('BranchId'); const AppId = getSession('AppId'); const UserId = getSession('UserId'); const UserType = getSession('UserType'); const SalesReport = getSession('SalesReport'); const [open, setOpen] = useState(false); const [applicationValue, setApplicationValue] = useState(null); const [applicationName, setApplicationName] = useState(null); const [messageType, setMessageType] = useState(null); const [messageData, setMessageData] = useState(null); const [activeSection, setActiveSection] = useState('general'); const [empData, setEmpData] = useState(); const [preferenceConfigs, setPreferenceConfigs] = useState([]); const [addnewAccess, setAddNewAccess] = useState(true); const tableValue = useSelector(settingDataSelector); const appPreferences = useSelector(ApplicationPreferences); const bookingTypePreference = appPreferences?.find( (pre) => pre?.PreferredCatName === 'Booking Type' )?.PreferenceCatDetails; const dinePreference = bookingTypePreference?.find( (type) => type?.PreferredSubCatName?.toLowerCase() === 'dine in' && type?.PreferredStatus === 'Y' ); const settingsList = tableValue[0]?.SettingDtlDetails ?? preferenceConfigs; const allSettingsMap = {}; settingsList?.forEach( ({ SettingIdName, SettingValue, SettingCount, SettingTime, Comments }) => { allSettingsMap[SettingIdName?.toLowerCase()] = { value: SettingValue, count: SettingCount, time: SettingTime?.split(' ')?.[0], comments: Comments, }; } ); console.log(featureaddDetails, 'featureaddDetails'); const buildInitialValues = () => ({ dashboard: allSettingsMap['dashboard']?.value === 'Y', shortcutkeys: allSettingsMap['shortcutkeys']?.value === 'Y', autoreceivestock: allSettingsMap['autoreceivestock']?.value === 'Y', salespageaskiosk: allSettingsMap['salespageaskiosk']?.value === 'Y', kioskotp: allSettingsMap['kioskotp']?.value === 'Y', decimal: allSettingsMap['decimal']?.value === 'Y', enabledModules: [ ...(allSettingsMap['dinein']?.value === 'Y' ? ['dinein'] : []), ...(allSettingsMap['estimation']?.value === 'Y' ? ['estimation'] : []), ], searchfocus: allSettingsMap['searchfocus']?.value === 'Y', addproductcard: allSettingsMap['addproductcard']?.value === 'Y', enabledFeatures: [ ...(allSettingsMap['parking']?.value === 'Y' ? ['parking'] : []), ...(allSettingsMap['offer']?.value === 'Y' ? ['offer'] : []), ], stockselection: allSettingsMap['stockselection']?.value === 'Y', weightasquantity: allSettingsMap['weightasquantity']?.value === 'Y', expiredselection: allSettingsMap['expiredselection']?.value === 'Y', salesmode: allSettingsMap['sales mode']?.value === 'Y', retailshortname: allSettingsMap['retailshortname']?.comments || 'RT', wholesaleshortname: allSettingsMap['wholesaleshortname']?.comments || 'WS', billitemsorder: allSettingsMap['billitemsorder']?.value === 'Y', preorderpricechange: allSettingsMap['preorderpricechange']?.value === 'Y', quantityrestriction: allSettingsMap['overalllimit']?.value === 'Y' || allSettingsMap['itemwiselimit']?.value === 'Y', quantityrestrictiontype: allSettingsMap['overalllimit']?.value === 'Y' ? 'overall' : allSettingsMap['itemwiselimit']?.value === 'Y' ? 'itemwise' : 'overall', kiosktimelimit: allSettingsMap['hours']?.value === 'Y' || allSettingsMap['minutes']?.value === 'Y', kiosktimelimittype: allSettingsMap['hours']?.value === 'Y' ? 'hours' : allSettingsMap['minutes']?.value === 'Y' ? 'minutes' : 'minutes', maxquantity: allSettingsMap['overalllimit']?.value === 'Y' ? allSettingsMap['overalllimit']?.count || 1 : allSettingsMap['itemwiselimit']?.value === 'Y' ? allSettingsMap['itemwiselimit']?.count || 1 : 1, maxtime: allSettingsMap['hours']?.value === 'Y' ? allSettingsMap['hours']?.time ? parseFloat(allSettingsMap['hours']?.time / 60) : 0.5 : allSettingsMap['minutes']?.value === 'Y' ? parseFloat(allSettingsMap['minutes']?.time || 30) : 30, billOption: ['whatsapp', 'sms', 'email'].some( (k) => allSettingsMap[k]?.value === 'Y' ) ? 'pdf' : 'print', pdfSendOptions: ['whatsapp', 'email', 'sms'].filter( (k) => allSettingsMap[k]?.value === 'Y' ), tokenonly: allSettingsMap['tokenonly']?.value === 'Y', allproductindividualtoken: allSettingsMap['allproductindividualtoken']?.value === 'Y', printOptions: ['printuom', 'printlogo', 'printgreetings'].filter( (k) => allSettingsMap[k]?.value === 'Y' ), mobilea4: allSettingsMap['mobilea4']?.value === 'Y', estimateSettings: [ ...(allSettingsMap['estimateprintheader']?.value === 'Y' ? ['estimateprintheader'] : []), ...(allSettingsMap['estimatetitle']?.value === 'Y' ? ['estimatetitle'] : []), ], scanlayout: allSettingsMap['scanlayout']?.value === 'Y', verifyproduct: allSettingsMap['verifyproduct']?.value === 'Y', imeidetails: allSettingsMap['imeidetails']?.value === 'Y', customisedbillnumber: allSettingsMap['customisedbillnumber']?.value === 'Y', upiqr: allSettingsMap['upiqr']?.value === 'Y', itemdiscount: allSettingsMap['itemdiscount']?.value === 'Y', lowstockreport: allSettingsMap['lowstockreport']?.value === 'Y', weeklysalesreport: allSettingsMap['weeklysalesreport']?.value === 'Y', notificationblink: allSettingsMap['notificationblink']?.value === 'Y', onlineindivdualslots: allSettingsMap['onlineindivdualslots']?.value === 'Y', editbill: allSettingsMap['editbill']?.value === 'Y', notification: [ ...(allSettingsMap['onbilltime']?.value === 'Y' ? ['onbilltime'] : []), ...(allSettingsMap['1daybefore']?.value === 'Y' ? ['1daybefore'] : []), ...(allSettingsMap['1hourbefore']?.value === 'Y' ? ['1hourbefore'] : []), ], BookingOptionsandmethod: [ ...(allSettingsMap['allow individual booking']?.value === 'Y' ? ['allow individual booking'] : []), ...(allSettingsMap['single booking only']?.value === 'Y' ? ['single booking only'] : []), ], }); const [initialValues, setInitialValues] = useState(); console.log(initialValues, 'initialValues'); useEffect(() => { dispatch( changeBreadCrumb({ items: [ { name: 'Home', link: `${import.meta.env.BASE_URL}app-page/home` }, ], }) ); fetchTableData(); const centerPageSub = document.querySelector('.centerPageSub'); if (centerPageSub) { centerPageSub.style.overflow = 'hidden'; } return () => { if (centerPageSub) { centerPageSub.style.overflow = ''; } }; }, []); useEffect(() => { if (settingsList) { const built = buildInitialValues(); setInitialValues(built); console.log(built, 'built'); formRef.current?.setFieldsValue(built); } }, [settingsList]); useEffect(() => { if (UserType === 'Employee') fetchEmpAccess(); }, [UserType]); useEffect(() => { let hasAccess = ['Admin', 'Super Admin'].includes(UserType) || (UserType === 'Employee' && empData?.AddAccess === 'Y') || (UserType === 'Super Admin User' && SadminuserAccess?.find((e) => e.MenuName === 'Preference') ?.AddAccess === 'Y'); setAddNewAccess(!hasAccess); }, [empData, SadminuserAccess, UserType]); useEffect(() => { if (open) applicationDropdownData(); }, [open]); useEffect(() => { fetchTableData(); if (tableValue?.length === 0) { getPreferenceConfigs(); } }, []); useEffect(() => { const container = document.querySelector('.preferences-list'); if (!container) return; const handleScroll = () => { const sections = [ 'general', 'notifications', 'sales-page', 'print-preferences', ]; const containerTop = container.scrollTop; for (const sectionId of sections) { const element = document.getElementById(sectionId); if (element) { const elementTop = element.offsetTop - container.offsetTop; const elementBottom = elementTop + element.offsetHeight; if ( elementTop <= containerTop + 100 && elementBottom > containerTop ) { setActiveSection(sectionId); break; } } } }; container.addEventListener('scroll', handleScroll); return () => container.removeEventListener('scroll', handleScroll); }, []); const getPreferenceConfigs = async () => { const res = await dispatch(getPreferenceAppNames02())?.unwrap(); if (res?.data?.statusCode === 1) { setPreferenceConfigs( res?.data?.data?.map((i) => ({ SettingIdName: i.ConfigName, SettingId: i.ConfigId, Comments: i.Comments || '', SettingValue: 'N', })) ); } }; const fetchEmpAccess = async () => { const data = { CompId, BranchId, AppId, EmpId: UserId }; const response = await dispatch(getEmpAccess(data)).unwrap(); const access = response?.data?.data?.[0]?.EmpAccessDetails?.find( (i) => i.ConfigName === 'Preference' ); setEmpData(access); }; const applicationDropdownData = async () => { const res = await dispatch(getApplicationDtlsByAppId({ AppId })).unwrap(); if (res?.data?.statusCode === 1) { setApplicationName(res?.data?.data?.[0]?.AppName); await fetchTableData(); } }; const fetchTableData = async () => { const res = await dispatch(gettableData({ CompId, BranchId, AppId })).unwrap(); if (res?.data?.statusCode === 1 && res?.data?.data?.length > 0) { await dispatch(changePreferenceData(res?.data?.data)); } else { await dispatch(changePreferenceData([])); } handleApplicationChange(AppId); }; const handleApplicationChange = async (value) => { setApplicationValue(value); formRef.current?.setFieldsValue({ AppId: value }); }; const onComplete = useCallback(() => { setMessageData(null); setMessageType(null); }, []); const handleMenuClick = (e, targetId) => { e.preventDefault(); setActiveSection(targetId); const container = document.querySelector('.preferences-list'); const target = document.getElementById(targetId); if (container && target) { const containerTop = container.offsetTop; const targetTop = target.offsetTop; const scrollPosition = targetTop - containerTop; container.scrollTo({ top: scrollPosition, behavior: 'smooth', }); } }; const handleSubmit = async () => { const values = formRef.current.getFieldsValue(); const salesMode = values.salesmode; console.log('Sales Mode:', values.salesmode); console.log('Retail Short Name:', values.retailshortname); console.log('Wholesale Short Name:', values.wholesaleshortname); const settings = settingsList || []; console.log(values, 'values'); const updatedSettings = settings.map((setting) => { const key = setting.SettingIdName.toLowerCase(); const boolValues = { dashboard: values.dashboard, shortcutkeys: values.shortcutkeys, autoreceivestock: values.autoreceivestock, salespageaskiosk: values.salespageaskiosk, kioskotp: values.kioskotp, decimal: values.decimal, searchfocus: values.searchfocus, addproductcard: values.addproductcard, stockselection: values.stockselection, weightasquantity: values.weightasquantity, expiredselection: values.expiredselection, 'sales mode': values?.['salesmode'], billitemsorder: values.billitemsorder, preorderpricechange: values.preorderpricechange, tokenonly: values.tokenonly, allproductindividualtoken: values.allproductindividualtoken, mobilea4: values.mobilea4, scanlayout: values.scanlayout, verifyproduct: values.verifyproduct, imeidetails: values.imeidetails, customisedbillnumber: values.customisedbillnumber, upiqr: values.upiqr, itemdiscount: values.itemdiscount, lowstockreport: values.lowstockreport, weeklysalesreport: values.weeklysalesreport, notificationblink: values.notificationblink, editbill: values.editbill, overalllimit: values.quantityrestriction && values.quantityrestrictiontype === 'overall', itemwiselimit: values.quantityrestriction && values.quantityrestrictiontype === 'itemwise', hours: values.kiosktimelimit && values.kiosktimelimittype === 'hours', minutes: values.kiosktimelimit && values.kiosktimelimittype === 'minutes', }; const arrayValues = { dinein: values.enabledModules, estimation: values.enabledModules, parking: values.enabledFeatures, estimateprintheader: values.estimateSettings, estimatetitle: values.estimateSettings, offer: values.enabledFeatures, whatsapp: values.pdfSendOptions, sms: values.pdfSendOptions, email: values.pdfSendOptions, printuom: values.printOptions, printlogo: values.printOptions, printgreetings: values.printOptions, onbilltime: values.notification, '1daybefore': values.notification, '1hourbefore': values.notification, 'allow individual booking': values['BookingOptionsandmethod'], 'single booking only': values['BookingOptionsandmethod'], }; const isChecked = key in boolValues ? boolValues[key] : (arrayValues[key]?.includes(key) ?? false); return { ConfigName: setting.SettingIdName, SettingId: setting.SettingId, Comments: key === 'retailshortname' ? values.retailshortname || '' : key === 'wholesaleshortname' ? values.wholesaleshortname || '' : setting.Comments || '', SettingValue: key === 'retailshortname' || key === 'wholesaleshortname' ? salesMode ? 'Y' : 'N' : isChecked ? 'Y' : 'N', SettingCount: (key === 'overalllimit' || key === 'itemwiselimit') && isChecked ? parseFloat(values.maxquantity) || null : null, SettingTime: key === 'hours' && isChecked ? (parseFloat(values.maxtime) * 60).toFixed(2) + ' Minutes' : key === 'minutes' && isChecked ? parseFloat(values.maxtime).toFixed(2) + ' Minutes' : null, }; }); const payload = { CompId, BranchId, CreatedBy: UserId, AppId, SettingDtlDetails: updatedSettings, }; const response = await dispatch(postPreferenceAppNames(payload)).unwrap(); if (response?.data?.statusCode === 1) { setMessageType('success'); setMessageData(response?.data?.response); await fetchTableData(); } else { setMessageType('error'); setMessageData(response?.data?.response); } }; const renderCheckbox = (name, label) => allSettingsMap[name] !== undefined && (