dojo.provide("parus.designerTableModelsDictionaryDialog");

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.designerTableModelTotalDialog");
dojo.require("parus.designerTableModelFormulaDialog");

dojo.declare("parus.designerTableModelsDictionaryDialog", [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, function() {
        this.showFormulas(false);
      })
    }));

    menu.addChild(new dijit.MenuItem({
      label: "Формулы коррекции итогов...",
      itemAction: true,
      onClick: dojo.hitch(this, function() {
        this.showFormulas(true);
      })
    }));

    menu.startup();

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

    return menu;
  },

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

    return this.detailMenu;
  },

  getTableModelRN: function() {
    var grid = this.detailGrid;
    var store = grid.store;
    return store.parentRN;
  },

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

    menu.addChild(new dijit.MenuItem({
      label: "Добавить списком...",
      itemAction: false,
      detailAction: isDetail,
      onClick: dojo.hitch(dictionaryDialog, function() {
        parus.balance.showIndexes(true, dojo.hitch(dictionaryDialog, function(dialog) {
          var checks = dialog.getValue();
          if (checks && checks.length > 0) {
            var prn = onGetTableModelRN.call(dictionaryDialog);

            if (parus.designerTableModelIndexDialog.prototype.executeInsertList(prn, checks)) parus.balance.refreshGrid(grid);
          }
        }));
      })
    }));

    menu.addChild(new dijit.MenuItem({
      label: "Добавить группу...",
      itemAction: false,
      detailAction: isDetail,
      onClick: dojo.hitch(dictionaryDialog, function() {
        parus.balance.showIndexesGroups(false, dojo.hitch(dictionaryDialog, function(dialog) {
          var group = dialog.getLabel();
          var prn = onGetTableModelRN.call(dictionaryDialog);

          if (parus.designerTableModelIndexDialog.prototype.executeInsertGroup(prn, group)) parus.balance.refreshGrid(grid);
        }));
      })
    }));

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

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

    menu.startup();

    return menu;
  },

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

    var prn = store.getValue(item, "RN");

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

    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);
      })
    }));

    menu.startup();

    dojo.mixin(dictionaryInfo, {
      dictionary: dictionaryInfo,
      unitCode: dictionaryInfo.unitCode,
      multiselect: true,
      hideCancel: true,
      editByDoubleClick: true,
      editDialogClass: parus.designerTableModelTotalDialog,
      gridConditions: {
        PRN: prn
      },
      menu: menu
    });

    dialog = new parus.editableDictionaryDialog(dictionaryInfo);
    dialog.show();
  },

  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.designerTableModelTotalDialog.prototype.executeChangePriority(store, item, directUp)) parus.balance.refreshGrid(grid, "RN", store.getValue(item, "RN"));
  },

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

    var prn = store.getValue(item, "RN");

    var dictionaryInfo = parus.dictionaries[isTotal ? "DesignerBalanceTableModelsTotalFormulas" : "DesignerBalanceTableModelsFormulas"];

    var dialog;

    dojo.mixin(dictionaryInfo, {
      dictionary: dictionaryInfo,
      unitCode: dictionaryInfo.unitCode,
      multiselect: true,
      hideCancel: true,
      editByDoubleClick: true,
      editDialogClass: parus.designerTableModelFormulaDialog,
      gridConditions: {
        PRN: prn,
        KIND: isTotal ? "1" : "0"
      }
    });

    dialog = new parus.editableDictionaryDialog(dictionaryInfo);
    dialog.show();
  }

});