<?php 
// Prevent direct access
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

// Start output buffering if not already started
if (!ob_get_level()) {
    ob_start();
}

// Verify nonce for incoming GET requests
if (isset($_GET['aifilterpage']) && isset($_GET['_wpnonce'])) {
    if (!wp_verify_nonce(sanitize_key($_GET['_wpnonce']), 'unleash_ai_aifilterpage_nonce')) {
        wp_die(esc_html__('Invalid request. Nonce verification failed.', 'unleashai'));
    }
}

  
// Current page number for filter
$aifilterpage = isset($_GET['aifilterpage']) ? absint($_GET['aifilterpage']) : 10;


$stored_response = get_option('unleash_ai_registration_response');
if ($stored_response) {
    $stored_response = json_decode($stored_response, true); // Decode if needed
}
if (empty($stored_response)) { ?>
			  <div>You are not logged in. Please log in <a href="?page=cg_meta_al">here</a> to continue.</div>
<?php exit; } ?>

<div class="flexButtonsCg">
    <div class="gridFlexCg">
        <ul class="buttonsCg">
            <li><a id="pages-tab" href="admin.php?page=cg_meta_al_list" class="normalBtnCG">Pages/Post</a></li>
			<li><a id="posts-tab" href="admin.php?page=cg_meta_al_altag" class="normalBtnCG normalBtnCGV2">Alt Tags</a></li>
           <!-- <li><a id="posts-tab" href="admin.php?page=cg_meta_al_product" class="normalBtnCG normalBtnCGV2">Products</a></li>-->
            
        </ul>
    </div>


     <div class="gridFlexCg justifyRight">
        <ul class="buttonsCg">
		
		<!--<li><a style="cursor:pointer;" onclick="metakeywordailogout()" herf="javascript:void(0);" class="normalBtnCG normalBtnCGV3 metakeywordailogout">Logout</a></li>
		 --->
		 
         <li>
    <select id="page_filter" onchange="call_filter();">
        <option value=""><?php esc_html_e('Select Filter', 'unleashai'); ?></option>
        <option value="10" <?php selected($aifilterpage, 10); ?>>10</option>
        <option value="30" <?php selected($aifilterpage, 30); ?>>30</option>
        <option value="50" <?php selected($aifilterpage, 50); ?>>50</option>
    </select>
</li>
<li>
    <div class="flexDirectionCg flexDirectionCgSB" style="margin:0;">
        <div class="space-betweenCls" style="order:2;">
            <div class="generatePlain heightBtnCenter2">
                <button class="btnWdH clickToExpand fetch_dataall"><?php esc_html_e('Generate All', 'unleashai'); ?></button>
            </div>
            <div class="generatePlain heightBtnCenter2">
                <button class="btnWdH blueBtncg clickToExpand update_btnall"><?php esc_html_e('Update All', 'unleashai'); ?></button>
            </div>
        </div>
    </div>
</li>

	<!-- 
            <li><a style="cursor:pointer;" class="normalBtnCG normalBtnCGV3 bulk_gen">Generate Bulk Information</a></li>
            <li><a style="cursor:pointer;" class="normalBtnCG normalBtnCGV3 bulk_update">Update Bulk Information</a></li>
			-->
        </ul>
    </div> 
</div>


<div class="tableStCg" class="tab-pane fade show active" id="pages" role="tabpanel" aria-labelledby="pages-tab">

    <div class="scrollTable">

        <div class="bgAreaGray37">
		
            <table cellpadding="0" cellspacing="0">
                <tbody>

                    <tr class="flexDs">

                        <td class="wd30s noBGColor noBGColor2 relabs20007">
						
				  <label class="containerCheckmark absCheckbocLeft selectall abs45New">
                    <input type="checkbox" id="selectAllCheckbox" class="selectall_checkbox">
                    <span class="checkmark"></span>
                  </label>
                  <table cellpadding="0" cellspacing="0" class="boldTd">
                <tbody>
                    <tr>
                        <td><b><?php esc_html_e('Page Title', 'unleashai'); ?></b></td>
                        <td><b><?php esc_html_e('URLs', 'unleashai'); ?></b></td>
                    </tr>
                </tbody>
            </table>


                        </td>

                        <td class="wd70s noBGColor noBGColor2">
                            <table cellpadding="0" cellspacing="0">
                                <tbody>
                                    <tr class="rightFlexForm">
                                        <td class="wd80s">
                                            <table cellpadding="0" cellspacing="0" class="boldTd">
                                                <tbody>
                                                <tr class="flexWdTd">
    <td>
        <label class="containerCheckmark">
            <b><?php esc_html_e('Meta Title', 'unleashai'); ?></b>
            <!-- <input type="checkbox" class="meta_titl_desc" name="meta_titl"><span class="checkmark"></span> -->
        </label>
    </td>
    <td>
        <label class="containerCheckmark">
            <b><?php esc_html_e('Meta Description', 'unleashai'); ?></b>
            <!-- <input type="checkbox" class="meta_desc" name="meta_des"><span class="checkmark"></span> -->
        </label>
    </td>
