<?php
/**
 * Content Management Display
 */

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

// Get post types.
$post_types = get_option( 'ccspllms_post_types', array( 'post', 'page' ) );
if ( ! is_array( $post_types ) ) {
    if ( empty( $post_types ) ) {
        $post_types = array();
    } else {
        $post_types = explode( ',', $post_types );
        $post_types = array_map( 'trim', $post_types );
    }
}

// Sanitize paged query var (read-only, so no nonce needed).
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$paged = isset( $_GET['paged'] )
    ? absint( wp_unslash( $_GET['paged'] ) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
    : 1;

$posts_per_page = 20;

$args  = array(
    'post_type'      => $post_types,
    'post_status'    => 'publish',
    'posts_per_page' => $posts_per_page,
    'paged'          => $paged,
    'meta_query'     => array(
        'relation' => 'OR',
        array(
            'key'     => '_ccspllms_excluded',
            'compare' => 'NOT EXISTS',
        ),
        array(
            'key'     => '_ccspllms_excluded',
            'value'   => array( '0', '1' ),
            'compare' => 'IN',
        ),
    ),
);

$query = new WP_Query( $args );
?>

<div class="ccspllms_container">
    
    <div class="ccspllms_header">
        <div class="ccspllmshead_left">
            <div class="ccspllmshead_logo">
            <img src="<?php echo esc_html( CCSPLLMS_PLUGIN_URL . 'assets/cloudcusplogo.svg' ); ?>" alt="Logo" class="ccspllms_logo">
            </div>
            <div class="ccspllmshead_text"><h1>LLMS.txt  Generator</h1></div>
        </div>
        <div class="ccspllmshead_right">
             <h1><i class="fas fa-list-alt"></i> Content Management</h1>
        <p>Manage which content appears in your LLMS.txt file</p>
        </div>
        
    </div>

    <div class="ccspllms_content">
        <div class="ccspllms_card">
            <div class="bulk-actions">
                <select id="ccspllms_bulk_action">
                    <option value="">Bulk Actions</option>
                    <option value="enable">Enable in LLMS.txt</option>
                    <option value="disable">Exclude from LLMS.txt</option>
                    <option value="reset">Reset Custom Content</option>
                </select>
                <button id="ccspllms_bulk_apply" class="ccspllms_button">Apply</button>
                
                <div class="search-box" style="float: right;">
                    <input type="search" id="content_search" placeholder="Search content...">
                    <button class="ccspllms_button">Search</button>
                </div>
            </div>

            <table class="ccspllms_table">
                <thead>
                    <tr>
                        <th><input type="checkbox" id="select_all"></th>
                        <th>Title</th>
                        <th>Type</th>
                        <th>Status</th>
                        <th>Included</th>
                        <th>Custom Content</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if ($query->have_posts()): ?>
                        <?php while ($query->have_posts()): $query->the_post(); ?>
                            <?php
                            $post_id = get_the_ID();
                            $excluded = get_post_meta($post_id, '_ccspllms_excluded', true);
                            $custom_content = get_post_meta($post_id, '_ccspllms_custom_content', true);
                            $included = !$excluded;
                            ?>
                            <tr>
                                <td><input type="checkbox" class="ccspllms_post_checkbox" value="<?php echo esc_html($post_id); ?>"></td>
                                <td>
                                    <strong><?php the_title(); ?></strong>
                                    <div class="row-actions">
                                        <a href="<?php echo esc_html(get_edit_post_link()); ?>">Edit</a> |
                                        <a href="<?php echo esc_html(get_permalink()); ?>" target="_blank">View</a>
                                    </div>
                                </td>
                                <td><?php echo esc_html(get_post_type()); ?></td>
                                <td><?php echo esc_html(get_post_status()); ?></td>
                                <td>
                                    <label class="ccspllms_toggle">
                                        <input type="checkbox" class="ccspllms_post_toggle" data-post-id="<?php echo esc_html($post_id); ?>" <?php checked($included, 1); ?>>
                                        <span class="ccspllms_slider"></span>
                                    </label>
                                </td>
                                <td>
                                    <?php if ($custom_content): ?>
                                        <span class="status-active">Yes</span>
                                        <div class="custom-content-preview" title="<?php echo esc_attr($custom_content); ?>">
                                            <?php echo esc_html(substr($custom_content, 0, 50)) . '...'; ?>
                                        </div>
                                    <?php else: ?>
                                        <span class="status-inactive">No</span>
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <button class="ccspllms_button small edit-custom-content" data-post-id="<?php echo esc_html($post_id); ?>">
                                        <i class="fas fa-edit"></i>
                                    </button>
                                </td>
                            </tr>
                        <?php endwhile; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="7">No posts found.</td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>

            <?php
            // Pagination
            $total_pages = $query->max_num_pages;
            if ($total_pages > 1) {
                echo '<div class="pagination">';
                echo wp_kses_post(paginate_links(array(
                    'total' => $total_pages,
                    'current' => $paged,
                    'format' => '?paged=%#%',
                    'prev_text' => '&laquo; Previous',
                    'next_text' => 'Next &raquo;'
                )));
                echo '</div>';
            }
            wp_reset_postdata();
            ?>
        </div>
    </div>
</div>

<!-- Custom Content Modal -->
<div id="custom_content_modal" class="ccspllms_modal" style="display: none;">
    <div class="ccspllms_modal_content">
        <div class="ccspllms_modal_header">
            <h3>Edit Custom Content</h3>
            <span class="ccspllms_modal_close">&times;</span>
        </div>
        <div class="ccspllms_modal_body">
            <textarea id="custom_content_textarea" rows="6" style="width: 100%;"></textarea>
            <p class="description">Leave empty to use auto-generated content.</p>
        </div>
        <div class="ccspllms_modal_footer">
            <button id="save_custom_content" class="ccspllms_button">Save</button>
            <button id="cancel_custom_content" class="ccspllms_button" style="background: #6c757d;">Cancel</button>
        </div>
    </div>
</div>