// $Id: si.js,v 1.1 2007/08/13 04:45:08 kurt Exp $
// Copyright (c) 2004 Kurt M. Brown, x2ii.info.
// This file is subject to the terms and conditions of the GNU General Public
// License. See the file COPYING for more details.

xi.BasicList = function(container, editorName, listLabel, columns)
{
    this.container = container;
    this.editorName = editorName;
    this.$newLabel = null;
    this.$listLabel = listLabel;
    this.$columns = columns;
    var headerRowLabels = [];
    for (var i = 0; i < columns.length; ++i)
        headerRowLabels.push(columns[i][2]);
    this.$headerRowLabels = headerRowLabels;
    this.$numCols = columns.length;
    this.$sortQueue = [];
    this.$sortArgs = '';
}
xi.BasicList.prototype.addNewButton = function(newLabel)
{
    this.$newLabel = newLabel;
}
xi.BasicList.prototype.send = function(method, onFault, filterArgs)
{
    this.$method = method;
    this.$onFault = onFault;
    this.$filterArgs = filterArgs;
    this.build();
    this.run();
}
xi.BasicList.prototype.buildAndRun = function()
{
    this.build();
    this.run();
}
xi.BasicList.prototype.run = function()
{
    this.$pager.showProcessing();
    var self = this;
    function onSuccess(params) {
        self.list(params);
    }
    this.$lastArgs = this.$pager.getArgs();
    const args = [this.$lastArgs, this.$sortArgs, this.$filterArgs];
    xi.xmlrpc.send(this.$method, args, onSuccess, this.$onFault);
}
xi.BasicList.prototype.sort = function(colNum, sortDir)
{
    ++colNum;
    var sortArgs = [];
    var newQueue = [];
    if (sortDir != 0) {
        newQueue.push(sortDir*colNum);
        sortArgs.push(colNum + (sortDir > 0 ? ' asc' : ' desc'));
    }
    for (var i = 0; i < this.$sortQueue.length; ++i) {
        const col = this.$sortQueue[i];
        if (Math.abs(col) != colNum) {
            newQueue.push(col);
            sortArgs.push(Math.abs(col) + (col > 0 ? ' asc' : ' desc'));
        }
    }
    this.$sortQueue = newQueue;
    this.$sortArgs = sortArgs.join(',');
    this.run();
}
xi.BasicList.prototype.build = function()
{
    var container = this.container;
    container.removeAllChildren();
    var vbox = container.appendChild(document.createElement('vbox'));
    vbox.setAttribute('align', 'central');
    if (this.$newLabel) {
        var button = vbox.appendChild(document.createElement('button'));
        button.setAttribute('label', this.$newLabel);
        button.onChoice = xi.BasicList.onChoice;
        button.data = null;
        button.control = this;
        button.setAttribute('onclick', 'this.onChoice()');
    }
    var self = this;
    this.$pager = container.appendChild(document.createElement('pager'));
    this.$pager.onChoice = function() {
        self.run();
    }
    if (this.$lastArgs)
        this.$pager.setArgs(this.$lastArgs);
    var viewgrid = container.appendChild(document.createElement('viewgrid'));
    viewgrid.init(this.$columns.length);
    viewgrid.addTitleRow(this.$listLabel);
    viewgrid.addHeaderRow(this.$headerRowLabels, this);
    this.$viewgrid = viewgrid;
}

xi.BasicList.prototype.list = function(params)
{
    var viewgrid = this.$viewgrid;
    this.$pager.showFinished(params[0]);
    viewgrid.deleteRows();
    if (params[0] == 0) {
        viewgrid.showNone('None');
        return;
    }
    var cols = this.$columns;
    const dataSet = params[1];
    for (var i = 0; i < dataSet.length; ++i) {
        var cells = [];
        const data = dataSet[i];
        var cellData = [];
        for (var j = 0; j < cols.length; ++j) {
            const col = cols[j];
            const name = col[0].toLowerCase();
            var value = data[name];
            cellData.push(value);
            if (typeof(value) == 'object')
                value = value.label;
            if (col.length == 4 && typeof(col[3]) == 'object')
                value = col[3][value];
            if (col.length == 4 && typeof(col[3]) == 'boolean')
                cells.push(
                    new xi.viewgrid.Cell(
                        col[1], value, xi.BasicList.onChoice, this
                    )
                );
            else
                cells.push(new xi.viewgrid.Cell(col[1], value));
        }
        viewgrid.addRow(cells, cellData);
    }
}
xi.BasicList.onChoice = function()
{
    var container = this.control.container;
    container.removeAllChildren();
    var editor = container.appendChild(
        document.createElement(this.control.editorName)
    );
    var self = this.control;
    function onReturn() {
        self.buildAndRun();
    }
    editor.begin(this.data, onReturn);
}

