<?php


class WPAdverts_CF_Field_Autocomplete {
    /**
     * Class constructor
     * 
     * Registers init action
     * 
     * @since 1.0
     * @return self
     */
    public function __construct() {
        
        add_filter( "wpadverts_cf_editor_fields", array( $this, "editor_fields"), 5, 3 );
    }
    
    public function editor_fields( $fields, $data, $form ) {
        
        if( $form["name"] == "advert" ) {
            $fields[] = $this->_editor_advert();
        } else if( $form["name"] == "search" ) {
            $fields[] = $this->_editor_search();
        } else if( $form["name"] == "author_profile") {
            $fields[] = $this->_editor_advert();
        } else if( $form["name"] == "contact" ) {
            //$fields[] = $this->_editor_contact();
        } else if( $form["name"] == "author_search" ) {
            $fields[] = $this->_editor_search();
        } else if( $form["name"] == "author_contact" ) {
            //$fields[] = $this->_editor_contact();
        }
        return $fields;
    }
    
    protected function _editor_advert() {
        return array(
            "type" => "adverts_field_autocomplete",
            "template_id" => "cf-visual-adverts_field_autocomplete",
            "callback_template" => array( $this, "field_visual" ),
            "uses_options" => true,
            "label" => __( "Autocomplete", "wpadverts-custom-fields" ),
            "config" => array(
                array(
                    "name" => "_header_common",
                    "callback_template" => "wpadverts_custom_fields_option_header",
                    "label" => __( "Common Settings", "wpadverts-custom-fields" )
                ),
                array(
                    "name" => "name",
                    "callback_template" => "wpadverts_custom_fields_option_name",
                    "callback_js" => "wpadverts_custom_fields_js_name",
                ),
                array(
                    "name" => "label",
                    "callback_template" => "wpadverts_custom_fields_option_label",
                    "callback_js" => "wpadverts_custom_fields_js_label"
                ),
                array(
                    "name" => "is_required",
                    "callback_template" => "wpadverts_custom_fields_option_is_required",
                    "callback_js" => "wpadverts_custom_fields_js_is_required"
                ),
                array(
                    "name" => "_header_specific",
                    "callback_template" => "wpadverts_custom_fields_option_header",
                    "label" => __( "Field Specific Settings", "wpadverts-custom-fields" )
                ),
                array(
                    "name" => "max_choices",
                    "callback_template" => "wpadverts_custom_fields_option_max_choices",
                    "callback_js" => "wpadverts_custom_fields_js_max_choices"
                ),
                array(
                    "name" => "autocomplete_options",
                    "callback_template" => array( $this, "field_specific_html" ),
                    "callback_js" => array( $this, "field_specific_js" )
                ),
                array(
                    "name" => "_header_display",
                    "callback_template" => "wpadverts_custom_fields_option_header",
                    "label" => __( "Display Settings", "wpadverts-custom-fields" )
                ),
                array(
                    "name" => "display",
                    "callback_template" => "wpadverts_custom_fields_option_display",
                    "callback_js" => "wpadverts_custom_fields_js_display"
                ),
            )
        );
    }
    
    protected function _editor_search() {
        return array(
            "type" => "adverts_field_autocomplete",
            "template_id" => "cf-visual-adverts_field_autocomplete",
            "callback_template" => array( $this, "field_visual" ),
            "uses_options" => true,
            "label" => __( "Autocomplete", "wpadverts-custom-fields" ),
            "config" => array(
                array(
                    "name" => "_header_common",
                    "callback_template" => "wpadverts_custom_fields_option_header",
                    "label" => __( "Common Settings", "wpadverts-custom-fields" )
                ),
                array(
                    "name" => "name",
                    "callback_template" => "wpadverts_custom_fields_option_name",
                    "callback_js" => "wpadverts_custom_fields_js_name",
                ),
                array(
                    "name" => "label",
                    "callback_template" => "wpadverts_custom_fields_option_label",
                    "callback_js" => "wpadverts_custom_fields_js_label"
                ),
                array(
                    "name" => "_header_specific",
                    "callback_template" => "wpadverts_custom_fields_option_header",
                    "label" => __( "Field Specific Settings", "wpadverts-custom-fields" )
                ),
                array(
                    "name" => "search_type",
                    "callback_template" => "wpadverts_custom_fields_option_search_type",
                    "callback_js" => "wpadverts_custom_fields_js_search_type"
                ),
                array(
                    "name" => "autocomplete_options",
                    "callback_template" => array( $this, "field_specific_html" ),
                    "callback_js" => array( $this, "field_specific_js" )
                ),
                array(
                    "name" => "_header_search",
                    "callback_template" => "wpadverts_custom_fields_option_header",
                    "label" => __( "Search", "wpadverts-custom-fields" )
                ),
                array(
                    "name" => "cf_search_field",
                    "callback_template" => "wpadverts_custom_fields_option_cf_search_field",
                    "callback_js" => "wpadverts_custom_fields_js_cf_search_field"
                )
            )
        );
    }
    
