<?php get_header();
$current_endpoint = WC()->query->get_current_endpoint();
$cartItemsCount = WC()->cart->get_cart_contents_count();


$completed_orders_count = wc_get_orders_count('completed');
$processing_orders_count = wc_get_orders_count('processing');
$onHold_orders_count = wc_get_orders_count('on-hold');
?>
    <main>
        <?php if (have_posts()): ?><?php while (have_posts()): the_post(); ?>

            <div class="mt-4 mb-5">
                <div class="custom-container">
                    <!-- breadcrumb -->
                    <?php banta_breadcrumb(); ?>

                    <!-- nav -->
                    <?php if(is_user_logged_in()):?>
                    <div class="profile-nav">

                        <!-- avatar -->
                        <div class="profile-user">

                            <?php
                            $display_name = __('Guest User',TEXT_DOMAIN);
                            $joinDate = "--";
                            echo get_user_custom_avatar(get_current_user_id(), 150);
                            if(is_user_logged_in()){
                                $current_user = wp_get_current_user();
                                $user_id = get_current_user_id();
                                $display_name = $current_user->display_name;
                                $joinDate = get_joined_date($user_id);
                            }?>

                            <div class="d-flex flex-column align-items-center align-items-lg-start flex-shrink-0">
                                <span class="user-id"><?php echo $display_name;?></span>
                                <span class="user-joined-date"><?php echo $joinDate;?></span>
                            </div>
                        </div>

                        <!-- nav items -->
                        <div class="profile-nav-items">
                            <?php foreach (wc_get_account_menu_items() as $endpoint => $label) :
                                $linkUrl = esc_url(wc_get_account_endpoint_url($endpoint)); ?>

                                <a href="<?php echo $linkUrl; ?>"
                                   class="profile-nav-item <?php echo $endpoint == 'customer-logout' ? 'logout' : ''; ?> <?php echo wc_is_current_account_menu_item($endpoint) ? 'active' : ''; ?>">
                                    <?php switch ($endpoint) {

                                        case "dashboard":
                                            echo "<span class='profile-nav-item-icon iconlyBulk-Discovery'><span class='path1'></span><span class='path2'></span></span>";
                                            break;
                                        case "orders":
                                            echo "<span class='profile-nav-item-icon iconlyBulk-Document'><span class='path1'></span><span class='path2'></span></span>";
                                            break;
                                        case "downloads":
                                            echo "<span class='profile-nav-item-icon iconlyBulk-Download'><span class='path1'></span><span class='path2'></span></span>";
                                            break;
                                        case "edit-address":
                                            echo "<span class='profile-nav-item-icon iconlyBulk-Location'><span class='path1'></span><span class='path2'></span></span>";
                                            break;
                                        case "wishlist":
                                            echo "<span class='profile-nav-item-icon iconlyBulk-Heart'><span class='path1'></span><span class='path2'></span><span class='path3'></span></span>";
                                            break;
                                        case "edit-account":
                                            echo "<span class='profile-nav-item-icon iconlyBulk-Profile'><span class='path1'></span><span class='path2'></span></span>";
                                            break;
                                        case "customer-logout":
                                            echo "<span class='profile-nav-item-icon iconlyBulk-Logout'><span class='path1'></span><span class='path2'></span></span>";
                                            break;
                                    } ?>
                                    <span class="title"><?php echo esc_html($label); ?></span>
                                </a>
                                <?php if ($endpoint == 'edit-account'): ?>
                                    <div class="vertical-line d-block d-lg-none"></div>
                                <?php elseif ($endpoint != 'customer-logout'): ?>
                                    <div class="vertical-line"></div>
                                <?php endif; ?>
                            <?php endforeach; ?>


                        </div>

                    </div>
                    <?php endif;?>


                    <!-- content -->
                    <div class="profile-content <?php echo is_account_page() && !is_user_logged_in() ? 'woocommerce-login-form' : '';?>">

                        <?php if (is_account_page() && !is_user_logged_in()):?>
                            <div class="custom-login-container u-columns col2-set" id="customer_login">
                                <div class="otp-form-container">
                                    <div class="custom-otp-form" id="custom-otp-login">
                                        <?php
                                        $logo = get_option(TEXT_DOMAIN . '_otp_form_logo');
                                        if (!empty($logo)) { ?>
                                            <a href="<?php bloginfo('url'); ?>" class="otp-form-logo">
                                                <img src="<?php echo $logo ?>" class="" alt="<?php bloginfo('title'); ?>"></a>
                                        <?php } ?>

                                        <?php the_content(); ?>

                                    </div>
                                </div>
                            </div>
                        <?php else:?>

                            <!-- alerts -->
                            <?php if(is_user_logged_in()):?>
                                <?php
                                $args = array('post_type' => 'alerts', 'posts_per_page' => -1,);
                                $alerts = new WP_Query($args);

                                if ($alerts->have_posts()) :?>
                                    <div class="mb-5">
                                        <?php while ($alerts->have_posts()) : $alerts->the_post();
                                            $alertType = get_post_meta( get_the_ID(), TEXT_DOMAIN . '_type', true );?>
                                            <div class="alert alert-<?php echo $alertType;?> alert-dismissible fade show">
                                                <?php the_content(); ?>
                                                <button type="button" class="btn-close" data-bs-dismiss="alert"
                                                        aria-label="Close"></button>
                                            </div>

                                        <?php endwhile;?>
                                    </div>
                                <?php endif;
                                wp_reset_postdata();
                                wp_reset_query();
                                ?>
                            <?php endif;?>


                            <!-- dashboard boxes -->
                            <?php if (is_user_logged_in() && ($current_endpoint === 'dashboard' || $current_endpoint === '') ):?>
                                <div class="row mb-5">
                                    <div class="col-lg-3 col-md-6 mb-3">
                                        <div class="profile-box">
                                            <i class="icon-shopping-cart"></i>
                                            <span class="count"><?php echo $cartItemsCount;?></span>
                                            <span class="title"><?php _e('Shopping cart',TEXT_DOMAIN)?></span>
                                        </div>
                                    </div>
                                    <div class="col-lg-3 col-md-6 mb-3">
                                        <div class="profile-box">
                                            <i class="icon-check-circle"></i>
                                            <span class="count"><?php echo $completed_orders_count;?></span>
                                            <span class="title"><?php _e('Completed orders',TEXT_DOMAIN)?></span>
                                        </div>
                                    </div>
                                    <div class="col-lg-3 col-md-6 mb-3">
                                        <div class="profile-box">
                                            <i class="icon-clock"></i>
                                            <span class="count"><?php echo $processing_orders_count;?></span>
                                            <span class="title"><?php _e('Orders being processed',TEXT_DOMAIN)?></span>
                                        </div>
                                    </div>
                                    <div class="col-lg-3 col-md-6 mb-3">
                                        <div class="profile-box">
                                            <i class="icon-file-text"></i>
                                            <span class="count"><?php echo $onHold_orders_count;?></span>
                                            <span class="title"><?php _e('Orders under review',TEXT_DOMAIN)?></span>
                                        </div>
                                    </div>
                                </div>
                            <?php endif;?>

                            <div class="html-content">
                                <?php the_content(); ?>
                            </div>

                        <?php endif;?>

                    </div>


                </div>
            </div>

        <?php endwhile; ?>
        <?php endif; ?>
    </main>
<?php get_footer(); ?>