import React from "react";
import Head from 'next/head';
import Image from 'next/image';
import { connect } from "react-redux";
import { withRouter } from 'next/router';
import moment from "moment";
import { SpinnerCircular } from 'spinners-react';

import { reduxWrapper } from '../../store';

import Header from '../components/Header';
import Footer from '../components/Footer';
import Company from "../components/Company";
import InnerMenu from "./components/InnerMenu";
import DownloadPdfButton from "../components/DownloadPdfButton";

import { getContractInfo, getContractMaterials } from "../../actions";
import AccountLayout from "../components/Layout/Account";
import ContractHeader from "./components/ContractHeader";

class ContractPage extends React.Component 
{
	constructor(props)
	{
		super(props);
		this.state = {
			loading: false, 
			contracts_info: {}, 
			materials: null, 
		}
	}

	static getDerivedStateFromProps(nextProps, prevState)
	{
		return {
			contracts_info: nextProps.contracts_info, 
			materials: nextProps.materials, 
		};
	}

	componentDidMount()
	{
		if(!this.state.loading && this.props.number !== undefined)
		{
			this.setState({ loading: true }, () => 
			{
				getContractInfo({ dispatch: this.props.dispatch, number: this.props.number }).then((info) => 
				{
					console.log("info", info);

					getContractMaterials({
						dispatch: this.props.dispatch, 
					}).then(() => {
						this.setState({ loading: false })
					}).catch(() => {});
				})
				.catch(() => 
				{

				});
			});	
		}
	}

	render()
	{
		const { loading, contracts_info, materials, } = this.state;
		const { number } = this.props;

		let { date, car, status } = contracts_info[ number ] !== undefined ? contracts_info[ number ] : {};

		return (
			<React.Fragment>
				<Head>
					<title>ЛК Эволюция автолизинга</title>
					<meta
						name="description"
						content="ЛК Эволюция автолизинга"
					/>
				</Head>
				<Header { ...this.props }/>
				<AccountLayout>
					<div className="title_wrapper">
						<div className="left" style={{ flexDirection: 'column', }}>
							<ContractHeader number={ number } date={ date } car={ car }/>
						</div>
						<Company { ...this.props }/>
					</div>
					<div className="aside_container about">
						<InnerMenu number={ number } status={ status } { ...this.props }/>
						<article>
							{ loading ? (
								<div className="table_row table_header" style={{ minHeight: 300, display: "flex", justifyContent: "center", alignItems: "center" }}>
									<SpinnerCircular size={90} thickness={51} speed={100} color="rgba(28, 1, 169, 1)" secondaryColor="rgba(236, 239, 244, 1)" />
								</div>
							) : (
								<div className="dosc_list">
									{ materials !== undefined && materials !== null && materials.map((document, index ) => (
										<div className="row" key={ index }>
											<p className="doc_name i-pdf">
												{ document.name }
												{ document.description !== null && document.description !== "" && (
													<span style={{ width: "100%"}}>{ document.description }</span>
												) }
											</p>
											<DownloadPdfButton url={ `${ process.env.NEXT_PUBLIC_MAIN_SITE }${ document.url }` } filename={ `${ document.filename }.pdf` } bitrix={ true } />
										</div>
									)) }
								</div>
							) }
						</article>
					</div>
				</AccountLayout>
				<Footer/>
			</React.Fragment>
		);
	}
}

function mapStateToProps(state, ownProps)
{
	return {
		contracts_info: state.contracts_info, 
		materials: state.contract.materials, 
	}
}

export const getServerSideProps = reduxWrapper.getServerSideProps(
(store) => 
	async ({ req, res, query }) => 
	{
		return { 
			props: {
				number: query.number, 
			}
		}
    }
);

export default withRouter(connect(mapStateToProps)(ContractPage));