<?php require KAVEHBOARD_PATH . 'template-part/header.php'; ?>
<?php
// Get current user information
$current_user = wp_get_current_user();

// Get user's first and last name
$first_name = $current_user->first_name;
$last_name = $current_user->last_name;

// If first name is empty, use display name instead
if (empty($first_name)) {
    $first_name = $current_user->display_name;
    $last_name = '';
}

// Get user's avatar URL
$avatar_url = kavehboard_get_user_avatar_url($current_user->ID);

// Get user's order statistics if WooCommerce is active
$order_stats = array(
    'processing' => 0,
    'completed' => 0,
    'refunded' => 0,
    'cancelled' => 0,
    'total' => 0
);

if (function_exists('wc_get_orders') && is_user_logged_in()) {
    $user_id = get_current_user_id();

    // Get processing orders
    $processing_orders = wc_get_orders(array(
        'customer' => $user_id,
        'status' => array('wc-processing', 'wc-pending', 'wc-on-hold'),
        'limit' => -1
    ));
    $order_stats['processing'] = count($processing_orders);

    // Get completed orders
    $completed_orders = wc_get_orders(array(
        'customer' => $user_id,
        'status' => 'wc-completed',
        'limit' => -1
    ));
    $order_stats['completed'] = count($completed_orders);

    // Get refunded orders
    $refunded_orders = wc_get_orders(array(
        'customer' => $user_id,
        'status' => 'wc-refunded',
        'limit' => -1
    ));
    $order_stats['refunded'] = count($refunded_orders);

    // Get cancelled orders
    $cancelled_orders = wc_get_orders(array(
        'customer' => $user_id,
        'status' => array('wc-cancelled', 'wc-failed'),
        'limit' => -1
    ));
    $order_stats['cancelled'] = count($cancelled_orders);

    // Calculate total orders
    $order_stats['total'] = $order_stats['processing'] + $order_stats['completed'] + $order_stats['refunded'] + $order_stats['cancelled'];
}

// Get dashboard header text from admin options
$kavehboard_options = get_option('kavehboard_options');
$header_text = $kavehboard_options['kavehboard_header_text'][0] ?? array();
$page_name = $header_text['page_name'] ?? __('Dashboard', 'kavehTheme');
$description_text = $header_text['description'] ?? __('This is a message that can write whatever you want 👋', 'kavehTheme');
?>