    protected function _editor_contact() {
        return array(
            "type" => "adverts_field_autocomplete",
            "template_id" => "cf-visual-adverts_field_autocomplete",
            "callback_template" => array( $this, "field_visual" ),
            "label" => __( "Autocomplete", "wpadverts-custom-fields" ),
            "config" => array(
                array(
                    "name" => "_header_common",
                    "callback_template" => "wpadverts_custom_fields_option_header",
                    "label" => __( "Common Settings", "wpadverts-custom-fields" )
                ),
                array(
                    "name" => "name",
                    "callback_template" => "wpadverts_custom_fields_option_name",
                    "callback_js" => "wpadverts_custom_fields_js_name",
                ),
                array(
                    "name" => "label",
                    "callback_template" => "wpadverts_custom_fields_option_label",
                    "callback_js" => "wpadverts_custom_fields_js_label"
                ),
                array(
                    "name" => "is_required",
                    "callback_template" => "wpadverts_custom_fields_option_is_required",
                    "callback_js" => "wpadverts_custom_fields_js_is_required"
                ),
                array(
                    "name" => "_header_specific",
                    "callback_template" => "wpadverts_custom_fields_option_header",
                    "label" => __( "Field Specific Settings", "wpadverts-custom-fields" )
                ),
                array(
                    "name" => "autocomplete_options",
                    "callback_template" => array( $this, "field_specific_html" ),
                    "callback_js" => array( $this, "field_specific_js" )
                ),
            )
        );
    }
    
    public function supports_tax( $tax ) {
        
        $post = get_post( absint( adverts_request('edit') ) );
        $arr = array(
            "wpad-form-au-author" => "advert-author",
            "wpad-form-add" => "advert"
        );
        
        $taxes = get_object_taxonomies( $arr[$post->post_status] );
        
        return in_array( $tax, $taxes );
    }
    
    public function field_specific_html() {
        ?>

        <div class="wpacf-config-field">
            <label class="wpacf-config-label"><?php _e( "Data Source", "wpadverts-custom-fields" ) ?></label>
            <div class="wpacf-config-input">
                <select name="cf_data_source">
                    <option value=""></option>
                <?php foreach( wpadverts_custom_fields_get_data_source() as $source ): ?>
                <?php if( isset( $source["type"] ) && $source["type"] == "taxonomy" && $this->supports_tax( $source["taxonomy"] ) ): ?>
                    <option value="<?php echo esc_attr( $source["name"] ) ?>" data-taxonomy="<?php echo isset( $source["taxonomy"] ) ? $source["taxonomy"] : "advert_category" ?>" {{ HTML.selected( field.getMeta("cf_data_source") == '<?php echo esc_attr( $source["name"] ) ?>' ) }}><?php echo esc_html( $source["title"] ) ?></option>    
                <?php endif; ?>
                <?php endforeach; ?>
                </select>
            </div>
        </div>
        
        <div class="wpacf-config-field" style="display: none">
            <label class="wpacf-config-label"><?php _e( "Data Source Args.", "wpadverts-custom-fields" ) ?></label>
            <div class="wpacf-config-input">
                <input type="text" name="cf_data_source_params" value="{{ field.getMeta( "cf_data_source_params" ) }}" />
            </div>
        </div>

        <div class="wpacf-config-field">
            <label class="wpacf-config-label"><?php _e( "Hierarchical", "wpadverts-custom-fields" ) ?></label>
            <div class="wpacf-config-input">
                <label>
                    <input type="checkbox" name="autocomplete__hierarchical" value="1" {{ HTML.checked( field.get( "autocomplete", "hierarchical" ) == "true" ) }} />
                    <?php _e( "Display suggestions hierarchically.", "wpadverts-custom-fields" ) ?>
                </label>
            </div>
        </div>

        <div class="wpacf-config-field">
            <label class="wpacf-config-label"><?php _e( "Leafs Only", "wpadverts-custom-fields" ) ?></label>
            <div class="wpacf-config-input">
                <label>
                    <input type="checkbox" name="autocomplete__leaf_only" value="1" {{ HTML.checked( field.get( "autocomplete", "leaf_only" ) == "true" ) }} />
                    <?php _e( "Allow selecting leafs (items without sub-items) only.", "wpadverts-custom-fields" ) ?>
                </label>
            </div>
        </div>

        <div class="wpacf-config-field">
            <label class="wpacf-config-label"><?php _e( "Ignore Restrictions", "wpadverts-custom-fields" ) ?></label>
            <div class="wpacf-config-input">
                <label>
                    <input type="checkbox" name="autocomplete__ignore_restricted" value="1" {{ HTML.checked( field.get( "autocomplete", "ignore_restricted" ) == "true" ) }} />
                    <?php _e( "Allow selecting restricted items.", "wpadverts-custom-fields" ) ?>
                </label>
            </div>
        </div>

        <?php
    }
    
