<?php
/**
 * Yaatra Theme Dashboard for managing theme options and settings.
 * 
 * Provides an interface for users to configure and customize Yaatra theme features.
 *
 * @package Yaatra
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Yaatra_Admin_Dashboard
 * 
 * Manages the admin dashboard functionalities for the Yaatra theme, including settings, notifications, and updates.
 * 
 * @since 1.2.0
 */
class Yaatra_Admin_Dashboard {

    public $theme_name;
    public $theme_slug;
    public $theme_author_uri;
    public $theme_author_name;
    public $free_plugins;

    /**
     * Constructor for the Yaatra_Admin_Dashboard class.
     * Initializes the admin dashboard functionalities for the theme.
     */
    public function __construct() {
        global $admin_main_class;

        // Hook to add admin menu
        add_action( 'admin_menu', array( $this, 'yaatra_admin_menu' ) );

        // Get theme details
        $theme = wp_get_theme();
        $this->theme_name           = $theme->get( 'Name' );
        $this->theme_slug           = $theme->get( 'TextDomain' );
        $this->theme_author_uri     = $theme->get( 'AuthorURI' );
        $this->theme_author_name    = $theme->get( 'Author' );
        if ( is_object( $admin_main_class ) && method_exists( $admin_main_class, 'yaatra_free_plugins_lists' ) ) {
            $this->free_plugins = $admin_main_class->yaatra_free_plugins_lists();
        } else {
            $this->free_plugins = array(); // fallback to empty
        }
    }

    /**
     * Add admin menu.
     */
    public function yaatra_admin_menu() {
        add_theme_page(
            sprintf( esc_html__( '%1$s Dashboard', 'yaatra' ), $this->theme_name ),
            sprintf( esc_html__( '%1$s Dashboard', 'yaatra' ), $this->theme_name ),
            'edit_theme_options',
            'yaatra-dashboard',
            array( $this, 'yaatra_get_started_screen' )
        );
    }

    /**
     * Get started screen.
     */
    public function yaatra_get_started_screen() {
        $current_tab = isset( $_GET['tab'] ) ? sanitize_title( $_GET['tab'] ) : 'yaatra_welcome';

        // Look for a {$current_tab}_screen method.
        if ( is_callable( array( $this, $current_tab . '_screen' ) ) ) {
            return $this->{$current_tab . '_screen'}();
        }

        // Fallback to the welcome screen.
        return $this->yaatra_welcome_screen();
    }

    /**
     * Dashboard header.
     *
     * @access private
     */
    private function yaatra_dashboard_header() {
        global $yaatra_theme_version;
?>
        <div class="dashboard-header">
            <div class="cv-container">
                <div class="header-top">
                    <h1 class="heading"><?php printf( esc_html__( '%1$s Options', 'yaatra' ), $this->theme_name ); ?></h1>
                    <span class="theme-version"><?php printf( esc_html__( 'Version: %1$s', 'yaatra'), $yaatra_theme_version ); ?></span>
                    <span class="author-link"><?php printf( wp_kses_post( 'By <a href="%1$s" target="_blank">%2$s</a>', 'yaatra' ), $this->theme_author_uri, $this->theme_author_name ); ?></span>
                </div><!-- .header-top -->
                <div class="header-nav">
                    <nav class="dashboard-nav">
                        <?php $this->yaatra_render_nav_tabs(); ?>
                    </nav>
                    <div class="upgrade-pro">
                        <a href="<?php echo esc_url( 'https://codevibrant.com/wpthemes/yaatra-pro/' ); ?>" target="_blank" class="button button-primary"><?php esc_html_e( 'More Features With Pro', 'yaatra' ); ?></a>
                    </div>
                </div><!-- .header-nav -->
            </div><!-- .cv-container -->
        </div><!-- .dashboard-header -->
<?php
    }

