<?xml version="1.0" encoding="utf-8"?>
<modification>
    <name>X-Shipping Pro</name>
    <code>xshippingpro</code>
    <version>4.1.2</version>
    <author>OpenCartMart</author>
    <link>http://www.opencartmart.com</link>
    <file path="admin/view/template/sale/order_form.twig">
        <operation>
            <search><![CDATA[ {{ footer }} ]]></search>
            <add position="before"><![CDATA[
            <script type="text/javascript">
            // TODO - controll centrally
            var _ocm_catalog = '{{ catalog }}' || $('select[name=\'store\'] option:selected').val();
            var _ocm_request_cache = {};
            var _ocm_request_counter = 1;
            var xshippingpro_sub_options = {};
            var xshippingpro_sub_options_flag = true;
            var api_token = '&api_token={{ api_token }}';
            $('#input-shipping-method').on('change', function() {
                if (!$.isEmptyObject(xshippingpro_sub_options)) {
                   setSubOptions();
                }
            });
            function setSubOptions() {
                var code = $('#input-shipping-method').val();
                var url = _ocm_catalog + 'index.php?route=api/shipping/update_sup_options' + api_token;
                $.post(url, {xshippingpro_code: code}, function( ) {
                   // for future    
                });
            }
            // following will be common for all mods
            $.ajaxPrefilter(_onOcmAjaxReq);
            $(document).ajaxComplete(_onOcmAjaxReqComplete);
            function _onOcmAjaxReqComplete(event, xhr, settings) {
                if (xshippingpro_sub_options_flag) {
                     $.get(_ocm_catalog + 'index.php?route=api/shipping/get_sup_options' + api_token, function(json) {
                        xshippingpro_sub_options = json.sub_options;
                     });
                     xshippingpro_sub_options_flag = false;  
                }
            }
            function _onOcmAjaxSuccess(data, status, jqXhr) {
                var xshippingpro_quote = parseAndGetData(data, 'shipping_methods.xshippingpro.quote');
                if (xshippingpro_quote) {
                    for (var key in xshippingpro_quote) {
                        var tab_id = key.replace('xshippingpro', '');
                        if (xshippingpro_sub_options[tab_id]) {
                            for (var _key in xshippingpro_sub_options[tab_id]) {
                                var sub_option = xshippingpro_sub_options[tab_id][_key];
                                var code = sub_option.code.replace('xshippingpro.', '');
                                xshippingpro_quote[code] = {
                                   code: sub_option.code,
                                   title: sub_option.title,
                                   text: ''
                                };
                            }
                        }
                    }
                    if (!$.isEmptyObject(xshippingpro_sub_options)) {
                        setTimeout(setSubOptions, 500);
                    }
                }
                if (_ocm_request_cache[jqXhr.xid]) {
                    _ocm_request_cache[jqXhr.xid].call(null, data, status, jqXhr);
                    _ocm_request_cache[jqXhr.xid] = null;
                }
            }
            function _onOcmAjaxReq(options, originalOptions, jqXhr) {
                if (options.dataType == 'json' || !options.dataType) {
                    jqXhr.xid = _ocm_request_counter++;
                    _ocm_request_cache[jqXhr.xid] = options.success;
                    options.success = _onOcmAjaxSuccess;
                }
            }
            function parseAndGetData(data, keys) {
                var _keys = keys.split('.');
                var _data = data;
                if (!data || !$.isPlainObject(data)) {
                   return false;
                }
                for (var i = 0; i < _keys.length; i++) {
                    var key = _keys[i];
                    if (_data[key]) {
                        _data = _data[key];    
                    } else {
                       return false;
                    }
                }
                return _data;
            }
            </script>
            ]]></add>
        </operation>
    </file>
   <file path="system/library/cart/cart.php">
    <operation error="log">
        <ignoreif regex="true"><![CDATA[ /ocm_special/ ]]></ignoreif>
        <search><![CDATA[ => ($price + $option_price), ]]></search>
        <add position="before"><![CDATA[
              'ocm_special'      => $price != $product_query->row['price'],
        ]]></add>
    </operation>
</file>
</modification>