import { useState } from "react";
import { useSession, signIn } from "next-auth/react";
import Layout from "../components/ui/Layout";
import { Card, CardHeader, CardContent, CardTitle, CardDescription, Button, Input, Badge, Alert } from "../components/ui/components";
import { BoltIcon as LightningBoltIcon, CalculatorIcon, DocumentArrowDownIcon as DocumentDownloadIcon, ClipboardDocumentListIcon as ClipboardListIcon } from '@heroicons/react/24/outline';
import DatePicker from "react-datepicker";
import { registerLocale } from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
import pl from "date-fns/locale/pl";
import PizZip from "pizzip";
import Docxtemplater from "docxtemplater";
registerLocale("pl", pl);
export default function Uziomy() {
const { data: session } = useSession();
const [currentStep, setCurrentStep] = useState(1);
const [isCalculating, setIsCalculating] = useState(false);
const [isGeneratingDoc, setIsGeneratingDoc] = useState(false);
const [isGeneratingDxf, setIsGeneratingDxf] = useState(false);
const [calDate, setCalDate] = useState(null);
const [ground, setGround] = useState({
wet_coef: 0,
resistivity: 0,
resistance: 0,
measure_dist: 0,
rod_len: 0,
rod_num: 0,
rod_coef: 0,
hor_len: 0,
result_v: 0,
result_h: 0,
result: 0,
wszrg_h: 0,
wszrg_v: 0,
wanted: 0,
date: undefined,
no: 0,
pr_title: "Budowa przyłącza kablowego nN",
in_city: undefined,
commune: undefined,
all_parcels: undefined,
target_parcel: undefined,
geo_data: undefined,
object: "Przyłącz kablowy nN",
objValue1: "proj.",
objName: undefined,
});
const [options] = useState([
{ label: "5 Ω", value: "5" },
{ label: "10 Ω", value: "10" },
{ label: "15 Ω", value: "15" },
{ label: "30 Ω", value: "30" },
]);
const [neededValue, setNeededValue] = useState("5");
const [resHValue, setResHValue] = useState("88");
const [resVValue, setResVValue] = useState("89");
const [objOptions1] = useState([
{ label: "proj.", value: "proj." },
{ label: "istn.", value: "istn." },
]);
const [objValue1, setObjValue1] = useState("proj.");
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function parseDate(dateString) {
const parts = dateString.split(".");
const day = parseInt(parts[0], 10);
const month = parseInt(parts[1], 10) - 1;
const year = parseInt(parts[2], 10);
return new Date(year, month, day);
}
function getGrounding(wanted, wszrg_h, wszrg_v, date) {
const dateObject = parseDate(date);
const month = dateObject.getMonth() + 1;
const wet_coef = month >= 6 && month <= 9 ? 1.2 : 1.6;
const rod_len = wanted === 30 ? 2 : 3;
const resistivity_h = wszrg_h / wet_coef;
const measure_dist_h = 1;
const resistance_h = resistivity_h / (2 * Math.PI * measure_dist_h);
const resistivity_v = wszrg_v / wet_coef;
const measure_dist_v = 1 + rod_len;
const resistance_v = resistivity_v / (2 * Math.PI * measure_dist_v);
const result_v = (wszrg_v / (2 * Math.PI * rod_len)) * (Math.log((8 * rod_len) / 0.016) - 1);
let rod_num = 2;
let hor_len = 1 + (rod_num - 1) * rod_len * 2;
let result_h = (wszrg_h / (2 * Math.PI * hor_len)) * Math.log((hor_len * hor_len) / (1 * 0.0191));
let rod_coef = Math.pow(rod_num, 4) * 0.00002 - Math.pow(rod_num, 3) * 0.0009 +
Math.pow(rod_num, 2) * 0.0137 - rod_num * 0.0981 + 1.0468;
let result = (result_v * result_h) / (result_v * rod_coef + rod_num * result_h * rod_coef);
while (result > wanted) {
rod_num += 1;
hor_len = 1 + (rod_num - 1) * rod_len * 2;
result_h = (wszrg_h / (2 * Math.PI * hor_len)) * Math.log((hor_len * hor_len) / (1 * 0.0191));
rod_coef = Math.pow(rod_num, 4) * 0.00002 - Math.pow(rod_num, 3) * 0.0009 +
Math.pow(rod_num, 2) * 0.0137 - rod_num * 0.0981 + 1.0468;
result = (result_v * result_h) / (result_v * rod_coef + rod_num * result_h * rod_coef);
}
return {
wet_coef: wet_coef,
resistivity_h: resistivity_h.toFixed(2),
resistance_h: resistance_h.toFixed(2),
measure_dist_h: measure_dist_h,
resistivity_v: resistivity_v.toFixed(2),
resistance_v: resistance_v.toFixed(2),
measure_dist_v: measure_dist_v,
rod_len: rod_len,
rod_num: rod_num,
rod_coef: rod_coef.toFixed(2),
hor_len: hor_len,
result_v: result_v.toFixed(2),
result_h: result_h.toFixed(2),
result: result.toFixed(2),
wszrg_h: wszrg_h,
wszrg_v: wszrg_v,
wanted: neededValue,
};
}
const calculateGrounding = () => {
setIsCalculating(true);
try {
const res = getGrounding(
parseInt(neededValue),
parseFloat(resHValue),
parseFloat(resVValue),
ground.date
);
setGround((current) => ({ ...current, ...res }));
} catch (error) {
alert("Błąd podczas obliczeń: " + error.message);
}
setIsCalculating(false);
};
const generateDocument = async () => {
setIsGeneratingDoc(true);
const data = {
...ground,
resisted_object: ground.objValue1 + " " + ground.objName,
};
try {
const response = await fetch("/api/generateDocx", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
});
if (!response.ok) throw new Error("Response was not ok.");
const blob = await response.blob();
const downloadUrl = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = downloadUrl;
a.download = "opis_uziemienia.docx";
document.body.appendChild(a);
a.click();
a.remove();
} catch (error) {
console.error("Error:", error);
alert("Błąd podczas generowania dokumentu");
}
setIsGeneratingDoc(false);
};
const generateDxf = async () => {
setIsGeneratingDxf(true);
const dateParts = ground.date.split(".");
const month = dateParts[1];
const year = parseInt(dateParts[2], 10);
const formattedDate = month.padStart(2, "0") + "." + year;
const inputData = {
args: [
ground.objValue1 + ground.objName,
ground.pr_title,
ground.object,
ground.in_city + ", " + ground.commune + ", dz. nr " + ground.all_parcels,
formattedDate,
ground.hor_len,
ground.rod_len,
],
};
try {
const response = await fetch("/api/generateDxf", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(inputData),
});
if (!response.ok) throw new Error("Response was not ok.");
const blob = await response.blob();
const downloadUrl = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = downloadUrl;
a.download = "uziom.dxf";
document.body.appendChild(a);
a.click();
a.remove();
} catch (error) {
console.error("Error:", error);
alert("Błąd podczas generowania rysunku");
}
setIsGeneratingDxf(false);
};
if (session) {
return (
Oblicz parametry układu uziomowego i wygeneruj dokumentację Wprowadź dane pomiarowe i parametry projektu Kliknij "Oblicz uziemienie" aby wykonać kalkulacje Wygeneruj dokumentację - opis i rysunek technicznyKalkulator uziemień
Uzyskaj dostęp do kalkulatora uziemień