dojo.provide("parus.gridActions");

dojo.require("parus.parus8");

if (!parus) parus = {};

if (!parus.gridActions) dojo.mixin(parus, {
  gridActions: {}
});


dojo.mixin(parus.gridActions, {

  checkPopupMenuDefaultAction: function(menu) {
    var hasDefault = false;

    if (menu) dojo.forEach(menu.getChildren(), function(menuItem) {
      if (menuItem["default"]) {
        hasDefault = true;
        if (menuItem.declaredClass != "dijit.MenuSeparator") if (!menuItem.attr("disabled")) menuItem.onClick();
      }
    });

    return hasDefault;
  },

  checkPopupMenuItemAction: function(menu, grid) {
    if (menu) {
      var noRecords = grid.selection.getSelectedCount() == 0;
      dojo.forEach(menu.getChildren(), function(menuItem) {
        if (menuItem.itemAction) menuItem.attr("disabled", noRecords);
        else if (menuItem.detailAction) menuItem.attr("disabled", grid.store.parentRN == null);

        if (menuItem.toolBarButton) menuItem.toolBarButton.attr("disabled", menuItem.disabled);
      });
    }
  },

  autoSelectRow: function(grid) {
    grid.selection.select(0);
    grid.updateRow(0);
    grid.scrollToRow(0);
    grid.autoSelected = true;
  },

  deleteSelected: function(grid, onDeleteRow) {
    (new parus.confirmationDialog({
      message: "Удалить запись (записи)?",
      onApply: dojo.hitch(this, function() {
        var store = grid.store;
        var item = grid.selection.getFirstSelected();

        for (var i = 0; i < grid.selection.getSelectedCount(); i++) {
          if (onDeleteRow) onDeleteRow(store, item);
          item = grid.selection.getNextSelected(item);
        }

        grid.selection.deselectAll();
        parus.balance.refreshGrid(grid);
        return true;
      })
    })).show();
  },

  moveDocumentToCatalog: function(grid, catalogTree, unitCode, executeMoveQueryid) {
    var store = grid.store;
    var item = grid.selection.getFirstSelected();

    var path = [];
    dojo.forEach(catalogTree.path, function(item) {
      path.push(catalogTree.model.getIdentity(item));
    });

    (new parus.treeDialog({
      model: new(dojo.getObject(catalogTree.model.declaredClass, false))({
        queryid: catalogTree.model.queryid,
        onGetConditions: catalogTree.model.onGetConditions
      }),

      restoreSelected: false,
      value: path.join(";"),

      key: unitCode + "-moveDocument",

      onDefaultNeeded: function() {
        var response = parus.database.execute("findAvailableCatalog", {
          UNITCODE: unitCode
        });
        return response ? parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "PATH") : null;
      },

      onPerform: function(catalog) {
        for (var i = 0; i < grid.selection.getSelectedCount(); i++) {
          parus.database.execute(executeMoveQueryid, {
            RN: store.getValue(item, "RN"),
            CRN: catalog
          });
          item = grid.selection.getNextSelected(item);
        }

        grid.selection.deselectAll();
        parus.balance.refreshGrid(grid);
        return true;
      }

    })).show();
  }

});