dojo.provide("parus.statusModelCoWorkingDialog");

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.declare("parus.statusModelCoWorkingDialog", [parus.Dialog], {

  documentRN: null,

  event: null,
  coWorkingDate: null,

  readOnly: false,

  _refreshDetailGridTimeoutID: null,

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

    if (this.readOnly) {
      dojo.forEach(this.popupMasterMenu.getChildren(), function(menuItem) {
        if (menuItem.itemAction) menuItem.destroyRecursive();
      });
    }

    this.masterGrid.attr("store", this.masterGridStore());
    this.detailGrid.attr("store", this.detailGridStore());

    parus.balance._createActionsButton(this.popupMasterMenu, this.headerPane);
    this.popupMasterMenu.bindDomNode(this.masterGrid.viewsNode);
    this.popupDetailMenu.bindDomNode(this.detailGrid.viewsNode);

    var settings = parus.userdata.get(this.declaredClass + "-masterGrid");
    if (settings) {
      parus.gridSettingsDialog.mergeGridSettings(this.masterGrid, settings.gridSettings);
      parus.gridSettingsDialog.mergeColorConditions(this.masterGrid, settings.colorConditions);
      parus.gridSettingsDialog.mergeFilterBar(this.masterGrid, settings.showFilterBar, settings.filterValues, settings.sortInfo);
      this.userSize = settings.userSize;
    }

    settings = parus.userdata.get(this.declaredClass + "-detailGrid");
    if (settings) {
      parus.gridSettingsDialog.mergeGridSettings(this.detailGrid, settings.gridSettings);
      parus.gridSettingsDialog.mergeColorConditions(this.detailGrid, settings.colorConditions);
      parus.gridSettingsDialog.mergeFilterBar(this.detailGrid, settings.showFilterBar, settings.filterValues, settings.sortInfo);
    }

    this.masterGrid.setQuery(this.masterGrid.query);
  },

  masterGridStore: function() {
    return parus.gridData({
      queryid: "BalanceStatusModelCoWorkingStatus",

      onGetConditions: dojo.hitch(this, function(store) {
        return {
          EVENT: this.event.event,
          EVENT_STAT: this.event.eventStatus,
          COWORKING_DATE: this.coWorkingDate
        };
      }),

      onComplete: dojo.hitch(this, function(store, items, keywordArgs) {
        var grid = this.masterGrid;

        if ((items.length > 0) && !grid.selection.getSelectedCount() && !keywordArgs.start) {
          grid.selection.select(0);
          grid.updateRow(0);
          grid.autoSelected = true;
        }
        else grid.onSelected();

        this.checkMasterGridMenu();
      })
    });
  },

  detailGridStore: function() {
    return parus.gridData({
      queryid: "BalanceStatusModelCoWorkingStatusHistory",

      onGetConditions: dojo.hitch(this, function(astore) {
        var store = this.masterGrid.store;
        var index = this.masterGrid.selection.getSelectedIndex();
        var item = (index >= 0) ? this.masterGrid.getItem(index) : null;

        astore.parentRN = store.getValue(item, "RN");
        return {
          EVENT: astore.parentRN || parus.parus8.CLASS_EmptyData,
          COWORKING_DATE: this.coWorkingDate
        };
      }),

      onComplete: dojo.hitch(this, function(store, items, keywordArgs) {
        var grid = this.detailGrid;

        if ((items.length > 0) && !grid.selection.getSelectedCount() && !keywordArgs.start) {
          grid.selection.select(0);
          grid.updateRow(0);
          grid.autoSelected = true;
        }
        else grid.onSelected();
      })
    });
  },

  checkMasterGridMenu: function() {
    if (this.readOnly) return;

    var noRecords = this.masterGrid.selection.getSelectedCount() == 0;

    var store = this.masterGrid.store;
    var index = this.masterGrid.selection.getSelectedIndex();
    var item = (index >= 0) ? this.masterGrid.getItem(index) : null;

    var allowSign = store.getValue(item, "PERF_MARK_SIGN") == "1";
    var signed = store.getValue(item, "PERF_MARK") != null;

    this.setMarkMenuItem.attr("disabled", noRecords || !allowSign || signed);
    this.removeMarkMenuItem.attr("disabled", noRecords || !allowSign || !signed);
  },

  masterGridChange: function() {
    if (this._refreshDetailGridTimeoutID) clearTimeout(this._refreshDetailGridTimeoutID);

    this._refreshDetailGridTimeoutID = setTimeout(dojo.hitch(this, function() {
      if (this._refreshDetailGridTimeoutID) clearTimeout(this._refreshDetailGridTimeoutID);
      this._refreshDetailGridTimeoutID = null;

      parus.balance.refreshGrid(this.detailGrid);
      this.checkMasterGridMenu();
    }), 300);
  },

  setMark: function() {
    parus.balance.statusModelSetMark(this.documentRN, this.event, dojo.hitch(this, function() {
      parus.balance.refreshGrid(this.masterGrid);
    }));
  },

  removeMark: function() {
    parus.balance.statusModelRemoveMark(this.documentRN, this.event, true, dojo.hitch(this, function() {
      parus.balance.refreshGrid(this.masterGrid);
    }));
  }

});