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, slice } from 'lodash';
import QuestionnaireForm from "../QuestionnaireForm";
import CalendarDatePicker from '../../../CalendarDatePicker';
import citizenships from "../../../../lib/citizenships.json";
import doctypes_personal from "../../../../lib/doctypes_personal.json";
import { reduxWrapper } from '../../../../store';
import AddressSuggests from "../../AddressSuggests";
import InputMask from 'react-input-mask';
import { getCitizenshipTitleByCode } from "../../../../utils/citizenship";
import { saveQuestionnaire } from "../../../../actions";
import SuggestsInput from "../../SuggestsInput";
import DocumentIssuerSuggestsInput from "../../DocumentIssuerSuggestsInput";
class ShareholderForm extends React.Component
{
_handle_onTextFieldChange = this.props._handle_onTextFieldChange;
_handle_onCheckboxFieldChange = this.props._handle_onCheckboxFieldChange;
_handle_onFieldChange = this.props._handle_onFieldChange;
_checkStrValue = this.props._checkStrValue;
_removeError = this.props._removeError;
_handle_onIssuerCodeChange = this.props._handle_onIssuerCodeChange;
_handle_onIssuerChange = this.props._handle_onIssuerChange;
_handle_onCitizenshipChange = (name, value) =>
{
console.log("_handle_onCitizenshipChange", value);
let citizenship = getCitizenshipTitleByCode(value);
this._handle_onFieldChange(name, {
title: citizenship,
code: value,
});
}
render()
{
const { index, shareholder, checking, errors } = this.props;
let citizenship = { label: getCitizenshipTitleByCode(shareholder.identity_document.citizenship.code), code: shareholder.identity_document.citizenship.code };
console.log("shareholder", "citizenship", citizenship);
console.log("shareholder", shareholder);
return (
this._handle_onTextFieldChange(`founder_persons[${ index }].lastname`, value) }
required={ true }
disabled={ checking }
/>
this._handle_onTextFieldChange(`founder_persons[${ index }].firstname`, value) }
required={ true }
disabled={ checking }
/>
this._handle_onTextFieldChange(`founder_persons[${ index }].middlename`, value) }
required={ false }
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(event.target.name, event.target.value) }
required={ true }
disabled={ checking }
/>
this._handle_onTextFieldChange(`founder_persons[${ index }].identity_document.issuedate`, date) }
required={ true }
disabled={ checking }
/>
-1 ? "error" : "" }
type="issuer"
id={ `founder_persons[${ index }].identity_document.code` }
name={ `founder_persons[${ index }].identity_document.code` }
value={ this._checkStrValue(shareholder.identity_document.code) }
placeholder="Введите код"
onChange={ (value) => { this._handle_onIssuerCodeChange(`founder_persons[${ index }]`, index, value); } }
maxResults={ 5 }
required={ true }
disabled={ checking }
/>
{/*}
this._handle_onTextFieldChange(event.target.name, event.target.value) }
required={ true }
disabled={ checking }
/>
{*/}
-1 ? "error" : "" }
type="issuer"
id={ `founder_persons[${ index }].identity_document.issueby` }
name={ `founder_persons[${ index }].identity_document.issueby` }
value={ this._checkStrValue(shareholder.identity_document.issueby) }
placeholder="Введите наименование подразделения выдавшего документ"
onChange={ (value) => { this._handle_onIssuerChange(`founder_persons[${ index }]`, index, value); } }
maxResults={ 10 }
required={ true }
disabled={ checking }
/>
{/*}
this._handle_onTextFieldChange(event.target.name, event.target.value) }
required={ true }
disabled={ checking }
/>
{*/}
this._handle_onTextFieldChange(`founder_persons[${ index }].identity_document.placebirth`, data.name) }
required={ true }
disabled={ checking }
/>
this._handle_onTextFieldChange(`founder_persons[${ index }].identity_document.registration_address`, data) }
required={ true }
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;
_checkStrValue = this.props._checkStrValue;
_removeError = this.props._removeError;
_checkContactListDisabled = (check) =>
{
const { shareholders } = this.props;
for(let i in shareholders)
{
const shareholder = shareholders[i];
const hash = `${ shareholder.lastname }_${ shareholder.firstname }_${ shareholder.middlename }`;
if(hash === check)
{
return true;
}
}
return false;
}
render()
{
const { index, shareholders, removeShareholder, signatories, contacts, changeFounderSelectionFromList, clearFounderFromListSelection, checking, errors, } = this.props;
const shareholder = shareholders[index];
return (
{ index > 0 && (
{ !checking && (
clearFounderFromListSelection(`founder_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 ? (
{ contacts !== undefined && contacts !== null && contacts.map((contact, s_index) =>
{
const hash = `${ contact.lastname }_${ contact.firstname }_${ contact.middlename }`;
const disabled = hash !== shareholder.hash ? this._checkContactListDisabled(hash) : false;
if(checking)
{
if(shareholder.hash !== hash)
{
return null;
}
}
return (
changeFounderSelectionFromList(`founder_persons[${ index }]`, { ...shareholder, ...contact, ...{
founder_from_list: false,
founder_number: shareholders.length,
} }) }
disabled={ disabled }
/>
);
}) }
) : (
) }
) : (
) }
-1 ? "error" : "" }
mask='999'
formatChars={{ '9': '[0-9]', }}
id={ `founder_persons[${ index }].founder_part` }
name={ `founder_persons[${ index }].founder_part` }
value={ this._checkStrValue(shareholder.founder_part) }
placeholder="Укажите размер доли"
onChange={ (event) => { this._removeError("founder_part"); this._handle_onTextFieldChange(event.target.name, event.target.value > 100 ? 100 : event.target.value); } }
required={ true }
disabled={ checking }
/>
)
}
}
class Form_4_Shareholders extends QuestionnaireForm
{
constructor(props)
{
super(props);
this.state = {
founder_persons: [],
founder_persons_template: {
signatory_id: null,
lastname: null,
firstname: null,
middlename: null,
no_middle_name: false,
jobtitle: null,
telephone: null,
email: null,
founder_from_list: true,
founder_number: 0,
founder_part: null,
is_beneficial: false,
identity_document:
{
doctype: 100000000,
seria: null,
docnumber: null,
issuedate: null,
code: null,
issueby: null,
issueby_search_dadata: null,
placebirth: null,
citizenship: {
title: null,
code: null,
},
registration_address: {
name: null,
fias_id: null,
}
}
},
loading: false,
status: "empty",
errors: [
[], [], [], []
],
client_contacts: [],
};
this.ref_form = React.createRef();
this.ref_submit = React.createRef();
}
static getDerivedStateFromProps(nextProps, prevState)
{
return {
founder_persons: nextProps.questionnaire.founder_persons,
client_contacts: nextProps.questionnaire.client_contacts,
status: nextProps.questionnaire.status,
};
}
componentDidMount()
{
console.log("Form_4_Shareholders", "this.state", this.state);
console.log("Form_4_Shareholders", "global.store.getState()", global.store.getState());
const founder_persons_template = JSON.parse(JSON.stringify(this.state.founder_persons_template));
if(this.state.founder_persons.length === 0)
{
this._updateQuestionnaire({
founder_persons: [{ ...founder_persons_template, ...{ founder_from_list: false, founder_number: this.state.founder_persons.length + 1 } }],
});
}
}
_handle_onAddShareholder = () =>
{
console.log("_handle_onAddShareholder");
const founder_persons = [ ...this.state.founder_persons ];
const founder_persons_template = JSON.parse(JSON.stringify(this.state.founder_persons_template));
console.log("_handle_onAddShareholder", { founder_persons_template });
if(founder_persons.length < 4)
{
founder_persons.push(founder_persons_template);
this._updateQuestionnaire({
founder_persons,
});
}
}
_handle_onRemoveShareholder = (index) =>
{
const founder_persons = [ ...this.state.founder_persons ];
founder_persons.splice(index, 1);
this._updateQuestionnaire({
founder_persons,
});
}
_handle_onClearFounderFromListSelection = (name, values) =>
{
const founder_persons_template = JSON.parse(JSON.stringify(this.state.founder_persons_template));
const update = { ...founder_persons_template , ...values };
console.log("_handle_onClearFounderFromListSelection", update);
this._handle_onFieldChange(name, update );
}
_handle_onChangeFounderSelectionFromList = (name, values) =>
{
console.log("_handle_onChangeFounderSelectionFromList");
console.log(name, values);
this._handle_onFieldChange(name, { ...values } );
}
_handle_onIssuerCodeChange = (branch, index, option) =>
{
this._onRemoveError(index, `${ branch }.identity_document.code`);
if(typeof option === "string")
{
this._handle_onTextFieldChange(`${ branch }.identity_document.code`, option);
}
else
{
this._handle_onBranchChange([
{ name: `${ branch }.identity_document.code`, value: option.data.code },
{ name: `${ branch }.identity_document.issueby`, value: option.value },
]);
}
}
_handle_onIssuerChange = (branch, index, option) =>
{
this._onRemoveError(index, `${ branch }.identity_document.issueby`);
if(typeof option === "string")
{
this._handle_onTextFieldChange(`${ branch }.identity_document.issueby`, option);
}
else
{
this._handle_onBranchChange([
{ name: `${ branch }.identity_document.code`, value: option.data.code },
{ name: `${ branch }.identity_document.issueby`, value: option.value },
]);
}
}
_handle_onFormSubmit = (event) =>
{
event.preventDefault();
console.log("Form_4_Shareholders", "_handle_onFormSubmit");
this._handle_onCheckboxFieldChange("step", 5);
setTimeout(() =>
{
saveQuestionnaire();
this.props.onNextStep("regulatory");
}, 10);
}
_handle_onNextPage = () =>
{
const errors = [
[], [], [], []
];
const { founder_persons } = this.state;
const check = [
"founder_part",
"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",
];
if(parseInt(_get("signatory_person.identity_document.doctype"), 10) === 100000000)
{
}
for(let f in founder_persons)
{
for(let i in check)
{
if(_get(founder_persons[f], check[i]) === "")
{
errors[f].push(check[i]);
}
}
}
this.setState({ errors }, () =>
{
window.scroll(0, 0);
this.ref_submit.current.click();
});
}
_onRemoveError = (index, name) =>
{
const errors = [ ...this.state.errors ];
if(errors.indexOf(name) > -1)
{
errors[index].splice(errors[index].indexOf(name), 1);
}
this.setState({ errors });
}
_checkErrorsList = () =>
{
const { errors } = this.state;
for(let i in errors)
{
if(errors[i].length > 0)
{
return true;
}
}
return false;
}
render()
{
const { checking } = this.props;
const { founder_persons, client_contacts, loading, address, status, errors, } = this.state;
//console.log("questionnaire", questionnaire);
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);