<?php
/**
 * @package     woobooking.Platform
 * @subpackage  Form
 *
 *  Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE
 */
namespace WpBookingEvent\Form\fields;
defined('_WPBOOKINGEVENT_EXEC') or die;
use WpBookingEvent\Form\FormField;

/**
 * Form Field class for the woobooking Platform.
 * Supports a one line text field.
 *
 * @link   http://www.w3.org/TR/html-markup/input.text.html#input.text

 */
class WpBookingEvent_FormFieldList extends FormField
{
    /**
     * The form field type.
     *
     * @var    string
     */
    protected $type = 'list';
    protected $list_option = [];
    /**
     * Name of the layout being used to render the field
     *
     * @var    string
     */
    protected $layout = 'WooBooking.form.field.list.list';
    public function __construct($form = null)
    {

        parent::__construct($form);
    }
    public function getInput()
    {

        return parent::getInput(); // TODO: Change the autogenerated stub
    }
    public function setup(\SimpleXMLElement $element, $value, $group = null)
    {
        // Make sure there is a valid FormField XML element.
        if ((string) $element->getName() != 'field')
        {
            return false;
        }

        // Reset the input and label values.
        $this->input = null;
        $this->label = null;

        // Set the XML element object.
        $this->element = $element;

        // Set the group of the field.
        $this->group = $group;

        $attributes = array(
            'multiple', 'name', 'id', 'hint', 'class', 'description', 'labelclass', 'onchange', 'onclick', 'validate', 'pattern', 'validationtext', 'default',
            'required', 'disabled', 'readonly', 'autofocus', 'hidden', 'autocomplete', 'spellcheck', 'translateHint', 'translateLabel',
            'translate_label', 'translateDescription', 'translate_description', 'size', 'showon');

        $this->default = isset($element['value']) ? (string) $element['value'] : $this->default;

        // Set the field default value.
        if ($element['multiple'] && is_string($value) && is_array(json_decode($value, true)))
        {
            $this->value = (array) json_decode($value);
        }
        else
        {
            $this->value = $value;
        }

        foreach ($attributes as $attributeName)
        {
            $this->__set($attributeName, $element[$attributeName]);
        }

        // Allow for repeatable elements
        $repeat = (string) $element['repeat'];
        $this->repeat = ($repeat == 'true' || $repeat == 'multiple' || (!empty($this->form->repeat) && $this->form->repeat == 1));

        // Set the visibility.
        $this->hidden = ($this->hidden || (string) $element['type'] == 'hidden');

        $this->layout = !empty($this->element['layout']) ? (string) $this->element['layout'] : $this->layout;

        // Add required to class list if field is required.
        if ($this->required)
        {
            $this->class = trim($this->class . ' required');
        }
        $this->list_option=$this->getOptions();
        return true;
    }
    /**
     * Method to get the data to be passed to the layout for rendering.
     *
     * @return  array
     *
     * @since 3.5
     */
    protected function getLayoutData()
    {
        // Label preprocess
        $label = $this->element['label'] ? (string) $this->element['label'] : (string) $this->element['name'];
        $label = $this->translateLabel ? esc_html($label) : $label;

        // Description preprocess
        $description = !empty($this->description) ? $this->description : null;
        $description = !empty($description) && $this->translateDescription ? esc_html($description) : $description;

        $alt = preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname);

        return array(
            'autocomplete'   => $this->autocomplete,
            'autofocus'      => $this->autofocus,
            'class'          => $this->class,
            'description'    => $description,
            'disabled'       => $this->disabled,
            'field'          => $this,
            'group'          => $this->group,
            'hidden'         => $this->hidden,
            'hint'           => $this->translateHint ? esc_html($this->hint, $alt) : $this->hint,
            'id'             => $this->id,
            'label'          => $label,
            'labelclass'     => $this->labelclass,
            'multiple'       => $this->multiple,
            'name'           => $this->name,
            'onchange'       => $this->onchange,
            'onclick'        => $this->onclick,
            'pattern'        => $this->pattern,
            'validationtext' => $this->validationtext,
            'readonly'       => $this->readonly,
            'repeat'         => $this->repeat,
            'required'       => (bool) $this->required,
            'size'           => $this->size,
            'spellcheck'     => $this->spellcheck,
            'validate'       => $this->validate,
            'value'          => $this->value,
            'list_option'          => $this->list_option,
        );
    }
    protected function getOptions()
    {
        $fieldname = preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname);
        $options   = array();

        foreach ($this->element->xpath('option') as $option)
        {


            $value = (string) $option['value'];
            $text  = trim((string) $option) != '' ? trim((string) $option) : $value;

            $disabled = (string) $option['disabled'];
            $disabled = ($disabled == 'true' || $disabled == 'disabled' || $disabled == '1');
            $disabled = $disabled || ($this->readonly && $value != $this->value);

            $checked = (string) $option['checked'];
            $checked = ($checked == 'true' || $checked == 'checked' || $checked == '1');

            $selected = (string) $option['selected'];
            $selected = ($selected == 'true' || $selected == 'selected' || $selected == '1');

            $tmp = array(
                'value'    => $value,
                'text'     => $text,
                'disable'  => $disabled,
                'class'    => (string) $option['class'],
                'selected' => ($checked || $selected),
                'checked'  => ($checked || $selected),
            );

            $options[] = (object) $tmp;
        }

        return $options;
    }

}