<div class="dashboard-kaveh">
    <!-- Start Sidebar -->
    <aside class="dashboard-kaveh-sidebar position-fixed top-0 start-0 w-100 h-100">
        <div class="dashboard-kaveh-sidebar-backdrop position-fixed top-0 end-0 w-100 h-100 d-xl-none"></div>
        <button type="button" class="dashboard-kaveh-sidebar-toggle d-flex align-items-center justify-content-center rounded-circle position-absolute end-0">
            <i class="icon-angle-right-5 lh-1"></i>
        </button>
        <div class="dashboard-kaveh-sidebar-content position-absolute top-0 start-0 w-100 h-100 d-flex flex-column scrollbar">
            <div class="bg-sm position-absolute top-0 end-0 w-100 h-100 d-xl-none">
                <span></span>
            </div>
            <!-- Start Logo -->
            <a href="<?= home_url(); ?>" class="dashboard-kaveh-sidebar-content-logo d-none d-xl-flex align-items-center mb-5">
                <span class="image position-relative flex-grow-0 flex-shrink-0 flex-fill">
                    <?php 
                    $kavehboard_options = get_option('kavehboard_options', array());
                    $logo_url = !empty($kavehboard_options['kavehboard_logo']['url']) ? $kavehboard_options['kavehboard_logo']['url'] : KAVEHBOARD_URL . '/assets/images/dashboard/logo.svg';
                    ?>
                    <img src="<?= esc_url($logo_url) ?>" alt="logo" width="48" height="48">
                    <img src="<?= esc_url($logo_url) ?>" alt="logo" width="48" height="48" class="position-absolute w-100 h-100">
                </span>
            </a>
            <!-- End Logo -->
            <!-- Start Info User -->
            <div class="dashboard-kaveh-sidebar-content-info-user d-flex align-items-center justify-content-between gap-2 position-relative">
                <div class="d-flex align-items-center gap-2">
                    <div class="image rounded-circle d-none d-xl-flex align-items-center justify-content-center flex-fill flex-grow-0 flex-shrink-0">
                        <span class="rounded-circle position-relative d-flex align-items-center justify-content-center">
                            <img src="<?php echo esc_url(kavehboard_get_user_avatar_url($current_user->ID, 32)); ?>" alt="avatar" width="32" height="32" class="d-block rounded-circle">
                        </span>
                    </div>
                    <div class="detail text-truncate">
                        <div class="name fw-bold text-white text-truncate"><?= $first_name . ' ' . $last_name; ?></div>
                        <div class="sub-name fw-normal text-truncate"><?php _e('Welcome 👋', 'kavehTheme'); ?></div>
                    </div>
                </div>
                <a href="<?php echo esc_url(wp_logout_url(home_url())); ?>" class="logout d-flex align-items-center justify-content-center">
                    <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="none" viewBox="0 0 20 20">
                        <path stroke="#FFD166" class="clr-1" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" stroke-width="1.5" d="m14.533 12.183 2.133-2.133-2.133-2.133m-6.4 2.133h8.475" />
                        <path stroke="#fff" class="clr-2" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" stroke-width="1.5" d="M9.8 16.667c-3.683 0-6.667-2.5-6.667-6.667S6.117 3.333 9.8 3.333" />
                    </svg>
                </a>
            </div>
            <!-- End Info User -->

            <div class="dashboard-kaveh-sidebar-content-titr position-relative text-truncate fw-normal mb-4 d-none d-xl-table"><?php _e('Main Menu', 'kavehTheme'); ?></div>
            <ul class="dashboard-kaveh-sidebar-content-menu d-flex flex-column">
                <?php
                // Get routing instance and menu items
                global $kavehboardrouting;
                if (!$kavehboardrouting) {
                    $kavehboardrouting = new kavehboardrouting();
                }
                $menu_items = $kavehboardrouting->get_dashboard_menu();
                $current_page = $kavehboardrouting->get_current_page();
                
                foreach ($menu_items as $slug => $menu_item):
                    $is_active = $kavehboardrouting->is_menu_active($slug);
                    $active_class = $is_active ? 'active' : '';
                    
                    // Special handling for orders menu to show badge
                    $show_badge = false;
                    $badge_count = '';
                    if ($slug === 'orders' || $slug === 'woo-orders' || (isset($menu_item['endpoint']) && $menu_item['endpoint'] === 'orders')) {
                        $show_badge = true;
                        $badge_count = $order_stats['total'];
                    }
                    
                    // Special handling for notifications menu to show badge
                    if ($slug === 'notifications' && isset($menu_item['badge_count']) && $menu_item['badge_count'] > 0) {
                        $show_badge = true;
                        $badge_count = $menu_item['badge_count'];
                    }
                    
                    // Special handling for logout menu item
                    $menu_class = '';
                    $icon_class = $menu_item['icon'];
                    if ($slug === 'logout') {
                        $menu_class = 'logout-menu-item';
                        // Use SVG icon for logout if icon class is icon-logout
                        if ($icon_class === 'icon-logout') {
                            $icon_class = 'logout-svg-icon';
                        }
                    }
                ?>
                <li>
                    <a href="<?php echo esc_url($menu_item['url']); ?>" class="<?php echo esc_attr($active_class . ' ' . $menu_class); ?> d-flex align-items-center gap-2 position-relative">
                        <span class="icon position-relative">
                            <?php if ($slug === 'logout' && $icon_class === 'logout-svg-icon'): ?>
                                <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="none" viewBox="0 0 20 20">
                                    <path stroke="#EE273A" class="clr-1" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" stroke-width="1.5" d="m14.533 12.183 2.133-2.133-2.133-2.133m-6.4 2.133h8.475" />
                                    <path stroke="#000" class="clr-2" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" stroke-width="1.5" d="M9.8 16.667c-3.683 0-6.667-2.5-6.667-6.667S6.117 3.333 9.8 3.333" />
                                </svg>
                            <?php else: ?>
                                <i class="<?php echo esc_attr($icon_class); ?> lh-1 fw-normal"></i>
                            <?php endif; ?>
                        </span>
                        <span class="title text-truncate"> <?php echo esc_html($menu_item['title']); ?> </span>
                        <?php if ($show_badge): ?>
                            <span class="badge badge-warning rounded-pill"> <?php echo esc_html($badge_count); ?> </span>
                        <?php endif; ?>
                        <i class="icon-arrow-left-9 arrow lh-1 ms-auto fw-normal"></i>
                    </a>
                </li>
                <?php endforeach; ?>
            </ul>
       
            <div class="mt-auto">
                <?php
                // Get admin options
                $kavehboard_options = get_option('kavehboard_options', array());
                
                // Safely get contact info with validation
                $contact_info = array();
                if (isset($kavehboard_options['kavehboard_contact_info']) && 
                    is_array($kavehboard_options['kavehboard_contact_info']) && 
                    !empty($kavehboard_options['kavehboard_contact_info'][0])) {
                    $contact_info = $kavehboard_options['kavehboard_contact_info'][0];
                }
                
                $copyright_text = isset($kavehboard_options['kavehboard_copyright_text']) ? $kavehboard_options['kavehboard_copyright_text'] : '';
                $social_media_title = isset($kavehboard_options['kavehboard_social_media_title']) ? $kavehboard_options['kavehboard_social_media_title'] : '';
                $social_media_links = isset($kavehboard_options['kavehboard_social_media_links']) && is_array($kavehboard_options['kavehboard_social_media_links']) ? $kavehboard_options['kavehboard_social_media_links'] : array();
                ?>
                
                <?php if (!empty($contact_info) && isset($contact_info['enable']) && $contact_info['enable'] === '1'): ?>
                <!-- Start Contact -->
                <div class="dashboard-kaveh-sidebar-content-contact d-flex align-items-center gap-3 gap-xxl-4 mb-xl-5">
                    <div class="icon d-flex align-items-center justify-content-center rounded-circle">
                        <i class="<?php echo esc_attr($contact_info['icon_class'] ?? 'icon-call-calling'); ?> lh-1 d-flex align-items-center justify-content-center rounded-circle"></i>
                    </div>
                    <div class="text d-flex align-items-xl-center fw-normal text-truncate flex-column flex-xl-row flex-grow-1">
                        <?php echo esc_html(isset($contact_info['title']) ? $contact_info['title'] : ''); ?>
                        <?php if (!empty($contact_info['phone'])): ?>
                        <a href="tel:<?php echo esc_attr($contact_info['phone']); ?>" class="f-iransans ms-auto ms-xl-0"> <?php echo esc_html($contact_info['phone']); ?> </a>
                        <?php endif; ?>
                    </div>
                </div>
                <!-- End Contact -->
                <?php endif; ?>
                
                <?php if (!empty($copyright_text)): ?>
                <!-- Start Copyright -->
                <div class="dashboard-kaveh-sidebar-content-copyright fw-normal mb-3 text-truncate d-none d-xl-block"><?php echo esc_html($copyright_text); ?></div>
                <!-- End Copyright -->
                <?php endif; ?>
                
                <?php if (!empty($social_media_links) && is_array($social_media_links)): ?>
                <!-- Start Socials -->
                <div class="dashboard-kaveh-sidebar-content-socials d-none d-xl-flex align-items-center justify-content-between gap-2 text-truncate">
                    <?php if (!empty($social_media_title)): ?>
                    <div class="text text-truncate"><?php echo esc_html($social_media_title); ?></div>
                    <?php endif; ?>
                    <ul class="d-flex align-items-center">
                        <?php foreach ($social_media_links as $social_link): ?>
                            <?php if (!empty($social_link['url']) && !empty($social_link['name'])): ?>
                            <li>
                                <a href="<?php echo esc_url($social_link['url']); ?>" class="d-block" title="<?php echo esc_attr($social_link['name']); ?>" target="_blank" rel="noopener">
                                    <?php if (!empty($social_link['icon_class'])): ?>
                                        <i class="<?php echo esc_attr($social_link['icon_class']); ?>"></i>
                                    <?php else: ?>
                                        <!-- Default social media icon -->
                                        <svg xmlns="http://www.w3.org/2000/svg" width="23" height="23" fill="none" viewBox="0 0 23 23">
                                            <path stroke="#696D81" class="clr-1" stroke-linecap="round" stroke-linejoin="round" stroke-opacity=".18" stroke-width="1.5" d="M8.438 20.625h5.624c4.688 0 6.563-1.875 6.563-6.563V8.439c0-4.688-1.875-6.563-6.563-6.563H8.439c-4.688 0-6.563 1.875-6.563 6.563v5.624c0 4.688 1.875 6.563 6.563 6.563Z" />
                                            <path stroke="#040414" class="clr-2" stroke-linecap="round" stroke-linejoin="round" stroke-opacity=".7" d="M11.25 14.531a3.281 3.281 0 1 0 0-6.562 3.281 3.281 0 0 0 0 6.562Z" />
                                            <path stroke="#040414" class="clr-2" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16.534 6.563h.01" />
                                        </svg>
                                    <?php endif; ?>
                                </a>
                            </li>
                            <?php endif; ?>
                        <?php endforeach; ?>
                    </ul>
                </div>
                <!-- End Socials -->
                <?php endif; ?>
            </div>
        </div>
    </aside>
    <!-- End Sidebar -->
    <!-- Start Main -->
    <div class="dashboard-kaveh-main">
        <!-- Start Header -->
        <header class="dashboard-kaveh-header d-flex align-items-center">
            <!-- Start Logo -->
            <a href="<?= home_url(); ?>" class="dashboard-kaveh-header-logo d-flex d-xl-none align-items-center gap-2">
                <?php 
                $kavehboard_options = get_option('kavehboard_options', array());
                $logo_url = !empty($kavehboard_options['kavehboard_logo']['url']) ? $kavehboard_options['kavehboard_logo']['url'] : KAVEHBOARD_URL . '/assets/images/dashboard/logo.svg';
                ?>
                <img src="<?= esc_url($logo_url) ?>" alt="logo" width="42" height="42">
            </a>
            <!-- End Logo -->
            <!-- Start Btn Toggle Sidebar -->
            <button type="button" class="dashboard-kaveh-header-toggle-sidebar d-none d-xl-block">
                <i class="icon-arrow-left-9 lh-1"></i>
            </button>
            <!-- End Btn Toggle Sidebar -->
            <div class="dashboard-kaveh-header-text d-none d-xl-flex align-items-center me-auto">
                <div class="name-page"> <?php echo esc_html($page_name); ?> </div>
                <div class="text fw-normal"> <?php echo esc_html($description_text); ?> </div>
            </div>
            <!-- Start icons -->
            <div class="dashboard-kaveh-header-icons d-flex align-items-center ms-auto ms-xl-0">
                <button type="button" id="toggle-mode" class="d-flex align-items-center justify-content-center rounded-circle position-relative">
                    <i class="icon-moon lh-1 fw-normal d-flex align-items-center justify-content-center rounded-circle"></i>
                </button>

                <?php
                // Get notifications information
                $current_user_id = get_current_user_id();
                $unread_notifications_count = kavehboard_get_unread_notifications_count($current_user_id);
                ?>
                <a href="<?php echo esc_url(get_permalink() . 'notifications/'); ?>" class="d-flex align-items-center justify-content-center rounded-circle position-relative text-decoration-none">
                    <i class="icon-notification-2 lh-1 fw-normal d-flex align-items-center justify-content-center rounded-circle"></i>
                    <?php if ($unread_notifications_count > 0): ?>
                        <span class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-danger" style="font-size: 10px;width: 22px !important;color: #fff;height: 22px !important;">
                            <?php echo esc_html($unread_notifications_count); ?>
                        </span>
                    <?php endif; ?>
                </a>

                <?php
                // Get cart information
                $cart_count = 0;
                $cart_url = '#';
                
                if (function_exists('WC') && WC()->cart) {
                    $cart_count = WC()->cart->get_cart_contents_count();
                    $cart_url = wc_get_cart_url();
                }
                ?>
                <a href="<?php echo esc_url($cart_url); ?>" class="d-flex align-items-center justify-content-center rounded-circle position-relative text-decoration-none">
                    <i class="icon-bag-4 lh-1 fw-normal d-flex align-items-center justify-content-center rounded-circle"></i>
                </a>
            </div>
            <!-- End icons -->
            <!-- Start Info User -->
            <div class="dashboard-kaveh-header-info-user d-none d-xl-flex align-items-center gap-3 position-relative dropdown">
                <div class="dashboard-kaveh-header-info-user-info d-flex align-items-center gap-3" data-toggle="dropdown" data-target="#dropdown-info-user">
                    <div>
                        <div class="name fw-bold mb-1"><?= $first_name . ' ' . $last_name; ?> </div>
                        <div class="date fw-normal"><?php _e('Login Date', 'kavehTheme'); ?> <?php echo date_i18n(get_option('date_format')); ?></div>
                    </div>
                    <svg xmlns="http://www.w3.org/2000/svg" width="34" height="34" fill="none" viewBox="0 0 34 34">
                        <path stroke="#292D32" class="clr-1" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" stroke-width="1.5" d="m22.28 14.967-4.347 4.346a1.324 1.324 0 0 1-1.866 0l-4.347-4.346" />
                        <rect width="32.5" height="32.5" x=".5" y=".5" stroke="#5F7086" class="clr-2" stroke-opacity=".2" rx="14.5" />
                    </svg>
                </div>
                <div id="dropdown-info-user" class="dashboard-kaveh-header-info-user-dropdown position-absolute end-0 d-flex flex-column">
                    <a href="<?= home_url(); ?>" class="link edit-profile d-flex align-items-center">
                        <i class="icon-home-4 icon lh-1"></i>
                        <span class="fw-normal"><?php _e('Back to Site', 'kavehTheme'); ?></span>
                        <i class="icon-arrow-left-9 arrow lh-1 ms-auto"></i>
                    </a>
                    <a href="<?php echo esc_url($kavehboardrouting->get_dashboard_url('edit-profile')); ?>" class="link edit-profile d-flex align-items-center">
                        <i class="icon-user-edit-2 icon lh-1"></i>
                        <span class="fw-normal"><?php _e('Edit Profile', 'kavehTheme'); ?></span>
                        <i class="icon-arrow-left-9 arrow lh-1 ms-auto"></i>
                    </a>
                    <a href="<?php echo esc_url(wp_logout_url(home_url())); ?>" class="link logout d-flex align-items-center">
                        <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="none" viewBox="0 0 20 20">
                            <path stroke="#EE273A" class="clr-1" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" stroke-width="1.5" d="m14.533 12.183 2.133-2.133-2.133-2.133m-6.4 2.133h8.475" />
                            <path stroke="#000" class="clr-2" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" stroke-width="1.5" d="M9.8 16.667c-3.683 0-6.667-2.5-6.667-6.667S6.117 3.333 9.8 3.333" />
                        </svg>
                        <span class="fw-bold"><?php _e('Logout from Dashboard', 'kavehTheme'); ?></span>
                        <i class="icon-arrow-left-9 arrow lh-1 ms-auto"></i>
                    </a>
                </div>
            </div>
            <!-- End Info User -->
            <!-- Start Btn Open Sidebar -->
            <button type="button" class="dashboard-kaveh-header-open-sidebar d-flex d-xl-none align-items-center justify-content-center rounded-circle">
                <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="none" viewBox="0 0 20 20">
                    <path stroke="#000" class="clr-1" d="M13.975 2.167h1.583c.825 0 1.372.216 1.715.56.343.343.56.89.56 1.715v1.583c0 .824-.216 1.372-.56 1.715-.343.343-.89.56-1.715.56h-1.583c-.824 0-1.372-.217-1.715-.56-.344-.343-.56-.89-.56-1.715V4.442c0-.824.216-1.372.56-1.716.343-.343.89-.56 1.715-.56ZM4.45 11.692h1.583c.825 0 1.372.216 1.714.56.3.3.502.758.545 1.423l.009.298v1.585c0 .824-.217 1.372-.56 1.716-.344.343-.891.56-1.716.56H4.442c-.824 0-1.371-.217-1.714-.561-.344-.344-.561-.894-.561-1.723v-1.583c0-.824.217-1.372.56-1.715.345-.343.894-.56 1.723-.56Z" opacity=".4" />
                    <path stroke="#0010F7" class="clr-2" d="M5.242 2.167a3.075 3.075 0 1 1 0 6.15 3.075 3.075 0 0 1 0-6.15Zm9.517 9.516a3.076 3.076 0 1 1 0 6.151 3.076 3.076 0 0 1 0-6.15Z" />
                </svg>
            </button>
            <!-- End Btn Open Sidebar -->
        </header>
        <!-- End Header -->
        <!-- Start Info User -->
        <div class="dashboard-kaveh-info-user-sm position-relative">
            <div class="dashboard-kaveh-sidebar-content-info-user d-flex d-xl-none align-items-center justify-content-between gap-2 position-relative" data-toggle="dropdown" data-target="#dropdown-info-user">
                <div class="d-flex align-items-center gap-2">
                    <div class="image rounded-circle d-flex align-items-center justify-content-center flex-fill flex-grow-0 flex-shrink-0">
                        <span class="rounded-circle position-relative d-flex align-items-center justify-content-center">
                            <img src="<?php echo esc_url(kavehboard_get_user_avatar_url($current_user->ID, 32)); ?>" alt="avatar" width="32" height="32" class="d-block rounded-circle">
                        </span>
                    </div>
                    <div class="detail text-truncate">
                        <div class="name fw-bold text-white text-truncate"><?= $first_name . ' ' . $last_name; ?></div>
                        <div class="sub-name fw-normal text-truncate"><?php _e('Welcome 👋', 'kavehTheme'); ?></div>
                    </div>
                </div>
                <svg xmlns="http://www.w3.org/2000/svg" width="34" height="34" fill="none" viewBox="0 0 34 34">
                    <path stroke="#fff" class="clr-1" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" stroke-width="1.5" d="m22.28 14.967-4.347 4.346a1.324 1.324 0 0 1-1.866 0l-4.347-4.346" />
                    <rect width="32.5" height="32.5" x=".5" y=".5" stroke="#fff" class="clr-2" stroke-opacity=".2" rx="14.5" />
                </svg>
            </div>
            <div id="dropdown-info-user-sm" class="dashboard-kaveh-header-info-user-dropdown position-absolute end-0 d-flex flex-column">
                <a href="<?= home_url(); ?>" class="link edit-profile d-flex align-items-center">
                    <i class="icon-home-4 icon lh-1"></i>
                    <span class="fw-normal"><?php _e('Back to Site', 'kavehTheme'); ?></span>
                    <i class="icon-arrow-left-9 arrow lh-1 ms-auto"></i>
                </a>
                <a href="#" class="link edit-profile d-flex align-items-center">
                    <i class="icon-user-edit-2 icon lh-1"></i>
                    <span class="fw-normal"><?php _e('Edit Profile', 'kavehTheme'); ?></span>
                    <i class="icon-arrow-left-9 arrow lh-1 ms-auto"></i>
                </a>
                <a href="#" class="link logout d-flex align-items-center">
                    <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="none" viewBox="0 0 20 20">
                        <path stroke="#EE273A" class="clr-1" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" stroke-width="1.5" d="m14.533 12.183 2.133-2.133-2.133-2.133m-6.4 2.133h8.475" />
                        <path stroke="#000" class="clr-2" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" stroke-width="1.5" d="M9.8 16.667c-3.683 0-6.667-2.5-6.667-6.667S6.117 3.333 9.8 3.333" />
                    </svg>
                    <span class="fw-bold"><?php _e('Logout from Dashboard', 'kavehTheme'); ?></span>
                    <i class="icon-arrow-left-9 arrow lh-1 ms-auto"></i>
                </a>
            </div>
        </div>
        <!-- End Info User -->
        <div class="dashboard-kaveh-header-text dashboard-kaveh-text-sm d-xl-none">
            <div class="d-flex align-items-center">
                <button type="button" class="dashboard-kaveh-header-toggle-sidebar p-0">
                    <i class="icon-arrow-left-9 lh-1"></i>
                </button>
                <div class="name-page"> <?php echo esc_html($page_name); ?> </div>
            </div>
            <div class="text fw-normal"> <?php echo esc_html($description_text); ?> </div>
        </div>
        <!-- Start icons -->
        <!-- Start Content -->
        <div class="dashboard-kaveh-content">
            <?php
            // Get routing instance and render content based on current page
            global $kavehboardrouting;
            if (!$kavehboardrouting) {
                $kavehboardrouting = new kavehboardrouting();
            }
            
            $current_page = $kavehboardrouting->get_current_page();
            
            // If it's the main dashboard page, show the default dashboard content
            if ($current_page === 'dashboard') {
            ?>
            <div class="row row-dashboard">

                <!-- Start Orders -->
                <div class="col-lg-6 order-1 order-lg-2">
                    <div class="dashboard-kaveh-orders">
                        <!-- Start Heading -->
                        <div class="dashboard-kaveh-orders-heading d-flex align-items-center">
                            <div class="dashboard-kaveh-orders-heading-title d-flex align-items-center gap-2">
                                <i class="icon-fast-car lh-1"></i>
                                <span class="text fw-normal"> سفارشات من </span>
                                <span class="badge badge-warning rounded-pill"> <?= $order_stats['total']; ?> </span>
                            </div>
                            <a href="<?php echo esc_url(get_permalink() . 'orders/'); ?>" class="dashboard-kaveh-link d-flex align-items-center justify-content-center fw-bold ms-auto">
                                مشاهده بیشتر
                                <i class="icon-arrow-left-9 lh-1 fw-normal"></i>
                            </a>
                        </div>
                        <!-- End Heading -->
                        <!-- Start Info Orders -->
                        <div class="dashboard-kaveh-orders-info-orders">
                            <div class="row">
                                <!-- Start Info Orders Item -->
                                <div class="col-6">
                                    <div class="dashboard-kaveh-orders-info-orders-item bg-white position-relative d-flex align-items-center">
                                        <div class="image d-flex align-items-center justify-content-center position-relative">
                                            <img src="<?= KAVEHBOARD_URL ?>/assets/images/dashboard/stat-01.png" alt="stat" width="32" height="32" class="d-block">
                                        </div>
                                        <div>
                                            <div class="value fw-bold"> <?= $order_stats['processing']; ?> عدد </div>
                                            <div class="sub-value fw-normal"> سفارشات جاری </div>
                                        </div>
                                        <a href="<?php echo esc_url(get_permalink() . 'orders/'); ?>" class="more position-absolute">
                                            <i class="icon-arrow-left-9 lh-1"></i>
                                        </a>
                                    </div>
                                </div>
                                <!-- End Info Orders Item -->
                                <!-- Start Info Orders Item -->
                                <div class="col-6">
                                    <div class="dashboard-kaveh-orders-info-orders-item bg-white position-relative d-flex align-items-center">
                                        <div class="image d-flex align-items-center justify-content-center position-relative">
                                            <img src="<?= KAVEHBOARD_URL ?>/assets/images/dashboard/stat-02.png" alt="stat" width="32" height="32" class="d-block">
                                        </div>
                                        <div>
                                            <div class="value fw-bold"> <?= $order_stats['completed']; ?> عدد </div>
                                            <div class="sub-value fw-normal"> تحویل شده </div>
                                        </div>
                                        <a href="<?php echo esc_url(get_permalink() . 'orders/'); ?>" class="more position-absolute">
                                            <i class="icon-arrow-left-9 lh-1"></i>
                                        </a>
                                    </div>
                                </div>
                                <!-- End Info Orders Item -->
                                <!-- Start Info Orders Item -->
                                <div class="col-6">
                                    <div class="dashboard-kaveh-orders-info-orders-item bg-white position-relative d-flex align-items-center">
                                        <div class="image d-flex align-items-center justify-content-center position-relative">
                                            <img src="<?= KAVEHBOARD_URL ?>/assets/images/dashboard/stat-03.png" alt="stat" width="32" height="32" class="d-block">
                                        </div>
                                        <div>
                                            <div class="value fw-bold"> <?= $order_stats['refunded']; ?> عدد </div>
                                            <div class="sub-value fw-normal"> مرجوع شده </div>
                                        </div>
                                        <a href="<?php echo esc_url(get_permalink() . 'orders/'); ?>" class="more position-absolute">
                                            <i class="icon-arrow-left-9 lh-1"></i>
                                        </a>
                                    </div>
                                </div>
                                <!-- End Info Orders Item -->
                                <!-- Start Info Orders Item -->
                                <div class="col-6">
                                    <div class="dashboard-kaveh-orders-info-orders-item bg-white position-relative d-flex align-items-center">
                                        <div class="image d-flex align-items-center justify-content-center position-relative">
                                            <img src="<?= KAVEHBOARD_URL ?>/assets/images/dashboard/stat-04.png" alt="stat" width="32" height="32" class="d-block">
                                        </div>
                                        <div>
                                            <div class="value fw-bold"> <?= $order_stats['cancelled']; ?> عدد </div>
                                            <div class="sub-value fw-normal"> لغو شده </div>
                                        </div>
                                        <a href="<?php echo esc_url(get_permalink() . 'orders/'); ?>" class="more position-absolute">
                                            <i class="icon-arrow-left-9 lh-1"></i>
                                        </a>
                                    </div>
                                </div>
                                <!-- End Info Orders Item -->
                            </div>
                        </div>
                        <!-- End Info Orders -->
                    </div>
                </div>
                <!-- End Orders -->
                <!-- Start Carousel -->
                <div class="col-lg-6 order-5 order-lg-3">
                    <div class="dashboard-kaveh-carousel">
                        <div class="swiper swiper-carousel-dashboard overflow-visible position-relative">
                            <div class="swiper-wrapper">
                                <?php
                                // Get carousel slides from admin options
                                $carousel_slides = get_option('kavehboard_options')['kavehboard_dashboard_carousel'] ?? array();
                                
                                // If no slides configured, use default
                                if (empty($carousel_slides)) {
                                    $carousel_slides = array(
                                        array(
                                            'image' => array('url' => KAVEHBOARD_URL . '/assets/images/dashboard/carousel.png'),
                                            'title' => 'Default Slide 1',
                                            'link' => '#',
                                            'link_target' => '_self'
                                        ),
                                        array(
                                            'image' => array('url' => KAVEHBOARD_URL . '/assets/images/dashboard/carousel.png'),
                                            'title' => 'Default Slide 2',
                                            'link' => '#',
                                            'link_target' => '_self'
                                        )
                                    );
                                }
                                
                                foreach ($carousel_slides as $slide):
                                    $image_url = $slide['image']['url'] ?? KAVEHBOARD_URL . '/assets/images/dashboard/carousel.png';
                                    $title = $slide['title'] ?? 'carousel';
                                    $link = $slide['link'] ?? '#';
                                    $link_target = $slide['link_target'] ?? '_self';
                                ?>
                                <!-- Start Slide -->
                                <div class="swiper-slide">
                                    <a href="<?php echo esc_url($link); ?>" class="slide d-block w-100" target="<?php echo esc_attr($link_target); ?>">
                                        <img src="<?php echo esc_url($image_url); ?>" alt="<?php echo esc_attr($title); ?>" width="493" height="302" class="d-block w-100">
                                    </a>
                                </div>
                                <!-- End Slide -->
                                <?php endforeach; ?>
                            </div>
                            <!-- Start Pagination -->
                            <div class="swiper-pagination d-none d-lg-flex align-items-center flex-column top-50 end-0 m-0 w-auto h-auto start-100"></div>
                            <!-- End Pagination -->
                        </div>
                    </div>
                </div>
                <!-- End Carousel -->
                <div class="col-lg-6 order-3 order-lg-4">
                    <?php
                    // Get discount code settings from admin options
                    if(!empty(get_option('kavehboard_options')['kavehboard_discount_code'])){
                        $discount_options = get_option('kavehboard_options')['kavehboard_discount_code'][0];
                    }
                    if (!empty($discount_options['enable']) && $discount_options['enable'] === '1'):
                        $discount_image = $discount_options['image']['url'];
                        $discount_code = $discount_options['code'];
                        $discount_description = $discount_options['description'];
                    ?>
                    <!-- Start Discount Code -->
                    <div class="dashboard-kaveh-discount-code d-flex align-items-center overflow-hidden mb-3">
                        <div class="image position-relative flex-fill flex-grow-0 flex-shrink-0 d-flex align-items-center justify-content-center">
                            <img src="<?php echo esc_url($discount_image); ?>" alt="gift" width="124" height="124">
                        </div>
                        <div class="detail w-100 flex-fill flex-grow-0 flex-shrink-0 position-relative">
                            <div class="code"> <?php echo esc_html($discount_code); ?> </div>
                            <div class="description"> <?php echo esc_html($discount_description); ?> </div>
                        </div>
                    </div>
                    <!-- End Discount Code -->
                    <?php endif; ?>
                    <!-- Start Info User -->
                    <div class="dashboard-kaveh-info-user">
                        <!-- Start Heading -->
                        <div class="dashboard-kaveh-info-user-heading d-flex align-items-center justify-content-between mb-4">
                            <div class="dashboard-kaveh-info-user-heading-text d-flex align-items-center gap-2">
                                <i class="icon-user-octagon lh-1"></i>
                                <span class="fw-normal"> مشخصات شخصی </span>
                            </div>
                            <a href="<?php echo esc_url(get_permalink() . 'woo-edit-account/'); ?>" class="dashboard-kaveh-link d-flex align-items-center fw-bold">
                                ویرایش اطلاعات
                                <i class="icon-arrow-left-9 lh-1"></i>
                            </a>
                        </div>
                        <!-- End Heading -->
                        <!-- Start Info -->
                        <ul class="dashboard-kaveh-info-user-info d-flex flex-column">
                            <?php
                            // Get current user information
                            if (is_user_logged_in()) {
                                $current_user = wp_get_current_user();
                                $user_id = get_current_user_id();
                                
                                // Get user's full name
                                $first_name = get_user_meta($user_id, 'first_name', true);
                                $last_name = get_user_meta($user_id, 'last_name', true);
                                $full_name = trim($first_name . ' ' . $last_name);
                                if (empty($full_name)) {
                                    $full_name = $current_user->display_name;
                                }
                                
                                // Get user's email
                                $user_email = $current_user->user_email;
                                
                                // Get user's phone number from WooCommerce billing data
                                $user_phone = '';
                                if (function_exists('get_user_meta')) {
                                    $user_phone = get_user_meta($user_id, 'billing_phone', true);
                                }
                            } else {
                                $full_name = 'مهمان';
                                $user_email = '';
                                $user_phone = '';
                            }
                            ?>
                            <li class="d-flex align-items-center">
                                <i class="icon-user-octagon lh-1"></i>
                                <span class="label fw-bold"> نام: </span>
                                <span class="value fw-normal"> <?php echo esc_html($full_name); ?> </span>
                            </li>
                            <?php if (!empty($user_phone)): ?>
                            <li class="d-flex align-items-center">
                                <i class="icon-phone-6 lh-1"></i>
                                <span class="label fw-bold"> شماره: </span>
                                <span class="value fw-normal"> <?php echo esc_html($user_phone); ?> </span>
                            </li>
                            <?php endif; ?>
                            <?php if (!empty($user_email)): ?>
                            <li class="d-flex align-items-center">
                                <i class="icon-sms lh-1"></i>
                                <span class="label fw-bold"> ایمیل: </span>
                                <span class="value fw-normal"> <?php echo esc_html($user_email); ?> </span>
                            </li>
                            <?php endif; ?>
                        </ul>
                        <!-- End Info -->
                    </div>
                    <!-- End Info User -->
                </div>
                <!-- Start Latest products purchased -->
                <div class="col-lg-6 order-4 order-lg-5">
                    <div class="dashboard-kaveh-latest-products-purchased">
                        <!-- Start Heading -->
                        <div class="dashboard-kaveh-latest-products-purchased-heading d-flex align-items-center justify-content-between gap-2 bg-white">
                            <div class="dashboard-kaveh-latest-products-purchased-heading-title">
                                <i class="icon-fast-car lh-1"></i>
                                <span class="text fw-normal"> اخرین محصولات خریداری شده </span>
                                <span class="badge badge-warning rounded-pill"> <?= $order_stats['total']; ?> </span>
                            </div>
                            <a href="<?php echo esc_url(get_permalink() . 'orders/'); ?>" class="dashboard-kaveh-link d-flex align-items-center fw-bold flex-fill flex-grow-0 flex-shrink-0">
                                مشاهده بیشتر
                                <i class="icon-arrow-left-9 lh-1"></i>
                            </a>
                        </div>
                        <!-- End Heading -->
                        <!-- Start Products -->
                        <ul class="dashboard-kaveh-latest-products-purchased-products d-flex flex-column scrollbar position-relative">
                        <?php
                        // Get recent orders for current user
                        if (function_exists('wc_get_orders') && is_user_logged_in()) {
                            $user_id = get_current_user_id();
                            
                            // Get all user orders
                            $customer_orders = wc_get_orders(array(
                                'customer' => $user_id,
                                'limit' => 6, // Show only 6 most recent orders
                                'orderby' => 'date',
                                'order' => 'DESC',
                            ));
                            
                            if (!empty($customer_orders)) {
                                foreach ($customer_orders as $order) {
                                    // Get order details
                                    $order_id = $order->get_id();
                                    $order_date = $order->get_date_created();
                                    $order_date_formatted = $order_date ? $order_date->date_i18n('Y/m/d') : '';
                                    $order_total = $order->get_total();
                                    $order_status = $order->get_status();
                                    
                                    // Get order items
                                    $items = $order->get_items();
                                    
                                    // Display each item in the order
                                    foreach ($items as $item) {
                                        $product_id = $item->get_product_id();
                                        $product = wc_get_product($product_id);
                                        
                                        if ($product) {
                                            $product_name = $item->get_name();
                                            $product_quantity = $item->get_quantity();
                                            $product_total = $item->get_total();
                                            $product_image = wp_get_attachment_image_src(get_post_thumbnail_id($product_id), 'thumbnail');
                                            $product_image_url = $product_image ? $product_image[0] : KAVEHBOARD_URL . '/assets/images/dashboard/product.png';
                                            $product_permalink = get_permalink($product_id);
                                            
                                            // Format price in Persian style
                                            $formatted_price = number_format($product_total, 0, '.', ',');
                                            ?>
                                            <!-- Start Product -->
                                            <li class="bg-white">
                                                <div class="info d-flex align-items-center">
                                                    <a href="<?php echo esc_url($product_permalink); ?>" class="image flex-fill flex-grow-0 flex-shrink-0">
                                                        <img src="<?php echo esc_url($product_image_url); ?>" alt="<?php echo esc_attr($product_name); ?>" width="60" height="60">
                                                    </a>
                                                    <a href="<?php echo esc_url($product_permalink); ?>" class="title me-3"><?php echo esc_html($product_name); ?></a>
                                                    <a href="<?php echo esc_url(get_permalink() . 'order-details/' . $order_id . '/'); ?>" class="icon position-relative rounded-circle ms-auto me-3 d-flex align-items-center justify-content-center flex-fill flex-grow-0 flex-shrink-0">
                                                        <i class="icon-box-time lh-1 d-flex align-items-center justify-content-center"></i>
                                                    </a>
                                                    <a href="<?php echo esc_url($product_permalink); ?>" class="go d-flex align-items-center justify-content-center">
                                                        <i class="icon-arrow-left-9 lh-1"></i>
                                                    </a>
                                                </div>
                                                <div class="bottom d-flex align-items-center f-iransans flex-wrap">
                                                    <div class="count"> تعداد : <?php echo esc_html($product_quantity); ?> </div>
                                                    <div class="date"> تاریخ : <?php echo esc_html($order_date_formatted); ?> </div>
                                                    <div class="code"> #<?php echo esc_html($order_id); ?> </div>
                                                    <div class="price fw-normal">
                                                        <span class="fw-bold"> <?php echo esc_html($formatted_price); ?> </span>
                                                        تومان
                                                    </div>
                                                </div>
                                            </li>
                                            <!-- End Product -->
                                            <?php
                                        }
                                    }
                                }
                            } else {
                                // No orders found
                                echo '<li class="bg-white text-center py-4">هیچ محصولی خریداری نشده است.</li>';
                            }
                        } else {
                            // WooCommerce not active or user not logged in
                            echo '<li class="bg-white text-center py-4">برای مشاهده محصولات خریداری شده، لطفا وارد حساب کاربری خود شوید.</li>';
                        }
                        ?>
                        </ul>
                        <!-- End Products -->
                    </div>
                </div>
                <!-- End Latest products purchased -->
                
                <!-- Start Notifications Section -->
                <?php 
                $current_user_id = get_current_user_id();
                $notifications = kavehboard_get_user_notifications($current_user_id, 5); // Get latest 5 notifications
                $unread_count = kavehboard_get_unread_notifications_count($current_user_id);
                ?>
                <?php if (!empty($notifications)): ?>
                <div class="col-12 order-6">
                    <div class="dashboard-kaveh-notifications">
                        <!-- Start Heading -->
                        <div class="dashboard-kaveh-notifications-heading d-flex align-items-center justify-content-between gap-2 bg-white">
                            <div class="dashboard-kaveh-notifications-heading-title">
                                <i class="icon-notification-2 lh-1"></i>
                                <span class="text fw-normal"> اعلانات اخیر </span>
                                <?php if ($unread_count > 0): ?>
                                    <span class="badge badge-danger rounded-pill"> <?= $unread_count; ?> </span>
                                <?php endif; ?>
                            </div>
                            <a href="<?php echo esc_url(get_permalink() . 'notifications/'); ?>" class="dashboard-kaveh-link d-flex align-items-center fw-bold flex-fill flex-grow-0 flex-shrink-0">
                                مشاهده بیشتر
                                <i class="icon-arrow-left-9 lh-1"></i>
                            </a>
                        </div>
                        <!-- End Heading -->
                        <!-- Start Notifications -->
                        <ul class="dashboard-kaveh-notifications-list d-flex flex-column scrollbar position-relative">
                            <?php foreach ($notifications as $notification): 
                                $is_read = kavehboard_is_notification_read($current_user_id, $notification->ID);
                                $notification_type = get_post_meta($notification->ID, '_kaveh_notification_type', true) ?: 'info';
                                $action_url = get_post_meta($notification->ID, '_kaveh_notification_action_url', true);
                                
                                // Get notification type icon and color
                                $type_config = array(
                                    'info' => array('icon' => 'icon-info-circle', 'color' => '#17a2b8'),
                                    'success' => array('icon' => 'icon-check-circle', 'color' => '#28a745'),
                                    'warning' => array('icon' => 'icon-warning', 'color' => '#ffc107'),
                                    'error' => array('icon' => 'icon icon-close', 'color' => '#dc3545')
                                );
                                
                                $config = $type_config[$notification_type] ?? $type_config['info'];
                                $notification_date = get_the_date('Y/m/d', $notification->ID);
                            ?>
                            <!-- Start Notification -->
                            <li class="bg-white <?php echo !$is_read ? 'unread' : ''; ?>">
                                <div class="info d-flex align-items-center justify-content-center">
                                    <div class="icon flex-fill flex-grow-0 flex-shrink-0 d-flex align-items-center justify-content-center">
                                        <i class="<?php echo esc_attr($config['icon']); ?> lh-1 d-flex align-items-center justify-content-center" style="color: <?php echo esc_attr($config['color']); ?>; font-size: 20px;"></i>
                                    </div>
                                    <div class="content me-3">
                                        <div class="title <?php echo !$is_read ? 'fw-bold' : ''; ?>"><?php echo esc_html($notification->post_title); ?></div>
                                        <div class="message text-muted"><?php echo wp_trim_words(wp_strip_all_tags($notification->post_content), 10); ?></div>
                                    </div>
                                    <?php if (!$is_read): ?>
                                        <span class="badge badge-danger rounded-pill ms-auto me-3 flex-fill flex-grow-0 flex-shrink-0">جدید</span>
                                    <?php endif; ?>
                                    <?php if ($action_url): ?>
                                        <a href="<?php echo esc_url($action_url); ?>" class="icon position-relative rounded-circle ms-auto me-3 d-flex align-items-center justify-content-center flex-fill flex-grow-0 flex-shrink-0" target="_blank">
                                            <i class="icon-external-link lh-1 d-flex align-items-center justify-content-center"></i>
                                        </a>
                                    <?php endif; ?>
                                    <a href="<?php echo esc_url(get_permalink() . 'notifications/'); ?>" class="go d-flex align-items-center justify-content-center">
                                        <i class="icon-arrow-left-9 lh-1"></i>
                                    </a>
                                </div>
                                <div class="bottom d-flex align-items-center f-iransans flex-wrap">
                                    <div class="type"> نوع : <?php echo esc_html(ucfirst($notification_type)); ?> </div>
                                    <div class="date"> تاریخ : <?php echo esc_html($notification_date); ?> </div>
                                    <div class="status <?php echo !$is_read ? 'fw-bold text-primary' : 'text-muted'; ?>">
                                        <?php echo !$is_read ? 'خوانده نشده' : 'خوانده شده'; ?>
                                    </div>
                                </div>
                            </li>
                            <!-- End Notification -->
                            <?php endforeach; ?>
                        </ul>
                        <!-- End Notifications -->
                    </div>
                </div>
                <?php endif; ?>
                <!-- End Notifications Section -->
            </div>
            <?php
            } else {
                // For other pages, render the appropriate content
                echo '<div class="dashboard-kaveh-dynamic-content">';
                $kavehboardrouting->render_dashboard_content();
                echo '</div>';
            }
            ?>
            <div class="d-xl-none mt-4">
                <?php if (!empty($copyright_text)): ?>
                <!-- Start Copyright -->
                <div class="dashboard-kaveh-sidebar-content-copyright fw-normal mb-3 text-truncate"><?php echo esc_html($copyright_text); ?></div>
                <!-- End Copyright -->
                <?php endif; ?>
                
                <?php if (!empty($social_media_links) && is_array($social_media_links)): ?>
                <!-- Start Socials -->
                <div class="dashboard-kaveh-sidebar-content-socials d-flex align-items-center justify-content-between gap-2 text-truncate">
                    <?php if (!empty($social_media_title)): ?>
                    <div class="text text-truncate"><?php echo esc_html($social_media_title); ?></div>
                    <?php endif; ?>
                    <ul class="d-flex align-items-center">
                        <?php foreach ($social_media_links as $social_link): ?>
                        <?php if (!empty($social_link['url']) && !empty($social_link['name'])): ?>
                        <li>
                            <a href="<?php echo esc_url($social_link['url']); ?>" class="d-block" title="<?php echo esc_attr($social_link['name']); ?>" target="_blank" rel="noopener">
                                <?php if (!empty($social_link['icon_class'])): ?>
                                <i class="<?php echo esc_attr($social_link['icon_class']); ?>"></i>
                                <?php else: ?>
                                <!-- Default social media icon -->
                                <svg xmlns="http://www.w3.org/2000/svg" width="23" height="23" fill="none" viewBox="0 0 23 23">
                                    <path stroke="#696D81" class="clr-1" stroke-linecap="round" stroke-linejoin="round" stroke-opacity=".18" stroke-width="1.5" d="M8.438 20.625h5.624c4.688 0 6.563-1.875 6.563-6.563V8.439c0-4.688-1.875-6.563-6.563-6.563H8.439c-4.688 0-6.563 1.875-6.563 6.563v5.624c0 4.688 1.875 6.563 6.563 6.563Z" />
                                    <path stroke="#040414" class="clr-2" stroke-linecap="round" stroke-linejoin="round" stroke-opacity=".7" d="M11.25 14.531a3.281 3.281 0 1 0 0-6.562 3.281 3.281 0 0 0 0 6.562Z" />
                                    <path stroke="#040414" class="clr-2" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16.534 6.563h.01" />
                                </svg>
                                <?php endif; ?>
                            </a>
                        </li>
                        <?php endif; ?>
                        <?php endforeach; ?>
                    </ul>
                </div>
                <!-- End Socials -->
                <?php endif; ?>
            </div>
        </div>
        <!-- End Content -->
    </div>
    <!-- End Main -->
</div>
<?php require KAVEHBOARD_PATH . 'template-part/footer.php'; ?>