    /**
     * Render navigation tabs
     */
    private function yaatra_render_nav_tabs() {
        $tabs = array(
            'welcome'   => array( 'icon' => 'dashicons-admin-appearance', 'label' => esc_html__( 'Welcome', 'yaatra') ),
            'starter'   => array( 'icon' => 'dashicons-images-alt2', 'label' => esc_html__( 'Starter Sites', 'yaatra' ) ),
            'plugin'    => array( 'icon' => 'dashicons-admin-plugins', 'label' => esc_html__( 'Plugin', 'yaatra' ) ),
            'free_pro'  => array( 'icon' => 'dashicons-dashboard', 'label' => esc_html__( 'Free Vs Pro', 'yaatra' ) ),
            'changelog' => array( 'icon' => 'dashicons-flag', 'label' => esc_html__( 'Changelog', 'yaatra' ) )
        );

        foreach ( $tabs as $tab => $data ) {
            $active_class = isset( $_GET['tab'] ) && $_GET['tab'] === 'yaatra_' . $tab ? 'active' : '';
?>
            <li>
                <a class="nav-tab <?php echo $active_class; ?>" href="<?php echo esc_url( admin_url( add_query_arg( array(
                    'page' => 'yaatra-dashboard',
                    'tab' => 'yaatra_' . $tab
                ), 'themes.php' ) ) ); ?>">
                    <span class="dashicons <?php echo esc_attr( $data['icon'] ); ?>"></span> <?php echo esc_html( $data['label'] ); ?>
                </a>
            </li>
