<?php
defined( 'ABSPATH' ) || exit;

class Jalpi_Element_Stats_Card extends Jalpi_Element {
    public static function type(): string { return 'stats-card'; }

    public function render( array $node, Jalpi_Renderer $renderer ): string {
        $id       = sanitize_html_class( $node['id'] ?? '' );
        $content  = $node['content'] ?? [];
        $styles   = $node['styles']  ?? [];
        $value    = esc_html( $content['value']    ?? '0' );
        $label    = esc_html( $content['label']    ?? '' );
        $sublabel = esc_html( $content['sublabel'] ?? '' );
        $a        = $renderer->get_css_attrs( $node );

        $renderer->add_css( '.jalpi-el.jalpi-' . $id . '{display:block;' . $renderer->styles_to_css( $styles ) . '}' );

        return '<div class="jalpi-el jalpi-stats jalpi-' . esc_attr( $id ) . $a['extra_cls'] . '"' . $a['id_attr'] . '>'
             . '<span class="jalpi-stats__value">' . $value . '</span>'
             . ( $label    ? '<span class="jalpi-stats__label">'    . $label    . '</span>' : '' )
             . ( $sublabel ? '<span class="jalpi-stats__sublabel">' . $sublabel . '</span>' : '' )
             . '</div>';
    }
}
