﻿

(function ($) {
    $.widget("nmk.signavtal", {
        options: {
            identifier: "",
            session: "",
            chk: "",
            avtal: 0,
            clickCallback: function (item) { },
            callbackRefresh: function () { },
            user: function (user) { },
            showvillkor: false,
            datum: ""
        },
        url: 'json/signavtal.ashx',
        currentStatus: 0,
        refresh: function (options2) {
            if (options2)
                this.options = $.extend({}, options2);
            this._load();
        },
        _create: function () {
            this.randomnr = randomFromInterval(0, 99999);
            this._load();
        },
        _ajax: function (detta, item, callback) {
            if (item.title == undefined) {
                item.title = "";
            }
            item.session = detta.options.session;
            item.identifier = detta.options.identifier;
            item.chk = detta.options.chk;
            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);
                }
            });
        },
        download: function (id) {
            this._download(this, id);
        },
        _download: function (detta, id) {
            $.fileDownload(detta.url + '?command=downloadabonnemang&a=' + id + '&_=' + new Date().getTime() + '&session=' + detta.options.session);
        },
        _getPDF: function (detta, id) {
            var url = detta.url + '?command=downloadavtal&a=' + id + '&_=' + new Date().getTime() + '&session=' + detta.options.session + '&chk=' + detta.options.chk + '&d=' + detta.options.datum;
            $.pdfviewer(url, "Signera avtal".translate(), function () {
                //console.log("Utskrift startad.");
            }, [{
                label: 'SIGNERA',
                action: function (dialog) {
                    dialog.close();
                    $("<div/>").bankid({
                        dialog: true, avtal: detta.options.avtal, session: detta.options.session, onFinnish: function () {
                            detta.options.callbackRefresh();
                        }
                    });
                }
            }]);
        },
        _printPDF: function (detta, id) {
            var url = this.url + '?command=downloadabonnemang&a=' + id + '&_=' + new Date().getTime() + '&session=' + detta.options.session;
            $.pdfviewerPrint(url, "Förhandsgranska avtal".translate(), function () {
                console.log("Utskrift startad.");
            });
        },
        item: null,
        _load: function () {
            this.element.empty();
            var detta = this;
            detta._getPDF(detta, detta.options.avtal);

        },
        destroy: function () {
            this.element.empty();
            // Call the base destroy function.
            $.Widget.prototype.destroy.call(this);
        }
    });
})(jQuery);