<?php
if (!defined('ABSPATH')) {
    exit;
}

// Default attributes
$block = $block ?? null;
$current_post_id = get_the_ID() ?: 0;
$attributes = $attributes ?? [];
$caption = $attributes['caption'] ?? '';
$showCaption = $attributes['showCaption'] ?? true;
$block_class = $attributes['blockClass'] ?? '';
$block_id = $attributes['blockID'] ?? '';

// Conditional formatting data and element
$conditionalFormat = [
    'data'    => $attributes['tableConditionalFormat'] ?? [],
    'element' => $attributes['blockClass'] ?? '',
];

// Table settings
$options = [
    'rowPerPage'            => $attributes['rowPerPage'] ?? '',
    'prevFirstIcon'         => $attributes['prevFirstIcon'] ?? '',
    'prevIcon'              => $attributes['prevIcon'] ?? '',
    'nextIcon'              => $attributes['nextIcon'] ?? '',
    'nextEndIcon'           => $attributes['nextEndIcon'] ?? '',
    'ascIcon'               => $attributes['ascIcon'] ?? '',
    'descIcon'              => $attributes['descIcon'] ?? '',
    'defaultSortIcon'       => $attributes['defaultSortIcon'] ?? '',
    'hasHeader'             => $attributes['hasHeader'] ?? true,
    'hasFooter'             => $attributes['hasFooter'] ?? false,
    'showPaginationControl' => $attributes['showPaginationControl'] ?? true,
    'showResult'            => $attributes['showResult'] ?? true,
    'ShowSearchInput'       => $attributes['ShowSearchInput'] ?? true,
    'showRowPerPage'        => $attributes['showRowPerPage'] ?? true,
    'itemRowPerPage'        => $attributes['itemRowPerPage'] ?? '',
    'itemPerPageContent'    => $attributes['itemPerPageContent'] ?? '',
    'enableSorting'         => $attributes['enableSorting'] ?? true,
    'showPaginationRange'   => $attributes['showPaginationRange'] ?? '',
    'showPageOf'            => $attributes['showPageOf'] ?? '',
    'freezeSettings'        => $attributes['freezeSettings'] ?? '',
    'conditionalFormat'     => $conditionalFormat,
    'enableFreeze'          => $attributes['enableFreeze'] ?? false,
    'columnWidths'          => $attributes['columnWidths'] ?? [],
    'rowHeights'            => $attributes['rowHeights'] ?? [],
    'headerHeight'          => $attributes['headerHeight'] ?? 0,
    'postType'              => $attributes['postType'] ?? 'post',
    'postsPerPage'          => $attributes['postsPerPage'] ?? 10,
    'postTypeFilters'       => $attributes['postTypeFilters'] ?? [],
    'selectedTaxonomy'      => $attributes['selectedTaxonomy'] ?? '',
    'selectedTerm'          => $attributes['selectedTerm'] ?? '',
    'selectedAuthor'        => $attributes['selectedAuthor'] ?? '',
    'orderBy'               => $attributes['orderBy'] ?? '',
    'order'                 => $attributes['order'] ?? '',
    'search'                => $attributes['search'] ?? '',
    'page'                  => $attributes['page'] ?? 1,
    'offset'                => $attributes['offset'] ?? 0,
    'postTableColumn'       => $attributes['postTableColumn'],
    'restUrl'               => rest_url('table-builder/v1/'),
    'currentPostId'   => $current_post_id,
    'groupingEnabled'       => $attributes['groupingEnabled'] ?? false,
    'groupingFields'        => $attributes['groupingFields'] ?? [],
    'currency'          => $attributes['currency'] ?? '',
];

// Encode settings
$settings_json = esc_attr(json_encode($options));
$block_wrapper_attrs = TableBuilderPro\Helpers\Utils::get_dynamic_block_wrapper_attributes($block);
?>

<figure <?php echo wp_kses_post($block_wrapper_attrs); ?> class="<?php echo esc_attr($block_class); ?>" id="<?php echo esc_attr($block_id); ?>">
    <div class="table-builder-loading active">
        <span class="spinner is-active"></span>
    </div>
    <div class="table-builder-post-data-table" data-settings="<?php echo $settings_json; ?>" style="display: none;">
        <div class="table-toolbar-container">
            <?php if (!empty($attributes['showExportButton'])) : ?>
                <div class="table-export-dropdown">
                    <button class="export-dropdown-button">
                        <span><?php esc_html_e('Export', 'your-textdomain'); ?></span>
                        <span class="export-dropdown-icon">
                            <?php
                            if (!empty($attributes['exportBtnIcon']['src'])) {
                                echo wp_kses(
                                    TableBuilderPro\Helpers\Utils::add_class_to_svg($attributes['exportBtnIcon']['src']),
                                    TableBuilderPro\Helpers\Utils::svg_allowed_html()
                                );
                            }
                            ?>
                        </span>
                    </button>
                    <div class="export-dropdown-content">
                        <button class="table-builder-csv-export-button"><?php esc_html_e('Export CSV', 'your-textdomain'); ?></button>
                        <button class="table-builder-full-csv-export-button"><?php esc_html_e('Export Full CSV', 'your-textdomain'); ?></button>
                        <button class="table-builder-excel-export-button"><?php esc_html_e('Export Excel', 'your-textdomain'); ?></button>
                        <button class="table-builder-pdf-export-button"><?php esc_html_e('Export PDF', 'your-textdomain'); ?></button>
                        <button class="table-builder-print-export-button"><?php esc_html_e('Print', 'your-textdomain'); ?></button>
                    </div>
                </div>
            <?php endif; ?>

            <?php if (!empty($attributes['ShowSearchInput'])) : ?>
                <div class="table-search-input-container">
                    <div class="table-search-input-wrapper">
                        <span class="search-icon">
                            <?php
                            if (!empty($attributes['searchIcon']['src'])) {
                                echo wp_kses(
                                    TableBuilderPro\Helpers\Utils::add_class_to_svg($attributes['searchIcon']['src']),
                                    TableBuilderPro\Helpers\Utils::svg_allowed_html()
                                );
                            }
                            ?>
                        </span>
                        <input
                            type="text"
                            class="table-search-input"
                            id="table-search-input"
                            placeholder="<?php echo esc_attr($attributes['searchPlaceholder'] ?? __('Search', 'your-textdomain')); ?>">
                    </div>
                    <?php if (!empty($attributes['showIndividualSearch'])) : ?>
                        <div
                            class="table-search-column-select"
                            id="table-search-column-select">
                        </div>
                    <?php endif; ?>
                </div>
            <?php endif; ?>
        </div>
        <table class="gkit-table post-data-table">
            <thead class="table-builder-post-data-table__header">
                <tr class="table-builder-post-data-table__header-row"></tr>
            </thead>
            <tbody class="table-builder-post-data-table__body"></tbody>
        </table>

        <?php if (!empty($attributes['hasFooter'])) : ?>
            <div id="pagination-container">pagination</div>
        <?php endif; ?>
    </div>

    <?php if (!empty($caption) && $showCaption) : ?>
        <figcaption class="table-builder-post-data_caption"><?php echo wp_kses_post($caption); ?></figcaption>
    <?php endif; ?>
</figure>