import React, { Component } from "react";
import MyButton from "../../components/MyButton/index.jsx";
import Spin from 'antd/lib/spin';
import notification from 'antd/lib/notification';
import "./index.less";
import TableUsers from "../../components/TableUsers/index.jsx";
import UserCalendar from "../../components/UserCalendar/index.jsx";

export default class DashboardContainer extends Component {
    constructor(props) {
        super(props);
        this.state = {
            counter: 0,
            title: 'Bom Dia!',
            loading: false,
            user: null
        };
        this.button = React.createRef();
        this.click = this.click.bind(this);
        this.openCalendar = this.openCalendar.bind(this);
        this.closeCalendar = this.closeCalendar.bind(this);
    }

    
      

    componentWillMount() {
        console.log("will mount")
    }

    componentDidMount() {
        console.log("did mount")
    }

    componentWillUpdate(prevProps, prevState, snapshot) {
        console.log("will update");
    }

    componentDidUpdate(prevProps, prevState, snapshot) {
        console.log("update");
        $(this.button.current).fadeOut(250).fadeIn(250);
    }

  
    click() {
        this.setState({ counter: this.state.counter + 1 });
    }

    openCalendar(user) {
        this.setState({ user });
    }
    
    closeCalendar() {
        this.setState({user: null});
    }

    render() {
        console.log("render");
        const { counter, title, loading, user } = this.state;
        if (loading) {
            return <Spin/>;
        }

        if (user) {
            return <>
            <UserCalendar 
            user={user} 
            closeCalendar={() => this.closeCalendar()}>
            </UserCalendar>
            </>;
        }
        return (
            <div className="my-dashboard">
                
                <h1>Table Users</h1>
                <TableUsers onView={ this.openCalendar } />

                <h1>Table...</h1>
                <div className="my-dashboard__button">
                    <MyButton
                        mainRef={ this.button }
                        title={ title }
                        text={ `ReactJS ⚡ Ant.Design 👉 Click me! ${ counter }` }
                        click={ this.click }
                    />
                </div>
            </div>
        );
    }
}
