// react
import React from 'react';
// third-party
import classNames from 'classnames';
// application
import { useAuth } from '~/contexts/AuthContext';

interface Props {
  onToggleSidebar: () => void;
}

function Header({ onToggleSidebar }: Props) {
  const { admin, logout } = useAuth();
  const [showMenu, setShowMenu] = React.useState(false);

  const handleLogout = async () => {
    setShowMenu(false);
    await logout();
  };

  return (
    <header className="admin-header">
      <div className="admin-header__left">
        
      </div>
      <div className="admin-header__right">
        <div className="admin-header__user" style={{ position: 'relative' }}>
          <button
            type="button"
            className="btn btn-link p-0 admin-header__user-button"
            onClick={() => setShowMenu(!showMenu)}
            style={{ border: 'none', background: 'none' }}
          >
            <i className="fas fa-user-circle mr-2" />
            <span>{admin?.nombre || 'Administrador'}</span>
            <i className="fas fa-chevron-down ml-2" style={{ fontSize: '0.8em' }} />
          </button>
          {showMenu && (
            <div
              className="dropdown-menu show"
              style={{
                position: 'absolute',
                right: 0,
                top: '100%',
                marginTop: '0.5rem',
                minWidth: '200px',
                zIndex: 1000,
              }}
            >
              <div className="dropdown-item-text">
                <small className="text-muted">Email:</small>
                <div>{admin?.email}</div>
              </div>
              <div className="dropdown-divider" />
              <button
                className="dropdown-item"
                onClick={handleLogout}
                type="button"
              >
                <i className="fas fa-sign-out-alt mr-2" />
                Cerrar Sesión
              </button>
            </div>
          )}
        </div>
      </div>
      {showMenu && (
        <div
          style={{
            position: 'fixed',
            top: 0,
            left: 0,
            right: 0,
            bottom: 0,
            zIndex: 999,
          }}
          onClick={() => setShowMenu(false)}
        />
      )}
    </header>
  );
}

export default Header;

