<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}
$myaccount_link = wp_login_url();
$current_user   = wp_get_current_user();
if ( class_exists( 'WooCommerce' ) ) {
	$myaccount_link = get_permalink( get_option( 'woocommerce_myaccount_page_id' ) );
}
$user_classes = array( 'menu-item', 'block-user', 'block-woo' );
if ( is_user_logged_in() ) {
	$user_classes[] = 'draly-dropdown';
}
?>
<div class="<?php echo esc_attr( implode( ' ', $user_classes ) ); ?>">
    <a data-draly="draly-dropdown" class="block-link"
       href="<?php echo esc_url( $myaccount_link ); ?>"
       aria-label="<?php echo esc_attr__( 'User', 'draly-dropshipping-starter' ); ?>">
        <span class="block-icon">
            <svg width="30" height="30" viewBox="0 0 30 30" fill="none" xmlns="http://www.w3.org/2000/svg">
            <path d="M20.7778 7.77778C20.7778 9.31014 20.1691 10.7797 19.0855 11.8633C18.002 12.9468 16.5324 13.5556 15 13.5556C13.4676 13.5556 11.998 12.9468 10.9145 11.8633C9.83096 10.7797 9.22223 9.31014 9.22223 7.77778C9.22223 6.24542 9.83096 4.77582 10.9145 3.69227C11.998 2.60873 13.4676 2 15 2C16.5324 2 18.002 2.60873 19.0855 3.69227C20.1691 4.77582 20.7778 6.24542 20.7778 7.77778Z"
                  stroke="currentColor" stroke-width="1.5"/>
            <path d="M17.8889 17.8889H12.1111C10.1957 17.8889 8.35869 18.6498 7.00426 20.0043C5.64983 21.3587 4.88892 23.1957 4.88892 25.1111C4.88892 25.8773 5.19328 26.6121 5.73505 27.1539C6.27682 27.6957 7.01162 28 7.7778 28H22.2222C22.9884 28 23.7232 27.6957 24.265 27.1539C24.8068 26.6121 25.1111 25.8773 25.1111 25.1111C25.1111 23.1957 24.3502 21.3587 22.9958 20.0043C21.6414 18.6498 19.8044 17.8889 17.8889 17.8889Z"
                  stroke="currentColor" stroke-width="1.5" stroke-linejoin="round"/>
            </svg>
        </span>
        <span class="block-text">
            <?php if ( is_user_logged_in() ): ?>
                <span><?php echo esc_html__( 'My Account', 'draly-dropshipping-starter' ) ?></span>
                <strong><?php echo esc_html( $current_user->display_name ); ?></strong>
            <?php else: ?>
                <span><?php echo esc_html__( 'Welcome', 'draly-dropshipping-starter' ) ?></span>
                <strong><?php echo esc_html__( 'Login', 'draly-dropshipping-starter' ) ?></strong>
            <?php endif; ?>
        </span>
    </a>
	<?php if ( is_user_logged_in() ): ?>
		<?php if ( function_exists( 'wc_get_account_menu_items' ) ): ?>
            <ul class="sub-menu">
				<?php foreach ( wc_get_account_menu_items() as $endpoint => $label ) : ?>
                    <li class="menu-item <?php echo esc_attr( wc_get_account_menu_item_classes( $endpoint ) ); ?>">
                        <a href="<?php echo esc_url( wc_get_account_endpoint_url( $endpoint ) ); ?>"><?php echo esc_html( $label ); ?></a>
                    </li>
				<?php endforeach; ?>
            </ul>
		<?php else: ?>
            <ul class="sub-menu">
                <li class="menu-item">
                    <a href="<?php echo esc_url( wp_logout_url( get_permalink() ) ); ?>"><?php esc_html_e( 'Logout', 'draly-dropshipping-starter' ); ?></a>
                </li>
            </ul>
		<?php endif; ?>
	<?php endif; ?>
</div>