<% /*
    $Revision$
    $Id$

    File:        rs_columns.inc

    Description: User-defineable columns for non-scaffolding lists
    Parameters:
    Context:     resultset_table_v2.inc
    Note:

*/ %>
<%

// Deze zet de kolommen in de juiste volgorde en/of onderdrukt ze, conform de door de gebruiker zelf gemaakte keuze
// Sommige kolommen sluiten we hard uit met colName "IGNORE", die zijn niet in de tabel geregistreerd en worden ongeacht
// altijd achteraan volgens de standaard volgorde getoond (bv bij MJOB).

// Deze functie past het meegegeven cols-argument aan en geeft true terug als er een persoonlijke kolom-voorkeur is gevonden.
function reorderPerslidCols(code, cols)
{
    var result = { modified: false, columns: cols };
    function colExists(p_field, p_arr)
    {
        for (var r=0; r<p_arr.length; r++)
        {
            var fld = (p_arr[r].colName || p_arr[r].orgContent);
            if (p_field == fld) {
                return true;
            }
        }
        return false;
    }

    var sql = "SELECT c.prs_perslid_cols_key"
            + "     , c.prs_perslid_cols_column_name"
            + "     , c.prs_perslid_cols_visible"
            + "     , c.prs_perslid_cols_combine"
            + "     , c.prs_perslid_cols_volgnr"
            + "  FROM prs_perslid_tabs t, prs_perslid_cols c"
            + " WHERE t.prs_perslid_tabs_code = " + safe.quoted_sql(code)
            + "   AND c.prs_perslid_tabs_key = t.prs_perslid_tabs_key"
            + "   AND t.prs_perslid_key = " + user_key
            + " ORDER BY c.prs_perslid_tabs_key"
            + "        , c.prs_perslid_cols_volgnr"
            + "        , c.prs_perslid_cols_column_name";
    var oRs = Oracle.Execute(sql);
    var arr = [];
    if (!oRs.eof)
    {
        while (!oRs.eof)
        {
            for (var index=0; index<cols.length; index++)
            {
                var field = (cols[index].colName || cols[index].orgContent);
                if (field == oRs("prs_perslid_cols_column_name").Value)
                {
                    cols[index].combine = oRs("prs_perslid_cols_combine").Value == 1;
                    cols[index].hidden  = oRs("prs_perslid_cols_visible").Value == "I";
                    arr.push(cols[index]);
                    break;
                }
            }
            oRs.MoveNext();
        }
        // Toevoegen ontbrekende colommen
        for (var index=0; index<cols.length; index++)
        {
            var field = (cols[index].colName || cols[index].orgContent);
            if (cols[index].ignore)
            {
                continue; // Skip
            }
            if (!colExists(field, arr))
            {
                cols[index].combine = false;
                cols[index].hidden  = true;
                arr.push(cols[index]);
            }
        }
        // Als laatste de IGNORE kolommen toevoegen
        for (var index=0; index<cols.length; index++)
        {
            var field = (cols[index].colName || cols[index].orgContent);
            if (cols[index].ignore)
            {
                if (!colExists(field, arr))
                {
                    arr.push(cols[index]);
                }
            }
        }
        result = { modified: true, columns: arr };
    }
    oRs.Close();
    return result;
}