    public function field_specific_js($field) {
        ?>
        <script type="text/javascript">
        _WPADVERTS_CF_CALLBACK.<?php echo $field["type"] ?>.push(function(field, form) {
            
            field.meta.cf_options_fill_method = "callback";
            field.meta.cf_data_source = form.find("select[name=cf_data_source]").val();
            
            field.autocomplete = {
                taxonomy: form.find("select[name=cf_data_source] > option:selected").data("taxonomy"),
                multi: parseInt( form.find("input[name=max_choices]").val() ) > 1 ? 'true' : 'false',
                hierarchical: form.find("input[name=autocomplete__hierarchical]").is(":checked") ? 'true' : 'false',
                max_choices: parseInt( form.find("input[name=max_choices]").val() ),
                save_as: 'array',
                separator: ',',
                leaf_only: form.find("input[name=autocomplete__leaf_only]").is(":checked") ? 'true' : 'false',
                ignore_restricted: form.find("input[name=autocomplete__ignore_restricted]").is(":checked") ? 'true' : 'false'
                
            };
            return field;
        });
        </script>
        <?php
    }
    
    public function field_visual($field, $form, $post ) {
        ?>
        <script type="text/html" id="tmpl-cf-visual-adverts_field_autocomplete">
            <# var field = new WPADVERTS.CF.Helper(data) #>
            <# var HTML = WPADVERTS.CF.HTML; #>
            <div class="inside wpacf-mode-visual">
                <div class="main wpacf-visual">
                    <span class="wpacf-visual-label">
                        <# if( data.label.length > 0 ) { #>
                            <span class="wpacf-visual-label-pad">{{ data.label }}</span>

                            <# if( field.hasValidator("is_required") ) { #>
                            <span class="wpacf-field-is-required">*</span>
                            <# } #>
                        <# } #>
                    </span>

                    <div class="wpacf-visual-input-wrap">
                        <input type="text" name="{{ data.name }}" placeholder="{{ data.placeholder }}" />
                    </div>

                    <div class="wpacf-visual-actions">
                        <a href="#" class="wpacf-action-edit button-primary"><span class="dashicons dashicons-edit"></span></a>
                        <a href="#" class="wpacf-action-trash button-secondary"><span class="dashicons dashicons-trash"></span></a>
                    </div>
                </div>
                <div class="main wpacf-config" style="display:none">
                    <form action="" method="get" class="wpacf-config-form">

                        <?php
                            foreach($field["config"] as $config) {
                                call_user_func( $config["callback_template"], $config , $form, $post );
                            }
                        ?>

                    </form>
                    <hr/>
                    <button class="button wpacf-action-update"><?php _e("Update", "wpadverts-custom-fields") ?></button>
                    <button class="button wpacf-action-cancel"><?php _e("Cancel", "wpadverts-custom-fields") ?></button>
                </div>
            </div>
        </script>
        <?php
    }
    
}