</tr>


                                                </tbody>
                                            </table>

                                        </td>


                                        <td class="wd20s">
                                            <table cellpadding="0" cellspacing="0" class="boldTd">
                                                <tbody>
                                                    <tr>

                                                    <td><b><?php esc_html_e('Action', 'unleashai'); ?></b></td>


                                                    </tr>
                                                </tbody>
                                            </table>

                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </td>


                    </tr>
                </tbody>
            </table>
        </div>
        <div class="borderSeparate">
 <?php

            // Get the current page number from the query string, default to 1 if not set
            $paged = isset($_GET['paged']) ? sanitize_key($_GET['paged']) : 1;

            // Arguments for the WP_Query to fetch posts and pages
            $args = array(
                'post_type' => array('post', 'page'), // Include both posts and pages
                'post_status' => 'publish', // Only published posts/pages
                'posts_per_page' => $aifilterpage, // Limiting to 10 posts/pages per page (variable $aifilterpage)
                'offset' => ($paged - 1) * $aifilterpage, // Offset calculation based on current page
            );

            // Create a new WP_Query instance with the specified arguments
            $query = new WP_Query($args);

            // Loop through the posts/pages retrieved by the query

            $il = 1;
            while ($query->have_posts()) : $query->the_post();
                // Get the ID of the current post/page
                $post_id = get_the_ID();

                // Initialize variables for SEO title and description
                $seo_title = get_post_meta($post_id, '_yoast_wpseo_title', true);
                $seo_description = get_post_meta($post_id, '_yoast_wpseo_metadesc', true);

                // Check which SEO plugin is active and get the corresponding meta values
                if ($plgactive == "cg_aioseo") {
                    $seo_title = get_post_meta($post_id, '_yoast_wpseo_title', true);
                    $seo_description = get_post_meta($post_id, '_yoast_wpseo_metadesc', true);
                } else if ($plgactive == "cg_yoast") {
                    $seo_title = get_post_meta($post_id, '_yoast_wpseo_title', true);
                    $seo_description = get_post_meta($post_id, '_yoast_wpseo_metadesc', true);
                } else if ($plgactive == "cg_rank_math") {
                    $seo_title = get_post_meta($post_id, 'rank_math_title', true);
                    $seo_description = get_post_meta($post_id, 'rank_math_description', true);
                }

                if ($il == 1) {
                    $activsl = "active";
                    $displaysty = "display:block;";
                } else {
                    $activsl = "";
                    $displaysty = "display:none;";
                }

            ?>



                <!---------------------------->
               <button id="accordionBtn_<?php echo esc_attr($il); ?>" class="accordionBtn <?php echo esc_attr($activsl); ?>">
    <?php the_permalink(); ?>
    <label class="containerCheckmark absCheckbocLeft">
        <input type="checkbox" class="pre_title_checkbox pageitem" reqatt="0" name="meta_t_chk_0_<?php echo esc_attr($post_id); ?>" data-id="<?php echo esc_attr($post_id); ?>" value="0">
        <span class="checkmark"></span>
    </label>
</button>
<div class="bgAreaGray37 panel" style="<?php echo esc_attr($displaysty); ?>">

                    <table cellpadding="0" cellspacing="0">
                        <tbody>



                            <tr class="flexDs">

                                <td class="wd30s noBGColor">

                                    <table cellpadding="0" cellspacing="0">
                                        <tbody>
                                            <tr>
                                            <td>
                                                    <div class="titLeLink titLeLinkWeight500"><?php the_title(); ?></a>
                                                </td>
                                                <td>
                                                    <div class="titLeLink"><a href="<?php the_permalink(); ?>"><?php the_permalink(); ?></a></div>
                                                </td>
                                            </tr>



                                        </tbody>
                                    </table>

                                </td>

                                <td class="wd70s noBGColor">
                                    <table cellpadding="0" cellspacing="0">
                                        <tbody>
                                            <tr class="rightFlexForm">
                                                <td class="wd80s">
                                                    <table cellpadding="0" cellspacing="0">
                                                        <tbody>
                                                            <tr class="flexWdTd">
                                                            <td>
    <div class="flexCheckCg flexCheckCgNoFlex extraHeight">
        <div class="flexCheckCgChild flexCheckCgChildV2">
            <label class="containerCheckmark">
                <input type="checkbox" class="title_checkbox" name="metapretitle_<?php echo esc_attr($post_id); ?>" value="2">
                <span class="checkmark"></span>
            </label>
        </div>
        <div class="flexCheckCgChild PDleft22">
            <textarea name="meta_title_<?php echo esc_attr($post_id); ?>" readonly><?php echo esc_html($seo_title); ?></textarea>
        </div>
    </div>
