//Programma per stampa report tabelle

#include <applicat.h>
#include <automask.h>
#include <relation.h>
#include <reprint.h>
#include <tabutil.h>
#include "ba3200.h"

///////////////////////////////////////////////////////////
// TMask_print_table
///////////////////////////////////////////////////////////

class TMask_print_table : public TAutomask
{
protected:
  bool on_field_event(TOperable_field& o, TField_event e, long jolly) {return true;}

public:
  TMask_print_table(const char * name);
  virtual ~TMask_print_table() {}
};

TMask_print_table::TMask_print_table(const char* name)
								  :TAutomask(name)
{
  const int reporty = toolbar() != NULL_WIN ? -2 : -3;
  TEdit_field & f = add_string(F_REPORT, 0, "Report    ", 2, reporty, 20, "B");
  f.set_query_button(new TReport_select(&f, name));
  f.enable_check();
}

///////////////////////////////////////////////////////////
// TTable_recordset
///////////////////////////////////////////////////////////

class TTable_recordset : public TISAM_recordset
{
	TMask* _mask;

protected:
	virtual void set_custom_filter(TCursor& cursor) const;

public:
  TTable_recordset(const char* use, TMask* mask) : TISAM_recordset(use), _mask(mask) { }
  virtual ~TTable_recordset() { }
};

void TTable_recordset::set_custom_filter(TCursor& cursor) const
{
	CHECK(_mask, "NULL selection mask");
  TRectype from(cursor.curr()); from.zero();
  TRectype to(from);
  bool key1_full = false;
  TString80 val;
  for (int i = _mask->fields() - 1; i >= 0; i--)
  {
    const TMask_field& f = _mask->fld(i);
  	const TFieldref* ref = f.field();
		if (ref != NULL && ref->name() == "CODTAB" && !f.empty())
		{
			const bool is_final = f.in_group(2);
      val = f.get();
      if (f.is_kind_of(CLASS_DATE_FIELD))
        val = TDate(val).string(full, '\0', full, full, amg_date);
			ref->write(val, is_final ? to : from);
      key1_full = true;
		}
  }
  if (key1_full) // Se e' stato impostato il filtro tramite CODTAB (non #FROM e #TO)
  	cursor.setregion(from, to);
}

///////////////////////////////////////////////////////////
// TTable_report
///////////////////////////////////////////////////////////

class TTable_report : public TReport
{
	TMask* _mask;

protected:
	virtual bool set_recordset(const TString& query);

public:
  virtual bool use_mask() { return false;}

  TTable_report(TMask * mask) : _mask(mask) {}
  virtual ~TTable_report() {}
};

bool TTable_report::set_recordset(const TString& query)
{
  return TReport::set_recordset(new TTable_recordset(query, _mask));
}

///////////////////////////////////////////////////////////
// TTable_reporter 
///////////////////////////////////////////////////////////

class TTable_reporter : public TSkeleton_application
{ 
protected:
  virtual void main_loop();

public:
  bool get_rpt_name(TFilename& rptname) const;
};

bool TTable_reporter::get_rpt_name(TFilename& rptname) const
{
  TTable tab(argv(2));  
  rptname = tab.module();
  rptname << "st" << tab.name();
  rptname.ext("rep");
  rptname.lower();
  return rptname.custom_path();
}

void TTable_reporter::main_loop()
{
  TFilename rptname;
  if (get_rpt_name(rptname))
  {
	  TFilename msk(rptname.name()); msk.ext(""); msk.lower();
	  TMask_print_table m(msk);
  
    while (m.run() == K_ENTER)
    {
	    TTable_report rep(&m);
			TString name(m.get(F_REPORT));

			if (name.blank())
				name = rptname;

      if (rep.load(name))
      {
    	  rep.mask2report(m);
        rep.print_or_preview();
      }
      else
        error_box(FR("Report %s mancante !"), name);
    }
  }
  else
    error_box(FR("Manca il file %s !"), (const char *)rptname);
}


int ba3200(int argc, char* argv[])
{     
  TTable_reporter app;
  app.run(argc, argv, TR("Stampa Tabelle"));
  return 0;
}