<?php

/**
 * Term meta form renderer.
 *
 * @package B3WVS\VariationSwatches\Admin\TermMeta
 */
namespace B3WVS\VariationSwatches\Admin\TermMeta;

defined( 'ABSPATH' ) || exit;
/**
 * Term Meta Form Renderer - Handles rendering of add/edit term forms
 */
class Form {
    /**
     * Render add term form fields.
     */
    public static function render_add_fields() {
        $default_color_label = __( 'Color', 'b3-swatches' );
        $button_color_label = __( 'Button Color (optional)', 'b3-swatches' );
        ?>
		<?php 
        wp_nonce_field( 'b3wvs_save_term_meta', 'b3wvs_term_meta_nonce' );
        ?>
		<div class="form-field">
			<label for="b3_wvs_type"><?php 
        esc_html_e( 'Swatch Type', 'b3-swatches' );
        ?></label>
			<select name="b3_wvs_type" id="b3_wvs_type">
				<option value=""><?php 
        esc_html_e( 'Default', 'b3-swatches' );
        ?></option>
				<option value="color"><?php 
        esc_html_e( 'Color', 'b3-swatches' );
        ?></option>
				<?php 
        ?>
				<option value="button"><?php 
        esc_html_e( 'Button', 'b3-swatches' );
        ?></option>
			</select>
		</div>
		<div class="form-field b3-wvs-color-field" style="display:none;" data-label-default="<?php 
        echo esc_attr( $default_color_label );
        ?>" data-label-button="<?php 
        echo esc_attr( $button_color_label );
        ?>">
			<label for="b3_wvs_color"><?php 
        echo esc_html( $default_color_label );
        ?></label>
			<input type="text" name="b3_wvs_color" id="b3_wvs_color" class="b3-wvs-color-picker" />
		</div>
		<div class="form-field b3-wvs-label-field" style="display:none;">
			<label for="b3_wvs_label"><?php 
        esc_html_e( 'Label Text', 'b3-swatches' );
        ?></label>
			<input type="text" name="b3_wvs_label" id="b3_wvs_label" placeholder="<?php 
        esc_attr_e( 'Optional custom label', 'b3-swatches' );
        ?>" />
		</div>
		<?php 
        ?>
		<?php 
    }

    /**
     * Render edit term form fields.
     *
     * @param \WP_Term $term     Term object.
     * @param string   $taxonomy Taxonomy slug.
     */
    public static function render_edit_fields( $term, $taxonomy ) {
        unset($taxonomy);
        $type = get_term_meta( $term->term_id, 'b3_wvs_type', true );
        $color = get_term_meta( $term->term_id, 'b3_wvs_color', true );
        $label = get_term_meta( $term->term_id, 'b3_wvs_label', true );
        $default_color_label = __( 'Color', 'b3-swatches' );
        $button_color_label = __( 'Button Color (optional)', 'b3-swatches' );
        ?>
		<?php 
        wp_nonce_field( 'b3wvs_save_term_meta', 'b3wvs_term_meta_nonce' );
        ?>
		<tr class="form-field">
			<th scope="row"><label for="b3_wvs_type"><?php 
        esc_html_e( 'Swatch Type', 'b3-swatches' );
        ?></label></th>
			<td>
				<select name="b3_wvs_type" id="b3_wvs_type">
					<option value="" <?php 
        selected( $type, '' );
        ?>><?php 
        esc_html_e( 'Default', 'b3-swatches' );
        ?></option>
					<option value="color" <?php 
        selected( $type, 'color' );
        ?>><?php 
        esc_html_e( 'Color', 'b3-swatches' );
        ?></option>
					<?php 
        ?>
					<option value="button" <?php 
        selected( $type, 'button' );
        ?>><?php 
        esc_html_e( 'Button', 'b3-swatches' );
        ?></option>
				</select>
			</td>
		</tr>
		<tr class="form-field b3-wvs-color-field" style="<?php 
        echo esc_attr( ( 'color' === $type || 'button' === $type ? '' : 'display:none;' ) );
        ?>" data-label-default="<?php 
        echo esc_attr( $default_color_label );
        ?>" data-label-button="<?php 
        echo esc_attr( $button_color_label );
        ?>">
			<th scope="row"><label for="b3_wvs_color"><?php 
        echo esc_html( $default_color_label );
        ?></label></th>
			<td>
				<input type="text" name="b3_wvs_color" id="b3_wvs_color" value="<?php 
        echo esc_attr( $color );
        ?>" class="b3-wvs-color-picker" />
			</td>
		</tr>
		<tr class="form-field b3-wvs-label-field" style="<?php 
        echo esc_attr( ( 'button' === $type ? '' : 'display:none;' ) );
        ?>">
			<th scope="row"><label for="b3_wvs_label"><?php 
        esc_html_e( 'Label Text', 'b3-swatches' );
        ?></label></th>
			<td>
				<input type="text" name="b3_wvs_label" id="b3_wvs_label" value="<?php 
        echo esc_attr( $label );
        ?>" placeholder="<?php 
        esc_attr_e( 'Optional custom label', 'b3-swatches' );
        ?>" />
			</td>
		</tr>
		<?php 
        ?>
		<?php 
    }

}
