import React from "react";
import { SpinnerCircular } from "spinners-react";
import moment from "moment";
import pluralize from 'pluralize-ru';
import numeral from "numeral";
import DateInput from "../../../../../components/DatePicker";
import { getContractGraphicChangeOptions, getContractGraphicChangeCalculate } from "../../../../../actions";
class PaymentDate extends React.Component
{
constructor(props)
{
super(props);
this.state = {
value: null,
min: null,
max: null,
help: false,
error: false,
}
}
componentDidMount()
{
const { option, onOption } = this.props;
this.setState({
value: moment(option.value).toDate(),
min: moment(option.from).toDate(),
max: moment(option.to).toDate(),
}, () =>
{
onOption(this.state.value);
});
}
_handle_onChange = (value) =>
{
const { onOption } = this.props;
const { min, max } = this.state;
const v = moment(value).toDate();
if(v >= min && v <= max )
{
this.setState({ value, error: false, }, () =>
{
onOption(value);
});
}
else
{
this.setState({ value: undefined, error: true }, () =>
{
onOption(v, true);
});
}
}
_handle_onMobileHelp = () =>
{
if(window.innerWidth < 768)
{
this.setState({ help: this.state.help ? false : true });
}
}
render()
{
const { option } = this.props;
const { value, min, max, error } = this.state;
return (
this.setState({ error: false }) }
disabled={ option.disable ? true : false }
plain={ true }
className={ error ? "error" : "" }
/>
{ error && (
Выберите дату между { min !== null && moment(min).format("DD.MM.YYYY") } и { max !== null && moment(max).format("DD.MM.YYYY") }
) }
)
}
}
class FixLastPayment extends React.Component
{
constructor(props)
{
super(props);
this.state = {
value: false,
help: false,
};
}
componentDidMount()
{
const { option, onOption } = this.props;
this.setState({
value: option.value,
}, () =>
{
onOption(this.state.value);
});
}
_handle_onChange = () =>
{
const { onOption } = this.props;
this.setState({ value: this.state.value ? false : true }, () =>
{
onOption(this.state.value);
});
}
_handle_onMobileHelp = () =>
{
if(window.innerWidth < 768)
{
this.setState({ help: this.state.help ? false : true });
}
}
render()
{
const { option } = this.props;
const { value, help } = this.state;
return (
{ option.information !== undefined && option.information !== null && (
this._handle_onMobileHelp() }>
this._handle_onMobileHelp() }>Закрыть
) }
)
}
}
class DateOffestType extends React.Component
{
constructor(props)
{
super(props);
this.state = {
value: null,
}
}
componentDidMount()
{
const { option, onOption } = this.props;
this.setState({
value: parseInt(option.value, 10),
}, () =>
{
onOption(this.state.value);
});
}
_handle_onChange = (value) =>
{
const { onOption } = this.props;
this.setState({ value: parseInt(value, 10) }, () =>
{
onOption(this.state.value);
});
}
render()
{
const { option } = this.props;
const { value } = this.state;
return (
)
}
}
class PeriodSelector extends React.Component
{
constructor(props)
{
super(props);
this.state = {
value: null,
periods: [],
}
}
componentDidMount()
{
const { option, onOption } = this.props;
const periods = [];
if(option.min !== undefined && option.min !== null)
{
for(let i = parseInt(option.min, 10); i <= parseInt(option.max, 10); i++)
{
periods.push(i);
}
this.setState({
value: periods[0], periods
}, () =>
{
onOption(this.state.value);
});
}
}
_handle_onChange = (event) =>
{
const { onOption } = this.props;
this.setState({ value: parseInt(event.target.value, 10) }, () =>
{
onOption(this.state.value);
});
}
render()
{
const { option } = this.props;
const { value, periods } = this.state;
if(value !== null)
{
return (
)
}
return null;
}
}
class SumSelector extends React.Component
{
constructor(props)
{
super(props);
this.state = {
value: "",
min: null,
max: null,
help: false,
}
}
componentDidMount()
{
const { option, onOption } = this.props;
this.setState({
value: option.value !== null ? option.value : option.min,
min: option.min,
max: option.max,
}, () =>
{
onOption(this.state.value, false);
});
}
_handle_onChange = (event) =>
{
const { onOption } = this.props;
const { min, max } = this.state;
const value = parseFloat(event.target.value.replace(',','.').replace(/[^\d,.]/mg, ''));
if(value >= min && value <= max && !isNaN(value))
{
this.setState({ value: value }, () =>
{
onOption(this.state.value, false);
});
}
else
{
onOption(this.state.value, true);
}
}
_handle_onKeyDown = (event) =>
{
if(event.keyCode === 13)
{
event.preventDefault();
this.props.onSubmit(event);
}
}
_handle_onMin = () =>
{
const { onOption } = this.props;
const { min } = this.state;
const value = parseFloat(min).toFixed(2);
this.setState({ value }, () =>
{
onOption(value, false);
});
}
_handle_onMax = () =>
{
const { onOption } = this.props;
const { max } = this.state;
const value = parseFloat(max).toFixed(2);
this.setState({ value }, () =>
{
onOption(value, false);
});
}
_handle_onMobileHelp = () =>
{
if(window.innerWidth < 768)
{
this.setState({ help: this.state.help ? false : true });
}
}
render()
{
const { option } = this.props;
const { value, min, max, help } = this.state;
return (
от { numeral(min).format(' ., ') } ₽
до { numeral(max).format(' ., ') } ₽
{ option.information !== undefined && option.information !== null && (
this._handle_onMobileHelp() }>
this._handle_onMobileHelp() }>Закрыть
) }
)
}
}
class InsurancePriceSelector extends React.Component
{
constructor(props)
{
super(props);
this.state = {
value: "",
min: null,
max: null,
help: false,
}
}
componentDidMount()
{
const { option, onOption } = this.props;
this.setState({
value: option.min,
min: option.min,
max: option.max,
}, () =>
{
onOption(this.state.value, false);
});
}
_handle_onChange = (event) =>
{
const { option, onOption } = this.props;
const { min, max } = this.state;
//if(option.disable)
//{
// //console.log("////", option.disable);
// this.setState({ value: this.state.value });
//}
//else
//{
const value = parseFloat(event.target.value.replace(',','.').replace(/[^\d,.]/mg, ''));
if(value >= min && value <= max && !isNaN(value))
{
this.setState({ value: value }, () =>
{
onOption(this.state.value, false);
});
}
else
{
onOption(this.state.value, true);
}
//}
}
_handle_onKeyDown = (event) =>
{
if(event.keyCode === 13)
{
event.preventDefault();
this.props.onSubmit(event);
}
}
_handle_onMin = () =>
{
const { min } = this.state;
this.setState({ value: min });
}
_handle_onMax = () =>
{
const { max } = this.state;
this.setState({ value: max });
}
_handle_onMobileHelp = () =>
{
if(window.innerWidth < 768)
{
this.setState({ help: this.state.help ? false : true });
}
}
render()
{
const { option } = this.props;
const { value, min, max, help } = this.state;
return (
{ option.disable ? (
<>{ numeral(value).format(' ., ') } ₽>
) : (
<>
от { numeral(min).format(' ., ') } ₽
до { numeral(max).format(' ., ') } ₽
>
) }
{ option.information !== undefined && option.information !== null && (
this._handle_onMobileHelp() }>
this._handle_onMobileHelp() }>Закрыть
) }
)
}
}
class InsuranceDateFromSelector extends React.Component
{
constructor(props)
{
super(props);
this.state = {
value: null,
min: null,
max: null,
}
}
componentDidMount()
{
const { option, onOption } = this.props;
this.setState({
value: moment(option.value !== null ? option.value : option.from).toDate(),
min: moment(option.from).toDate(),
max: moment(option.to).toDate(),
}, () =>
{
onOption(this.state.value);
});
}
_handle_onChange = (value) =>
{
const { onOption } = this.props;
this.setState({ value }, () =>
{
onOption(value);
});
}
render()
{
const { option } = this.props;
const { value, min, max } = this.state;
return (
)
}
}
export default class Options extends React.Component
{
constructor(props)
{
super(props);
this.state = {
loading: true,
sending: false,
date: null,
car: null,
contract_date: null,
agreement: null,
rules: null,
number_paydate: null,
period_new: null,
fix_last_payment_available: false,
date_offset_type: null,
sum: null,
insurance_price_result: null,
datefrom: null,
errors: {},
};
}
componentDidMount()
{
const { dispatch, number, variants } = this.props;
const varianst_for_options = {};
for(let i in variants.types)
{
if(variants.types[i].value)
{
varianst_for_options[variants.types[i].name] = true;
}
}
getContractGraphicChangeOptions({ dispatch, number, variants: varianst_for_options })
.then(() =>
{
this.setState({ loading: false });
})
.catch(() =>
{
});
}
_handle_onBack = (event) =>
{
event.preventDefault();
this.props.onVariants();
}
_handle_onCalculate = (event) =>
{
event.preventDefault();
event.stopPropagation();
const { sending, number_paydate, period_new, fix_last_payment_available, date_offset_type, sum, insurance_price_result, datefrom, } = this.state;
const { number, options, variants, onCalculate } = this.props;
if(!sending)
{
this.setState({ sending: true, }, () =>
{
//console.log("_handle_onCalculate ?");
const selected = {
number_planpayment: options.number_planpayment.value,
};
if(number_paydate !== null) { selected.number_paydate = moment(number_paydate).format("YYYY-MM-DD"); }
if(period_new !== null) { selected.period_new = period_new; }
if(fix_last_payment_available !== null) { selected.fix_last_payment_available = fix_last_payment_available; }
if(date_offset_type !== null) { selected.date_offset_type = date_offset_type; }
if(sum !== null) { selected.sum = sum; }
if(insurance_price_result !== null) { selected.insurance_price_result = insurance_price_result; }
//if(early_redemption_change !== null) { selected.early_redemption_change = early_redemption_change; }
if(datefrom !== null) { selected.datefrom = moment(datefrom).format("YYYY-MM-DD"); }
const v = {};
for(let i in variants.types) { v[ variants.types[i].name ] = variants.types[i].value; }
const payload = {
...{ contract_number: number },
...v,
...selected
};
getContractGraphicChangeCalculate(payload)
.then((calculation) =>
{
onCalculate(calculation.addcontract_number);
})
.catch(() =>
{
this.setState({ sending: false, }, () =>
{
alert("К сожалению, при расчёте возникла ошибка.\n\nДля расчёта вариантов изменения графика обратитесь, пожалуйста, в Службу клиентского сервиса по адресу электронной почты client@evoleasing.ru или по телефону Горячей линии 8 800 333 75 75.");
});
});
});
}
}
_handle_onPaymentDateChange = (date, error) =>
{
const { errors } = this.state;
if(error) { errors['paydate'] = true; } else { delete errors['paydate']; }
this.setState({ number_paydate: date, errors });
}
_handle_onFixLastPaymentChange = (value) =>
{
this.setState({ fix_last_payment_available: value });
}
_handle_onDateOffsetTypeChange = (value) =>
{
this.setState({ date_offset_type: value });
}
_handle_onPeriodChange = (value) =>
{
this.setState({ period_new: value });
}
_handle_onSumChange = (value, error) =>
{
const { errors } = this.state;
if(error) { errors['sum'] = true; } else { delete errors['sum']; }
this.setState({ sum: value, errors });
}
_handle_onInsurancePriceChange = (value, error) =>
{
const { errors } = this.state;
if(error) { errors['insurance_price_result'] = true; } else { delete errors['insurance_price_result']; }
this.setState({ insurance_price_result: value, errors });
}
_handle_onInsuranceDateFromChange = (date) =>
{
this.setState({ datefrom: date });
}
_checkAllowCalculate = () =>
{
const { errors } = this.state;
for(let i in errors)
{
if(errors[i])
{
return false;
}
}
return true;
}
render()
{
const { loading, sending, } = this.state;
const { variants, options } = this.props;
/*this._checkAllowCalculate() ? this._handle_onCalculate : {}*/
return (
{ options !== undefined && options !== null && options.number_planpayment_header.value }
Параметры опций изменений графика платежей
{ loading ? (
) : (
) }
);
}
}