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

// Default attributes
$block = $block ?? null;
$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,
    'groupingEnabled'       => $attributes['groupingEnabled'] ?? false,
    'groupingFields'        => $attributes['groupingFields'] ?? [],
    'itemRowPerPage'        => $attributes['itemRowPerPage'] ?? '',
    'itemPerPageContent'    => $attributes['itemPerPageContent'] ?? '',
    'enableSorting'         => $attributes['enableSorting'] ?? true,
    'errorMassage'          => $attributes['errorMassage'] ?? '',
    'nestedJsonSelectedPath' => $attributes['nestedJsonSelectedPath'] ?? '',
    'tableDataSource'       => $attributes['tableDataSource'] ?? '',
    'selectedSourceUrl'     => $attributes['selectedSourceUrl'] ?? '',
    'enableLiveUpdate'      => $attributes['enableLiveUpdate'] ?? false,
    'liveUpdateInterval'    => $attributes['liveUpdateInterval'] ?? 12,
    '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,
    'restUrl'               => rest_url('table-builder/v1/')
];

// 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-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 table-builder-csv-table">
            <thead class="gkit-table__header">
                <tr class="table-builder-header-row"></tr>
            </thead>
            <tbody class="gkit-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="gkit-table__caption"><?php echo wp_kses_post($caption); ?></figcaption>
    <?php endif; ?>
</figure>