</td>
<td>
    <div class="flexCheckCg flexCheckCgNoFlex extraHeight">
        <div class="flexCheckCgChild flexCheckCgChildV2">
            <label class="containerCheckmark">
                <input type="checkbox" class="title_checkbox" name="metapredescription_<?php echo esc_attr($post_id); ?>" value="2">
                <span class="checkmark"></span>
            </label>
        </div>
        <div class="flexCheckCgChild PDleft22">
            <textarea name="meta_des_<?php echo esc_attr($post_id); ?>" id="meta_des_<?php echo esc_attr($post_id); ?>" readonly><?php echo esc_html($seo_description); ?></textarea>
        </div>
    </div>
</td>

                                                            </tr>

                                                        </tbody>
                                                    </table>

                                                </td>


                                                <td class="wd20s">
                                                    <table cellpadding="0" cellspacing="0">
                                                        <tbody>
                                                            <tr>

                                                                <td class="centerAppBtn">
                                                                </td>

                                                            </tr>
                                                        </tbody>
                                                    </table>

                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </td>







                            </tr>
                            <tr>
                            <td>
    <h5><?php echo esc_html__('Focus Keywords', 'unleashai'); ?></h5>
</td>

                            </tr>
                            <tr class="flexDs">

                                <td class="wd30s">
                                    <table cellpadding="0" cellspacing="0">
                                        <tbody>
                                            <tr>
                                            <td>
    <div class="flexCheckCg flexCheckCgNoFlex">
        <div class="flexCheckCgChild">
            <textarea 
                name="meta_focus_<?php echo esc_attr($post_id); ?>" 
                class="postp_title" 
                placeholder="<?php echo esc_attr__('Enter your focus keywords, comma separated', 'unleashai'); ?>">
            </textarea>
        </div>
    </div>
