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

class Jalpi_Element_Pricing_Table extends Jalpi_Element {
    public static function type(): string { return 'pricing-table'; }

    public function render( array $node, Jalpi_Renderer $renderer ): string {
        $id       = sanitize_html_class( $node['id'] ?? '' );
        $content  = $node['content'] ?? [];
        $styles   = $node['styles']  ?? [];
        $name     = esc_html( $content['name']       ?? __( 'Plan', 'jalpi-page-builder' ) );
        $tagline  = esc_html( $content['tagline']    ?? '' );
        $currency = esc_html( $content['currency']   ?? '$' );
        $amount   = esc_html( $content['amount']     ?? '0' );
        $period   = esc_html( $content['period']     ?? 'mo' );
        $features = (array)   ( $content['features'] ?? [] );
        $btn_text = esc_html( $content['btnText']    ?? __( 'Get Started', 'jalpi-page-builder' ) );
        $btn_url  = esc_url(  $content['btnUrl']     ?? '#' );
        $featured = ! empty(  $content['featured'] );
        $a        = $renderer->get_css_attrs( $node );

        $feat_li = '';
        foreach ( $features as $f ) {
            $feat_li .= '<li class="jalpi-pricing__feature">'
                      . '<span class="jalpi-pricing__check" aria-hidden="true"><svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round"><polyline points="20 6 9 17 4 12"/></svg></span>'
                      . esc_html( $f )
                      . '</li>';
        }

        $featured_class = $featured ? ' jalpi-pricing--featured' : '';
        $featured_badge = $featured ? '<span class="jalpi-pricing__badge">' . esc_html__( 'Popular', 'jalpi-page-builder' ) . '</span>' : '';

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

        return '<div class="jalpi-el jalpi-pricing' . $featured_class . ' jalpi-' . esc_attr( $id ) . $a['extra_cls'] . '"' . $a['id_attr'] . '>'
             . $featured_badge
             . '<div class="jalpi-pricing__head">'
             .   '<h3 class="jalpi-pricing__name">' . $name . '</h3>'
             .   ( $tagline ? '<p class="jalpi-pricing__tagline">' . $tagline . '</p>' : '' )
             . '</div>'
             . '<div class="jalpi-pricing__price">'
             .   '<span class="jalpi-pricing__currency">' . $currency . '</span>'
             .   '<span class="jalpi-pricing__amount">' . $amount . '</span>'
             .   '<span class="jalpi-pricing__period">/' . $period . '</span>'
             . '</div>'
             . '<ul class="jalpi-pricing__features">' . $feat_li . '</ul>'
             . '<a href="' . $btn_url . '" class="jalpi-pricing__cta">' . $btn_text . '</a>'
             . '</div>';
    }
}
