var cdp_shortcode_data = {};
var cdp_shortcode_call = '';
var cdp_shortcode_request = '';

//inventory_search
if (jQuery('.cdp-inventory-shortcode.search-shortcode-container').length) {
    jQuery('.shortcode-input-action').change(function (e) {
        searchParent = jQuery(e.target).parents('.cdp-inventory-shortcode.search-shortcode-container');
        cdp_shortcode_request = jQuery(e.target).attr('name');
        if (cdp_shortcode_request == 'saleclass' && jQuery(e.target).val() == '') {
            jQuery(searchParent).find('select.lockable').prop('disabled', true);
        } else {
            jQuery(searchParent).addClass('running').find('select.lockable').prop('disabled', true);
            cdp_shortcode_call = 'search_shortcode';
            jQuery(searchParent).find('select').each(function () {
                cdp_shortcode_data[jQuery(this).attr('name')] = jQuery(this).val();
            });
            cdp_shortcode_data['search'] = jQuery(searchParent).find('.shortcode-input-search').val();
            cdp_shortcode_ajax_call().done(function (result) {
                cdp_shortcode_update_dropdown(searchParent, result['content']['dropdown']);
            }).fail(function () {
                //console.log('Connection Error');
            });
            jQuery(searchParent).removeClass('running');
            cdp_shortcode_request = cdp_shortcode_call = '';
            cdp_shortcode_data = {};
        }
    });

    jQuery('.shortcode-search-button').click(function (e) {
        searchParent = jQuery(e.target).parents('.cdp-inventory-shortcode.search-shortcode-container');
        cdp_shortcode_request = jQuery(e.target).attr('data-name');
        cdp_shortcode_call = 'search_shortcode';
        jQuery(searchParent).find('select').each(function () {
            cdp_shortcode_data[jQuery(this).attr('name')] = jQuery(this).val();
        });
        cdp_shortcode_data['search'] = jQuery(searchParent).find('.shortcode-input-search').val();
        cdp_shortcode_ajax_call().done(function (result) {
            window.location = result['url'];
        }).fail(function () {
            //console.log('Connection Error');
        });
    });
}

function cdp_shortcode_update_dropdown(parentWrapper, data) {
    hideCounts = jQuery(parentWrapper).find('.hide-counts').length;
    jQuery(searchParent).find('select.lockable').each(function () {
        jQuery(this).children().remove();
        let name = jQuery(this).attr('name');
        jQuery(this).append('<option value="0">' + name.charAt(0).toUpperCase() + name.slice(1) +'s</option>');
    });
    jQuery.each(data, function (key, values) {
        jQuery(parentWrapper).find('.shortcode-select-' + key).children().remove();
        //var list = '<option value="0">All '+key.extender_capitalizeFirstLetter()+'s</option>';
        var list = '<option value="0">All</option>';
        jQuery.each(values['data'], function (id, item) {

            if (values['needle'] === item && item !== '') {
                selected = 'selected';
            } else {
                selected = '';
            }
            if (values['dataCount'] && values['dataCount'][item] && !hideCounts) {
                count = '(' + values['dataCount'][item] + ')';
            } else {
                count = '';
            }
            list += '<option value="' + item + '" ' + selected + '>' + item + ' ' + count + '</option>';

        })
        jQuery(parentWrapper).find('.shortcode-select-' + key).append(list);
        jQuery(parentWrapper).find('.shortcode-select-' + key).prop('disabled', false);
        // if ( jQuery(parentWrapper).find('.shortcode-select-' + key + ' option').length === 2 && (key === 'trim' || key === 'model_code') ) {
        //     jQuery(parentWrapper).find('.shortcode-select-' + key).prop('disabled', true);
        // }

    })
}

//inventory_search
function cdp_shortcode_ajax_call() {
    // console.log('handler: ', cdp_shortcode_call);
    // console.log('params: ', cdp_shortcode_data);
    // console.log('request: ', cdp_shortcode_request);
    return jQuery.ajax({
        url: ajax_object.url,
        data: {
            'action': 'sc_ajax_handler',
            'handler': cdp_shortcode_call,
            'params': cdp_shortcode_data,
            'request': cdp_shortcode_request,
            'nonce': ajax_object.nonce
        },
        dataType: 'json',
        beforeSend: function () {
        },
        complete: function () {

        },
        success: function (data) {

        },
        error: function (xhr, status, error) {

        }
    });
}