import { useEffect, useRef, useState } from 'react'; import { useLocation, useNavigate } from 'react-router-dom'; import { Form, Switch } from 'antd'; import { Messages } from '../../Components/Notifications/Messages'; import moment from 'moment'; import FormHeader from '../PageComponents/FormHeader'; import { InputField } from '../../Components/Forms/InputField'; import Buttons from '../../Components/Forms/Buttons'; import { ArrowRightOutlined } from '@ant-design/icons'; import { RadioGrpButton } from '../../Components/Forms/RadioGroup.jsx'; import { TextAreaInput } from '../../Components/Forms/TextArea.jsx'; import { DatePicProd } from '../../Components/Forms/DatePickerProduct.jsx'; import '../../Styles/CancelReschedule/Cancellation.scss'; import { getSession, validateSafeInput } from '../../Services/Others.js'; import { postCancelReshRules, putCancelReshRules, } from '../../Features/BookingScreen/BookingData/BookingData.js'; import { useDispatch } from 'react-redux'; import { changeBreadCrumb } from '../../Features/AppPage/CenterPage.js'; const subDirectory = import.meta.env.BASE_URL; const Cancellation = ({ formType }) => { const formRef = useRef(); const dispatch = useDispatch(); const location = useLocation(); const navigate = useNavigate(); const state = location?.state; const editstate = state?.editstate; const CompId = getSession('CompId'); const BranchId = getSession('BranchId'); const AppId = getSession('AppId'); const UserId = getSession('UserId'); const [ActivityType, setActivityType] = useState( editstate?.ActivityType ? editstate?.ActivityType : 'C' ); const [ChargeType, setChargeType] = useState( editstate?.ChargeType ? editstate?.ChargeType : 'P' ); const [CancellationfromDate, setCancellationfromDate] = useState(); const [CancellationToDate, setCancellationToDate] = useState(); const [messageType, setMessageType] = useState(null); const [messageData, setMessageData] = useState(null); const [applicableBefore, setApplicableBefore] = useState(null); const [isDays, setIsDays] = useState(true); const items = [ { name: 'Home', link: `${subDirectory}app-page/home`, }, { name: 'CancelReschedule', link: `${subDirectory}setting/cancel-reschedule-rules`, }, { name: editstate ? 'Edit' : 'New', link: null, // link: editstate ? `${subDirectory}setting/cancel-reschedule-rules/update` : `${subDirectory}setting/cancel-reschedule-rules/new`, }, ]; useEffect(() => { if (formType === 'edit') { setCancellationfromDate(editstate?.EffectiveFrom + 'T00:00:00'); setCancellationToDate(editstate?.EffectiveTill + 'T00:00:00'); setIsDays(editstate && editstate?.ApplicableType === 'D' ? true : false); setApplicableBefore(editstate && editstate?.ApplicableBefore); dispatch(changeBreadCrumb({ items: items })); } else { dispatch(changeBreadCrumb({ items: items })); formRef.current?.setFieldsValue({ ActivityType: 'C' }); formRef.current?.setFieldsValue({ ChargeType: 'P' }); } }, []); const CancellationFromDatefetch = async (date, dateString) => { if (dateString === '') { formRef.current?.setFieldsValue({ EffectiveFrom: '' }); setCancellationfromDate(); } if (dateString) { const Date3 = moment(dateString, ['DD-MM-YYYY']).format( 'YYYY-MM-DDTHH:mm:ss' ); formRef.current?.setFieldsValue({ EffectiveFrom: Date3 }); setCancellationfromDate(Date3); } }; const CancellationToDatefetch = async (date, dateString) => { if (dateString === '') { formRef.current?.setFieldsValue({ EffectiveTill: '' }); setCancellationToDate(); } if (dateString) { const Date4 = moment(dateString, ['DD-MM-YYYY']).format( 'YYYY-MM-DDTHH:mm:ss' ); formRef.current?.setFieldsValue({ EffectiveTill: Date4 }); setCancellationToDate(Date4); } }; const selectActivityType = (e) => { setActivityType(e); formRef.current?.setFieldsValue({ ActivityType: e }); }; const selectChargeType = (e) => { setChargeType(e); formRef.current?.setFieldsValue({ ChargeType: e }); formRef.current?.setFieldsValue({ ChargesFixed: null }); }; const validateDateRange = (rule, value, callback) => { const toDate = moment(CancellationToDate); const fromDate = moment(CancellationfromDate); if (toDate.isBefore(fromDate)) { callback('To date should be after From date'); } else { callback(); // Valid date range } }; const onFinish = async (values) => { const Postdata = { CompId: CompId, AppId: AppId, BranchId: BranchId, CreatedBy: UserId, ApplicableType: isDays ? 'D' : 'H', ...values, }; if (formType === 'add') { let response = await dispatch(postCancelReshRules(Postdata)).unwrap(); if (response?.data?.statusCode === 1) { setMessageType('success'); setMessageData(response?.data?.response); setTimeout(() => { navigate(`${subDirectory}setting/cancel-reschedule-rules`); }, 500); formRef.current?.resetFields(); setCancellationfromDate(); setCancellationToDate(); } else { setMessageType('error'); setMessageData(response?.data?.response); } } else if (formType === 'edit') { Postdata['EffectiveFrom'] = moment(values?.EffectiveFrom).format( 'YYYY-MM-DDTHH:mm:ss' ); Postdata['EffectiveTill'] = moment(values?.EffectiveTill).format( 'YYYY-MM-DDTHH:mm:ss' ); Postdata['UpdatedBy'] = UserId; Postdata['UniqueId'] = editstate?.UniqueId; let response = await dispatch(putCancelReshRules(Postdata)).unwrap(); if (response?.data?.statusCode === 1) { setMessageType('success'); setMessageData(response?.data?.response); // setTimeout(() => { navigate(`${subDirectory}setting/cancel-reschedule-rules`, { state: { Notiffy: { messageType: 'success', messageData: response?.data?.response, }, }, }); // }, 50); // navigate(`${subDirectory}setting/product-master/`); formRef.current?.resetFields(); setCancellationfromDate(); setCancellationToDate(); } else { setMessageType('error'); setMessageData(response?.data?.response); } } }; const handleApplicationBefore = (e) => { setApplicableBefore(e?.target?.value); }; return ( <>
  • selectActivityType(e)} onClickFunction={(e) => selectActivityType(e)} />
  • selectChargeType(e)} onClickFunction={(e) => selectChargeType(e)} />
  • { await validateSafeInput(value); if (!value) { return Promise.resolve(); } if (ChargeType === 'P') { if (!/^\d{1,3}(\.\d{1,2})?$/.test(value)) { return Promise.reject( 'Invalid format (Max 3 digits, up to 2 decimals)' ); } if (parseFloat(value) < 0.01 || parseFloat(value) > 100) { return Promise.reject( 'Charges Percentage must be between 0.01 and 100%' ); } } if (value?.length > 10) { return Promise.reject( 'Charges Fixed (Max 10 characters allowed)' ); } return Promise.resolve(); }, }, ]} > {ChargeType == 'F' ? `Charges Fixed(₹)` : `Charges Percentage(%)`} } isOnChange={editstate?.ChargesFixed ? true : false} inputMode="decimal" onInput={(e) => { const cleanedValue = e.target.value.replace( /[^0-9.]/g, '' ); const parts = cleanedValue.split('.'); e.target.value = parts.length > 2 ? `${parts[0]}.${parts.slice(1).join('')}` : cleanedValue; }} /> { await validateSafeInput(value); if (value && value.length > 10) { return Promise.reject( 'Applicable Before should not exceed 10 characters' ); } return Promise.resolve(); }, }, ]} >
    Applicable Type
    setIsDays(checked)} checkedChildren="Days" unCheckedChildren="Hours" />
    {`Applicable Before (${isDays ? 'Days' : 'Hours'})`} } isOnChange={ editstate?.ApplicableBefore ? true : false } inputMode="decimal" onInput={(e) => { const cleanedValue = e.target.value.replace( /[^0-9.]/g, '' ); const parts = cleanedValue.split('.'); e.target.value = parts.length > 2 ? `${parts[0]}.${parts.slice(1).join('')}` : cleanedValue; }} />

    From Date

    To Date

    >
    {ActivityType === 'R' && (
    { await validateSafeInput(value); if (value && value.length > 10) { return Promise.reject( 'Max No.of Rescheduling should not exceed 10 characters' ); } return Promise.resolve(); }, }, ]} > Max No.of Rescheduling } isOnChange={ editstate?.NoofReschedule ? true : false } inputMode="decimal" onInput={(e) => { const cleanedValue = e.target.value.replace( /[^0-9.]/g, '' ); const parts = cleanedValue.split('.'); e.target.value = parts.length > 2 ? `${parts[0]}.${parts.slice(1).join('')}` : cleanedValue; }} />
    )} { await validateSafeInput(value); if (value?.length > 100) { return Promise.reject( 'Description (Max 100 characters allowed)' ); } return Promise.resolve(); }, }, ]} > Description} isOnChange={editstate?.Description ? true : false} />
    } onClick={() => onFinish()} />
    ); }; export default Cancellation;