<?php
/**
 * TableFields.php
 *
 * @since      1.0.0
 * @package    WcElectronInvoice\Admin\Settings\Fields
 * @author     alfiopiccione <alfio.piccione@gmail.com>
 * @license    http://opensource.org/licenses/gpl-2.0.php GPL v2
 *
 * Copyright (C) 2025 POP
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

namespace WcElectronInvoice\Admin\Settings;

if (! defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}

/**
 * TableFields
 */
class TableFields
{
    /**
    * @param $value
    *
    * @return array
    */
    public static function get_field_description( $value ) {
    $description  = '';
    $tooltip_html = '';

    if ( true === $value['desc_tip'] ) {
        $tooltip_html = $value['desc'];
    } elseif ( ! empty( $value['desc_tip'] ) ) {
        $description  = $value['desc'];
        $tooltip_html = $value['desc_tip'];
    } elseif ( ! empty( $value['desc'] ) ) {
        $description = $value['desc'];
    }

    $error_class = ( ! empty( $value['description_is_error'] ) ) ? 'is-error' : '';

    if ( $description && in_array( $value['type'], array( 'textarea', 'radio' ), true ) ) {
        $description = '<p style="margin-top:0">' . wp_kses_post( $description ) . '</p>';
    } elseif ( $description && in_array( $value['type'], array( 'checkbox' ), true ) ) {
        $description = wp_kses_post( $description );
    } elseif ( $description ) {
        $description = '<p class="description ' . $error_class . '">' . wp_kses_post( $description ) . '</p>';
    }

    if ( $tooltip_html && in_array( $value['type'], array( 'checkbox' ), true ) ) {
        $tooltip_html = '<p class="description ' . $error_class . '">' . $tooltip_html . '</p>';
    } elseif ( $tooltip_html ) {
        $tooltip_html = '<span class="dashicons dashicons-editor-help" data-tip="'.esc_attr($tooltip_html).'"></span>';
    }

    return array(
        'description'  => $description,
        'tooltip_html' => $tooltip_html,
    );
}

    /**
    * @param $option_name
    * @param $default
    *
    * @return array|false|mixed|string
    */
    public static function get_option( $option_name, $default = '' ) {
			if ( ! $option_name ) {
				return $default;
			}

			// Array value.
			if ( strstr( $option_name, '[' ) ) {

				parse_str( $option_name, $option_array );

				// Option name is first key.
				$option_name = current( array_keys( $option_array ) );

				// Get value.
				$option_values = get_option( $option_name, '' );

				$key = key( $option_array[ $option_name ] );

				if ( isset( $option_values[ $key ] ) ) {
					$option_value = $option_values[ $key ];
				} else {
					$option_value = null;
				}
			} else {
				// Single value.
				$option_value = get_option( $option_name, null );
			}

			if ( is_array( $option_value ) ) {
				$option_value = wp_unslash( $option_value );
			} elseif ( ! is_null( $option_value ) ) {
				$option_value = stripslashes( $option_value );
			}

			return ( null === $option_value ) ? $default : $option_value;
		}

