<?php
namespace Storzen\Fields;
use Storzen\Contracts\Field;
defined('ABSPATH') || exit;

class Textarea extends Field {
    /** @var int    */ protected $rows        = 5;
    /** @var string */ protected $placeholder = '';

    /** @return static */ public function set_rows( $r )        { $this->rows = (int)$r;   return $this; }
    /** @return static */ public function rows( $r )            { return $this->set_rows($r); }
    /** @return static */ public function set_placeholder( $p ) { $this->placeholder = $p; return $this; }
    /** @return static */ public function placeholder( $p )     { return $this->set_placeholder($p); }

    public function sanitize( $raw ) { return sanitize_textarea_field( (string)$raw ); }

    public function render( $value ) {
    $id = $this->field_id();
    $is_pro_active = defined('STORZEN_IS_PRO') && STORZEN_IS_PRO === 'yes';
    $show_pro = $this->pro && ! $is_pro_active;
    echo '<textarea class="sz-textarea"';
    echo ' id="' . esc_attr($id) . '" name="' . esc_attr($this->field_name()) . '"';
    // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- disabled attribute may be added
    echo ' rows="' . esc_attr($this->rows) . '" placeholder="' . esc_attr($this->placeholder) . '"' . ( $show_pro ? ' disabled' : '' ) . '>';
    echo esc_textarea( (string)$value );
    echo '</textarea>';
    if ( $show_pro ) echo '<span class="sz-pro-badge">PRO</span>';
    }
}