</td>

                                            </tr>

                                        </tbody>
                                    </table>

                                </td>

                                <td class="wd70s">
                                    <table cellpadding="0" cellspacing="0">
                                        <tbody>
                                            <tr class="rightFlexForm" id="page_<?php echo  esc_attr($post_id); ?>">
                                                <td class="wd80s">
                                                    <table cellpadding="0" cellspacing="0">
                                                        <tbody>
                                                            <tr class="flexWdTd">
                                                                <td>
                                                                    <div class="flexCheckCg flexCheckCgNoFlex extraHeight">
                                                                        <div class="flexCheckCgChild flexCheckCgChildV2"><label class="containerCheckmark"><input type="checkbox" class="pre_title_checkbox" reqatt="0" name="meta_t_chk_0_<?php echo esc_attr($post_id); ?>" value="0"><span class="checkmark"></span></label></div>
                                                                        <div class="flexCheckCgChild PDleft22"><textarea class="postp_title" name="meta_title_0_<?php echo  esc_attr($post_id); ?>" placeholder=""></textarea></div>

                                                                    </div>
                                                                </td>
                                                                <td>
                                                                    <div class="flexCheckCg flexCheckCgNoFlex extraHeight">
                                                                        <div class="flexCheckCgChild flexCheckCgChildV2"><label class="containerCheckmark"><input type="checkbox" class="pre_des_chk" desreqatt="0" name="meta_d_0_chkd_<?php echo  esc_attr($post_id); ?>" value="0"><span class="checkmark"></span></label></div>
                                                                        <div class="flexCheckCgChild PDleft22"><textarea class="postp_title" name="meta_des_0_<?php echo  esc_attr($post_id); ?>" placeholder=""></textarea></div>

                                                                    </div>
                                                                </td>
                                                            </tr>
                                                            <tr class="flexWdTd">
                                                                <td>
                                                                    <div class="flexCheckCg flexCheckCgNoFlex extraHeight">
                                                                        <div class="flexCheckCgChild flexCheckCgChildV2"><label class="containerCheckmark"><input type="checkbox" class="pre_title_checkbox" reqatt="1" name="meta_t_chk_1_<?php echo  esc_attr($post_id); ?>" value="1"><span class="checkmark"></span></label></div>
                                                                        <div class="flexCheckCgChild PDleft22"><textarea class="postp_title" name="meta_title_1_<?php echo  esc_attr($post_id); ?>" placeholder=""></textarea></div>

                                                                    </div>
                                                                </td>
                                                                <td>
                                                                    <div class="flexCheckCg flexCheckCgNoFlex extraHeight">
                                                                        <div class="flexCheckCgChild flexCheckCgChildV2"><label class="containerCheckmark"><input type="checkbox" class="pre_des_chk" desreqatt="1" name="meta_d_1_chkd_<?php echo  esc_attr($post_id); ?>" value="1"><span class="checkmark"></span></label></div>
                                                                        <div class="flexCheckCgChild PDleft22"><textarea class="postp_title" name="meta_des_1_<?php echo  esc_attr($post_id); ?>" placeholder=""></textarea></div>

                                                                    </div>
                                                                </td>
                                                            </tr>
                                                            <tr class="flexWdTd">
                                                                <td>
                                                                    <div class="flexCheckCg flexCheckCgNoFlex extraHeight">
                                                                        <div class="flexCheckCgChild flexCheckCgChildV2"><label class="containerCheckmark"><input type="checkbox" class="pre_title_checkbox" reqatt="2" name="meta_t_chk_2_<?php echo  esc_attr($post_id); ?>" value="2"><span class="checkmark"></span></label></div>
                                                                        <div class="flexCheckCgChild PDleft22"><textarea class="postp_title" name="meta_title_2_<?php echo  esc_attr($post_id); ?>" placeholder=""></textarea></div>

                                                                    </div>
                                                                </td>
                                                                <td>
                                                                    <div class="flexCheckCg flexCheckCgNoFlex extraHeight">
                                                                        <div class="flexCheckCgChild flexCheckCgChildV2"><label class="containerCheckmark"><input type="checkbox" class="pre_des_chk" desreqatt="2" name="meta_d_2_chkd_<?php echo  esc_attr($post_id); ?>" value="2"><span class="checkmark"></span></label></div>
                                                                        <div class="flexCheckCgChild PDleft22"><textarea class="postp_title" name="meta_des_2_<?php echo  esc_attr($post_id); ?>" placeholder=""></textarea></div>

                                                                    </div>
                                                                </td>
                                                            </tr>
                                                        </tbody>
                                                    </table>

                                                </td>

                                                <input type="hidden" id="regen_<?php echo  esc_attr($post_id); ?>" value="">
                                                <td class="wd20s">
                                                    <table cellpadding="0" cellspacing="0">
                                                        <tbody>
                                                            <tr>

                                                                <td class="centerAppBtn">
                                                                 <div class="generatePlain heightBtnCenter2"><button class="btnWdH fetch_data" typattrb="meta_both" data-id="<?php echo esc_attr($post_id); ?>" data-content ="<?php echo esc_html($seo_description); ?>">Generate</button></div>
																	<div class="generatePlain heightBtnCenter2"><button class="btnWdH blueBtncg update-btn" data-page-id="<?php echo esc_attr($post_id); ?>">Update</button></div>
                                                                </td>

                                                            </tr>
                                                        </tbody>
                                                    </table>

                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </td>







                            </tr>

                        </tbody>
                    </table>
                </div>
                <!------------------------------>

            <?php //endforeach;
                $il = $il + 1;
            endwhile;
            // Restore original Post Data
            wp_reset_postdata();
            ?>
        </div>
        <?php

        // Get the total number of published pages
        $total_pages = ceil(wp_count_posts('page')->publish / $aifilterpage);

        // Get the current page number
        // $paged = max(1, get_query_var('paged', 1));
        $paged = isset($_GET['paged']) ? sanitize_key($_GET['paged']) : 1; // Current page number

        // Check if there's more than one page
        if ($total_pages > 1) {
            echo '<div class="flexDirectionCg flexDirectionCgSB">'; // Opening div tag
            echo '<div class="space-betweenCls" style="order:2;">
            <div class="generatePlain heightBtnCenter2">
                <button class="btnWdH clickToExpand fetch_dataall">' . esc_html__('Generate All', 'unleashai') . '</button>
            </div>
            <div class="generatePlain heightBtnCenter2">
                <button class="btnWdH blueBtncg clickToExpand update_btnall">' . esc_html__('Update All', 'unleashai') . '</button>
            </div>
        </div>';
        
									echo '<div class="space-betweenCls"  style="order:1;">';
           
            $base_url = esc_url_raw(add_query_arg('paged', '%#%'));

            echo  wp_kses_post(paginate_links(array(
                'base'      => $base_url . '%_%',  // Correct base URL for pagination
                'format'    => '',
                'current'   => $paged,
                'total'     => $total_pages,
                'mid_size'  => 2,
                'prev_text' => esc_html__('« Previous', 'unleashai'),
                'next_text' => esc_html__('Next »', 'unleashai'),
            )));
        
            echo '</div></div>';
         //   echo '</div></div>'; // Closing div tag
        }

        ?>

    </div>
</div>
