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

$user_id = $user->ID;

// Get current page from query string for posts pagination
$posts_page     = isset( $_GET['posts_page'] ) ? max( 1, absint( wp_unslash( $_GET['posts_page'] ) ) ) : 1;
$posts_per_page = 10;

// Get user posts using WP_Query
$post_args = [
    'author'         => $user_id,
    'post_status'    => 'publish',
    'posts_per_page' => $posts_per_page,
    'paged'          => $posts_page,
];

$user_posts = new WP_Query( $post_args );

// Prepare pagination data
$pagination = [
    'total_pages'   => $user_posts->max_num_pages,
    'current_page'  => $posts_page,
    'per_page'      => $posts_per_page,
    'total_items'   => $user_posts->found_posts,
];
?>

<div class="wpuf-profile-section wpuf-posts-section !wpuf-mb-8">
    <h1 class="profile-section-heading !wpuf-text-2xl !wpuf-font-bold !wpuf-text-gray-700 !wpuf-mb-5 !wpuf-pb-3">
        <?php esc_html_e( 'Posts', 'wpuf-pro' ); ?>
    </h1>
    
    <?php if ( $user_posts->have_posts() ) : ?>
        <div class="posts-table !wpuf-bg-white !wpuf-rounded-lg !wpuf-border !wpuf-border-gray-200 !wpuf-overflow-hidden">
            <!-- Table Header -->
            <div class="table-header !wpuf-border-b !wpuf-border-gray-200 !wpuf-h-[50px] !wpuf-flex !wpuf-items-center">
                <div class="!wpuf-grid !wpuf-grid-cols-12 !wpuf-gap-4 !wpuf-px-4 !wpuf-items-center !wpuf-w-full">
                    <div class="!wpuf-col-span-5 !wpuf-text-sm !wpuf-font-medium !wpuf-text-gray-700">
                        <?php esc_html_e( 'Post Title', 'wpuf-pro' ); ?>
                    </div>
                    <div class="!wpuf-col-span-4 !wpuf-text-sm !wpuf-font-medium !wpuf-text-gray-700">
                        <?php esc_html_e( 'Publish Date', 'wpuf-pro' ); ?>
                    </div>
                    <div class="!wpuf-col-span-3 !wpuf-text-sm !wpuf-font-medium !wpuf-text-gray-700">
                        <?php esc_html_e( 'Link', 'wpuf-pro' ); ?>
                    </div>
                </div>
            </div>
            
            <!-- Table Body -->
            <div class="table-body">
                <?php while ( $user_posts->have_posts() ) : $user_posts->the_post(); ?>
                <div class="table-row !wpuf-border-b !wpuf-border-gray-100 last:!wpuf-border-b-0 hover:!wpuf-bg-purple-50 !wpuf-transition-colors !wpuf-duration-150 !wpuf-h-[58px] !wpuf-flex !wpuf-items-center">
                    <div class="!wpuf-grid !wpuf-grid-cols-12 !wpuf-gap-4 !wpuf-px-4 !wpuf-items-center !wpuf-w-full">
                        <div class="!wpuf-col-span-5">
                            <div class="!wpuf-text-sm !wpuf-font-medium !wpuf-text-gray-900">
                                <?php echo esc_html( wp_trim_words( get_the_title(), 8 ) ); ?>
                            </div>
                        </div>
                        <div class="!wpuf-col-span-4">
                            <div class="!wpuf-text-sm !wpuf-text-gray-600">
                                <?php echo esc_html( get_the_date( 'F j, Y' ) ); ?>
                            </div>
                        </div>
                        <div class="!wpuf-col-span-3">
                            <a href="<?php echo esc_url( get_permalink() ); ?>"
                               target="_blank"
                               class="!wpuf-inline-flex !wpuf-items-center !wpuf-text-sm !wpuf-text-purple-600 hover:!wpuf-text-purple-800 !wpuf-no-underline">
                                <?php esc_html_e( 'Post Link', 'wpuf-pro' ); ?>
                                <svg class="!wpuf-w-3 !wpuf-h-3 !wpuf-ml-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"></path>
                                </svg>
                            </a>
                        </div>
                    </div>
                </div>
                <?php endwhile; ?>
            </div>
        </div>

    <?php else : ?>
        <div class="!wpuf-flex !wpuf-flex-col !wpuf-items-center !wpuf-justify-center !wpuf-py-16 !wpuf-bg-gray-50 !wpuf-rounded-lg !wpuf-border !wpuf-border-gray-200">
            <div class="!wpuf-mb-4">
                <svg class="!wpuf-w-20 !wpuf-h-20 !wpuf-text-gray-300" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M19 20H5a2 2 0 01-2-2V6a2 2 0 012-2h10a2 2 0 012 2v1m2 13a2 2 0 01-2-2V7m2 13a2 2 0 002-2V9a2 2 0 00-2-2h-2m-4-3H9M7 16h6M7 8h6v4H7V8z"></path>
                </svg>
            </div>
            <p class="!wpuf-text-lg !wpuf-font-medium !wpuf-text-gray-900 !wpuf-mb-1"><?php esc_html_e( 'No posts found', 'wpuf-pro' ); ?></p>
            <p class="!wpuf-text-sm !wpuf-text-gray-500"><?php esc_html_e( 'This user hasn\'t published any posts yet.', 'wpuf-pro' ); ?></p>
        </div>
    <?php endif; ?>

    <?php
    // Shared pagination template
    $pagination_type  = 'posts';
    $url_builder_func = 'wpuf_ud_build_posts_page_url';
    include __DIR__ . '/pagination-4.php';
    ?>
</div>

<?php
wp_reset_postdata();
?>
