import React from "react"
import { connect } from "react-redux"
import DealsList from "./DealsList";
import SingleDeal from "./SingleDeal";
import { getDeals, getDealOffers, getDealDocuments, getDealContracts } from "../../actions";

class DealsStatus extends React.Component 
{
	constructor(props) 
	{
		super(props)
		this.state = {
			status: 2, 
			currentContractModalOpened: false, 
			allContractModalOpened: false, 
			currentSelected: null, 
			dealSelected: undefined, 
			deals: undefined, 
			lastDealNumber: 0, 
		}
	}

	static getDerivedStateFromProps(nextProps, prevState) 
	{
		return {
			deals: nextProps.deals, 
		}
	}

	componentDidMount() 
	{
		const { dispatch } = this.props;
		getDeals({ dispatch });
	}

	componentDidUpdate(prevProps, prevState)
	{
		if(prevState.deals.list === null && this.state.deals.list !== null)
		{
			let lastDealNumber = 0;
			for(let i in this.state.deals.list)
			{
				console.log("DealsStatus", this.state.deals.list[i]);
			}
		}
	}

	_onDealsUpdate = () => 
	{
		return new Promise((resolve) => 
		{
			const { dispatch } = this.props;

			getDeals({ dispatch, update: true })
			.then(() => 
			{
				resolve();
			})
			.catch(() => 
			{
				resolve();
			});
		});
	}

	_onDealContractsUpdate = (deal_id) => 
	{
		const { dispatch } = this.props;
		getDealContracts({ dispatch, deal_id, });
	}

	_handleModalToggle = (modal) => 
	{
		if (modal === "current") 
		{
			this.setState({
				currentContractModalOpened: !this.state.currentContractModalOpened
			})
		} 
		else 
		{
			this.setState({
				allContractModalOpened: !this.state.allContractModalOpened
			})
		}
	}

	_handleContractSelected = (index) => 
	{
		this.setState({
			currentSelected: index
		})
	}

	_handle_onSelectDeal = (deal_id) => 
	{
		const { dispatch } = this.props;

		this.setState({ dealSelected: deal_id });

		getDealOffers({ dispatch, deal_id, });
		getDealDocuments({ dispatch, deal_id, });
		getDealContracts({ dispatch, deal_id, });
	}

	_handle_onCloseDeal = () => 
	{
		this.setState({ dealSelected: undefined });
	}

	render() 
	{
		const { currentContractModalOpened, allContractModalOpened, currentSelected, dealSelected, deals, status, } = this.state
		const { questionnaire_status, onQuestionnaire, } = this.props;

		console.log("render", { deals });

		return (
			<>
				{ deals.list !== undefined && deals.list !== null && deals.list.length > 0 && (
					<>
						<div>
							<p className="deals_list_title">Статусы сделок</p>
						</div>
						<DealsList 
							dispatch={ this.props.dispatch } 
							router={ this.props.router } 
							status={ status } 
							deals={ deals } 
							questionnaire_status={ questionnaire_status } 
							dealSelected={ dealSelected } 
							onSelectDeal={ this._handle_onSelectDeal } 
							onCloseDeal={ this._handle_onCloseDeal } 
							onDealsUpdate={ this._onDealsUpdate } 
							onDealContractsUpdate={ this._onDealContractsUpdate } 
							onQuestionnaire={ onQuestionnaire } 
						/>
					</>
				) }
			</>
		)
	}
}

function mapStateToProps(state, ownProps) 
{
	return {
		deals: state.deals, 
	}
}

export default connect(mapStateToProps)(DealsStatus)
