$(function () {

    function ViewModel() {

        var self = this;

        function findSetting(data, type, code) {
            var settings = data[type] || [];
            for (var i in settings) {
                if (settings[i].Code === code) {
                    return settings[i];
                }
            }
            return null;
        }

        self.save = function () {
            var entries = [];
            [ ].push.apply(entries, self.settings().A);
            [ ].push.apply(entries, self.settings().B);
            [ ].push.apply(entries, self.settings().C);

            for (var i in entries) {
                if (typeof entries[i].Value === "boolean") {
                    entries[i].Value = entries[i].Value ? 1 : 0;
                }
            }

            var param = { jsonData: JSON.stringify(entries) };

            $.tools.post(param, "/AlarmSetting/Save/", function () { }, this);
        };

        self.load = function () {
            $.tools.ajaxLoading(lang.LoadingData);
            $.ajax({
                url: "/AlarmSetting/LoadAll2/",
                type: "GET",
                success: function (r) {
                    if (r.IsSuccess) {
                        self.settings(r.Data);
                        var setting = findSetting(r.Data, "A", "A01");
                        if (setting !== null) {
                            self.a01Enable(setting.Value);
                        }
                        setting = findSetting(r.Data, "A", "A03");
                        if (setting !== null) {
                            self.a03Enable(setting.Value);
                        }
                        setting = findSetting(r.Data, "A", "A05");
                        if (setting !== null) {
                            self.a05Enable(setting.Value);
                        }
                        setting = findSetting(r.Data, "A", "A09");
                        if (setting !== null) {
                            self.a09Enable(setting.Value);
                        }
                        $.tools.ajaxLoadEnd();
                    }
                    else {
                        $.tools.ajaxLoadEnd();
                        $.tools.alert(r.Data);
                    }
                },
                error: function () {
                    $.tools.alert(lang.LoadingDataError);
                }
            });
        };

        self.a01Enable = ko.observable(true);
        self.a01Change = function (model, event) {
            var element = event.srcElement ? event.srcElement : event.target;
            self.a01Enable($(element).is(":checked"));
        }

        self.a03Enable = ko.observable(true);
        self.a03Change = function (model, event) {
            var element = event.srcElement ? event.srcElement : event.target;
            self.a03Enable($(element).is(":checked"));
        }

        self.a05Enable = ko.observable(true);
        self.a05Change = function (model, event) {
            var element = event.srcElement ? event.srcElement : event.target;
            self.a05Enable($(element).is(":checked"));
        }

        self.a09Enable = ko.observable(true);
        self.a09Change = function (model, event) {
            var element = event.srcElement ? event.srcElement : event.target;
            self.a09Enable($(element).is(":checked"));
        }

        self.settings = ko.observable();
    }

    var viewModel = new ViewModel();

    ko.applyBindings(viewModel);

    viewModel.load();
});