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 ( <>