dojo.provide("parus.balanceAbsentReportsDialog");

dojo.require("dijit.Dialog");
dojo.require("dijit.form.Button");

dojo.require("dojox.grid.DataGrid");

dojo.require("parus.parus8");
dojo.require("parus.gridData");
dojo.require("parus.dialog");


dojo.declare("parus.balanceAbsentReportsDialog", [parus.Dialog], {

  date: null,

  agentGroup: null,

  kind: null,

  postCreate: function() {
    this.inherited(arguments);

    this.reportsGrid.attr("store", this.gridStore());


    this.reportsPopupMenu.bindDomNode(this.reportsGrid.viewsNode);
    parus.balance._createActionsButton(this.reportsPopupMenu, this.headerPane);

    var settings = parus.userdata.get(this.declaredClass);
    if (settings) {
      parus.gridSettingsDialog.mergeGridSettings(this.reportsGrid, settings.gridSettings);
      parus.gridSettingsDialog.mergeColorConditions(this.reportsGrid, settings.colorConditions);
      parus.gridSettingsDialog.mergeMenuSettings(this.reportsPopupMenu, settings.actionSettings, settings.userProcsActions, settings.userReportsActions, settings.userAppsActions);
      parus.gridSettingsDialog.mergeFilterBar(this.reportsGrid, settings.showFilterBar, settings.filterValues, settings.sortInfo);
    };

    this.reportsGrid.setQuery(this.reportsGrid.query);

  },

  gridStore: function() {
    return parus.gridData({
      queryid: "AbsentReports",

      onGetConditions: dojo.hitch(this, function(store) {
        return {
          KIND: this.kind,
          AGNGRP: this.agentGroup,
          BDATE: this.date
        };
      }),

      onComplete: dojo.hitch(this, function(store, items) {
        dojo.forEach(items, dojo.hitch(this, function(item, idx) {
          if (parus.parus8.nodeField(item, "CREATEREPORT") == "1") this.reportsGrid.selection.addToSelection(idx);

        }));
        this.gridChanged();

      })
    });
  },

  gridChanged: function() {
    dojo.forEach(this.reportsPopupMenu.getChildren(), dojo.hitch(this, function(menuItem) {
      if (menuItem.itemAction) if (menuItem.declaredClass != "dijit.MenuSeparator")

      menuItem.attr("disabled", !this.reportsGrid.selection.getSelectedCount());
    }));

    parus.parus8.checkMenuSeparators(this.reportsPopupMenu);
  },

  createReports: function() {

    var rns = [];

    var item = this.reportsGrid.selection.getFirstSelected();

    for (var i = 0; i < this.reportsGrid.selection.getSelectedCount(); i++) {

      rns.push({
        RN: this.reportsGrid.store.getValue(item, "RN"),
        BDATE: this.reportsGrid.store.getValue(item, "BDATE")
      });


      item = this.reportsGrid.selection.getNextSelected(item);
    };

    if (!rns.length) return;

    (new parus.confirmationDialog({

      message: "Создать все отмеченные несозданные отчеты?",

      onApply: dojo.hitch(this, function() {

        dojo.forEach(rns, function(rn) {
          parus.database.execute("createAbsentReports", rn);
        });

        parus.balance.refreshGrid(this.reportsGrid);

        return true;

      })
    })).show();


  },


  sendMessages: function() {

    var rns = [];
    var item = this.reportsGrid.selection.getFirstSelected();

    for (var i = 0; i < this.reportsGrid.selection.getSelectedCount(); i++) {
      rns.push(this.reportsGrid.store.getValue(item, "RN"));
      item = this.reportsGrid.selection.getNextSelected(item);
    };

    if (!rns.length) return;

    (new parus.confirmationDialog({

      message: "Разослать предупредительные сообщения по выбранным несозданным отчетам?",

      onApply: dojo.hitch(this, function() {

        parus.database.execute("createAbsentReportsMessages", {
          RN: rns.join(";")
        });
        return true;

      })
    })).show();
  }


});