﻿

(function ($) {
    $.widget("nmk.orderresult", {
        options: {
            identifier: "",
            session: "",
            id: 0,
            result: false,
            orderref: "",
            c: "",
            d: "",
            t: 0,
            clickCallback: function (item) { },
            user: function (user) { },
            type: 'order',
            resettimeout: 3 * 60 * 1000,
            returnUrl: "",
        },
        resetTimer: null,
        resetResetTimer: function (detta) {
            if (detta.resetTimer != null) {
                clearTimeout(detta.resetTimer);
            }

            detta.resetTimer = setTimeout(function () {
                var divTitle = $("<div/>").html("<h3>" + "Vill du fortsätta?".translate() + "</h3>");
                var nr = 10;
                var resetFunc = function (nr) {
                    divTitle.html("<h3>" + "Vill du fortsätta?".translate() + " (" + nr + ")</h3>");
                    if (nr == 0) {
                        $(location).attr('href', "default.html");
                    } else {
                        smallTimer = setTimeout(function () {
                            resetFunc(nr);
                        }, 1000);
                    }
                    nr--;
                }
                var smallTimer = setTimeout(function () {
                    resetFunc(nr);
                }, 1000);
                BootstrapDialog.confirm({
                    message: divTitle,
                    btnCancelLabel: 'NEJ'.translate(), // <-- Default value is 'Cancel',
                    btnOKLabel: 'JA'.translate(), // <-- Default value is 'OK',
                    callback: function (result) {
                        if (result) {
                            detta.resetResetTimer(detta);
                            clearTimeout(smallTimer);
                        } else {

                            $(location).attr('href', "default.html");
                        }
                    }
                });

            }, detta.options.resettimeout);
        },
        url: 'json/orderresult.ashx',
        currentStatus: 0,
        refresh: function (options2) {
            if (options2)
                this.options = $.extend({}, options2);
            this._load();
        },
        _create: function () {
            this.randomnr = randomFromInterval(0, 99999);
            this._load();
        },
        _download: function (detta, command, id, type) {
            var url = detta.url + '?command=' + command + '&f=' + id + '&_=' + new Date().getTime() + '&session=' + detta.options.session + '&fakturatype=' + type;
            $.fileDownload(url);
            detta.resetResetTimer(detta);
        },
        _ajax: function (detta, item, callback) {
            if (item.title == undefined) {
                item.title = "";
            }
            item.session = detta.options.session;
            item.identifier = detta.options.identifier;
            console.log(item);
            if (item.title.length > 0)
                loadingBar(true, 1, "Vänta...", item.title + "...");
            $.ajax({
                url: detta.url,
                error: function (xhr, textStatus, errorThrown) {
                    if (item.title.length > 0)
                        loadingBar(false, 1);
                },
                cache: false,
                data: item,
                type: "POST"
            }).done(function (data) {
                var obj = $.parseJSON(data);
                if (item.title.length > 0)
                    loadingBar(false, 1);
                callback(obj);
                if (obj.checkitem) {
                    detta.options.user(obj.checkitem.medlem);
                }

            });
        },
        item: null,
        loggedout: false,
        _closeFunc: function (detta) {
            var item = new Object();
            item.command = "closeorderresult";
            item.iid = detta.options.id;
            item.type = detta.options.type;
            detta._ajax(detta, item, function (obj) {
                if (!obj.resultat) {
                    BootstrapDialog.alert(obj.text);
                } else {
                    $(location).attr('href', obj.url);
                }
            });
        },
        _load: function () {
            this.element.empty();
            var detta = this;

            var div = $("<div/>").appendTo(this.element).addClass('loader');

            var funcLoad = function (nr) {


                detta.resetResetTimer(detta);

                var item = new Object();
                item.command = detta.options.type + "result";
                item.id = detta.options.id;
                item.orderref = detta.options.orderref;
                item.success = detta.options.success;
                item.c = detta.options.c;
                item.d = detta.options.d;
                item.t = detta.options.t;

                detta._ajax(detta, item, function (obj) {
                    if (nr > 0) {
                        if (obj.fakturabetald != 1) {
                            nr--;
                            setTimeout(function () { funcLoad(nr); }, 1000);

                            return;
                        }
                    }

                    //if (obj.medlem) {
                    //    funcLogout(function (user) {

                    //    }, true);
                    //    return;
                    //}
                    detta.element.empty();
                    div = $("<div/>").appendTo(detta.element);
                    var divButtons = $("<div/>");
                    detta.item = obj.item;
                    var divItem = $("<div/>");
                    console.log("Text:", obj.text);
                    divItem.append(obj.text);
                    $("<button/>").appendTo(divButtons).attr("type", "button").addClass("btn btn-primary").attr("data-loading-text", "<i class='fa fa-circle-o-notch fa-spin'></i> " + "Stänger".translate()).html("Stäng".translate()).click(function (e) {
                        e.preventDefault();
                        $(this).button('loading');
                        //$(location).attr('href', "default.html");
                        //funcLogout(function (user) {
                        //    $(location).attr('href', "default.html");
                        //}, true);
                        detta._closeFunc(detta);
                    });
                    if (obj.orderresulturl != undefined) {
                        if (obj.orderresulturl.length > 0) {
                            setTimeout(function () { window.location.href = obj.orderresulturl; }, 1000);
                        }

                    }
                    if (obj.avtaltosign) {
                        if (obj.avtaltosign.length > 0) {
                            var divList = $("<div/>").addClass("list-group").appendTo(divItem);
                            for (var i = 0; i < obj.avtaltosign.length; i++) {
                                $("<a/>").attr("href", "#").addClass("list-group-item list-group-item-info").html("SIGNERA AVTAL".translate()).appendTo(divList).bind('click', {
                                    item: obj.avtaltosign[i]
                                }, function (e) {
                                    $("<div/>").signavtal({
                                        avtal: e.data.item.id, chk: e.data.item.chk, datum: e.data.item.datum, session: detta.options.session, callbackRefresh: function () {
                                            divItem.html("Avtalet signerades utan problem.".translate());
                                            setTimeout(function () {
                                                detta.refresh();
                                            }, 2000);

                                            detta.resetResetTimer(detta);
                                        }
                                    });
                                });
                                divButtons.empty();
                                $("<button/>").appendTo(divButtons).attr("type", "button").addClass("btn btn-primary").attr("data-loading-text", "<i class='fa fa-circle-o-notch fa-spin'></i> " + "Stänger".translate()).html("Stäng".translate()).click(function (e) {
                                    e.preventDefault();
                                    $(this).button('loading');
                                    //$(location).attr('href', "default.html");

                                    detta._closeFunc(detta);
                                });
                                divButtons.append(" ");
                                $("<button/>").appendTo(divButtons).attr("type", "button").addClass("btn btn-primary").attr("data-loading-text", "<i class='fa fa-circle-o-notch fa-spin'></i> " + "Fortsätter".translate()).html("Fortsätt".translate()).bind('click', {
                                    item: obj.avtaltosign[i]
                                }, function (e) {
                                    e.preventDefault();
                                    var $button = $(this);
                                    $button.button('loading');
                                    $("<div/>").signavtal({
                                        avtal: e.data.item.id, chk: e.data.item.chk, datum: e.data.item.datum, session: detta.options.session, callbackRefresh: function () {
                                            $button.button('reset');
                                            divItem.html("Avtalet signerades utan problem.".translate());
                                            setTimeout(function () {
                                                detta.refresh();
                                            }, 2000);
                                        }
                                    });

                                    detta.resetResetTimer(detta);
                                });
                            }

                        } else {
                            if (obj.faktura) {
                                if (obj.faktura > 0) {
                                    divButtons.append(" ");
                                    $("<button/>").appendTo(divButtons).attr("type", "button").addClass("btn btn-primary").attr("data-loading-text", "<i class='fa fa-circle-o-notch fa-spin'></i> " + "Laddar".translate() + "...").html("Ladda hem faktura".translate()).bind('click', {
                                        id: obj.faktura
                                    }, function (e) {
                                        e.preventDefault();
                                        var $button = $(this);
                                        detta._download(detta, 'showfaktura', e.data.id, "");

                                        detta.resetResetTimer(detta);
                                    });
                                }
                            }
                            if (obj.kvitto) {
                                if (obj.kvitto > 0) {
                                    divButtons.append(" ");
                                    $("<button/>").appendTo(divButtons).attr("type", "button").addClass("btn btn-primary").attr("data-loading-text", "<i class='fa fa-circle-o-notch fa-spin'></i> " + "Laddar".translate() + "...").html("Ladda hem kvitto".translate()).bind('click', {
                                        id: obj.kvitto
                                    }, function (e) {
                                        e.preventDefault();
                                        var $button = $(this);
                                        detta._download(detta, 'showfaktura', e.data.id, "kvitto");

                                        detta.resetResetTimer(detta);
                                    });
                                }
                            }
                        }

                    }
                    var design = "default";
                    if (obj.design) {
                        design = obj.design;
                    }

                    div.mypanel({
                        header: { text: obj.title.translate() }, content: { text: divItem }, design: design, footer: {
                            text: divButtons
                        }
                    });


                    if (detta.options.returnUrl) {
                        if (detta.options.returnUrl.length > 0) {
                            window.location.replace(detta.options.returnUrl);
                        }
                    }
                });
            };
            funcLoad(5);

        },
        destroy: function () {
            this.element.empty();
            // Call the base destroy function.
            $.Widget.prototype.destroy.call(this);
        }
    });
})(jQuery);