    /**
    * @param $options
    *
    * @return void
    */
    public static function output($options) {
        foreach ( $options as $value ) {
            if ( ! isset( $value['type'] ) ) {
                continue;
            }
            if ( ! isset( $value['id'] ) ) {
                $value['id'] = '';
            }

            // The 'field_name' key can be used when it is useful to specify an input field name that is different
            // from the input field ID. We use the key 'field_name' because 'name' is already in use for a different
            // purpose.
            if ( ! isset( $value['field_name'] ) ) {
                $value['field_name'] = $value['id'];
            }
            if ( ! isset( $value['title'] ) ) {
                $value['title'] = isset( $value['name'] ) ? $value['name'] : '';
            }
            if ( ! isset( $value['class'] ) ) {
                $value['class'] = '';
            }
            if ( ! isset( $value['css'] ) ) {
                $value['css'] = '';
            }
            if ( ! isset( $value['default'] ) ) {
                $value['default'] = '';
            }
            if ( ! isset( $value['desc'] ) ) {
                $value['desc'] = '';
            }
            if ( ! isset( $value['desc_tip'] ) ) {
                $value['desc_tip'] = false;
            }
            if ( ! isset( $value['placeholder'] ) ) {
                $value['placeholder'] = '';
            }
            if ( ! isset( $value['row_class'] ) ) {
                $value['row_class'] = '';
            }
            if ( ! empty( $value['row_class'] ) && substr( $value['row_class'], 0, 16 ) !== 'wc-settings-row-' ) {
                $value['row_class'] = 'wc-settings-row-' . $value['row_class'];
            }
            if ( ! isset( $value['suffix'] ) ) {
                $value['suffix'] = '';
            }
            if ( ! isset( $value['value'] ) ) {
                $value['value'] = self::get_option( $value['id'], $value['default'] );
            }

            // Custom attribute handling.
            $custom_attributes = array();

            if ( ! empty( $value['custom_attributes'] ) && is_array( $value['custom_attributes'] ) ) {
                foreach ( $value['custom_attributes'] as $attribute => $attribute_value ) {
                    $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
                }
            }

            // Description handling.
            $field_description = self::get_field_description( $value );
            $description       = $field_description['description'];
            $tooltip_html      = $field_description['tooltip_html'];

            // Switch based on type.
            switch ( $value['type'] ) {

                // Section Titles.
                case 'title':
                    if ( ! empty( $value['title_html'] ) ) {
                        echo '<h2>' . wp_kses_post( $value['title_html'] ) . '</h2>';
                    } elseif ( ! empty( $value['title'] ) ) {
                        echo '<h2>' . esc_html( $value['title'] ) . '</h2>';
                    }
                    if ( ! empty( $value['desc'] ) ) {
                        echo '<div id="' . esc_attr( sanitize_title( $value['id'] ) ) . '-description">';
                        echo wp_kses_post( wpautop( wptexturize( $value['desc'] ) ) );
                        echo '</div>';
                    }
                    echo '<table class="form-table">' . "\n\n";
                    if ( ! empty( $value['id'] ) ) {
                        do_action( 'wc_el_inv-pop_table_fields_settings_' . sanitize_title( $value['id'] ) );
                    }
                    break;

                case 'info':
                    ?><tr class="<?php echo esc_attr( $value['row_class'] ); ?>">
                        <th scope="row" class="titledesc"><?php echo esc_html( $value['title'] ); ?></th>
                        <td style="<?php echo esc_attr( $value['css'] ); ?>">
                    <?php
                    if(! empty($value['text'])) {
                      echo wp_kses_post( wpautop( wptexturize( $value['text'] ) ) );
                    }
                    echo '</td></tr>';
                    break;

                // Section Ends.
                case 'sectionend':
                    if ( ! empty( $value['id'] ) ) {
                        do_action( 'wc_el_inv-pop_table_fields_settings_' . sanitize_title( $value['id'] ) . '_end' );
                    }
                    echo '</table>';
                    if ( ! empty( $value['id'] ) ) {
                        do_action( 'wc_el_inv-pop_table_fields_settings_' . sanitize_title( $value['id'] ) . '_after' );
                    }
                    break;

                // Standard text inputs and subtypes like 'number'.
                case 'text':
                case 'password':
                case 'datetime':
                case 'datetime-local':
                case 'date':
                case 'month':
                case 'time':
                case 'week':
                case 'number':
                case 'email':
                case 'url':
                case 'tel':
                    $option_value = $value['value'];
                    ?>
                    <tr class="<?php echo esc_attr( $value['row_class'] ); ?>">
                        <th scope="row" class="titledesc">
                        <?php if(isset($value['title_html'])) : ?>
                        <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo wp_kses_post( $value['title_html'] ); ?> <?php
                            // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                            echo $tooltip_html; // WPCS: XSS ok. ?></label>
                        <?php else : ?>
                        <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?> <?php
                            // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                            echo $tooltip_html; // WPCS: XSS ok. ?></label>
                        <?php endif; ?>
                        </th>
                        <td class="forminp forminp-<?php echo esc_attr( sanitize_title( $value['type'] ) ); ?>">
                            <input
                                name="<?php echo esc_attr( $value['field_name'] ); ?>"
                                id="<?php echo esc_attr( $value['id'] ); ?>"
                                type="<?php echo esc_attr( $value['type'] ); ?>"
                                style="<?php echo esc_attr( $value['css'] ); ?>"
                                value="<?php echo esc_attr( $option_value ); ?>"
                                class="<?php echo esc_attr( $value['class'] ); ?>"
                                placeholder="<?php echo esc_attr( $value['placeholder'] ); ?>"
                                <?php
                                 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                                echo implode( ' ', $custom_attributes ); // WPCS: XSS ok. ?>
                                /><?php echo esc_html( $value['suffix'] ); ?> <?php
                                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                                echo $description; // WPCS: XSS ok. ?>
                        </td>
                    </tr>
                    <?php
                    break;

                // Color picker.
                case 'color':
                    $option_value = $value['value'];

                    ?>
                    <tr class="<?php echo esc_attr( $value['row_class'] ); ?>">
                        <th scope="row" class="titledesc">
                            <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?> <?php
                            // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                            echo $tooltip_html; // WPCS: XSS ok. ?></label>
                        </th>
                        <td class="forminp forminp-<?php echo esc_attr( sanitize_title( $value['type'] ) ); ?>">&lrm;
                            <span class="colorpickpreview" style="background: <?php echo esc_attr( $option_value ); ?>">&nbsp;</span>
                            <input
                                name="<?php echo esc_attr( $value['field_name'] ); ?>"
                                id="<?php echo esc_attr( $value['id'] ); ?>"
                                type="text"
                                dir="ltr"
                                style="<?php echo esc_attr( $value['css'] ); ?>"
                                value="<?php echo esc_attr( $option_value ); ?>"
                                class="<?php echo esc_attr( $value['class'] ); ?>colorpick"
                                placeholder="<?php echo esc_attr( $value['placeholder'] ); ?>"
                                <?php
                                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                                echo implode( ' ', $custom_attributes ); // WPCS: XSS ok. ?>
                                />&lrm; <?php
                                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                                echo $description; // WPCS: XSS ok. ?>
                                <div id="colorPickerDiv_<?php echo esc_attr( $value['id'] ); ?>" class="colorpickdiv" style="z-index: 100;background:#eee;border:1px solid #ccc;position:absolute;display:none;"></div>
                        </td>
                    </tr>
                    <?php
                    break;

                // Textarea.
                case 'textarea':
                    $option_value = $value['value'];

                    ?>
                    <tr class="<?php echo esc_attr( $value['row_class'] ); ?>">
                        <th scope="row" class="titledesc">
                            <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?> <?php
                            // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                            echo $tooltip_html; // WPCS: XSS ok. ?></label>
                        </th>
                        <td class="forminp forminp-<?php echo esc_attr( sanitize_title( $value['type'] ) ); ?>">
                            <?php
                            // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                            echo $description; // WPCS: XSS ok. ?>

                            <textarea
                                name="<?php echo esc_attr( $value['field_name'] ); ?>"
                                id="<?php echo esc_attr( $value['id'] ); ?>"
                                style="<?php echo esc_attr( $value['css'] ); ?>"
                                class="<?php echo esc_attr( $value['class'] ); ?>"
                                placeholder="<?php echo esc_attr( $value['placeholder'] ); ?>"
                                <?php
                                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                                echo implode( ' ', $custom_attributes ); // WPCS: XSS ok. ?>
                                ><?php
                                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                                echo esc_textarea( $option_value ); // WPCS: XSS ok. ?></textarea>
                        </td>
                    </tr>
                    <?php
                    break;

                // Select boxes.
                case 'select':
                case 'multiselect':
                    $option_value = $value['value'];

                    ?>
                    <tr class="<?php echo esc_attr( $value['row_class'] ); ?>">
                        <th scope="row" class="titledesc">
                            <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?> <?php
                            // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                            echo $tooltip_html; // WPCS: XSS ok. ?></label>
                        </th>
                        <td class="forminp forminp-<?php echo esc_attr( sanitize_title( $value['type'] ) ); ?>">
                            <select
                                name="<?php echo esc_attr( $value['field_name'] ); ?><?php echo ( 'multiselect' === $value['type'] ) ? '[]' : ''; ?>"
                                id="<?php echo esc_attr( $value['id'] ); ?>"
                                style="<?php echo esc_attr( $value['css'] ); ?>"
                                class="<?php echo esc_attr( $value['class'] ); ?>"
                                <?php
                                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                                echo implode( ' ', $custom_attributes ); // WPCS: XSS ok. ?>
                                <?php echo 'multiselect' === $value['type'] ? 'multiple="multiple"' : ''; ?>
                                >
                                <?php
                                foreach ( $value['options'] as $key => $val ) {
                                    ?>
                                    <option value="<?php echo esc_attr( $key ); ?>"
                                        <?php

                                        if ( is_array( $option_value ) ) {
                                            selected( in_array( (string) $key, $option_value, true ), true );
                                        } else {
                                            selected( $option_value, (string) $key );
                                        }

                                        ?>
                                    ><?php echo esc_html( $val ); ?></option>
                                    <?php
                                }
                                ?>
                            </select> <?php
                            // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                            echo $description; // WPCS: XSS ok. ?>
                        </td>
                    </tr>
                    <?php
                    break;

                // Radio inputs.
                case 'radio':
                    $option_value     = $value['value'];
                    $disabled_values  = $value['disabled'] ?? array();
                    $show_desc_at_end = $value['desc_at_end'] ?? false;

                    ?>
                    <tr class="<?php echo esc_attr( $value['row_class'] ); ?>">
                        <th scope="row" class="titledesc">
                            <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?> <?php
                            // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                            echo $tooltip_html; // WPCS: XSS ok. ?></label>
                        </th>
                        <td class="forminp forminp-<?php echo esc_attr( sanitize_title( $value['type'] ) ); ?>">
                            <fieldset>
                                <?php
                                if ( ! $show_desc_at_end ) {
                                    echo wp_kses_post( $description );
                                }
                                ?>
                                <ul>
                                <?php
                                foreach ( $value['options'] as $key => $val ) {
                                    ?>
                                    <li>
                                        <label><input
                                            name="<?php echo esc_attr( $value['field_name'] ); ?>"
                                            value="<?php echo esc_attr( $key ); ?>"
                                            type="radio"
                                            <?php
                                            if ( in_array( $key, $disabled_values, true ) ) {
                                                echo 'disabled'; }
                                            ?>
                                            style="<?php echo esc_attr( $value['css'] ); ?>"
                                            class="<?php echo esc_attr( $value['class'] ); ?>"
                                            <?php
                                            // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                                            echo implode( ' ', $custom_attributes ); // WPCS: XSS ok. ?>
                                            <?php checked( $key, $option_value ); ?>
                                            /> <?php echo esc_html( $val ); ?></label>
                                    </li>
                                    <?php
                                }
                                if ( $show_desc_at_end ) {
                                    echo wp_kses_post( "<p class='description description-thin'>{$description}</p>" );
                                }
                                ?>
                                </ul>
                            </fieldset>
                        </td>
                    </tr>
                    <?php
                    break;

                // Checkbox input.
                case 'checkbox':
                    $option_value     = $value['value'];
                    $visibility_class = array();

                    if ( ! isset( $value['hide_if_checked'] ) ) {
                        $value['hide_if_checked'] = false;
                    }
                    if ( ! isset( $value['show_if_checked'] ) ) {
                        $value['show_if_checked'] = false;
                    }
                    if ( 'yes' === $value['hide_if_checked'] || 'yes' === $value['show_if_checked'] ) {
                        $visibility_class[] = 'hidden_option';
                    }
                    if ( 'option' === $value['hide_if_checked'] ) {
                        $visibility_class[] = 'hide_options_if_checked';
                    }
                    if ( 'option' === $value['show_if_checked'] ) {
                        $visibility_class[] = 'show_options_if_checked';
                    }
                    if ( $value['row_class'] ) {
                        $visibility_class[] = $value['row_class'];
                    }

                    $must_disable = $value['disabled'] ?? false;

                    if ( $must_disable ) {
                        $visibility_class[] = 'disabled';
                    }

                    $container_class = implode( ' ', $visibility_class );
                    $has_title       = isset( $value['title'] ) && '' !== $value['title'];
                    $has_legend      = isset( $value['legend'] ) && '' !== $value['legend'];

                    if ( ! isset( $value['checkboxgroup'] ) || 'start' === $value['checkboxgroup'] ) {
                        $has_tooltip = isset( $value['tooltip'] ) && '' !== $value['tooltip'];

                        $tooltip_container_class = $has_tooltip ? 'with-tooltip' : '';
                        ?>
                            <tr class="<?php echo esc_attr( $container_class ); ?>">
                                <th scope="row" class="titledesc"><?php echo esc_html( $value['title'] ); ?></th>
                                <td class="forminp forminp-checkbox <?php echo esc_html( $tooltip_container_class ); ?>">
                                    <?php if ( $has_tooltip ) : ?>
                                        <span class="help-tooltip"><span class="dashicons dashicons-editor-help" data-tip="<?php echo esc_html( $value['tooltip'] ); ?>"></span></span>
                                    <?php endif; ?>
                                    <fieldset>
                        <?php
                    } else {
                        ?>
                            <fieldset class="<?php echo esc_attr( $container_class ); ?>">
                        <?php
                    }

                    if ( $has_title || $has_legend ) {
                        ?>
                            <legend class="<?php echo $has_legend ? '' : 'screen-reader-text'; ?>"><span><?php echo esc_html( $has_legend ? $value['legend'] : $value['title'] ); ?></span></legend>
                        <?php
                    }

                    ?>
                        <label for="<?php echo esc_attr( $value['id'] ); ?>">
                            <input
                                <?php echo $must_disable ? 'disabled' : ''; ?>
                                name="<?php echo esc_attr( $value['field_name'] ); ?>"
                                id="<?php echo esc_attr( $value['id'] ); ?>"
                                type="checkbox"
                                class="<?php echo esc_attr( isset( $value['class'] ) ? $value['class'] : '' ); ?>"
                                value="1"
                                <?php checked( $option_value, 'yes' ); ?>
                                <?php
                                 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                                echo implode( ' ', $custom_attributes ); // WPCS: XSS ok. ?>
                            /> <?php
                             // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                            echo $description; // WPCS: XSS ok. ?>
                        </label> <?php
                         // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                        echo $tooltip_html; // WPCS: XSS ok. ?>
                    <?php

                    if ( ! isset( $value['checkboxgroup'] ) || 'end' === $value['checkboxgroup'] ) {
                        ?>
                                    </fieldset>
                                </td>
                            </tr>
                        <?php
                    } else {
                        ?>
                            </fieldset>
                        <?php
                    }
                    break;

                // Single page selects.
                case 'single_select_page':
                    $args = array(
                        'name'             => $value['field_name'],
                        'id'               => $value['id'],
                        'sort_column'      => 'menu_order',
                        'sort_order'       => 'ASC',
                        'show_option_none' => ' ',
                        'class'            => $value['class'],
                        'echo'             => false,
                        'selected'         => absint( $value['value'] ),
                        'post_status'      => 'publish,private,draft',
                    );

                    if ( isset( $value['args'] ) ) {
                        $args = wp_parse_args( $value['args'], $args );
                    }

                    ?>
                    <tr class="single_select_page <?php echo esc_attr( $value['row_class'] ); ?>">
                        <th scope="row" class="titledesc">
                            <label><?php echo esc_html( $value['title'] ); ?> <?php
                             // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                            echo $tooltip_html; // WPCS: XSS ok. ?></label>
                        </th>
                        <td class="forminp">
                            <?php
                             // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                            echo str_replace( ' id=', " data-placeholder='" . esc_attr__( 'Select a page&hellip;', WC_EL_INV_TEXTDOMAIN ) . "' style='" . $value['css'] . "' class='" . $value['class'] . "' id=", wp_dropdown_pages( $args ) ); // WPCS: XSS ok. ?>
                            <?php
                             // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                            echo $description; // WPCS: XSS ok. ?>
                        </td>
                    </tr>
                    <?php
                    break;

                case 'single_select_page_with_search':
                    $option_value = $value['value'];
                    $page         = get_post( $option_value );

                    if ( ! is_null( $page ) ) {
                        $page                = get_post( $option_value );
                        $option_display_name = sprintf(
                            /* translators: 1: page name 2: page ID */
                            __( '%1$s (ID: %2$s)', WC_EL_INV_TEXTDOMAIN ),
                            $page->post_title,
                            $option_value
                        );
                    }
                    ?>
                    <tr class="single_select_page <?php echo esc_attr( $value['row_class'] ); ?>">
                        <th scope="row" class="titledesc">
                            <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?> <?php
                             // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                            echo $tooltip_html; ?></label>
                        </th>
                        <td class="forminp forminp-<?php echo esc_attr( sanitize_title( $value['type'] ) ); ?>">
                            <select
                                name="<?php echo esc_attr( $value['field_name'] ); ?>"
                                id="<?php echo esc_attr( $value['id'] ); ?>"
                                style="<?php echo esc_attr( $value['css'] ); ?>"
                                class="<?php echo esc_attr( $value['class'] ); ?>"
                                <?php
                                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                                echo implode( ' ', $custom_attributes ); ?>
                                data-placeholder="<?php esc_attr_e( 'Search for a page&hellip;', WC_EL_INV_TEXTDOMAIN ); ?>"
                                data-allow_clear="true"
                                data-exclude="<?php
                                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                                echo esc_js( wp_json_encode( $value['args']['exclude'] ) ); ?>"
                                >
                                <option value=""></option>
                                <?php if ( ! is_null( $page ) ) { ?>
                                    <option value="<?php echo esc_attr( $option_value ); ?>" selected="selected">
                                    <?php
                                     // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                                    echo wp_strip_all_tags( $option_display_name ); ?>
                                    </option>
                                <?php } ?>
                            </select> <?php
                             // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                            echo $description; ?>
                        </td>
                    </tr>
                    <?php
                    break;

                // Days/months/years selector.
                case 'relative_date_selector':
                    $periods      = array(
                        'days'   => __( 'Day(s)', WC_EL_INV_TEXTDOMAIN ),
                        'weeks'  => __( 'Week(s)', WC_EL_INV_TEXTDOMAIN ),
                        'months' => __( 'Month(s)', WC_EL_INV_TEXTDOMAIN ),
                        'years'  => __( 'Year(s)', WC_EL_INV_TEXTDOMAIN ),
                    );
                    $option_value = pop_parse_relative_date_option( $value['value'] );
                    ?>
                    <tr class="<?php echo esc_attr( $value['row_class'] ); ?>">
                        <th scope="row" class="titledesc">
                            <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?> <?php
                             // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                            echo $tooltip_html; // WPCS: XSS ok. ?></label>
                        </th>
                        <td class="forminp">
                        <input
                                name="<?php echo esc_attr( $value['field_name'] ); ?>[number]"
                                id="<?php echo esc_attr( $value['id'] ); ?>"
                                type="number"
                                style="width: 80px;"
                                value="<?php echo esc_attr( $option_value['number'] ); ?>"
                                class="<?php echo esc_attr( $value['class'] ); ?>"
                                placeholder="<?php echo esc_attr( $value['placeholder'] ); ?>"
                                step="1"
                                min="1"
                                <?php
                                 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                                echo implode( ' ', $custom_attributes ); // WPCS: XSS ok. ?>
                            />&nbsp;
                            <select name="<?php echo esc_attr( $value['field_name'] ); ?>[unit]" style="width: auto;">
                                <?php
                                foreach ( $periods as $value => $label ) {
                                    echo '<option value="' . esc_attr( $value ) . '"' . selected( $option_value['unit'], $value, false ) . '>' . esc_html( $label ) . '</option>';
                                }
                                ?>
                            </select> <?php
                             // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                            echo ( $description ) ? $description : ''; // WPCS: XSS ok. ?>
                        </td>
                    </tr>
                    <?php
                    break;

                case 'slotfill_placeholder':
                    ?>
                    <div
                        id="<?php echo esc_attr( $value['id'] ); ?>"
                        class="<?php echo esc_attr( $value['class'] ); ?>"
                    >
                    </div>
                    <?php
                    break;
                // Default: run an action.
                default:
                    do_action( 'wc_el_inv-pop_table_field_' . $value['type'], $value );
                    break;
            }
        }
    }
}

/**
 * Parse a relative date option from the settings API into a standard format.
 *
 * @param mixed $raw_value Value stored in DB.
 *
 * @return array Nicely formatted array with number and unit values.
 *@since 1.0.0
 *
 * @see wp-content/plugins/woocommerce/includes/wc-formatting-functions.php
 */
function pop_parse_relative_date_option( $raw_value ) {
	$periods = array(
		'days'   => __( 'Day(s)', WC_EL_INV_TEXTDOMAIN ),
		'weeks'  => __( 'Week(s)', WC_EL_INV_TEXTDOMAIN ),
		'months' => __( 'Month(s)', WC_EL_INV_TEXTDOMAIN ),
		'years'  => __( 'Year(s)', WC_EL_INV_TEXTDOMAIN ),
	);

	$value = wp_parse_args(
		(array) $raw_value,
		array(
			'number' => '',
			'unit'   => 'days',
		)
	);

	$value['number'] = ! empty( $value['number'] ) ? absint( $value['number'] ) : '';

	if ( ! in_array( $value['unit'], array_keys( $periods ), true ) ) {
		$value['unit'] = 'days';
	}

	return $value;
}
