<%@language = "javascript" %>
<%
/*  $Revision$
    $Id$

    File:               fac_edit_widget_page.asp
    Description:        Dynamische widget pagina
    Parameters:         wgt_page_key, if omitted the personal widgetpage is used.
                        wellicht iets met edit=1, anders automatisch obv autorisatie
    Context:
    Note:               Edit-mode voor een widget-pagina

*/
%>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="fac_widget.inc" -->
<%
var wgt_page_key = getQParamInt("wgt_page_key", -1);
var this_wgt_page = wgt.func_enabled_wgt_page(wgt_page_key);
user.auth_required_or_abort(this_wgt_page.canWrite);
var mode = getQParamSafe("mode", "show");
var verynew = false;

if (wgt_page_key == -1)
{
    var sql = "SELECT fac_widget_page_key"
            + "  FROM fac_widget_page"
            + " WHERE prs_perslid_key = " + user_key;
    var oRs = Oracle.Execute(sql);
    if (!oRs.EoF)
        wgt_page_key = oRs("fac_widget_page_key").value;
    oRs.Close();
}

if (wgt_page_key == -1) // nog steeds, dan is er dus ook geen persoonlijke pagina -> maak er 1 aan
{
    wgt_page_key = wgt.newWidgetPage();
    verynew = true;
}

var transitParam = buildTransitParam(["wgt_page_key", "mode"], { "wgt_page_key": wgt_page_key });

FCLTHeader.Requires({ plugins: ["suggest", "jQuery"],
                      js: ["jQuery-ui.js", "./fac_edit_widget_page.js", "./fac_widget_page.js"]
                    });

var widgets = wgt.get_widgets_onpage(wgt_page_key);
if (widgets.length === 0)
    verynew = true;

__Log(widgets.length + " widgets on this page");

%>
<html>
    <head>
<%
        FCLTHeader.Generate();
%>
        <script>
            FcltMgr.setTitle("<%=(this_wgt_page.title ? safe.jsstring(this_wgt_page.title) : (this_wgt_page.public ? safe.jsstring(L("fac_widget_page")) : safe.jsstring(L("lcl_menu_my_wgt_page"))))%>", { hot: true });
            function wgt_submit()
            {
                var data = { "mode": "page",
                             "wgt_page_key": <%=wgt_page_key%> };
                data.widgets = [];

                $(".widget-container > .widget-col-wrapper:not(.widget-new-wrapper)").each(function(i)
                {
                    widget = {};
                    widget.key = parseInt($(this).attr("data-key"), 10);
                    widget.order = i;
                    widget.width = parseInt(100 / 12 * $(this).attr("data-width"), 10);
                    if ($(this).closest(".widget-container").find(".fix").prop("checked")) // Hoogte vastzetten ?
                    {
                        widget.height = parseInt($(this).find("[class*=widget-type-]").height(), 10);
                    }
                    data.widgets.push(widget);
                });

<%              if (wgt_page_key == -1 || widgets.length == 0) // New or empty page
                { %>
                if (data.widgets.length === 0)
                {
                    FcltMgr.closeDetail(window, { cancel: true, verynew: 1 } );
                    return false;
                }
<%              } %>

                protectRequest.dataToken(data);

                // Hier wordt $.ajax gebruikt ipv $.post omdat we contentType: json willen verzenden, dat kan niet met $.post
                $.ajax({ "url": "fac_edit_widget_page_save.asp"
                       , "type": "POST"
                       , "data": JSON.stringify(data)
                       , "dataType": "json"
                       , "contentType": "application/json; charset=utf-8"
                       , "success": FcltCallbackAndThen(wgt_submit_callback)
                });
            }

            function wgt_submit_callback(json)
            {
                if (json.success)
                {
                    if (FcltMgr.stopEdit(window))
                        window.location.href = "fac_show_widget_page.asp?1=1<%=transitParam%>";
                }
                else
                    iface.button.enable("wgt_submit");
            }

            function wgt_cancel()
            {
<%
                if (wgt_page_key == -1 || (widgets.length == 0 && mode != "wrap"))
                {
%>
                FcltMgr.closeDetail(window, { cancel: true, verynew: <%=verynew ? 1 : 0%> } );
<%
                }
                else
                {
%>
                if (FcltMgr.stopEdit(window))
                    window.location.href = "fac_show_widget_page.asp?1=1<%=transitParam%>";
<%
                }
%>
            }
        </script>
    </head>
    <body class="widget-body">
<%
        var buttons = [ { title: L("lcl_submit"), action:"wgt_submit()", icon: "fa-fclt-save", id: "wgt_submit", singlepress: true },
                        { title: L("lcl_cancel"), action:"wgt_cancel()", icon: "fa-fclt-cancel" } ];
        IFRAMER_HEADER("", buttons);

        if (widgets.length !== 0)
        { %>
            <div class="widget-container low-clearance">
<%              var rowWidth = 0;
                for (i in widgets)
                {
                    widget = widgets[i];
                    if (widget.width <= 0)
                        continue;

                    if (rowWidth + widget.width > 12)
                    {
                        wgt.empty_col(); %>
                    </div>
                    <div class="widget-container low-clearance">
<%                      rowWidth = 0;
                    }

                    wgt.renderWidget(widget, "edit");

                    rowWidth += widget.width;
                }
                wgt.empty_col(); %>
            </div>
<%      }
        wgt.tooltip_row(); %>

    </body>
</html>