<?php
/**
 * Template for the Links Overview Page
 *
 * This template displays the main overview of all LinkCentral links.
 */

// Ensure this file is being included by a parent file
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

// Display the admin header
do_action( 'linkcentral_admin_header' );

// Current view state (may be set by parent; fallback for standalone include)
if ( isset( $post_status ) ) {
    $linkcentral_post_status = $post_status;
} else {
    $linkcentral_raw = filter_input( INPUT_GET, 'post_status', FILTER_UNSAFE_RAW );
    $linkcentral_post_status = ( $linkcentral_raw !== null && $linkcentral_raw !== '' ) ? sanitize_key( wp_unslash( $linkcentral_raw ) ) : 'any';
}

// Get current filters
$linkcentral_filter_category_raw = filter_input( INPUT_GET, 'linkcentral_category', FILTER_VALIDATE_INT );
$linkcentral_filter_category = ( $linkcentral_filter_category_raw !== null && $linkcentral_filter_category_raw !== false ) ? $linkcentral_filter_category_raw : 0;
$linkcentral_search_raw = filter_input( INPUT_GET, 's', FILTER_UNSAFE_RAW );
$linkcentral_search_term = $linkcentral_search_raw !== null ? sanitize_text_field( wp_unslash( $linkcentral_search_raw ) ) : '';

// Prepare active filter chips (search term and/or category)
$linkcentral_filter_chips = array();
$linkcentral_clear_url      = '';

if ( ! empty( $linkcentral_search_term ) ) {
    $linkcentral_filter_chips[] = array(
        'type_label'  => __( 'Search', 'linkcentral' ),
        'value'       => $linkcentral_search_term,
        'dismiss_url' => remove_query_arg( array( 's', 'paged' ) ),
    );
}

if ( $linkcentral_filter_category > 0 ) {
    $linkcentral_category_term = get_term( $linkcentral_filter_category, 'linkcentral_category' );
    if ( $linkcentral_category_term && ! is_wp_error( $linkcentral_category_term ) ) {
        $linkcentral_filter_chips[] = array(
            'type_label'  => __( 'Category', 'linkcentral' ),
            'value'       => $linkcentral_category_term->name,
            'dismiss_url' => remove_query_arg( array( 'linkcentral_category', 'paged' ) ),
        );
    }
}

if ( ! empty( $linkcentral_filter_chips ) ) {
    $linkcentral_clear_url = remove_query_arg( array( 's', 'linkcentral_category', 'paged' ) );
}
?>

