import Link from "next/link";
import { useState, useEffect } from "react";
import { useRouter } from "next/router";
import useTranslation from "next-translate/useTranslation";
import { JSONPath as jsonpath } from "jsonpath-plus";
import _, { property } from "lodash";
import {
Field,
FieldArray,
reduxForm,
formValueSelector,
reset,
} from "redux-form";
import { connect } from "react-redux";
import { useDropzone } from "react-dropzone";
import {
setRepresentationCapacity,
setRepresentationSubmit,
setSubmitBack,
setRepresentationSubmitConfirmation,
setRepresentationMessageSent,
setRepresentationReset,
} from "../../../store/currentView/action";
import RepCapacitySelection from "./representationCapacitySelection";
import RepLPACapacitySelection from "./representationLPACapacitySelection";
import RepDetails from "./representationDetails";
import RepAppellant from "./representationAppellant";
import RepAgent from "./representationAgent";
import RepInterestedPartyPerson from "./representationInterestedPartyPerson";
import RepLandOwner from "./representationLandowner";
import RepLPA from "./representationLPA";
import RepCompleteSubmit from "./representationCompleteSubmit";
import { useSession, signIn, signOut } from "next-auth/react";
import {
updateCase,
patchCase,
uploadRepFiles,
sendEmail,
generateRepPDF,
createWatchedCases,
} from "../../../actions";
import { formatDates } from "../../../components/utils";
import RepresentationProgress_s78 from "./representationProgress/representationProgress_s78";
import RepresentationProgress_enforcement from "./representationProgress/representationProgress_enforcement";
let MakeRepresentation = (props) => {
let { t } = useTranslation();
const { acceptedFiles, getRootProps, getInputProps } = useDropzone();
const [clearRepForm, setClearRepForm] = useState(false);
const [savingStatus, setSavingStatus] = useState(false);
const { data: session } = useSession();
const router = useRouter();
const { locale } = router;
const {
caseReference,
myCases,
myRepresentations,
watchedCases,
awaitingSubmission,
searchResultsObj,
representationSubmit,
currentType,
currentView,
setRepresentationCapacity,
setRepresentationSubmit,
setSubmitBack,
handleSubmit,
setRepresentationSubmitConfirmation,
setRepresentationMessageSent,
initialValues,
} = props;
const formObj = props.props.form;
let setCaseQueryObj = props[currentType] || {};
var casesObj = {};
const [showQuestionnaireSection, setShowQuestionnaireSection] = useState(1);
const [questionnaireCount, setQuestionnaireCount] = useState(0);
const setRepFileName = (values) => {
//console.log("---------------\n", values);
if (
values.hasOwnProperty("representationCapacity") &&
values.hasOwnProperty("representationType")
) {
let repType = "";
let repCap = "";
const repDate = new Date();
let day = repDate.getDate();
let month = repDate.getMonth() + 1;
let year = repDate.getFullYear();
switch (values.representationCapacity) {
case t("myrepresentations:capacity-options-arr-appellant"):
repCap = "APP";
break;
case t("myrepresentations:capacity-options-arr-agent"):
repCap = "APP"; //set as App even tho agent as is from an appeallant by proxy
break;
case t("myrepresentations:capacity-options-arr-interested"):
repCap = "IP";
break;
case "Land Owner":
repCap = "LO";
break;
case "lpa":
repCap = "LPA";
break;
default:
// code block
}
switch (values.representationType) {
case "Statement":
repType = "Statement";
break;
case "Statement of common ground":
repType = "SCG";
break;
case "Written statement":
repType = "WS";
break;
case "Written statement of evidence":
repType = "WSE";
break;
case "Questionnaire":
repType = "Questionnaire";
break;
case "Final comments":
repType = "Comments";
break;
case "Local Impact Report":
repType = "Impact";
break;
case "Other":
repType = "OTHER";
break;
default:
// code block
}
Object.assign(values, {
"repfile_name": values.hasOwnProperty("repfile_name")
? values.repfile_name
: year +
"-" +
month +
"-" +
day +
"-" +
("0" + repDate.getHours()).slice(-2) +
("0" + repDate.getMinutes()).slice(-2) +
("0" + repDate.getSeconds()).slice(-2) +
"_-_" +
repCap +
"_-_" +
repType +
"_-_" +
values.lastname.replace(
/[\s~`!@#$%^&*(){}\[\];:"'<,.>?\/\\|_+=-]/g,
""
) +
"_" +
values.firstname.replace(
/[\s~`!@#$%^&*(){}\[\];:"'<,.>?\/\\|_+=-]/g,
""
),
});
}
return values;
};
const isLPA =
props.props.accountDetails.accountDetails[
"pinswg_typeofinvolvement@OData.Community.Display.V1.FormattedValue"
] == "LPA"
? true
: false;
currentType == "searchResultsObj"
? (casesObj = jsonpath(
'$..[?(@.title=="' + caseReference + '")]',
setCaseQueryObj
))
: currentType == "watchedCases"
? (casesObj = jsonpath(
'$..[?(@.ticketnumber=="' + caseReference + '")]',
watchedCases
))
: (casesObj = jsonpath(
'$..[?(@.reference=="' + caseReference + '")]',
setCaseQueryObj
));
casesObj = Object.assign({}, ...casesObj);
var detailsObj = {};
const capacityOptionsArr = [
t("myrepresentations:capacity-options-arr-appellant"),
t("myrepresentations:capacity-options-arr-agent"),
];
props.props.currentView.caseReference.appealType != 846040017
? capacityOptionsArr.push(
t("myrepresentations:capacity-options-arr-interested")
)
: props.props.currentView.caseReference.specialistProcess !=
846040000 &&
capacityOptionsArr.push(
t("myrepresentations:capacity-options-arr-interested")
);
console.log(props.props.currentView);
let setCaseDetailsObj = router.query.hasOwnProperty("state")
? router.query.state == "edit"
? props.props.myRepresentations.myRepresentations
: props.props.searchResultsObj.searchDetailsObj
: props.currentType == "watchedCases"
? props.props.watchedCases.watchedCasesDetails
: props.props.searchResultsObj.searchDetailsObj;
var detailsObj = {};
detailsObj = jsonpath(
'$..[?(@.pinswg_name=="' +
currentView.caseReference.currentReference +
'")]',
setCaseDetailsObj
);
detailsObj = detailsObj[0];
const buildRepsArr = (detailsObj) => {
let buildArr = [];
console.log(currentView.caseReference.appealType, "");
let todaysDate = new Date();
const isDNS = currentView.caseReference.appealType == 846040011;
let startDate;
let questionnaireDate;
let finalCommentsDate;
let statementDueDate;
isDNS
? ((startDate = new Date(
detailsObj.pinswg_applicationacceptedasvalid
)),
(questionnaireDate = new Date(
new Date(
detailsObj.pinswg_endofrepresentationperiod
).setHours(23, 59, 59, 999)
)),
(finalCommentsDate = new Date(
new Date(detailsObj.pinswg_finalcommentsduedate).setHours(
23,
59,
59,
999
)
)),
(statementDueDate = new Date(
new Date(
detailsObj.pinswg_endofrepresentationperiod
).setHours(23, 59, 59, 999)
)))
: ((startDate = new Date(detailsObj.pinswg_startdate)),
(questionnaireDate = new Date(
new Date(detailsObj.pinswg_questionnaireduedate).setHours(
23,
59,
59,
999
)
)),
(finalCommentsDate = new Date(
new Date(detailsObj.pinswg_finalcommentsduedate).setHours(
23,
59,
59,
999
)
)),
(statementDueDate = new Date(
new Date(
detailsObj.pinswg_statementduedate ||
detailsObj.pinswg_statementsduedate
).setHours(23, 59, 59, 999)
)));
isLPA &&
currentView.caseReference.appealType != 846040019 &&
currentView.caseReference.appealType != 846040011 &&
todaysDate >= startDate &&
todaysDate <= questionnaireDate &&
buildArr.push("Questionnaire");
isLPA &&
isDNS &&
currentView.caseReference.appealType != 846040019 &&
todaysDate >= startDate &&
todaysDate <= questionnaireDate &&
buildArr.push("Local Impact Report");
!isDNS &&
todaysDate >= startDate &&
todaysDate <= statementDueDate &&
buildArr.push("Statement");
isDNS &&
!isLPA &&
todaysDate >= startDate &&
todaysDate <= statementDueDate &&
buildArr.push("Statement");
currentView.caseReference.appealType != 846040004 &&
todaysDate >= statementDueDate &&
todaysDate <= finalCommentsDate &&
buildArr.push("Final comments");
currentView.caseReference.appealType == 846040004 &&
currentView.caseReference.specialistProcess != 846040001 &&
todaysDate >= statementDueDate &&
todaysDate <= finalCommentsDate &&
buildArr.push("Final comments");
return buildArr;
};
const repCapacityType = () => {
return _.isEmpty(formObj["representationForm"])
? false
: _.isEmpty(formObj["representationForm"].values)
? false
: _.isEmpty(
formObj["representationForm"].values.representationCapacity
)
? false
: formObj["representationForm"].values.representationCapacity
.replace(/(\band|[\s\-\+\(\)\,\&])/g, "")
.toLowerCase();
};
const whichControl = () => {
if (isLPA && _.isEmpty(currentView.representationCapacity)) {
return (