<?php
        }
    }

    /**
     * Dashboard sidebar.
     *
     * @access private
     */
    private function yaatra_dashboard_sidebar() {
        $review_url = 'https://wordpress.org/support/theme/' . $this->theme_slug . '/reviews/?filter=5#new-post';
?>
        <div class="sidebar-wrapper">
            <aside class="sidebar">
                <div class="sidebar-block">
                    <h4 class="block-title"><?php esc_html_e( 'Leave us a review', 'yaatra' ); ?></h4>
                    <p><?php printf( wp_kses_post( 'Are you are enjoying <b>%1$s</b>? We would love to hear your feedback.', 'yaatra' ), $this->theme_name ); ?></p>
                    <a class="button button-primary" href="<?php echo esc_url( $review_url ); ?>" target="_blank" rel="external noopener noreferrer">
                        <?php esc_html_e( 'Submit a review', 'yaatra' ); ?>
                        <span class="dashicons dashicons-external"></span>
                    </a>
                </div><!-- .sidebar-block -->
            </aside>
        </div><!-- .sidebar-wrapper -->
<?php
    }

    /**
     * Render the welcome screen.
     */
    public function yaatra_welcome_screen() {
?>
        <div id="cv-dashboard">
            <?php $this->yaatra_dashboard_header(); ?>
            <div class="dashboard-content-wrapper">
                <div class="cv-container">
                    <div class="main-content welcome-content-wrapper">
                        <?php
                            $this->yaatra_render_quick_links();
                            $this->yaatra_render_documentation_block();
                            $this->yaatra_render_support_block();
                            $this->yaatra_render_tutorial_block();
                        ?>
                        <div class="return-to-dashboard">
                            <?php if ( current_user_can( 'update_core' ) && isset( $_GET['updated'] ) ): ?>
                                <a href="<?php echo esc_url( self_admin_url( 'update-core.php' ) ); ?>">
                                    <?php is_multisite() ? esc_html_e( 'Return to Updates', 'yaatra' ) : esc_html_e( 'Return to Dashboard &rarr; Updates', 'yaatra' ); ?>
                                </a> |
                            <?php endif; ?>
                            <a href="<?php echo esc_url( self_admin_url() ); ?>"><?php is_blog_admin() ? esc_html_e( 'Go to Dashboard &rarr; Home', 'yaatra' ) : esc_html_e( 'Go to Dashboard', 'yaatra' ); ?></a>
                        </div><!-- .return-to-dashboard -->
                    </div><!-- .welcome-content-wrapper -->
                    <?php $this->yaatra_dashboard_sidebar(); ?>
                </div><!-- .cv-container -->
            </div><!-- .dashboard-content-wrapper -->
        </div><!-- #cv-dashboard -->
        <?php
    }

    /**
     * Render quick links.
     */
    private function yaatra_render_quick_links() {
?>
        <div class="welcome-block quick-links">
            <div class="block-header">
                <img class="block-icon" src="<?php echo esc_url( get_template_directory_uri() . '/inc/admin/assets/img/quick-link.svg' ); ?>" alt="icon">
                <h3 class="block-title"><?php esc_html_e( 'Customizer quick link', 'yaatra' ); ?></h3>
            </div>
            <div class="block-content content-column">
                <div class="col">
                    <li>
                        <a href="<?php echo esc_url( admin_url( 'customize.php' ) . '?autofocus[section]=title_tagline' ); ?>" target="_blank" class="welcome-icon"><span class="dashicons dashicons-visibility"></span><?php esc_html_e( 'Setup site logo', 'yaatra' ); ?></a>
                    </li>
                    <li>
                        <a href="<?php echo esc_url( admin_url( 'customize.php' ) . '?autofocus[section]=yaatra_section_social_icons' ); ?>" target="_blank" class="welcome-icon"> <span class="dashicons dashicons-menu-alt"> </span><?php esc_html_e( 'Social Icons', 'yaatra' ); ?></a>
                    </li>
                </div>
                <div class="col">
                    <li>
                        <a href="<?php echo esc_url( admin_url( 'customize.php' ) . '?autofocus[section]=yaatra_section_frontpage_slider' ); ?>" target="_blank" class="welcome-icon"> <span class="dashicons dashicons-slides"> </span> <?php esc_html_e( 'Slider Setting', 'yaatra' ); ?></a>
                    </li>
                    <li>
                        <a href="<?php echo esc_url( admin_url( 'customize.php' ) . '?autofocus[section]=yaatra_section_page_error' ); ?>" target="_blank" class="welcome-icon"><span class="dashicons dashicons-media-document"> </span><?php esc_html_e( '404 Error Page Settings', 'yaatra' ); ?></a>
                    </li>
                </div>
            </div><!-- .block-content -->
        </div><!-- .welcome-block.quick-links -->
<?php
    }

    /**
     * Render documentation block.
     */
    private function yaatra_render_documentation_block() {
?>
        <div class="welcome-block documentation">
            <div class="block-header">
                <img class="block-icon" src="<?php echo esc_url( get_template_directory_uri() . '/inc/admin/assets/img/docs.svg' ); ?>" alt="icon">
                <h3 class="block-title"><?php esc_html_e( 'Theme Documentation', 'yaatra' ); ?></h3>
            </div>
            <div class="block-content">
                <p>
                    <?php printf( wp_kses_post( 'Need more details? Please check our full documentation for detailed information on how to use <b>%1$s</b>.', 'yaatra' ), $this->theme_name ); ?>
                    <a href="<?php echo esc_url( 'https://docs.codevibrant.com/yaatra' ); ?>" target="_blank"><?php esc_html_e( 'Go to doc', 'yaatra' ); ?><span class="dashicons dashicons-external"></span></a>
                </p>
            </div><!-- .block-content -->
        </div><!-- .welcome-block documentation -->
<?php
    }

    /**
     * Render support block.
     */
    private function yaatra_render_support_block() {
?>
        <div class="welcome-block support">
            <div class="block-header">
                <img class="block-icon" src="<?php echo esc_url( get_template_directory_uri() . '/inc/admin/assets/img/support.svg' ); ?>" alt="icon">
                <h3 class="block-title"><?php esc_html_e( 'Contact Support', 'yaatra' ); ?></h3>
            </div>
            <div class="block-content">
                <p>
                    <?php printf( wp_kses_post( 'We want to make sure you have the best experience using <b>%1$s</b>, and that is why we have gathered all the necessary information here for you. We hope you will enjoy using <b>%1$s</b> as much as we enjoy creating great products.', 'yaatra' ), $this->theme_name ); ?>
                    <a href="<?php echo esc_url( 'https://wordpress.org/support/theme/'.$this->theme_slug ); ?>" target="_blank"><?php esc_html_e( 'Contact Support', 'yaatra' ); ?><span class="dashicons dashicons-external"></span></a>
                </p>
            </div><!-- .block-content -->
        </div><!-- .welcome-block support -->
<?php
    }

    /**
     * Render tutorial block.
     */
    private function yaatra_render_tutorial_block() {
?>
        <div class="welcome-block tutorial">
            <div class="block-header">
                <img class="block-icon" src="<?php echo esc_url( get_template_directory_uri() . '/inc/admin/assets/img/tutorial.svg' ); ?>" alt="icon">
                <h3 class="block-title"><?php esc_html_e( 'Tutorial', 'yaatra' ); ?></h3>
            </div>
            <div class="block-content">
                <p>
                    <?php printf( wp_kses_post( 'This tutorial has been prepared for those who have a basic knowledge of HTML and CSS and has an urge to develop websites. After completing this tutorial, you will find yourself at a moderate level of expertise in developing sites or blogs using WordPress.', 'yaatra' ), $this->theme_name ); ?>
                    <a href="<?php echo esc_url( 'https://wpallresources.com/' ); ?>" target="_blank"><?php esc_html_e( 'WP Tutorials', 'yaatra' ); ?><span class="dashicons dashicons-external"></span></a>
                </p>
            </div><!-- .block-content -->
        </div><!-- .welcome-block tutorial -->
<?php
    }

    /**
     * Render the starter sites screen.
     */
    public function yaatra_starter_screen() {
        global $admin_main_class;

        $is_child_theme = is_child_theme();
        $parent_theme = $activated_theme = get_template();

        $demodata = get_transient( 'yaatra_demo_packages' );

        if ( empty( $demodata ) || false === $demodata ) {
            $demodata = get_transient( 'cvdi_theme_packages' );
            if ( $demodata ) {
                set_transient( 'yaatra_demo_packages', $demodata, WEEK_IN_SECONDS );
            }
        }

        $activated_demo_check = get_option( 'cvdi_activated_check' );

        $is_demo_empty = empty( $demodata );

?>

        <div id="cv-dashboard">
            <?php $this->yaatra_dashboard_header(); ?>

            <div class="dashboard-content-wrapper starter-dashboard-content-wrapper">
                <div class="cv-container">
                    <div class="main-content starter-content-wrapper">
                        <div class="cv-theme-demos rendered <?php echo $is_demo_empty ? 'no-demo-sites' : ''; ?>">
                            <?php $admin_main_class->yaatra_install_demo_import_plugin_popup(); ?>

                            <div class="demo-listing-wrapper wp-clearfix">
                                <?php if ( $is_demo_empty ): ?>
                                    <span class="configure-msg">
                                        <?php esc_html_e( 'No demos are configured for this theme, please contact the theme author', 'yaatra' ); ?>
                                    </span>
                                <?php else: ?>
                                    <div class="all-demos-wrap">
                                        <?php
                                        // List child theme demos if they exist
                                        if ( $is_child_theme ) {
                                            $is_child_exists = $demodata[$parent_theme]['is_child_exists'] ?? false;
                                            if ( $is_child_exists ) {
                                                $child_theme_demodatas = $demodata[$parent_theme]['child_themes'];
                                                foreach ( $child_theme_demodatas as $child_theme_value ) {
                                                    $this->yaatra_render_demo( $child_theme_value, $activated_theme, $activated_demo_check );
                                                }
                                            }
                                        }

                                        foreach ( $demodata as $value ) {
                                            if ( $this->should_display_demo( $value['theme_slug'], $activated_theme ) ) {
                                                $this->yaatra_render_demo( $value, $activated_theme, $activated_demo_check );
                                            }
                                        }
                                        ?>
                                    </div><!-- .all-demos-wrap -->
                                <?php endif; ?>
                            </div><!-- .demo-listing-wrapper -->
                        </div><!-- .cv-theme-demos -->
                    </div><!-- .starter-content-wrapper -->
                </div><!-- .cv-container -->
            </div><!-- .dashboard-content-wrapper -->
        </div><!-- #cv-dashboard -->

<?php
    }

    /**
     * Render a single demo item.
     *
     * @param array $demo_data Demo data.
     * @param string $activated_theme Activated theme slug.
     * @param string $activated_demo_check Slug of the activated demo.
     */
    private function yaatra_render_demo( $demo_data, $activated_theme, $activated_demo_check ) {
        $theme_name         = $demo_data['name'];
        $theme_slug         = $demo_data['theme_slug'];
        $preview_screenshot = $demo_data['preview_screen'];
        $demourl            = $demo_data['preview_url'];
        $is_pro_theme       = strpos( $theme_slug, 'pro' ) !== false;
        $is_pro_active      = strpos( $activated_theme, 'pro' ) !== false;
        $is_theme_activated = $activated_demo_check === $theme_slug;
        $purchase_url       = $is_pro_theme && !$is_pro_active ? "https://codevibrant.com/wpthemes/" . explode( "-pro", $theme_slug )[0] . "-pro" : '';
?>

        <div class="single-demo <?php echo !$is_pro_active && $is_pro_theme ? 'pro-demo' : ''; ?>" data-categories="ltrdemo" data-name="<?php echo esc_attr( $theme_slug ); ?>" style="display: block;">
            <div class="preview-screenshot">
                <a href="<?php echo esc_url( $demourl ); ?>" target="_blank">
                    <img class="preview" src="<?php echo esc_url( $preview_screenshot ); ?>" />
                </a>
            </div><!-- .preview-screenshot -->
            <div class="demo-info-wrapper">
                <h2 class="cvdi-theme-name theme-name"><?php echo esc_html( $theme_name ); ?></h2>
                <div class="cvdi-theme-actions theme-actions">
                    <?php if ( $is_theme_activated ): ?>
                        <a class="button disabled button-primary hide-if-no-js" href="javascript:void(0);" data-name="<?php echo esc_attr( $theme_name ); ?>" data-slug="<?php echo esc_attr( $theme_slug ); ?>" aria-label="<?php printf( esc_html__( 'Imported %1$s', 'yaatra' ), $theme_name ); ?>">
                            <?php esc_html_e( 'Imported', 'yaatra' ); ?>
                        </a>
                    <?php else: ?>
                        <?php if ( is_plugin_active( 'cv-demo-importer/cv-demo-importer.php' ) ): ?>
                            <a class="button button-primary hide-if-no-js cvdi-demo-import" href="javascript:void(0);" data-name="<?php echo esc_attr( $theme_name ); ?>" data-slug="<?php echo esc_attr( $theme_slug ); ?>" aria-label="<?php printf( esc_attr__( 'Import %1$s', 'yaatra' ), $theme_name ); ?>">
                                <?php esc_html_e( 'Import', 'yaatra' ); ?>
                            </a>
                        <?php else: ?>
                            <a title="<?php esc_attr_e( 'Demo importer plugin is not installed or activated', 'yaatra' ); ?>" class="button button-primary hide-if-no-js cvdi-demo-import" href="javascript:void(0);" data-name="<?php echo esc_attr( $theme_name ); ?>" data-slug="<?php echo esc_attr( $theme_slug ); ?>" aria-label="<?php printf( esc_attr__( 'Import %1$s', 'yaatra' ), $theme_name ); ?>">
                                <?php esc_html_e( 'Import', 'yaatra' ); ?>
                            </a>
                        <?php endif; ?>
                        <?php if ( $purchase_url ): ?>
                            <a class="button button-primary cvdi-purchasenow" href="<?php echo esc_url( $purchase_url ); ?>" target="_blank" data-name="<?php echo esc_attr( $theme_name ); ?>" data-slug="<?php echo esc_attr( $theme_slug ); ?>" aria-label="<?php printf( esc_html__( 'Purchase Now', 'yaatra' ), $theme_name ); ?>">
                                <?php esc_html_e( 'Buy Now', 'yaatra' ); ?>
                            </a>
                        <?php endif; ?>
                    <?php endif; ?>
                    <a class="button preview install-demo-preview" target="_blank" href="<?php echo esc_url( $demourl ); ?>">
                        <?php esc_html_e( 'View Demo', 'yaatra' ); ?>
                    </a>
                </div><!-- .cvdi-theme-actions -->
            </div><!-- .demo-info-wrapper -->
        </div><!-- .single-demo -->

        <?php
    }

    /**
     * Check if a demo should be displayed.
     *
     * @param string $theme_slug Theme slug.
     * @param string $activated_theme Activated theme slug.
     * @return bool
     */
    private function should_display_demo( $theme_slug, $activated_theme ) {
        return ( strpos( $activated_theme, 'pro' ) !== false && strpos( $theme_slug, 'pro' ) !== false) || ( strpos( $activated_theme, 'pro' ) === false );
    }

    /**
     * Render the free vs pro screen.
     */
    public function yaatra_free_pro_screen() {
        $features = array( 
            'Price' => array( 'Free', '$59' ),
            'Import Demo Data' => array( '<span class="dashicons dashicons-no"></span>', '<span class="dashicons dashicons-yes"></span>'),
            'Pre Loaders' => array( '<span class="dashicons dashicons-no"></span>', '<span class="dashicons dashicons-yes"></span>' ),
            'Header Layouts' => array( '1', '3'),
            'Archive Pages Layouts' => array( '1', '3' ),
            'Excerpt Length on Archive Options' => array( '<span class="dashicons dashicons-no"></span>', '<span class="dashicons dashicons-yes"></span>' ),
            'Drop Caps Options' => array( '<span class="dashicons dashicons-no"></span>', '<span class="dashicons dashicons-yes"></span>' ),
            'Related Posts on Post Page' => array( '<span class="dashicons dashicons-yes"></span>', '<span class="dashicons dashicons-yes"></span>' ),
            'Image Hover Effects Options' => array( '<span class="dashicons dashicons-no"></span>', '<span class="dashicons dashicons-yes"></span>' ),
            'Sidebar Sticky Options' => array( '<span class="dashicons dashicons-no"></span>', '<span class="dashicons dashicons-yes"></span>' ),
            'Google Fonts' => array( '2', '600+' ),
            'Breadcrumbs' => array( '<span class="dashicons dashicons-no"></span>', '<span class="dashicons dashicons-yes"></span>' ),
            'Typography Options' => array( '<span class="dashicons dashicons-no"></span>', '<span class="dashicons dashicons-yes"></span>' ),
            'WooCommerce Compatible' => array( '<span class="dashicons dashicons-yes"></span>', '<span class="dashicons dashicons-yes"></span>' ),
            'Featured Section' => array( '<span class="dashicons dashicons-no"></span>', '<span class="dashicons dashicons-yes"></span>' ),
            'Custom 404 Page' => array( '<span class="dashicons dashicons-no"></span>', '<span class="dashicons dashicons-yes"></span>' ),
            'WordPress Page Builders Compatible' => array( '<span class="dashicons dashicons-yes"></span>', '<span class="dashicons dashicons-yes"></span>' ),
        );
        
?>
        <div id="cv-dashboard">
            <?php $this->yaatra_dashboard_header(); ?>

            <div class="dashboard-content-wrapper">
                <div class="cv-container">
                    <div class="main-content free-pro-content-wrapper">
                        <table class="compare-table">
                            <thead>
                                <tr>
                                    <th class="table-feature-title">
                                        <h3><?php esc_html_e( 'Features', 'yaatra' ); ?></h3>
                                    </th>
                                    <th><h3><?php esc_html_e( 'Yaatra', 'yaatra' ); ?></h3></th>
                                    <th><h3><?php esc_html_e( 'Yaatra Pro', 'yaatra' ); ?></h3></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ( $features as $feature => [ $free, $pro ] ): ?>
                                    <tr>
                                        <td><h3><?php echo esc_html( $feature ); ?></h3></td>
                                        <td><?php echo wp_kses_post( $free ); ?></td>
                                        <td><?php echo wp_kses_post( $pro ); ?></td>
                                    </tr>
                                <?php endforeach; ?>
                                <tr>
                                    <td></td>
                                    <td></td>
                                    <td class="btn-wrapper">
                                        <a href="https://codevibrant.com/wpthemes/yaatra-pro/" class="button button-secondary docs" target="_blank">
                                            <?php esc_html_e( 'Buy Pro', 'yaatra' ); ?>
                                        </a>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div><!-- .free-pro-content-wrapper -->

                    <?php $this->yaatra_dashboard_sidebar(); ?>
                </div><!-- .cv-container -->
            </div><!-- .dashboard-content-wrapper -->
        </div><!-- #cv-dashboard -->
<?php
    }

    /**
     * Render the changelog screen.
     */
    public function yaatra_changelog_screen() {
        global $admin_main_class;

        $changelogFilePath = is_child_theme()
            ? get_stylesheet_directory() . '/changelog.txt'
            : get_template_directory() . '/changelog.txt';

        $get_changelog = $admin_main_class->get_changelog( $changelogFilePath );

        // Helper function to render changelog notes
        $render_notes = function( $notes, $class, $title ) {
            if ( empty( $notes ) ) {
                return '';
            }
            $output = '';
            foreach ( $notes as $note ) {
                $output .= sprintf(
                    '<li><span class="%s" title="%s">%s</span>%s</li>',
                    esc_attr( $class ),
                    esc_attr( $title ),
                    esc_html( $title[0] ),
                    esc_html( $note )
                );
            }
            return $output;
        };
        
?>
        <div id="cv-dashboard">
            <?php $this->yaatra_dashboard_header(); ?>

            <div class="dashboard-content-wrapper">
                <div class="changelog-top-wrapper">
                    <ul class="cv-container">
                        <li>
                            <span class="new"><?php esc_html_e( 'N', 'yaatra' ); ?></span>
                            <?php esc_html_e( 'New', 'yaatra' ); ?>
                        </li>
                        <li>
                            <span class="improvement"><?php esc_html_e( 'I', 'yaatra' ); ?></span>
                            <?php esc_html_e( 'Improvement', 'yaatra' ); ?>
                        </li>
                        <li>
                            <span class="fixed"><?php esc_html_e( 'F', 'yaatra' ); ?></span>
                            <?php esc_html_e( 'Fixed', 'yaatra' ); ?>
                        </li>
                        <li>
                            <span class="tweak"><?php esc_html_e( 'T', 'yaatra' ); ?></span>
                            <?php esc_html_e( 'Tweak', 'yaatra' ); ?>
                        </li>
                    </ul>
                </div><!-- .changelog-top-wrapper -->

                <div class="cv-container">
                    <div class="changelog-content-wrapper">
                        <?php foreach ( $get_changelog as $log ): ?>
                            <section class="changelog-block">
                                <div class="block-top">
                                    <span class="block-version">
                                        <?php printf( esc_html__( 'Version: %1$s', 'yaatra' ), esc_html( $log['version'] ) ); ?>
                                    </span>
                                    <span class="block-date">
                                        <?php printf( esc_html__( 'Released on: %1$s', 'yaatra' ), esc_html( $log['date'] ) ); ?>
                                    </span>
                                </div><!-- .block-top -->
                                <div class="block-content">
                                    <ul>
                                        <?php
                                            echo $render_notes( $log['new'] ?? [], 'new', 'New' );
                                            echo $render_notes( $log['imp'] ?? [], 'improvement', 'Improvement' );
                                            echo $render_notes( $log['fix'] ?? [], 'fixed', 'Fixed' );
                                            echo $render_notes( $log['tweak'] ?? [], 'tweak', 'Tweak' );
                                        ?>
                                    </ul>
                                </div><!-- .block-content -->
                            </section><!-- .changelog-block -->
                        <?php endforeach; ?>
                    </div><!-- .changelog-content-wrapper -->

                    <?php $this->yaatra_dashboard_sidebar(); ?>
                </div><!-- .cv-container -->
            </div><!-- .dashboard-content-wrapper -->
        </div><!-- #cv-dashboard -->
<?php
    }

    /**
     * Render the plugin screen.
     */
    public function yaatra_plugin_screen() {
        global $admin_main_class;

        $free_plugins = $this->free_plugins;

        // Helper function to render plugin button
        $render_button = function( $action, $slug ) {
            $btn_class = '';
            $label = '';

            switch ( $action ) {
                case 'install':
                    $btn_class = 'cv-action-btn install-online button';
                    $label = esc_html__( 'Install and Activate', 'yaatra' );
                    break;

                case 'inactive':
                    $btn_class = 'cv-action-btn deactivate-online button disabled';
                    $label = esc_html__( 'Deactivate', 'yaatra' );
                    break;

                case 'active':
                    $btn_class = 'cv-action-btn activate-online button button-primary';
                    $label = esc_html__( 'Activate', 'yaatra' );
                    break;
            }

            return sprintf(
                '<button class="%s" data-status="%s" data-slug="%s" data-redirect="%s">%s</button>',
                esc_attr( $btn_class ),
                esc_attr( $action ),
                esc_attr( $slug ),
                esc_url( admin_url( 'themes.php' ) . '?page=yaatra-dashboard&tab=yaatra_plugin' ),
                esc_html( $label )
            );
        };

?>
        <div id="cv-dashboard">
            <?php $this->yaatra_dashboard_header(); ?>

            <div class="dashboard-content-wrapper">
                <div class="cv-container">
                    <div class="plugin-content-wrapper">
                        <div class="header-content">
                            <h3><?php esc_html_e( 'Recommended Free Plugins', 'yaatra' ); ?></h3>
                            <p><?php esc_html_e( 'These Free Plugins might be handy for you.', 'yaatra' ); ?></p>
                        </div><!-- .header-content -->

                        <div class="plugin-listing">
                            <?php foreach ( $free_plugins as $value ): ?>
                                <div class="single-plugin-wrap">
                                    <div class="plugin-thumb-wrap">
                                        <div class="plugin-thumb">
                                            <?php if ( !empty( $value['icon_url'] ) ): ?>
                                                <img src="<?php echo esc_url( $value['icon_url'] ); ?>" alt="<?php echo esc_attr( $value['name'] ); ?>">
                                            <?php endif; ?>
                                        </div>
                                    </div><!-- .plugin-thumb-wrap -->

                                    <div class="plugin-content-wrap">
                                        <h4 class="plugin-name"><?php echo esc_html( $value['name'] ); ?></h4>
                                        <div class="plugin-meta-wrap">
                                            <span class="version"><?php printf( esc_html__( 'Version %1$s', 'yaatra'), esc_html($value['version'] ) ); ?></span>
                                            <span class="seperator">|</span>
                                            <span class="author"><?php echo wp_kses_post( $value['author'] ); ?></span><br>
                                            <span class="description"><?php echo wp_kses_post( $value['description'] ); ?></span>
                                        </div><!-- .plugin-meta-wrap -->

                                        <div class="plugin-button-wrap plugin-card-<?php echo esc_attr( $value['slug'] ); ?>">
                                            <?php echo $render_button( $value['action'], $value['slug'] ); ?>
                                        </div><!-- .plugin-button-wrap -->
                                    </div><!-- .plugin-content-wrap -->
                                </div><!-- .single-plugin-wrap -->
                            <?php endforeach; ?>
                        </div><!-- .plugin-listing -->
                    </div><!-- .plugin-content-wrapper -->

                    <?php $this->yaatra_dashboard_sidebar(); ?>
                </div><!-- .cv-container -->
            </div><!-- .dashboard-content-wrapper -->
        </div><!-- #cv-dashboard -->
<?php
    }
    
}

// Initialize the class
new Yaatra_Admin_Dashboard();
