
import React from 'react';
import { Prompt } from 'react-router'
import { Nav } from 'react-bootstrap'

import AuthServices from '../../Services/AuthServices'

import BaseControl from '../BaseControl.jsx'
import UserListControl from '../Controls/Admin/UserListControl.jsx'
import GroupListControl from '../Controls/Admin/GroupListControl.jsx'


export default class AdministratorPage extends BaseControl {

    constructor(props) {
        super(props, "AdministratorPage");

        this.authServices = new AuthServices();
        //ActiveTab : 0 Users, 1 Groups
        this.state = {
            IsAuth: this.authServices.IsAuth(),
            IsAdmin: this.GlobalState.User != undefined
                ? this.GlobalState.User.IsAdmin
                : false,
            HaveChanges: false,
            ActiveTab: 0
        };        

        this.EventRegister.RegisterListener("OnAuthChange", this);
        this.EventRegister.RegisterListener("OnHaveDataChange", this);
    }

    Event_OnAuthChange() {
        this.setState({
            IsAuth: this.authServices.IsAuth(),
            IsAdmin: this.GlobalState.User.IsAdmin
        });
    }

    Event_OnHaveDataChange(HaveChanges) {
        this.setState({ HaveChanges: HaveChanges });
    }

    OnUsersClick() {
        if (this.state.ActiveTab == 0)
            return;

        //Несохраненные изменения
        if (this.state.HaveChanges) 
            if (!confirm("You have unsaved changes. Are you sure you want to leave this table?"))
                return;       

        this.setState({ ActiveTab: 0 });
    }
    OnGroupsClick() {
        if (this.state.ActiveTab == 1)
            return;

        //Несохраненные изменения
        if (this.state.HaveChanges)
            if (!confirm("You have unsaved changes. Are you sure you want to leave this table?"))
                return;  

        this.setState({ ActiveTab: 1 });
    }


    render() {
        return (
            <div>
                {/*Несохраненные изменения*/}
                <Prompt
                    when={this.state.HaveChanges}
                    message="You have unsaved changes. Are you sure you want to leave this page?"
                />

                {this.state.IsAdmin
                    ?
                    <div>
                        <Nav variant="tabs">
                            <Nav.Item>
                                <Nav.Link onClick={this.OnUsersClick}>Users</Nav.Link>
                            </Nav.Item>
                            <Nav.Item>
                                <Nav.Link onClick={this.OnGroupsClick}> Groups</Nav.Link>
                            </Nav.Item>
                        </Nav>

                        {this.state.ActiveTab == 0
                            ?   <UserListControl ref="UserListControl" />                            
                            :   <GroupListControl ref="GroupListControl" />                            
                        }
                    </div>
                    : <p>Вы не являетесь администратором</p>
                }

            </div>
        );
    }
}

