import React from "react";
import Head from 'next/head';
import Image from 'next/image';
import Link from "next/link";
import cookie from 'cookie';
import numeral from "numeral";
import pluralize from 'pluralize-ru';
import { SpinnerCircular } from 'spinners-react';
import Select from 'react-select';
import { connect } from "react-redux";
import { withRouter } from 'next/router';
import { get as _get } from 'lodash';
import QuestionnaireForm from "../QuestionnaireForm";
import CalendarDatePicker from '../../../CalendarDatePicker';
import citizenships from "../../../../lib/citizenships.json";
import AddressSuggestsSelect from "../AddressSuggestsSelect";
import { reduxWrapper } from '../../../../store';
class ShareholderForm extends React.Component
{
_handle_onTextFieldChange = this.props._handle_onTextFieldChange;
_handle_onCheckboxFieldChange = this.props._handle_onCheckboxFieldChange;
render()
{
const { index, shareholder, checking } = this.props;
let citizenship = undefined;
if(shareholder.identity_document.citizenship_code !== "")
{
for(let i in citizenships)
{
if(parseInt(citizenships[i].value, 10) === parseInt(shareholder.identity_document.citizenship_code, 10))
{
citizenship = citizenships[i];
}
}
}
return (
this._handle_onTextFieldChange(event.target.name, event.target.value) }
required={ true }
disabled={ checking }
/>
this._handle_onTextFieldChange(event.target.name, event.target.value) }
required={ true }
disabled={ checking }
/>
this._handle_onTextFieldChange(event.target.name, event.target.value) }
disabled={ checking }
/>
this._handle_onTextFieldChange(event.target.name, event.target.value) }
required={ false }
disabled={ checking }
/>
this._handle_onTextFieldChange(event.target.name, event.target.value) }
required={ true }
disabled={ checking }
/>
this._handle_onTextFieldChange(event.target.name, event.target.value) }
required={ true }
disabled={ checking }
/>
this._handle_onTextFieldChange(event.target.name, event.target.value) }
required={ true }
disabled={ checking }
/>
this._handle_onTextFieldChange(`founded_persons[${ index }].identity_document.placebirth`, data.title) }
disabled={ checking }
/>
this._handle_onTextFieldChange(`founded_persons[${ index }].identity_document.registration_address`, data) }
disabled={ checking }
/>
)
}
}
class Shareholder extends React.Component
{
constructor(props)
{
super(props);
this.state = {};
}
_handle_onTextFieldChange = this.props._handle_onTextFieldChange;
_handle_onCheckboxFieldChange = this.props._handle_onCheckboxFieldChange;
_checkSignatoryDisabled = (signatory_id) =>
{
const { shareholders } = this.props;
for(let i in shareholders)
{
if(shareholders[i].signatory_id === signatory_id)
{
return true;
}
}
return false;
}
render()
{
const { index, shareholders, removeShareholder, signatories, changeSignatorySelection, clearSignatorySelection, checking } = this.props;
const shareholder = shareholders[index];
return (
{ index > 0 && (
{ !checking && (
clearSignatorySelection(`founded_persons[${ index }]`, { founder_from_list: !shareholder.founder_from_list ? true : false, lastname: "", firstname: "", middlename: "", no_middle_name: false, }) }
/>
) }
{ !checking && (
) }
) }
{ index > 0 ? (
{ shareholder.founder_from_list ? (
{ signatories !== undefined && signatories !== null && signatories.map((signatory, s_index) => {
const disabled = signatory.signatoryid !== shareholder.signatory_id ? this._checkSignatoryDisabled(signatory.signatoryid) : false;
if(checking)
{
if(shareholder.signatory_id !== signatory.signatoryid)
{
return null;
}
}
return (
changeSignatorySelection(`founded_persons[${ index }]`, { ...shareholder, ...{
founder_from_list: true,
signatory_id: signatory.signatoryid,
lastname: signatory.lastname,
firstname: signatory.firstname,
middlename: signatory.middlename,
} }) }
disabled={ disabled }
/>
);
}) }
) : (
) }
) : (
) }
this._handle_onTextFieldChange(event.target.name, event.target.value) }
required={ true }
/>
)
}
}
class Form_4_Shareholders extends QuestionnaireForm
{
constructor(props)
{
super(props);
this.state = {
founded_persons: [],
founded_persons_template: {},
loading: false,
};
}
static getDerivedStateFromProps(nextProps, prevState)
{
return {
founded_persons: nextProps.questionnaire.founded_persons,
founded_persons_template: nextProps.questionnaire.founded_persons_template,
};
}
componentDidMount()
{
const { founded_persons_template } = this.state;
if(this.state.founded_persons.length === 0)
{
this._updateQuestionnaire({
founded_persons: [{ ...founded_persons_template, ...{ founder_from_list: false, founder_number: this.state.founded_persons.length + 1 } }],
});
}
}
_handle_onAddShareholder = () =>
{
const founded_persons = [ ...this.state.founded_persons ];
const { founded_persons_template } = this.state;
if(founded_persons.length < 4)
{
founded_persons.push({ ...founded_persons_template });
this._updateQuestionnaire({
founded_persons,
});
}
}
_handle_onRemoveShareholder = (index) =>
{
const founded_persons = [ ...this.state.founded_persons ];
founded_persons.splice(index, 1);
this._updateQuestionnaire({
founded_persons,
});
}
_handle_onClearSignatorySelection = (name, values) =>
{
this._handle_onFieldChange(name, { ...this.state.founded_persons_template , ...values } );
}
_handle_onChangeSignatorySelection = (name, values) =>
{
this._handle_onFieldChange(name, { ...values } );
}
_handle_onFormSubmit = (event) =>
{
event.preventDefault();
console.log("Form_4_Shareholders", "_handle_onFormSubmit");
this._handle_onCheckboxFieldChange("step", 5);
setTimeout(() =>
{
this.props.onNextStep("regulatory");
}, 10);
}
_checkDisabled = () =>
{
const { founded_persons } = this.state;
const check_all = [
"founder_part",
];
const check = [
"lastname",
"firstname",
"telephone",
"email",
"identity_document.seria",
"identity_document.docnumber",
"identity_document.issuedate",
"identity_document.icode",
"identity_document.issueby",
"identity_document.placebirth",
"identity_document.citizenship_code",
"identity_document.registration_address.title",
];
for(let f in founded_persons)
{
for(let i in check_all)
{
if(_get(founded_persons[f], check_all[i]) === "")
{
console.log("1. EMPTY", f, check_all[i]);
return true;
}
}
if(!founded_persons[f].founder_from_list)
{
for(let i in check)
{
if(_get(founded_persons[f], check[i]) === "")
{
console.log("2. EMPTY", f, check[i]);
return true;
}
}
}
else
{
if(founded_persons[f].signatory_id === null)
{
console.log("3. EMPTY", f, "signatory_id");
console.log("founded_persons[f]");
console.log(founded_persons[f]);
return true;
}
}
}
return false;
}
render()
{
const { signatories, checking } = this.props;
const { founded_persons, loading, address, } = this.state;
return (
)
}
}
function mapStateToProps(state, ownProps)
{
return {
questionnaire: state.questionnaire,
}
}
export const getServerSideProps = reduxWrapper.getServerSideProps(store =>
async ({ req, res, query }) =>
{
}
);
export default connect(mapStateToProps)(Form_4_Shareholders);