<?php
/**
 * Customizer Repeater Control.
 * 
 * @package PureNote
*/

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

if ( ! class_exists( 'Purenote_Control_Repeater' ) ) :
    
    /**
     * Repeater control
     *
     * @since 1.0.0
     */
    class Purenote_Control_Repeater extends WP_Customize_Control {

        /**
         * The control type.
         *
         * @access public
         * @var string
         * @since 1.0.0
         */
        public $type = 'mt-repeater';

        /**
         * Whitelisting the "required" argument.
         *
         * @since 1.0.0
         * @var array
         */
        public $required = array();

        /**
         * The repeater field label.
         *
         * @access public
         * @var string
         * @since 1.0.0
         */
        public $purenote_box_label = '';

        /**
         * The repeater field add button label.
         *
         * @access public
         * @var string
         * @since 1.0.0
         */
        public $purenote_box_add_control = '';

        /**
         * The repeater field limit count.
         *
         * @access public
         * @var int
         * @since 1.0.0
         */
        public $purenote_field_limit;

        /**
         * The fields that each container row will contain.
         *
         * @access public
         * @var array
         * @since 1.0.0
         */
        public $fields = array();

        /**
         * Get the listed categories.
         *
         * @access public
         * @var array
         * @since 1.0.0
         */
        public $categories = array();

        /**
         * Repeater drag and drop controller
         *
         * @since  1.0.0
         */
        public function __construct( $manager, $id, $args = array(), $fields = array() ) {

            parent::__construct( $manager, $id, $args );
            
            $this->fields                        = $fields;
            $this->purenote_box_label          = $args['purenote_box_label_text'] ;
            $this->purenote_box_add_control    = $args['purenote_box_add_control_text'];
            $this->purenote_field_limit        = $args['purenote_field_limit'];
            
            $this->categories                    = get_categories();

            $this->required                      = isset( $args['required'] ) ? $args['required'] : array(); // required dependencies

        }

        public function to_json() {
            parent::to_json();
            $this->json['required']    = $this->required; // Send required dependency to JSON
        }

        protected function render_content() {

            $repeater_id    = $this->id;
            $values         = json_decode( $this->value() );
            $field_count    = count( $values );
    ?>
            <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>

            <?php if ( $this->description ) { ?>
                <span class="description customize-control-description">
                    <?php echo wp_kses_post( $this->description ); ?>
                </span>
            <?php } ?>

            <ul class="mt-repeater-field-control-wrap">
                <?php $this->purenote_get_fields(); ?>
            </ul>

            <input type="hidden" <?php $this->link(); ?> class="mt-repeater-collector" value="<?php echo esc_attr( $this->value() ); ?>" />
            <input type="hidden" name="<?php echo esc_attr( $repeater_id ).'_count'; ?>" class="field-count" value="<?php echo absint( $field_count ); ?>">
            <input type="hidden" name="field_limit" class="field-limit" data-info="<?php echo esc_attr( $this->label ); ?>" value="<?php echo esc_attr( $this->purenote_field_limit ); ?>">
            <button type="button" class="button mt-repeater-add-control-field"><i class="bx bx-plus"></i><?php echo esc_html( $this->purenote_box_add_control ); ?></button>
    <?php
        }

        private function purenote_get_fields() {

            $fields = $this->fields;
            $values = json_decode( $this->value() );

            if ( is_array( $values ) ) {

                foreach ( $values as $value ) {
                    if ( 'show' === $value->item_visible ) {
                        $item_class = 'item-visible';
                        $dash_icon = 'visibility';
                    } else {
                        $item_class = 'item-not-visible';
                        $dash_icon = 'hidden';
                    }
        ?>
                <li class="mt-repeater-field-control <?php echo esc_attr( $item_class ); ?>">

                    <div class="heading-wrapper">
                        <span class="field-display dashicons dashicons-<?php echo esc_attr( $dash_icon ); ?>"></span>
                        <span class="mt-repeater-field-title <?php echo esc_attr( $item_class ); ?>"><?php echo esc_html( $this->purenote_box_label ); ?></span>
                        
                    </div><!-- .heading-wrapper -->
                
                    <div class="mt-repeater-fields">
                        <?php
                            foreach ( $fields as $key => $field ) {

                                $class = isset( $field['class'] ) ? $field['class'] : '';

                                $field_type  = isset( $field['type'] ) ? $field['type'] : 'none
                                ';
                        ?>
                                <div class="mt-repeater-field mt-repeater-type-<?php echo esc_attr( $field_type ).' '.esc_attr( $class ); ?>">

                                    <?php 
                                        $label          = isset( $field['label'] ) ? $field['label'] : '';
                                        $description    = isset( $field['description'] ) ? $field['description'] : '';
                                        $placeholder    = isset( $field['placeholder'] ) ? $field['placeholder'] : '';
                                        $new_value  = isset( $value->$key ) ? $value->$key : '';
                                        $default    = isset( $field['default'] ) ? $field['default'] : '';

                                        if ( 'checkbox' != $field['type'] && 'item_visible' !== $key ) {
                                    ?>
                                            <span class="customize-control-title"><?php echo esc_html( $label ); ?></span>
                                            <span class="description customize-control-description"><?php echo esc_html( $description ); ?></span>
                                    <?php 
                                        }

                                        switch ( $field['type'] ) {

                                            /**
                                             * URL field
                                             */
                                            case 'url':
                                                echo '<input data-default="'.esc_attr( $default ).'" data-name="'.esc_attr( $key ).'" type="text" placeholder="'. esc_attr( $placeholder ) .'" value="'.esc_url( $new_value ).'"/>';
                                                break;
     
                                            /**
                                             * Social Icon field
                                             */
                                            case 'social_icon':
                                                $purenote_social_icons_array = purenote_social_icons_array();
                                                echo '<div class="mt-repeater-selected-icon"><i class="'. esc_attr( $new_value ) .'"></i><span><i class="bx bx-chevron-down"></i></span></div><ul class="mt-repeater-icon-list mt-clearfix">';
                                                foreach ( $purenote_social_icons_array as $purenote_social_icon ) {
                                                    $icon_class = $new_value == $purenote_social_icon ? 'icon-active' : '';
                                                    echo '<li class='. esc_attr( $icon_class ) .'><i class="'. esc_attr( $purenote_social_icon ) .'"></i></li>';
                                                }
                                                echo '</ul><input data-default="'. esc_attr( $default ) .'" type="hidden" value="'. esc_attr( $new_value ) .'" data-name="'. esc_attr( $key ) .'"/>';
                                                break;

                                            /**
                                             * Upload field
                                             */
                                            case 'upload':
                                                $image_class = "";
                                                $upload_btn_label = esc_html__( 'Select Image', 'purenote' );
                                                $remove_btn_label = esc_html__( 'Remove', 'purenote' );
                                                if ( $new_value ) { 
                                                    $image_class = ' hidden';
                                                }
                                                echo '<div class="mt-fields-wrap"><div class="attachment-media-view"><div class="placeholder'. esc_attr( $image_class ).'">';
                                                esc_html_e( 'No image selected', 'purenote' );
                                                echo '</div><div class="thumbnail thumbnail-image"><img src="'.esc_url( $new_value ).'" style="max-width:100%;"/></div><div class="actions clearfix"><button type="button" class="button mt-delete-button align-left">'. esc_html( $remove_btn_label ) .'</button><button type="button" class="button mt-upload-button alignright">'. esc_html( $upload_btn_label ) .'</button><input data-default="'.esc_attr( $default ).'" class="upload-id" data-name="'.esc_attr( $key ).'" type="hidden" value="'.esc_attr( $new_value ).'"/></div></div></div>';
                                                break;

                                            /**
                                             * Dropdown Category
                                             */
                                            case 'dropdown_category':
                                                $options = purenote_get_categories();
                                                echo '<select  data-default="'.esc_attr( $default ).'"  data-name="'.esc_attr( $key ).'">';
                                                    foreach ( $options as $option => $val )
                                                    {
                                                        printf( '<option value="%1$s" %2$s>%3$s</option>', esc_attr( $option ), selected( $new_value, $option, false ), esc_html( $val ) );
                                                    }
                                                echo '</select>';
                                                break;

                                            case 'hidden' :
                                                echo '<input type="hidden" class="repeater-field-visible-holder" data-default="' .esc_attr( $default ). '" data-name="' .esc_attr( $key ). '" value="' .esc_attr( $new_value ). '">';
                                                break;

                                            default:
                                                break;
                                        }
                                    ?>
                                </div>
                        <?php
                            }
                        ?>
                        <div class="mt-clearfix mt-repeater-footer">
                            <div class="alignright">
                            <a class="mt-repeater-field-remove" href="#remove"><?php esc_html_e( 'Delete', 'purenote' ) ?></a> |
                            <a class="mt-repeater-field-close" href="#close"><?php esc_html_e( 'Close', 'purenote' ) ?></a>
                            </div>
                        </div><!-- .mt-repeater-footer -->
                    </div><!-- .mt-repeater-fields-->
                </li>
        <?php   
                }
            }
        }

    }

endif;