<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! current_user_can( 'manage_options' ) ) return;

$tables = get_posts( array(
    'post_type'      => 'cdtv_table',
    'posts_per_page' => -1,
    'post_status'    => 'publish',
    'orderby'        => 'date',
    'order'          => 'DESC',
) );
?>
<div class="wrap cdtv-wrap">
    <h1><?php esc_html_e( 'CodeDE Table Import/Export', 'codede-table-viewer' ); ?>
        <a href="<?php echo esc_url( admin_url( 'admin.php?page=cdtv-import' ) ); ?>" class="page-title-action">
            <?php esc_html_e( 'Add New Table', 'codede-table-viewer' ); ?>
        </a>
    </h1>

    <?php if ( empty( $tables ) ) : ?>
        <div class="cdtv-empty-state">
            <span class="dashicons dashicons-media-spreadsheet"></span>
            <p><?php esc_html_e( 'No tables yet. Upload your first Excel or CSV file!', 'codede-table-viewer' ); ?></p>
            <a href="<?php echo esc_url( admin_url( 'admin.php?page=cdtv-import' ) ); ?>" class="button button-primary button-large">
                <?php esc_html_e( '+ Add New Table', 'codede-table-viewer' ); ?>
            </a>
        </div>
    <?php else : ?>
        <table class="wp-list-table widefat fixed striped cdtv-table-list">
            <thead>
                <tr>
                    <th><?php esc_html_e( 'Title', 'codede-table-viewer' ); ?></th>
                    <th><?php esc_html_e( 'File', 'codede-table-viewer' ); ?></th>
                    <th><?php esc_html_e( 'Rows', 'codede-table-viewer' ); ?></th>
                    <th><?php esc_html_e( 'Columns', 'codede-table-viewer' ); ?></th>
                    <th><?php esc_html_e( 'Shortcode', 'codede-table-viewer' ); ?></th>
                    <th><?php esc_html_e( 'Actions', 'codede-table-viewer' ); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ( $tables as $table ) : ?>
                <tr>
                    <td><strong><?php echo esc_html( $table->post_title ); ?></strong></td>
                    <td><?php echo esc_html( get_post_meta( $table->ID, '_cdtv_file_name', true ) ?: '—' ); ?></td>
                    <td><?php echo esc_html( get_post_meta( $table->ID, '_cdtv_row_count', true ) ?: '—' ); ?></td>
                    <td><?php echo esc_html( get_post_meta( $table->ID, '_cdtv_column_count', true ) ?: '—' ); ?></td>
                    <td>
                        <code class="cdtv-shortcode" data-shortcode="[cdtv id=&quot;<?php echo esc_attr( $table->ID ); ?>&quot;]">
                            [cdtv id="<?php echo esc_attr( $table->ID ); ?>"]
                        </code>
                        <button class="button button-small cdtv-copy-shortcode" data-id="<?php echo esc_attr( $table->ID ); ?>">
                            <?php esc_html_e( 'Copy', 'codede-table-viewer' ); ?>
                        </button>
                    </td>
                    <td>
                        <a href="<?php echo esc_url( admin_url( 'admin.php?page=cdtv-import&table_id=' . $table->ID ) ); ?>" class="button button-small">
                            <?php esc_html_e( 'Edit', 'codede-table-viewer' ); ?>
                        </a>
                        <button class="button button-small cdtv-export-table" data-id="<?php echo esc_attr( $table->ID ); ?>">
                            <?php esc_html_e( 'CSV', 'codede-table-viewer' ); ?>
                        </button>
                        <button class="button button-small cdtv-export-xlsx" data-id="<?php echo esc_attr( $table->ID ); ?>">
                            <?php esc_html_e( 'XLSX', 'codede-table-viewer' ); ?>
                        </button>
                        <button class="button button-small cdtv-delete-table" data-id="<?php echo esc_attr( $table->ID ); ?>">
                            <?php esc_html_e( 'Delete', 'codede-table-viewer' ); ?>
                        </button>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>

</div>