<div class="wrap linkcentral-overview">
    <div class="linkcentral-overview-header-main">
        <div class="linkcentral-overview-header-top">
            <h1 class="wp-heading-inline"><?php esc_html_e('All Links', 'linkcentral'); ?></h1>
            <?php 
            // Display the "Add New" button if not in trash view and user has create capability
            if ( $linkcentral_post_status !== 'trash' && current_user_can( 'linkcentral_create_edit_links' ) ) : 
            ?>
                <a href="<?php echo esc_url(admin_url('post-new.php?post_type=linkcentral_link')); ?>" class="page-title-action"><?php esc_html_e('Add New', 'linkcentral'); ?></a>
            <?php endif; ?>
        </div>

        <?php if ( ! empty( $linkcentral_filter_chips ) ) : ?>
            <div class="linkcentral-filter-notice">
                <?php foreach ( $linkcentral_filter_chips as $linkcentral_filter_chip ) : ?>
                    <span class="linkcentral-filter-chip">
                        <span class="linkcentral-filter-chip-label">
                            <?php echo esc_html( $linkcentral_filter_chip['type_label'] ); ?>
                            <?php esc_html_e( 'is', 'linkcentral' ); ?>
                            <strong class="linkcentral-filter-chip-value"><?php echo esc_html( $linkcentral_filter_chip['value'] ); ?></strong>
                        </span>
                        <a href="<?php echo esc_url( $linkcentral_filter_chip['dismiss_url'] ); ?>" class="linkcentral-filter-chip-dismiss" aria-label="<?php esc_attr_e( 'Remove filter', 'linkcentral' ); ?>">&times;</a>
                    </span>
                <?php endforeach; ?>
                <?php if ( ! empty( $linkcentral_clear_url ) ) : ?>
                    <a href="<?php echo esc_url( $linkcentral_clear_url ); ?>" class="linkcentral-clear-filters"><?php esc_html_e( 'Clear all', 'linkcentral' ); ?></a>
                <?php endif; ?>
            </div>
        <?php endif; ?>
    </div>
    <hr class="wp-header-end">

    <?php
    // Display admin notices
    settings_errors('linkcentral_notices');
    ?>

    <ul class="subsubsub">
        <?php 
        // Display the list of post status views (All, Published, Trash, etc.)
        echo wp_kses_post(implode(' | ', $list_table->get_views())); 
        ?>
    </ul>

    <!-- Search form (GET method to update URL) -->
    <form method="get" class="linkcentral-search-form">
        <?php
        // Preserve current page state for search (read-only via filter_input)
        $linkcentral_preserve_keys = array( 'page', 'post_status', 'filter_broken', 'orderby', 'order', 'per_page' );
        $linkcentral_get_params = array();
        foreach ( $linkcentral_preserve_keys as $linkcentral_param ) {
            $linkcentral_val = filter_input( INPUT_GET, $linkcentral_param, FILTER_UNSAFE_RAW );
            if ( $linkcentral_val !== null && $linkcentral_val !== '' ) {
                $linkcentral_get_params[ $linkcentral_param ] = sanitize_text_field( wp_unslash( $linkcentral_val ) );
            }
        }
        foreach ( $linkcentral_preserve_keys as $linkcentral_param ) {
            if (!empty($linkcentral_get_params[$linkcentral_param])) {
                $linkcentral_value = sanitize_text_field($linkcentral_get_params[$linkcentral_param]);
                printf('<input type="hidden" name="%s" value="%s" />', esc_attr($linkcentral_param), esc_attr($linkcentral_value));
            }
        }
        if (empty($linkcentral_get_params['page'])) {
            echo '<input type="hidden" name="page" value="linkcentral" />';
        }
        
        // Category filter dropdown (hierarchical)
        wp_dropdown_categories(array(
            'taxonomy'         => 'linkcentral_category',
            'hide_empty'       => false,
            'hide_if_empty'    => true,
            'hierarchical'     => true,
            'name'             => 'linkcentral_category',
            'class'            => 'linkcentral-category-filter',
            'show_option_all'  => __('All Categories', 'linkcentral'),
            'show_count'       => true,
            'selected'         => $linkcentral_filter_category,
        ));
        ?>
        <?php $list_table->search_box(__('Search links', 'linkcentral'), 'linkcentral-search'); ?>
    </form>

    <!-- Bulk actions form (POST method) -->
    <form method="post">
        <?php
        // Add nonce for security
        wp_nonce_field('bulk-' . $list_table->_args['plural']);

        // Preserve current page state for bulk actions (read-only via filter_input)
        $linkcentral_bulk_preserve_keys = array( 'page', 'post_status', 'filter_broken', 'linkcentral_category', 'orderby', 'order', 's', 'per_page' );
        $linkcentral_get_params = array();
        foreach ( $linkcentral_bulk_preserve_keys as $linkcentral_param ) {
            $linkcentral_val = filter_input( INPUT_GET, $linkcentral_param, FILTER_UNSAFE_RAW );
            if ( $linkcentral_val !== null && $linkcentral_val !== '' ) {
                $linkcentral_get_params[ $linkcentral_param ] = sanitize_text_field( wp_unslash( $linkcentral_val ) );
            }
        }
        foreach ( $linkcentral_bulk_preserve_keys as $linkcentral_param ) {
            if (!empty($linkcentral_get_params[$linkcentral_param])) {
                $linkcentral_value = sanitize_text_field($linkcentral_get_params[$linkcentral_param]);
                printf('<input type="hidden" name="%s" value="%s" />', esc_attr($linkcentral_param), esc_attr($linkcentral_value));
            }
        }
        if (empty($linkcentral_get_params['page'])) {
            echo '<input type="hidden" name="page" value="linkcentral" />';
        }
        ?>
        <?php $list_table->display(); ?>
    </form>
</div>