/**
 * Premise field section
 *
 * Group of fields wrapped within one parent element.
 *
 * @since  1.2     Simplified parameters. You can no longer use 'container' parameters.
 *                 instead, use the 'premise_field_section_html' filter. 
 * 
 * @param  array   $args array of arrays. The fields to insert
 * @param  boolean $echo whether to echo ro return the string
 * @return string  html for field section
 */
function premise_field_section($args = array(), $echo = true)
{
    /**
     * Backward comaptibility with versions < 1.2
     *
     * if the $args array has the key 'fields', it was called using the old way. we need to fix that.
     *
     * @since  1.2 array of array no longer requires fields to be in its own array called 'fields'
     */
    $args = array_key_exists('fields', $args) && is_array($args['fields']) ? $args['fields'] : $args;
    $html = '';
    // Start with a clean section
    foreach ($args as $k => $v) {
        if (is_array($v)) {
            // Pass each field args as first parameter
            // We can do this because of backward compatibilty
            $html .= premise_field($v, '', false);
        }
    }
    /**
     * premise_field_section_html filter
     *
     * control the way your field section is displayed. This filter will pass the field section
     * html to the function that you hook to it. 
     *
     * @see https://codex.wordpress.org/Function_Reference/add_filter Form instructions on how to add a filter.
     *
     * @since  1.2 replaces the 'container' params from old function. use to alter section's html
     *
     * @premise-hook premise_field_section_html do hook for field section html
     * 
     * @var string
     */
    $html = apply_filters('premise_field_section_html', $html);
    remove_all_filters('premise_field_section_html');
    if (!$echo) {
        return $html;
    } else {
        echo (string) $html;
    }
}
    /**
     * Fields duplicate tests
     *
     * @see premiseFieldDuplicate jQuery plugin.
     *
     * `$('.my-fields').premiseFieldDuplicate()`
     *
     * @return string Fields to duplicate.
     */
    public static function fields_duplicate()
    {
        ?>
		<h2>Premise WP framework</h2>
		<h3>Fields demo</h3><br />
		<div class="premise-row">
		<?php 
        // 1. Text input.
        premise_field('text', array('name' => 'input1', 'label' => 'Text input <span class="increment">1</span>', 'wrapper_class' => 'premise-field-duplicate-text'));
        echo PHP_EOL;
        // 2. Select input.
        premise_field('select', array('name' => 'input3', 'label' => 'Select input <span class="increment">2</span>', 'options' => array('Option 1' => 'option1', 'Option 2' => 'option2'), 'wrapper_class' => 'premise-field-duplicate-select'));
        echo PHP_EOL;
        // 2b. Selected input.
        premise_field('select', array('value' => 'option2', 'name' => 'inputb3', 'label' => 'Selected input <span class="increment">3</span>', 'options' => array('Option 1' => 'option1', 'Option 2' => 'option2'), 'wrapper_class' => 'premise-field-duplicate-selected'));
        echo PHP_EOL;
        // 3. Multiple select input.
        premise_field('select', array('name' => 'input4', 'label' => 'Multiple select input <span class="increment">4</span>', 'multiple' => 'multiple', 'options' => array('Option 1' => 'option1', 'Option 2' => 'option2'), 'wrapper_class' => 'premise-field-duplicate-select-multiple'));
        echo PHP_EOL;
        // 4. Radio input.
        echo '<div class="premise-field-duplicate-radio"> <span class="increment">5</span>';
        premise_field('radio', array('value' => 'a', 'name' => 'input5', 'id' => 'input5a', 'label' => 'Radio input a', 'value_att' => 'c'));
        echo PHP_EOL;
        premise_field('radio', array('value' => 'b', 'name' => 'input5', 'id' => 'input5b', 'label' => 'Radio input b', 'value_att' => 'c'));
        echo PHP_EOL;
        premise_field('radio', array('value' => 'c', 'name' => 'input5', 'id' => 'input5c', 'label' => 'Radio input c (should be selected)', 'value_att' => 'c'));
        echo PHP_EOL;
        echo '</div>';
        // 5. Checkbox input.
        premise_field('checkbox', array('value' => 'a', 'name' => 'input6a', 'label' => 'Checkbox input (should be unchecked) <span class="increment">6</span>', 'value_att' => 'b', 'wrapper_class' => 'premise-field-duplicate-checkbox1'));
        echo PHP_EOL;
        premise_field('checkbox', array('value' => 'b', 'name' => 'input6b', 'label' => 'Checkbox input (should be checked) <span class="increment">7</span>', 'value_att' => 'b', 'wrapper_class' => 'premise-field-duplicate-checkbox2'));
        echo PHP_EOL;
        ?>
		</div>
		<script>

			jQuery('.premise-field-duplicate-checkbox1').premiseFieldDuplicate();
			jQuery('.premise-field-duplicate-checkbox2').premiseFieldDuplicate();
			jQuery('.premise-field-duplicate-select').premiseFieldDuplicate();
			jQuery('.premise-field-duplicate-selected').premiseFieldDuplicate();
			jQuery('.premise-field-duplicate-radio').premiseFieldDuplicate();
			jQuery('.premise-field-duplicate-select-multiple').premiseFieldDuplicate();
			jQuery('.premise-field-duplicate-text').premiseFieldDuplicate();
		</script>
		<?php 
    }