import { useState, useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import { fetchPropertyById } from "../redux/features/propertySlice";
import { updateProperty } from "../redux/features/propertySlice";
import { useParams } from "react-router-dom";
import FileBase64 from "react-file-base64";
import Navbar from "./Navbar";
const EditProperty = () => {
const { id } = useParams();
const dispatch = useDispatch();
const [activeTab, setActiveTab] = useState("propertydetails");
const handleContinue = () => {
if (activeTab === "propertydetails") setActiveTab("Images");
if (activeTab === "Images") setActiveTab("Accounting");
};
const handleBack = () => {
if (activeTab === "Images") setActiveTab("propertydetails");
if (activeTab === "Accounting") setActiveTab("Images");
};
const { selectedProperty } = useSelector((state) => state.property);
const [formData, setFormData] = useState({
address: "",
city: "",
state: "",
zip: "",
propertyTaxInfo: [
{ propertytaxowned: "0", ownedyear: "0", taxassessed: "0", taxyear: "0" },
],
images: [{ title: "", file: "" }], // Array to hold image objects
googleMapLink: "", // Field for Google Maps link
renovationRisk: null,
turnTime: "",
});
const renovationRiskOptions = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // Array of options
useEffect(() => {
dispatch(fetchPropertyById(id));
}, [dispatch, id]);
useEffect(() => {
if (selectedProperty) {
setFormData({
address: selectedProperty.address || "",
city: selectedProperty.city || "",
state: selectedProperty.state || "",
zip: selectedProperty.zip || "",
parcel: selectedProperty.parcel || "",
subdivision: selectedProperty.subdivision || "",
legal: selectedProperty.legal || "",
costpersqft: selectedProperty.costpersqft || "",
propertyType: selectedProperty.propertyType || "",
lotacres: selectedProperty.lotacres || "",
yearBuild: selectedProperty.yearBuild || "",
totallivingsqft: selectedProperty.totallivingsqft || "",
beds: selectedProperty.beds || "",
baths: selectedProperty.baths || "",
stories: selectedProperty.stories || "",
garage: selectedProperty.garage || "",
garagesqft: selectedProperty.garagesqft || "",
poolspa: selectedProperty.poolspa || "",
fireplaces: selectedProperty.fireplaces || "",
ac: selectedProperty.ac || "",
heating: selectedProperty.heating || "",
buildingstyle: selectedProperty.buildingstyle || "",
sitevacant: selectedProperty.sitevacant || "",
extwall: selectedProperty.extwall || "",
roofing: selectedProperty.roofing || "",
totalSqft: selectedProperty.totalSqft || "",
renovationRisk: selectedProperty.renovationRisk,
closeDateAtoB: selectedProperty.closeDateAtoB,
closeDateBtoC: selectedProperty.closeDateBtoC,
purchaseCost: selectedProperty.purchaseCost,
// sellingPriceBtoC:selectedProperty.sellingPriceBtoC,
// fundspriortoclosing:selectedProperty.fundspriortoclosing,
// shorttermrental:selectedProperty.shorttermrental,
// OtherIncome:selectedProperty.OtherIncome,
// InsuranceClaim:selectedProperty.InsuranceClaim,
// LongTermRental:selectedProperty.LongTermRental,
propertyTaxInfo: selectedProperty.propertyTaxInfo || [
{
propertytaxowned: "0",
ownedyear: "0",
taxassessed: "0",
taxyear: "0",
},
],
images: selectedProperty.images || [{ title: "", file: "" }],
googleMapLink: selectedProperty.googleMapLink,
rateofreturn: selectedProperty.rateofreturn,
});
}
}, [selectedProperty]);
const handleChange = (e) => {
setFormData({ ...formData, [e.target.name]: e.target.value });
};
// Handle specific changes for propertyTaxInfo array
const handleInputChange = (e, index) => {
const { name, value } = e.target;
// Make a shallow copy of the propertyTaxInfo array
const updatedPropertyTaxInfo = [...formData.propertyTaxInfo];
// Make a shallow copy of the object you want to modify (taxInfo at index)
const updatedTaxInfo = { ...updatedPropertyTaxInfo[index] };
// Update the field in the copied object
updatedTaxInfo[name] = value;
// Replace the old object with the updated one in the array
updatedPropertyTaxInfo[index] = updatedTaxInfo;
// Finally, update the formData with the updated propertyTaxInfo array
setFormData({ ...formData, propertyTaxInfo: updatedPropertyTaxInfo });
};
const handleSubmit = (e) => {
e.preventDefault();
dispatch(updateProperty({ id, propertyData: formData }));
};
const addPropertyTaxField = () => {
setFormData({
...formData,
propertyTaxInfo: [
...formData.propertyTaxInfo,
{ propertytaxowned: "", ownedyear: "", taxassessed: "", taxyear: "" },
],
});
};
const deletePropertyTaxField = (index) => {
const updatedPropertyTaxInfo = formData.propertyTaxInfo.filter(
(_, i) => i !== index
);
setFormData({ ...formData, propertyTaxInfo: updatedPropertyTaxInfo });
};
const handleImageChange = (file, index) => {
const updatedImages = [...formData.images];
updatedImages[index] = {
...updatedImages[index],
file: file.base64, // Store the base64 format of the image
};
setFormData({ ...formData, images: updatedImages });
};
const handleAddImage = () => {
setFormData({
...formData,
images: [...formData.images, { title: "", file: "" }],
});
};
const handleDeleteImage = (index) => {
const updatedImages = formData.images.filter((_, i) => i !== index);
setFormData({ ...formData, images: updatedImages });
};
const handleImageTitleChange = (index, e) => {
const updatedImages = [...formData.images];
updatedImages[index] = {
...updatedImages[index],
title: e.target.value,
};
setFormData({ ...formData, images: updatedImages });
};
useEffect(() => {
calculateTurnTime(formData.closeDateAtoB, formData.closeDateBtoC);
}, [formData.closeDateAtoB, formData.closeDateBtoC]);
const [turnTime, setTurnTime] = useState("0 days"); // Store the calculated turn time
const calculateTurnTime = (closeDateAtoB, closeDateBtoC) => {
if (closeDateAtoB && closeDateBtoC) {
const dateA = new Date(closeDateAtoB);
const dateB = new Date(closeDateBtoC);
// Calculate difference in time
const diffTime = Math.abs(dateB - dateA);
// Calculate difference in days
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
// Update state with the calculated turn time
// setTurnTime(diffDays);
setTurnTime(`${diffDays} days`);
} else {
setTurnTime("0 days"); // Reset if dates are not available
}
};
return (
<>
[M-D-Y]{" "} :{" "} {new Date(formData.closeDateAtoB).toLocaleDateString( "en-US", { month: "numeric", day: "numeric", year: "numeric", } )}
[M-D-Y] :{" "} {new Date(formData.closeDateBtoC).toLocaleDateString( "en-US", { month: "numeric", day: "numeric", year: "numeric", } )}