dojo.provide("parus.designerReportTablesDictionaryDialog");

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

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

dojo.require("parus.parus8");
dojo.require("parus.dialog");
dojo.require("parus.gridData");
dojo.require("parus.gridActions");
dojo.require("parus.catalogActions");
dojo.require("parus.dictionaryDialog");
dojo.require("parus.editDictionaryDialog");

dojo.require("parus.designerTableModelDialog");
dojo.require("parus.designerTableModelIndexDialog");
dojo.require("parus.designerTableIndexChangePriorityDialog");

dojo.require("parus.designerReportTableDialog");
dojo.require("parus.designerReportTableIndexDialog");

dojo.require("parus.designerReportTableNotIncludedIndexesDictionaryDialog");

dojo.require("parus.designerReportTableTotalDialog");

dojo.require("parus.designerReportTableConstraintDialog");
dojo.require("parus.designerReportTableConstraintAttributeDialog");

dojo.declare("parus.designerReportTablesDictionaryDialog", [parus.editableDictionaryDialog], {

  createMenu: function() {
    var menu = new dijit.Menu({
      "class": "balanceMenu"
    });

    menu.addChild(new dijit.MenuItem({
      label: "Итоги...",
      itemAction: true,
      onClick: dojo.hitch(this, this.showTotals)
    }));

    menu.addChild(new dijit.MenuItem({
      label: "Ограничения...",
      itemAction: true,
      onClick: dojo.hitch(this, this.showConstraints)
    }));

    menu.startup();

    this.menu = menu;
    this.inherited(arguments);

    return menu;
  },

  createDetailMenu: function() {
    this.detailMenu = this.createIndexPopupMenu(this, this.detailGrid, true, this.getTableRN);
    this.inherited(arguments);

    return this.detailMenu;
  },

  getTableRN: function() {
    var grid = this.grid;
    var store = grid.store;
    var item = grid.selection.getFirstSelected();

    return store.getValue(item, "RN");
  },

  //static
  createIndexPopupMenu: function(dictionaryDialog, grid, isDetail, onGetTableRN) {
    var menu = new dijit.Menu({
      "class": "balanceMenu"
    });

    menu.addChild(new dijit.MenuItem({
      label: "Добавить списком...",
      itemAction: false,
      detailAction: isDetail,
      onClick: dojo.hitch(dictionaryDialog, function() {
        var prn = onGetTableRN.call(dictionaryDialog);

        parus.designerReportTablesDictionaryDialog.prototype.showNonIncludedIndexes(
        dictionaryDialog, grid, prn);
      })
    }));

    menu.addChild(new dijit.MenuItem({
      label: "Изменить очередность расчета...",
      itemAction: false,
      detailAction: isDetail,
      onClick: dojo.hitch(dictionaryDialog, function() {
        var prn = onGetTableRN.call(dictionaryDialog);

        (new parus.designerTableIndexChangePriorityDialog({
          prn: prn,
          onApply: dojo.hitch(dictionaryDialog, function(dialog) {
            var result = parus.designerReportTableIndexDialog.prototype.executeChangePriority(
            prn, dialog.priorityEdit.attr("value"));
            if (result) parus.balance.refreshGrid(grid);
            return result;
          })
        })).show();
      })
    }));
    menu.addChild(new dijit.MenuSeparator());

    /*menu.addChild(new dijit.MenuItem({
     label: "Связанные словари...",
     itemAction: false,
     detailAction: isDetail,
     onClick: dojo.hitch(dictionaryDialog, function() {
     var prn = onGetTableRN.call(dictionaryDialog);
     //qqq
     })
     }));*/

    menu.startup();

    return menu;
  },

  showNonIncludedIndexes: function(dictionaryDialog, grid, prn) {
    var dictionaryInfo = parus.dictionaries["DesignerBalanceTablesNotIncludedIndexes"];
    dojo.mixin(dictionaryInfo, {
      multiselect: true,
      gridConditions: {
        PRN: prn
      },
      onApply: dojo.hitch(dictionaryDialog, function(dialog) {
        var checks = dialog.getValue();
        if (checks && checks.length > 0) {
          if (parus.designerReportTableIndexDialog.prototype.executeInsertList(prn, checks)) parus.balance.refreshGrid(grid);
        }
        return true;
      })
    });

    dictionaryDialog.showDictionaryAction(dictionaryInfo, parus.designerReportTableNotIncludedIndexesDictionaryDialog, parus.designerTableModelIndexDialog, null);
  },

  showTotals: function() {
    var grid = this.grid;
    var store = grid.store;
    var item = grid.selection.getFirstSelected();

    var dictionaryInfo = parus.dictionaries["DesignerBalanceTableTotals"];

    var dialog;

    var menu = new dijit.Menu({
      "class": "balanceMenu"
    });

    menu.addChild(new dijit.MenuItem({
      label: "Повысить уровень",
      itemAction: true,
      onClick: dojo.hitch(this, function() {
        this.totalsChangePriority(dialog, true);
      })
    }));

    menu.addChild(new dijit.MenuItem({
      label: "Понизить уровень",
      itemAction: true,
      onClick: dojo.hitch(this, function() {
        this.totalsChangePriority(dialog, false);
      })
    }));

    dojo.mixin(dictionaryInfo, {
      multiselect: true,
      hideCancel: true,
      editByDoubleClick: true,
      gridConditions: {
        PRN: store.getValue(item, "RN")
      },
      menu: menu
    });

    dialog = this.showDictionaryAction(dictionaryInfo, null, parus.designerReportTableTotalDialog, null);
  },

  totalsChangePriority: function(dialog, directUp) {
    var grid = dialog.grid;
    var store = grid.store;
    var item = grid.selection.getFirstSelected();

    if (store.getValue(item, "KIND") != "1") // != Общий
    if (parus.designerReportTableTotalDialog.prototype.executeChangePriority(store, item, directUp)) parus.balance.refreshGrid(grid, "RN", store.getValue(item, "RN"));
  },

  showConstraints: function() {
    var grid = this.grid;
    var store = grid.store;
    var item = grid.selection.getFirstSelected();

    var dictionaryInfo = parus.dictionaries["DesignerBalanceTableConstraintsWithDetails"];

    dojo.mixin(dictionaryInfo, {
      multiselect: true,
      hideCancel: true,
      editByDoubleClick: true,
      gridConditions: {
        PRN: store.getValue(item, "RN")
      },
      checkDetailGridPopupMenu: function(dialog, menu, grid) {
        var grid = dialog.grid;
        var store = grid.store;
        var item = grid.selection.getFirstSelected();
        if (!item) return;

        var isCheck = store.getValue(item, "CONSTR_TYPE") == "1";

        dojo.forEach(menu.getChildren(), function(menuItem) {
          if (!menuItem.disabled && (menuItem.itemAction || menuItem.detailAction)) {

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

    this.showDictionaryAction(dictionaryInfo, null, parus.designerReportTableConstraintDialog, parus.designerReportTableConstraintAttributeDialog);
  }

});