<?php

/**
 * Redux Framework 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
 * any later version.
 * Redux Framework 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 Redux Framework. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package     ReduxFramework
 * @subpackage  Field_multi_bp_select
 * @author      Kostas Seresiotis
 * @version     1.0.0
 */

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

// Don't duplicate me!
if ( ! class_exists( 'ReduxFramework_multi_bp_select' ) ) {

    /**
     * Main ReduxFramework_multi_bp_select class
     *
     * @since       1.0.0
     */
    class ReduxFramework_multi_bp_select {

        /**
         * Field Constructor.
         * Required - must call the parent constructor, then assign field and value to vars, and obviously call the render field function
         *
         * @since       1.0.0
         * @access      public
         * @return      void
         */
        function __construct( $field = array(), $value = '', $parent ) {
            $this->parent = $parent;
            $this->field  = $field;
            $this->value  = $value;

            if ( empty( $this->extension_dir ) ) {
                $this->extension_dir = trailingslashit( str_replace( '\\', '/', dirname( __FILE__ ) ) );
                $this->extension_url = site_url( str_replace( trailingslashit( str_replace( '\\', '/', ABSPATH ) ), '', $this->extension_dir ) );
            }    
            // Set default args for this field to avoid bad indexes. Change this to anything you use.
            $defaults = array(
                'options'           => array(),
                'stylesheet'        => '',
                'output'            => true,
                'enqueue'           => true,
                'enqueue_frontend'  => true
            );
            $this->field = wp_parse_args( $this->field, $defaults );     
        }

        /**
         * Field Render Function.
         * Takes the vars and outputs the HTML for the field in the settings
         *
         * @since       1.0.0
         * @access      public
         * @return      void
         */
        public function render() {

            $this->add_text   = ( isset( $this->field['add_text'] ) ) ? $this->field['add_text'] : esc_html__( 'Add More', 'dvl-uxshop-extensions' );
            $this->show_empty = ( isset( $this->field['show_empty'] ) ) ? $this->field['show_empty'] : true;

            echo '<ul id="' . esc_attr( $this->field['id'] ) . '-ul" class="redux-multi-bp-select">';

            if ( isset( $this->value ) && is_array( $this->value ) ) {

                $this->value = array_values($this->value); //reset keys

                foreach ( $this->value as $k => $value ) {
                    if ( $value != '' ) {
                        echo '<li data-id="'.esc_attr( $k ).'">
                                <span class="expl">' . _x('When', 'Like \'when\' this happens.', 'dvl-uxshop-extensions') . '</span>
                                <select name="' . esc_attr( $this->field['name'] . $this->field['name_suffix'] . '['.$k.'][opr]' ) . '">
                                    <option value="max" ' . ($value['opr'] == 'max' ? 'selected="selected"':'') . '>' . esc_html__( 'At max', 'dvl-uxshop-extensions' ) . '</option>
                                    <option value="min" ' . ($value['opr'] == 'min' ? 'selected="selected"':'') . '>' . esc_html__( 'At least', 'dvl-uxshop-extensions' ) . '</option>
                                </select>
                                <input type="text" id="' . esc_attr( $this->field['id'] . '-' . $k ) . '" name="' . esc_attr( $this->field['name'] . $this->field['name_suffix'] . '['.$k.'][width]' ) . '" value="' . esc_attr( $value['width'] ) . '" class="regular-text width ' . esc_attr( $this->field['class'] ) . '" />
                                <span class="expl">' . esc_html__('px width, show', 'dvl-uxshop-extensions') . '</span>
                                <input type="number" name="' . esc_attr( $this->field['name'] . $this->field['name_suffix'] . '['.$k.'][cols]' ) . '" value="' . esc_attr( $value['cols'] ) . '" class="cols"> 
                                <span class="expl">' . esc_html__('columns', 'dvl-uxshop-extensions') . '</span>
                                <a href="javascript:void(0);" class="deletion redux-multi-bp-select-remove">' . esc_html__( 'Remove', 'dvl-uxshop-extensions' ) . '</a>
                            </li>';
                    }
                }
            } elseif ( $this->show_empty == true ) {
                echo '<li data-id="0">
                        <span class="expl">' . _x('When', 'Like \'when\' this happens.', 'dvl-uxshop-extensions') . '</span>
                        <select name="' . esc_attr( $this->field['name'] . $this->field['name_suffix'] . '[0][opr]' ) . '">
                            <option value="max">' . esc_html__( 'At max', 'dvl-uxshop-extensions' ) . '</option>
                            <option value="min">' . esc_html__( 'At least', 'dvl-uxshop-extensions' ) . '</option>
                        </select>
                        <input type="text" id="' . esc_attr( $this->field['id'] ) . '" name="' . esc_attr( $this->field['name'] . $this->field['name_suffix'] . '[0][width]' ) . '" value="" class="regular-text width' . esc_attr( $this->field['class'] ) . '" />
                        <span class="expl">' . esc_html__('px width, show', 'dvl-uxshop-extensions') . '</span>
                        <input type="number" name="' . esc_attr( $this->field['name'] . $this->field['name_suffix'] . '[0][cols]' ) . '" class="cols"> 
                        <span class="expl">' . esc_html__('columns', 'dvl-uxshop-extensions') . '</span>
                        <a href="javascript:void(0);" class="deletion redux-multi-bp-select-remove">' . esc_html__( 'Remove', 'dvl-uxshop-extensions' ) . '</a>
                    </li>';
            }

            echo    '<li data-id="' . esc_attr( $k ) . '" style="display:none;">
                        <span class="expl">' . _x('When', 'Like \'when\' this happens.', 'dvl-uxshop-extensions') . '</span>
                        <select name="">
                            <option value="max">' . esc_html__( 'At max', 'dvl-uxshop-extensions' ) . '</option>
                            <option value="min">' . esc_html__( 'At least', 'dvl-uxshop-extensions' ) . '</option>
                        </select>
                        <input type="text" id="' . esc_attr( $this->field['id'] ) . '" name="" value="" class="regular-text width" /> 
                        <span class="expl">' . esc_html__('px width, show', 'dvl-uxshop-extensions') . '</span>
                        <input type="number" name="" class="cols"> 
                        <span class="expl">' . esc_html__('columns', 'dvl-uxshop-extensions') . '</span>
                        <a href="javascript:void(0);" class="deletion redux-multi-bp-select-remove">' . esc_html__( 'Remove', 'dvl-uxshop-extensions' ) . '</a>
                    </li>';

            echo '</ul>';
            echo '<span style="clear:both;display:block;height:0;" /></span>';
            $this->field['add_number'] = ( isset( $this->field['add_number'] ) && is_numeric( $this->field['add_number'] ) ) ? $this->field['add_number'] : 1;
            echo '<a href="javascript:void(0);" class="button button-primary redux-multi-bp-select-add" data-add_number="' . esc_attr( $this->field['add_number'] ) . '" data-id="' . esc_attr( $this->field['id'] ) . '-ul" data-name="' . esc_attr( $this->field['name'] . $this->field['name_suffix'] ) . '">' . $this->add_text . '</a><br/>';
        }

        /**
         * Enqueue Function.
         * If this field requires any scripts, or css define this function and register/enqueue the scripts/css
         *
         * @since       1.0.0
         * @access      public
         * @return      void
         */
        public function enqueue() {

            $extension = ReduxFramework_extension_multi_bp_select::getInstance();

            wp_enqueue_script(
                'redux-field-multi-bp-select-js',
                $this->extension_url . 'field_multi_bp_select.js',
                array( 'jquery', 'redux-js' ),
                time(),
                true
            );

            wp_enqueue_style(
                'redux-field-multi-bp-select-css',
                $this->extension_url . 'field_multi_bp_select.css',
                array(),
                NULL,
                'all'
            );
        }
    }
}