#include <relapp.h>
#include <stdtypes.h>
#include <lffiles.h>
#include "ba4.h"
#include "ba4500.h"

class BA4500_application : public TRelation_application
{
  TMask* _msk;
	TRelation* _rel;
  TLocalisamfile* _anag;
  TLocalisamfile* _ditte;

  bool user_create() ;                         
  bool user_destroy() ;                        
	virtual TMask* get_mask(int mode) { return _msk;}
	virtual bool changing_mask(int mode) { return FALSE;}
	virtual TRelation* get_relation() const { return _rel;}
	virtual int read(TMask& m);
  virtual void init_query_mode(TMask& m) ;
  virtual void init_query_insert_mode(TMask& m) ;

public:
  BA4500_application() {}
  virtual ~BA4500_application() {}
};

bool gest_res(TMask_field& f, KEY key)
{

	if (!f.to_check(key)) return TRUE;
	
 	TLocalisamfile	anagr(LF_ANAG) ;
	TMask&					msk = f.mask() ;

	anagr.curr().zero();
	anagr.curr().put(N_TIPOA, msk.get(LST_AT2_TIPOADS));
	anagr.curr().put(N_CODANAGR, msk.get(FLD_AT2_CODANGRDS));
	anagr.read();
	if (anagr.bad())
		return f.error_box("Anagrafica assente");

	TArray comarr(2), f1(2), f2(2), f3(2);
	TString					cod_com = anagr.curr().get(N_CODCOMRF);
	
	comarr.add(cod_com);
	cod_com = anagr.curr().get(N_CODCOMRES);
	comarr.add(cod_com);
	cod_com = N_INDRF;
	f1.add(cod_com);
	cod_com = N_CIVRF;
	f2.add(cod_com);
	cod_com = N_CAPRF;
	f3.add(cod_com);
	cod_com = N_INDRES;
	f1.add(cod_com);
	cod_com = N_CIVRES;
	f2.add(cod_com);
	cod_com = N_CAPRES;
	f3.add(cod_com);

	TLocalisamfile	comuni(LF_COMUNI) ;
	TRectype&				rec = comuni.curr() ;

	for (int i = 0; i < comarr.items(); i++)
	{
		cod_com = (TString&) comarr[i];
    if ( cod_com.not_empty() )
		{
			rec.zero() ;
			rec.put(N_STATO, anagr.curr().get(N_STATORF));
			rec.put(N_COM, cod_com );
			comuni.setkey(1) ;
			comuni.read() ;
			if ( comuni.good() )
			{
				msk.set( FLD_AT2_ANAGR_DEP_IND ,  (const char*) anagr.curr().get((TString&) f1[i]) ) ; 
				msk.set( FLD_AT2_ANAGR_DEP_CIV ,  (const char*) anagr.curr().get((TString&) f2[i]) ) ; 
				msk.set( FLD_AT2_ANAGR_DEP_CAP ,  (const char*) anagr.curr().get((TString&) f3[i]) ) ; 
				msk.set( FLD_AT2_DEP_ANAGR_COMUNI_DESCR ,  (const char*) rec.get(N_DENCOM) ) ; 
				msk.set( FLD_AT2_DEP_ANAGR_COMUNI_PROV ,  (const char*) rec.get(N_PROV) ) ; 
				break;
			}
		}
	}	
	return TRUE;
}

bool BA4500_application::user_create()
{
  _msk = new TMask("ba4500a") ;
  // _msk->set_handler(LST_AT2_TIPOADS, gest_res);
  _msk->set_handler(FLD_AT2_CODANGRDS, gest_res);
  _rel = new TRelation(LF_ATTIV) ;
  _anag = new TLocalisamfile(LF_ANAG) ;
  _ditte = new TLocalisamfile(LF_NDITTE) ;
	set_search_field(FLD_AT1_CODDITTAH);
	return TRUE;
}

void BA4500_application::init_query_mode(TMask& m)

{
	m.show(FLD_AT1_CODATTH);
	m.hide(FLD_AT1_CODATT);
}


void BA4500_application::init_query_insert_mode(TMask& m)

{
	m.show(FLD_AT1_CODATT);
	m.hide(FLD_AT1_CODATTH);
}


bool BA4500_application::user_destroy()
{
  delete  _msk;
  delete _rel;
  delete _anag;
  delete _ditte;
	return TRUE;
}

int BA4500_application::read(TMask& m) 

{
	m.autoload();
	m.field(FLD_AT2_CODANGRDS).set_focusdirty();
	gest_res(m.field(FLD_AT2_CODANGRDS), K_TAB);
	return NOERR;
}

int ba4500(int argc, char* argv[])
{
  
  BA4500_application a;
  a.run(argc, argv, "Anagrafica Attivita'");
  return 0;
}


