﻿ModulePerimetreAbonnement.SearchModule = ModulePerimetreAbonnement.SearchModule || (function () {
    var _args;
    var self = this;

    return {
        init: function (args) {
            _args = $.extend({
            }, args);


        },
        start: function () {
            //Critères de recherche
            var searchCriterias = {
                libelle: ko.observable(),
                site: ko.observable(),
                ExportExcel: ko.observable(false)
            }

            $('#' + Static.SearchPanelAccordionID).accordion({ header: 'h3', animate: false });


            var referencesViewModel = function (vueID) {
                var self = this;
                //références
                self.sites = ko.observableArray();
                //Flags
                self.sitesLoaded = ko.observable(false);

                //Chargement
                Func.getCombo('Site', vueID, '', function (data) { self.sites(data) }, function () { self.sitesLoaded(true); });
            }


            var searchViewModel = function () {
                var self = this;
                self.criterias = searchCriterias;
                self.vueID = _args.vueID;
                self.panelID = _args.panelID;
                self.references = new referencesViewModel(self.vueID);

                self.search = function () {
                    ko.postbox.publish(Enums.PostboxNotifiers.SearchFilter, ko.toJS(self.criterias));
                };
                self.clear = function () {
                    self.criterias.libelle(''),
                    self.criterias.site(null);
                    self.criterias.ExportExcel(false);
                };
                //Abonnement aux actions générales
                ko.postbox.subscribe(Enums.PostboxNotifiers.ActionGeneralExecution, function (data) {
                    if (Vars.CurrentVue.vueID == self.vueID && Vars.CurrentVue.panelID == self.panelID) {
                        alert('Perimetre abonnement.Search action générale : ' + JSON.stringify(Vars.CurrentVue) + '    -    ' + self.vueID + '    -   ' + self.panelID);
                    }
                });
                //Abonnement aux mises à jour des fiches
                ko.postbox.subscribe(Enums.PostboxNotifiers.AGUpdated, function () {
                    self.search();
                });

            };

            //Results panel
            var grid = $("#" + Static.SearchGridResultsID).jqGrid(Func.GetjqGridOptions({
                pager: Static.SearchGridResultsPagerID,
                colNames: [Func.translate('ID'), Func.translate('SITE'), Func.translate('LIBELLE'), Func.translate('DATE_CREATION')],
                colModel: [{ name: 'ID', width: '130px', key: true, sorttype: 'int', hidden: true }, { name: 'Site' }, { name: 'Libelle' }, { name: 'DateCreation', formatter:'date', formatoptions: { srcformat: 'ISO8601Long', newformat: 'd/m/Y' } }],
                onSelectRow: function (rowid, iRow, iCol, e) {
                    var rowdata = $(this).getRowData(rowid);
                    ko.postbox.publish(Enums.PostboxNotifiers.NewTabAdd, {
                        id: rowid,
                        title: Utils.formatPanelTitle(Func.translate("LOADING") + '...', 'italic'),
                        url: Func.getUrl("PERIMETRE_ABONNEMENT_DETAILS", { id: rowid, vueID: null }),
                        groupeVueID: Resources.GroupeVue.PerimetreAbonnementDetails
                    });
                }
            })).jqGrid('navGrid', "#" + Static.SearchGridResultsPagerID, { search: false, edit: false, add: false, del: false, refresh: false });

            
            var resultsViewModel = function () {
                var self = this;
                self.data = ko.observableArray([]);
                ko.postbox.subscribe(Enums.PostboxNotifiers.SearchFilter, function (criterias) {
                    grid.jqGrid('clearGridData').jqGrid('setGridParam', { datatype: 'json', url: Func.getUrl("PERIMETRE_ABONNEMENT_RECHERCHE"), postData: criterias }).trigger('reloadGrid');
                });
            };
            ko.applyBindings(new searchViewModel(), document.getElementById(Static.SearchPanelFilterID));
            ko.applyBindings(new resultsViewModel(), document.getElementById(Static.SearchPanelResultsID));
        }
    };
}());