/// <reference path="../vswd-ext_2.1.js" />

/**
 * @class Ext.ux.AjaxStore
 * @extends Ext.data.Store
 * A custom data store, support server side AjaxHandler which implements ISupportSelect
 * @constructor
 * @param config {Object} configuration options of Ext.data.Store, some config options
 * with default values can be ignored.
 */
Ext.ux.AjaxStore = function(config) {
    var config = config || {};
    Ext.applyIf(config, {
        id: 'EntityId',
        root: 'Data',
        totalProperty: 'Total',
        successProperty: 'Success'
    });
    Ext.ux.AjaxStore.superclass.constructor.call(this, config);
};

Ext.extend(Ext.ux.AjaxStore, Ext.data.JsonStore, {
    load: function(options) {
        if (!this.baseParams.op) {
            this.baseParams.op = 'Select';
        }

        var jsonData = {};
        jsonData.queryParam = { SkipResults: 0, MaxResults: 0, FiltByArea: true, Parameter: {} };
        // get pagging info
        if (options && options.params) {
            jsonData.queryParam.SkipResults = !options.params.start ? 0 : options.params.start;
            jsonData.queryParam.MaxResults = !options.params.limit ? 0 : options.params.limit;
        }
        // get sort info
        if (this.sortInfo) {
            jsonData.queryParam.Parameter.SortField = this.sortInfo.field;
            jsonData.queryParam.Parameter.SortDirection = this.sortInfo.direction;
        }
        // get query info [for editable combo only]
        if (this.baseParams.query) {
            jsonData.queryParam.Parameter.Query = this.baseParams.query;
        }
        // how to send condi ?
        jsonData.queryCondi = {};
        if (options && !options.queryCondi) {
            if (this.baseParams.param) {
                Ext.apply(jsonData.queryCondi,Ext.util.JSON.decode(this.baseParams.param).queryCondi);
            }
        }
        if (options && options.queryCondi) {
            Ext.apply(jsonData.queryCondi, options.queryCondi);
        }

        this.baseParams.param = Ext.util.JSON.encode(jsonData);

        Ext.ux.AjaxStore.superclass.load.call(this, options);
    }
});
