dojo.provide("parus.designerReportEditionDialog");

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

dojo.require("parus.parus8");
dojo.require("parus.dialog");
dojo.require("parus.dictEdit");
dojo.require("parus.formats");

dojo.require("parus.editDictionaryDialog");

dojo.require("parus.designerReportPeriodDialog");
dojo.require("parus.designerReportPeriodDateDialog");

dojo.require("parus.designerRecalculateTypeDialog");

dojo.declare("parus.designerReportEditionDialog", [parus.editDictionaryDialog], {

  onLoadParams: function(store, item, editMode) {
    this.inherited(arguments);

    if (store && item) {
      this.dateFromEdit.attr("value", parus.formats.stringToDate(store.getValue(item, "BEGDATE")));
      this.dateToEdit.attr("value", parus.formats.stringToDate(store.getValue(item, "ENDDATE")));
      this.recalculateTypeEdit.attr("value", store.getValue(item, "CALC_CODE"));
      this.calculateResultSubReportsCheckBox.attr("checked", store.getValue(item, "RESULTS") == "1");
      this.periodEdit.attr("value", store.getValue(item, "PERIOD_CODE"));
      this.autoRecalculateCheckBox.attr("checked", store.getValue(item, "AUTOCALC") == "1");
      this.noteEdit.attr("value", store.getValue(item, "NOTES"));
      this.subscriptToTextEdit.attr("value", store.getValue(item, "EXPORTFOOTER"));
      this.uploadWithSubReportsCheckBox.attr("checked", store.getValue(item, "EXPORTSUBREPORTS") == "1");
      this.ignoreWarningsCheckBox.attr("checked", store.getValue(item, "IGNORE_WARNINGS") == "1");
    }
  },

  executeInsert: function(duplicate) {
    var response = parus.database.execute(duplicate ? "duplicateDesignerReportEdition" : "insertDesignerReportEdition", {
      RN: duplicate ? this.rn : null,
      PRN: this.prn,
      BEGDATE: parus.formats.dateToString(this.dateFromEdit.attr("value")),
      ENDDATE: parus.formats.dateToString(this.dateToEdit.attr("value")),
      CALCTYPE: this.recalculateTypeEdit.attr("value"),
      RESULTS: (this.calculateResultSubReportsCheckBox.attr("checked") ? "1" : "0"),
      PERIOD: this.periodEdit.attr("value"),
      AUTOCALC: (this.autoRecalculateCheckBox.attr("checked") ? "1" : "0"),
      NOTES: this.noteEdit.attr("value"),
      EXPORTFOOTER: this.subscriptToTextEdit.attr("value"),
      EXPORTSUBREPORTS: (this.uploadWithSubReportsCheckBox.attr("checked") ? "1" : "0"),
      IGNORE_WARNINGS: (this.ignoreWarningsCheckBox.attr("checked") ? "1" : "0")
    });

    if (response) return parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "NRN");
  },

  executeUpdate: function() {
    var response = parus.database.execute("updateDesignerReportEdition", {
      RN: this.rn,
      BEGDATE: parus.formats.dateToString(this.dateFromEdit.attr("value")),
      ENDDATE: parus.formats.dateToString(this.dateToEdit.attr("value")),
      CALCTYPE: this.recalculateTypeEdit.attr("value"),
      RESULTS: (this.calculateResultSubReportsCheckBox.attr("checked") ? "1" : "0"),
      PERIOD: this.periodEdit.attr("value"),
      AUTOCALC: (this.autoRecalculateCheckBox.attr("checked") ? "1" : "0"),
      NOTES: this.noteEdit.attr("value"),
      EXPORTFOOTER: this.subscriptToTextEdit.attr("value"),
      EXPORTSUBREPORTS: (this.uploadWithSubReportsCheckBox.attr("checked") ? "1" : "0"),
      IGNORE_WARNINGS: (this.ignoreWarningsCheckBox.attr("checked") ? "1" : "0")
    });

    return !!response;
  },

  executeDelete: function(store, item) {
    var response = parus.database.execute("deleteDesignerReportEdition", {
      RN: store.getValue(item, "RN")
    });

    return !!response;
  }

});