Files
expert-front/src/components/dashboard/passenger-boss/Form/ConfirmForm/ConfirmContent.jsx
2024-05-08 14:01:48 +03:30

722 lines
36 KiB
JavaScript

import UploadSystem from "@/core/components/UploadSystem";
import useNotification from "@/lib/app/hooks/useNotification";
import useRequest from "@/lib/app/hooks/useRequest";
import {
Button,
DialogActions,
DialogContent,
FormHelperText,
Stack,
TextField,
Typography
} from "@mui/material"
import {useFormik} from "formik";
import {useTranslations} from "next-intl";
import {CONFIRM_PASSENGER_BOSS} from "@/core/data/apiRoutes";
import {useState} from "react";
import PriceField from "@/core/components/PriceField";
import * as Yup from "yup";
import SelectBox from "@/core/components/SelectBox";
const vehicle_amount = {
"اتوبوس": 7000,
"مینی بوس": 2000,
}
const ConfirmContent = ({rowId, mutate, setOpenConfirmDialog, vehicle_type}) => {
const t = useTranslations();
const [selectedImage, setSelectedImage] = useState("");
const [expertImg, setExpertImg] = useState("");
const [fileType, setFileType] = useState(null);
const [fileName, setFileName] = useState(null);
const [showAddIcon, setShowAddIcon] = useState(true);
const [expertFileType, setExpertFileType] = useState(null);
const [expertFileName, setExpertFileName] = useState(null);
const [showExpertAddIcon, setShowExpertAddIcon] = useState(true);
const requestServer = useRequest({auth: true, notification: false})
const {update_notification} = useNotification();
const [showFacility, setShowFacility] = useState(false)
const validationSchema = Yup.object().shape({
approved_amount: Yup.mixed().test(
"is-number",
`${t("ConfirmDialog.approved_amount_number")}`,
(value) => !isNaN(value)
).test("max-amount", `${t("PassengerBoss.max_amount", {value: parseInt(vehicle_amount[vehicle_type]).toLocaleString('en')})}`,
(value) => value <= vehicle_amount[vehicle_type])
.test("positive", `${t("ConfirmDialog.approved_amount_positive")}`, (value) => value >= 0)
.required(t("ConfirmDialog.approved_amount_error")),
bank_facility: Yup.mixed().test(
"is-number",
`${t("ConfirmDialog.bank_facility_number")}`,
(value) => !isNaN(value)
).test("positive", `${t("ConfirmDialog.bank_facility_positive")}`, (value) => value >= 0)
.test("max_bank", `${t("ConfirmDialog.bank_facility_max_amount")}`, function (value) {
const government_facility = this.parent.government_facility || 0;
const max_amount = (this.parent.approved_amount || 0) - government_facility;
return value <= max_amount;
})
.required(t("ConfirmDialog.bank_facility_error")),
government_facility: Yup.mixed()
.test(
"is-number",
`${t("ConfirmDialog.government_facility_number")}`,
(value) => !isNaN(value)
)
.test("positive", `${t("ConfirmDialog.government_facility_positive")}`, (value) => value >= 0)
.test("max_government", `${t("ConfirmDialog.government_facility_max_amount")}`, function (value) {
const bank_facility = this.parent.bank_facility || 0;
const max_amount = (this.parent.approved_amount || 0) - bank_facility;
return value <= max_amount;
})
.required(t("ConfirmDialog.government_facility_error")),
repayment_period: Yup.mixed().test(
"is-number",
`${t("ConfirmDialog.repayment_period_number")}`,
(value) => !isNaN(value)
).test("positive", `${t("ConfirmDialog.repayment_period_positive")}`, (value) => value >= 0)
.required(t("ConfirmDialog.repayment_period_error")),
participation_period: Yup.mixed().test(
"is-number",
`${t("ConfirmDialog.participation_period_number")}`,
(value) => !isNaN(value)
).test("positive", `${t("ConfirmDialog.participation_period_positive")}`, (value) => value >= 0)
.required(t("ConfirmDialog.participation_period_error")),
committed_employment: Yup.mixed().test(
"is-number",
`${t("ConfirmDialog.committed_employment_number")}`,
(value) => !isNaN(value)
).test("positive", `${t("ConfirmDialog.committed_employment_positive")}`, (value) => value >= 0)
.required(t("ConfirmDialog.committed_employment_error")),
available_jobs: Yup.mixed().test(
"is-number",
`${t("ConfirmDialog.available_jobs_number")}`,
(value) => !isNaN(value)
).test("positive", `${t("ConfirmDialog.available_jobs_positive")}`, (value) => value >= 0)
.required(t("ConfirmDialog.available_jobs_error")),
sakht_period: Yup.mixed()
.test("is-number", `${t("ConfirmDialog.sakht_period_number")}`,
(value) => !isNaN(value))
.test("positive", `${t("ConfirmDialog.sakht_period_positive")}`, (value) => value > 0)
.required(t("ConfirmDialog.sakht_period_error")),
tanafos_period: Yup.mixed()
.test("is-number", `${t("ConfirmDialog.tanafos_period_number")}`,
(value) => !isNaN(value))
.test("positive", `${t("ConfirmDialog.tanafos_period_positive")}`, (value) => value > 0)
.required(t("ConfirmDialog.tanafos_period_error")),
interest_rate: Yup.mixed()
.test("is-number", `${t("ConfirmDialog.interest_rate_number")}`,
(value) => !isNaN(value))
.test("positive", `${t("ConfirmDialog.interest_rate_positive")}`, (value) => value > 0)
.required(t("ConfirmDialog.interest_rate_error")),
confirm_img: Yup.mixed().notRequired(t("ConfirmDialog.approved_amount_error"))
.test('fileSize', `${t("PassengerBoss.upload_file_unit")}`, (value) => {
if (!value) return true
return value.size <= 5 * 1024 * 1024;
})
.test('fileType', `${t("PassengerBoss.upload_file_format")}`, (value) => {
if (!value) return true
const allowedTypes = ['image/jpg', 'image/jpeg', 'image/png', 'application/pdf'];
return allowedTypes.includes(value.type);
}),
expert_img: Yup.mixed()
.required(t("PassengerBoss.upload_expert_file_required"))
.test('fileSize', `${t("PassengerBoss.upload_expert_file_unit")}`, (value) => {
return value.size <= 5 * 1024 * 1024;
})
.test('fileType', `${t("PassengerBoss.upload_expert_file_format")}`, (value) => {
const allowedTypes = ['image/jpg', 'image/jpeg', 'image/png', 'application/pdf'];
return allowedTypes.includes(value.type);
})
});
const formik = useFormik({
initialValues: {
description: "",
approved_amount: "",
bank_facility: "0",
government_facility: "0",
user_facility: "0",
physical_progress: "",
repayment_period: "",
participation_period: "",
interest_rate: "",
committed_employment: "",
sakht_period: "",
tanafos_period: "",
history_facilities: "0",
available_jobs: "",
expert_img: null,
confirm_img: null,
}, validationSchema,
onSubmit: (values, {setSubmitting}) => {
const formData = new FormData();
formData.append("approved_amount", values.approved_amount);
formData.append("bank_facility", values.bank_facility);
formData.append("government_facility", values.government_facility);
formData.append("user_facility", values.user_facility);
formData.append("repayment_period", values.repayment_period);
formData.append("participation_period", values.participation_period);
formData.append("interest_rate", values.interest_rate);
formData.append("committed_employment", values.committed_employment);
formData.append("available_jobs", values.available_jobs);
formData.append("sakht_period", values.sakht_period);
formData.append("tanafos_period", values.tanafos_period);
formData.append("history_facilities", values.history_facilities);
if (values.description != "") formData.append("expert_description", values.description);
if (values.physical_progress != "") formData.append("physical_progress", values.physical_progress);
if (values.confirm_img != null) {
const attachments = [
{
title: "فایل ضمیمه کارتابل کارگروه",
file: values.confirm_img
}
];
attachments.forEach((attachment, index) => {
formData.append(`attachment[${index}][title]`, attachment.title);
formData.append(`attachment[${index}][file]`, attachment.file);
});
}
if (values.expert_img != null) {
const attachments = [
{
title: "فایل ضمیمه گزارش کارشناس",
file: values.expert_img
}
];
attachments.forEach((attachment, index) => {
formData.append(`attachment[${index}][title]`, attachment.title);
formData.append(`attachment[${index}][file]`, attachment.file);
});
}
requestServer(`${CONFIRM_PASSENGER_BOSS}/${rowId}`, 'post', {
notification: true,
data: formData,
}).then((response) => {
setOpenConfirmDialog(false)
mutate()
update_notification()
}).catch(() => {
}).finally(() => {
setSubmitting(false);
});
},
});
const handleUploadChange = (event) => {
const uploadedFile = event.target?.files?.[0];
if (uploadedFile) {
const fileType = event.target?.files?.[0].type;
const fileName = event.target?.files?.[0].name;
setSelectedImage(URL.createObjectURL(uploadedFile));
setFileType(fileType);
setFileName(fileName);
formik.setFieldValue("confirm_img", uploadedFile).then(() => {
formik.setFieldTouched("confirm_img", true);
})
setShowAddIcon(false);
}
};
const handleExpertFileChange = (event) => {
const uploadedFile = event.target?.files?.[0];
if (uploadedFile) {
const fileType = event.target?.files?.[0].type;
const fileName = event.target?.files?.[0].name;
setExpertImg(URL.createObjectURL(uploadedFile));
setExpertFileType(fileType);
setExpertFileName(fileName);
formik.setFieldValue("expert_img", uploadedFile).then(() => {
formik.setFieldTouched("expert_img", true);
})
setShowExpertAddIcon(false);
}
};
const handleAmountChange = (event) => {
if (!isNaN(event.target.value)) {
formik.setFieldValue("approved_amount", event.target.value).then(() => {
formik.setFieldValue("user_facility", Number(event.target.value) - (Number(formik.values.bank_facility) + Number(formik.values.government_facility)))
})
event.target.value !== "" ? setShowFacility(true) : setShowFacility(false)
} else {
formik.setFieldValue("approved_amount", formik.values.approved_amount)
setShowFacility(false)
}
};
return (
<>
<DialogContent>
<Stack spacing={2}>
<Stack>
<TextField
name="description"
multiline
rows={8}
label={<>
<span>{t("ConfirmDialog.description")}</span><small>{t("ConfirmDialog.optional")}</small></>}
value={formik.values.description}
onChange={formik.handleChange}
fullWidth
variant="outlined"
sx={{mt: 1}}
/>
</Stack>
<Stack>
<PriceField
name="approved_amount"
label={<>
<span>{t("ConfirmDialog.approved_amount")}</span><small>{t("ConfirmDialog.unit")}</small></>}
type="text"
inputProps={{
inputMode: "number",
min: 0,
pattern: "[0-9]*",
}}
variant="outlined"
value={formik.values.approved_amount}
onChange={handleAmountChange}
onBlur={formik.handleBlur("approved_amount")}
error={
formik.touched.approved_amount &&
Boolean(formik.errors.approved_amount)
}
helperText={
formik.touched.approved_amount && formik.errors.approved_amount
}
sx={{mt: 1}}
fullWidth
/>
</Stack>
<Stack>
<PriceField
name="bank_facility"
label={<>
<span>{t("ConfirmDialog.bank_facility")}</span><small>{t("ConfirmDialog.unit")}</small></>}
type="text"
inputProps={{
inputMode: "number",
min: 0,
pattern: "[0-9]*",
}}
variant="outlined"
value={formik.values.bank_facility}
onChange={(event) => {
if (!isNaN(event.target.value)) {
formik.setFieldValue("bank_facility", event.target.value).then(() => {
formik.setFieldValue("user_facility", Number(formik.values.approved_amount) - (Number(event.target.value) + Number(formik.values.government_facility)))
})
} else {
formik.setFieldValue("bank_facility", formik.values.bank_facility)
}
}}
onBlur={formik.handleBlur("bank_facility")}
error={
formik.touched.bank_facility &&
Boolean(formik.errors.bank_facility)
}
helperText={
formik.touched.bank_facility && formik.errors.bank_facility
}
fullWidth
disabled={!showFacility}
/>
</Stack>
<Stack>
<PriceField
name="government_facility"
label={<>
<span>{t("ConfirmDialog.government_facility")}</span><small>{t("ConfirmDialog.unit")}</small></>}
type="text"
inputProps={{
inputMode: "number",
min: 0,
pattern: "[0-9]*",
}}
variant="outlined"
value={formik.values.government_facility}
onChange={(event) => {
if (!isNaN(event.target.value)) {
formik.setFieldValue("government_facility", event.target.value).then(() => {
formik.setFieldValue("user_facility", Number(formik.values.approved_amount) - (Number(formik.values.bank_facility) + Number(event.target.value)))
})
} else {
formik.setFieldValue("government_facility", formik.values.government_facility)
}
}}
onBlur={formik.handleBlur("government_facility")}
error={
formik.touched.government_facility &&
Boolean(formik.errors.government_facility)
}
helperText={
formik.touched.government_facility && formik.errors.government_facility
}
disabled={!showFacility}
fullWidth
/>
</Stack>
<Stack>
<PriceField
name="user_facility"
label={<>
<span>{t("ConfirmDialog.user_facility")}</span><small>{t("ConfirmDialog.unit")}</small></>}
variant="outlined"
value={formik.values.user_facility}
disabled
fullWidth
/>
</Stack>
<Stack>
<TextField
name="physical_progress"
label={<>
<span>{t("ConfirmDialog.physical_progress")}</span><small>{t("ConfirmDialog.optional")}</small></>}
type="text"
inputProps={{
inputMode: "number",
min: 0,
pattern: "[0-9]*",
}}
variant="outlined"
value={formik.values.physical_progress}
onChange={(event) => {
if (!isNaN(event.target.value)) {
formik.setFieldValue("physical_progress", event.target.value)
} else {
formik.setFieldValue("physical_progress", formik.values.physical_progress)
}
}}
onBlur={formik.handleBlur("physical_progress")}
fullWidth
/>
</Stack>
<Stack>
<TextField
name="interest_rate"
label={t("ConfirmDialog.interest_rate")}
type="text"
inputProps={{
inputMode: "number",
min: 0,
pattern: "[0-9]*",
}}
variant="outlined"
value={formik.values.interest_rate}
onChange={(event) => {
if (isNaN(event.target.value) || event.target.value > 100) formik.setFieldValue("interest_rate", formik.values.interest_rate)
else
formik.setFieldValue("interest_rate", event.target.value)
}}
onBlur={formik.handleBlur("interest_rate")}
error={
formik.touched.interest_rate &&
Boolean(formik.errors.interest_rate)
}
helperText={
formik.touched.interest_rate && formik.errors.interest_rate
}
fullWidth
/>
</Stack>
<Stack>
<TextField
name="repayment_period"
label={<>
<span>{t("ConfirmDialog.repayment_period")}</span> <small>{t("ConfirmDialog.month")}</small></>}
type="text"
inputProps={{
inputMode: "number",
min: 0,
pattern: "[0-9]*",
}}
variant="outlined"
value={formik.values.repayment_period}
onChange={(event) => {
if (!isNaN(event.target.value)) {
formik.setFieldValue("repayment_period", event.target.value)
} else {
formik.setFieldValue("repayment_period", formik.values.repayment_period)
}
}}
onBlur={formik.handleBlur("repayment_period")}
error={
formik.touched.repayment_period &&
Boolean(formik.errors.repayment_period)
}
helperText={
formik.touched.repayment_period && formik.errors.repayment_period
}
fullWidth
/>
</Stack>
<Stack>
<TextField
name="participation_period"
label={<>
<span>{t("ConfirmDialog.participation_period")}</span> <small>{t("ConfirmDialog.month")}</small></>}
type="text"
inputProps={{
inputMode: "number",
min: 0,
pattern: "[0-9]*",
}}
variant="outlined"
value={formik.values.participation_period}
onChange={(event) => {
if (!isNaN(event.target.value)) {
formik.setFieldValue("participation_period", event.target.value)
} else {
formik.setFieldValue("participation_period", formik.values.participation_period)
}
}}
onBlur={formik.handleBlur("participation_period")}
error={
formik.touched.participation_period &&
Boolean(formik.errors.participation_period)
}
helperText={
formik.touched.participation_period && formik.errors.participation_period
}
fullWidth
/>
</Stack>
<Stack>
<SelectBox
name="sakht_period"
label={t("ConfirmDialog.sakht_period")}
size="small"
value={formik.values.sakht_period}
selectType="sakht_period"
selectors={[
{
id: 1,
value: "1",
name: "1 ماه"
},
{
id: 2,
value: "2",
name: "2 ماه"
},
{
id: 3,
value: "3",
name: "3 ماه"
},
{
id: 4,
value: "4",
name: "4 ماه"
},
{
id: 5,
value: "5",
name: "5 ماه"
},
{
id: 6,
value: "6",
name: "6 ماه"
}
]}
schema={{value: 'value', name: 'name'}}
select={formik.values.sakht_period}
handleChange={(event) => {
formik.setFieldValue('sakht_period', event.target.value)
}}
setFieldTouched={formik.setFieldTouched}
error={formik.touched.sakht_period && Boolean(formik.errors.sakht_period)}
helperText={formik.touched.sakht_period && formik.errors.sakht_period}
onBlur={formik.handleBlur("sakht_period")}
/>
</Stack>
<Stack>
<SelectBox
name="tanafos_period"
label={t("ConfirmDialog.tanafos_period")}
size="small"
value={formik.values.tanafos_period}
selectType="tanafos_period"
selectors={[
{
id: 1,
value: "1",
name: "1 ماه"
},
{
id: 2,
value: "2",
name: "2 ماه"
},
{
id: 3,
value: "3",
name: "3 ماه"
},
{
id: 4,
value: "4",
name: "4 ماه"
}
]}
schema={{value: 'value', name: 'name'}}
select={formik.values.tanafos_period}
handleChange={(event) => {
formik.setFieldValue('tanafos_period', event.target.value)
}}
setFieldTouched={formik.setFieldTouched}
error={formik.touched.tanafos_period && Boolean(formik.errors.tanafos_period)}
helperText={formik.touched.tanafos_period && formik.errors.tanafos_period}
onBlur={formik.handleBlur("tanafos_period")}
/>
</Stack>
<Stack>
<TextField
name="committed_employment"
label={t("ConfirmDialog.committed_employment")}
type="text"
inputProps={{
inputMode: "number",
min: 0,
pattern: "[0-9]*",
}}
variant="outlined"
value={formik.values.committed_employment}
onChange={(event) => {
if (!isNaN(event.target.value)) {
formik.setFieldValue("committed_employment", event.target.value)
} else {
formik.setFieldValue("committed_employment", formik.values.committed_employment)
}
}}
onBlur={formik.handleBlur("committed_employment")}
error={
formik.touched.committed_employment &&
Boolean(formik.errors.committed_employment)
}
helperText={
formik.touched.committed_employment && formik.errors.committed_employment
}
fullWidth
/>
</Stack>
<Stack>
<TextField
name="available_jobs"
label={t("ConfirmDialog.available_jobs")}
type="text"
inputProps={{
inputMode: "number",
min: 0,
pattern: "[0-9]*",
}}
variant="outlined"
value={formik.values.available_jobs}
onChange={(event) => {
if (!isNaN(event.target.value)) {
formik.setFieldValue("available_jobs", event.target.value)
} else {
formik.setFieldValue("available_jobs", formik.values.available_jobs)
}
}}
onBlur={formik.handleBlur("available_jobs")}
error={
formik.touched.available_jobs &&
Boolean(formik.errors.available_jobs)
}
helperText={
formik.touched.available_jobs && formik.errors.available_jobs
}
fullWidth
/>
</Stack>
<Stack>
<SelectBox
name="history_facilities"
label={t("ConfirmDialog.history_facilities")}
size={'medium'}
value={formik.values.history_facilities}
selectType="history_facilities"
selectors={[{id: 1, value: "1", name: "دارد"}, {id: 0, value: "0", name: "ندارد"}]}
schema={{value: 'value', name: 'name'}}
select={formik.values.history_facilities}
handleChange={(event) => {
formik.setFieldValue('history_facilities', event.target.value)
}}
setFieldTouched={formik.setFieldTouched}
error={formik.touched.history_facilities && Boolean(formik.errors.history_facilities)}
helperText={formik.touched.history_facilities && formik.errors.history_facilities}
onBlur={formik.handleBlur("history_facilities")}
/>
</Stack>
<Stack>
<Typography>{t("PassengerBoss.upload_expert_img")}</Typography>
<UploadSystem
selectedImage={expertImg}
handleUploadChange={handleExpertFileChange} // Pass the updated function directly
setselectedImage={setExpertImg}
setFieldValue={formik.setFieldValue} // Remove props.setFieldValue, use formik.setFieldValue
fieldname="expert_img"
fileType={expertFileType}
fileName={expertFileName}
imageAlt={t("app_name")}
imageSize={[250, 150]}
onBlur={formik.handleBlur("expert_img")}
error={
formik.touched.expert_img &&
Boolean(formik.errors.expert_img)
}
setShowAddIcon={setShowExpertAddIcon}
showAddIcon={showExpertAddIcon}
/>
<FormHelperText
error={Boolean(formik.errors.expert_img)}
>
{formik.touched.expert_img && formik.errors.expert_img}
</FormHelperText>
</Stack>
<Stack>
<Typography>{t("PassengerBoss.upload_file")}</Typography>
<UploadSystem
selectedImage={selectedImage}
handleUploadChange={handleUploadChange}
setselectedImage={setSelectedImage}
setFieldValue={formik.setFieldValue}
fieldname="confirm_img"
fileType={fileType}
fileName={fileName}
imageAlt={t("app_name")}
imageSize={[250, 150]}
onBlur={formik.handleBlur("confirm_img")}
error={
formik.touched.confirm_img &&
Boolean(formik.errors.confirm_img)
}
setShowAddIcon={setShowAddIcon}
showAddIcon={showAddIcon}
/>
<FormHelperText
error={Boolean(formik.errors.confirm_img)}
>
{formik.touched.confirm_img && formik.errors.confirm_img}
</FormHelperText>
</Stack>
</Stack>
</DialogContent>
<DialogActions>
<Button onClick={() => setOpenConfirmDialog(false)} variant="outlined" color="secondary"
disabled={formik.isSubmitting} autoFocus>
{t("ConfirmDialog.button-cancel")}
</Button>
<Button onClick={formik.handleSubmit} variant="contained" color="primary"
disabled={formik.isSubmitting || !formik.dirty || !formik.isValid}>
{t("ConfirmDialog.button-confirm")}
</Button>
</DialogActions>
</>
)
}
export default ConfirmContent