function rs_columns(code, cols)
{   // Maak de lijst met kolommen voor het popup scherm om de volgorde te kunnen wijzigen
    // en kolommen aan/uit te zetten.
%>
    <script>
        var $dialog;
        function rscols_submit(method)
        {
            $dialog.dialog("close");
            rscols_doSubmit(method);
        }

        function rscols_reset(method)
        {
            $dialog.dialog("close");
            rscols_doReset(method);
        }

        function openColumns()
        {
            var params = {  width: "auto",
                            minWidth: 200,
                            resizable: false,
                            position: { my: "right top", at: "right+16 top+30", of: event },
                            title: L("lcl_scf_columns"),
                            create: FcltMgr.fnCreateCloseButton("div#rscolpicker")
                          };
            $dialog = $('div#rscolpicker').dialog(params).dialog('open');
            FcltMgr.resized();
        }
    </script>

    <div id="rscolpicker" style="display:none;">
        <input type='hidden' name='code' id='code' value="<%=code%>">
        <input type='hidden' name='columns' id='columns' value="">
        <input type='hidden' name='resetcols' id='resetcols' value="0">
        <table id="rscolumnstable" class="no-floating">
<%
        Response.Write("<thead><tr><th><input id='chkall' type='checkbox'>" + L("lcl_rap_visible") + "</th><th>" + L("lcl_combine") + "</th></tr></thead>");
        Response.Write("<tbody>");

        var sql = "SELECT c.prs_perslid_cols_key"
                + "     , c.prs_perslid_cols_column_name"
                + "     , c.prs_perslid_cols_visible"
                + "     , c.prs_perslid_cols_combine"
                + "     , c.prs_perslid_cols_volgnr"
                + "  FROM prs_perslid_tabs t, prs_perslid_cols c"
                + " WHERE t.prs_perslid_tabs_code = " + safe.quoted_sql(code)
                + "   AND c.prs_perslid_tabs_key = t.prs_perslid_tabs_key"
                + "   AND t.prs_perslid_key = " + user_key
                + " ORDER BY c.prs_perslid_tabs_key"
                + "        , c.prs_perslid_cols_volgnr"
                + "        , c.prs_perslid_cols_column_name";
        var oRs = Oracle.Execute(sql);
        // Zet eerst de settings van prs_perslid_cols in een array
        var db_cols = [];
        while (!oRs.eof)
        {
            db_cols.push( { col_volgnr:  oRs("prs_perslid_cols_volgnr").Value
                          , col_keys:    oRs("prs_perslid_cols_key").Value
                          , col_name:    oRs("prs_perslid_cols_column_name").Value
                          , col_visible: oRs("prs_perslid_cols_visible").Value
                          , col_combine: oRs("prs_perslid_cols_combine").Value
                          }
                        );
            oRs.MoveNext();
        }
        oRs.Close();

        if (db_cols.length == 0)
        {   // show default columns and combine settings
            for (var index=0; index<cols.length; index++)
            {
                if (!cols[index].colName && cols[index].orgContent instanceof Function)
                {
                    __Log("Column {0} ('{1}') is missing colName attibute, unable to choose it!".format(index, cols[index].caption), "#ff8800")
                    continue;
                }
                if ((cols[index].purpose == undefined || cols[index].purpose == VIEW_ONLY || cols[index].purpose == PRINT_AND_VIEW) && !cols[index].ignore)
                {
                    combine = cols[index].combine;
                    Response.Write("<tr id='" + safe.htmlattr(cols[index].colName || cols[index].orgContent) + "'" + (combine?" class='rscols_combined'":"") + ">"
                                    + "<td><label for='usefield"+index+"'><input id='usefield"+index+"' name='usefield' type='checkbox'" + (cols[index].showDefault ? " checked='checked'" : "") + ">" + (cols[index].colText || cols[index].caption) + "</label></td>"
                                    + "<td><label for='combine"+index+"'><input id='combine"+index+"' name='combine' type='checkbox'" + (combine?" checked='checked'":"") + "></label></td>"
                                    + "</tr>");
                }
            }
        }
        else // Er is een persoonlijke kolom-voorkeur gevonden
        {
            var unknown_nr = cols.length -1;
            var x_collist = [];
            for (var index=0; index<cols.length; index++)
            {
                var field = (cols[index].colName || cols[index].orgContent);
                if (!cols[index].ignore && cols[index].purpose != PRINTING_ONLY)
                {
                    x_collist.push(cols[index]);
                }
            }
            for (var index=0; index<x_collist.length; index++)
            {
                x_collist[index].collist = { nr: -1
                                           , id: (x_collist[index].colName || x_collist[index].orgContent)
                                           , on: false
                                           , combine: false
                                           , text: (x_collist[index].colText || x_collist[index].caption)
                                           };
                for (var r=0; r<db_cols.length; r++)
                {   // Staat de kolom al in de setting dan gegevens overnemen.
                    if (db_cols[r].col_name == x_collist[index].collist.id)
                    {
                        x_collist[index].collist.nr      = db_cols[r].col_volgnr;
                        x_collist[index].collist.on      = db_cols[r].col_visible == "V";
                        x_collist[index].collist.combine = db_cols[r].col_combine == 1;
                        break;
                    }
                }
                if (x_collist[index].collist.nr == -1)
                {   // kolommen die nog niet in prs_perslid_cols staan achteraan toevoegen.
                    x_collist[index].collist.nr = unknown_nr;
                    unknown_nr--;
                }
            }

            // cols sorteren op volnummer
            function cols_compare(a,b)
            {
                if (a.collist.nr < b.collist.nr) return -1;
                if (a.collist.nr > b.collist.nr) return 1;
                return 0;
            }
            x_collist.sort(cols_compare);

            // Maak de popup-lijst met kolommen
            for (var index=0; index<x_collist.length; index++)
            {
                Response.Write("<tr id='" + safe.htmlattr(x_collist[index].collist.id) + "'"  + (x_collist[index].collist.combine?" class='rscols_combined'":"") + ">"
                             + "<td><label for='usefield"+index+"'><input id='usefield"+index+"' name='usefield' type='checkbox'" + (x_collist[index].collist.on?" checked='checked'":"") + ">" + x_collist[index].collist.text + "</label></td>"
                             + "<td><label for='combine"+index+"'><input id='combine"+index+"' name='combine' type='checkbox'" + (x_collist[index].collist.combine?" checked='checked'":"") + "></label></td>"
                             + "</tr>");
            }
        }
%>
            </tbody>
        </table>
<%
      buttons = [];
      buttons.push({title: L("lcl_submit"), icon: "fa-fclt-save", action: "rscols_submit('" + Request.ServerVariables("REQUEST_METHOD") + "')", importance: 1 });
      buttons.push({title: L("lcl_reset"), icon: "fa-fclt-cancel", action: "rscols_reset('" + Request.ServerVariables("REQUEST_METHOD") + "')", importance: 3 });
      CreateButtons(buttons, { showIcons: true });
%>
    </div>
<% } %>
