#include <relation.h>
#include <mask.h>
#include <sheet.h>
#include <progind.h>
#include <defmask.h>

#include "ba1100.h"


bool TEdit_file::browse(int logicnum, const char* name)
{
  TRelation relation(logicnum);
  TCursor cursor(&relation);
  TRectype& curr = cursor.curr();
  TRectype svrec(cursor.curr());
  
  TToken_string head(256);
  TToken_string trac(256);
  
  const char* n;
  trac.add("");
  head.add("@1");

  for(int i = 0; (n = curr.fieldname(i)) != NULL; i++)
  {
    trac.add(n);
    head.add(n);
    
    TFieldtypes t = curr.type(n);
    const int len = (t == _datefld) ? 10 : curr.length(n);
    head << '@' << max(len, (int)strlen(n));
    if (t == _realfld) head << 'R';
  }
  
  KEY ch;
  TCursor_sheet sheet(&cursor, trac, name, head, 6);

  while ((ch = sheet.run()) != K_ESC)
  {
    if (ch == K_DEL && sheet.one_checked())
    {
      TProgind* pind = NULL;
      cursor.freeze(TRUE);
      bool go = FALSE;
      bool first = FALSE;
      int j;
      for (j = 0, cursor = 0; j < sheet.items(); ++cursor, j++)
      {
        if (sheet.checked(j))
        {
          if (!go && !first) 
            go = yesno_box("Confermi la cancellazione dei %d "
                           "record indicati?", sheet.checked());
          first = TRUE; 

          if (!pind)
          {
            pind = new TProgind(sheet.checked(),"Attendere...",
                                TRUE, TRUE, 32);
            pind->addstatus(1);
          }
          else 
          {
            if (pind->iscancelled()) break;
            pind->addstatus(1);
          }  

          if (go) cursor.file().remove();
          sheet.uncheck(j);
        }
      }
      if (pind) delete pind;
      cursor.freeze(FALSE);
      
      if (first) {sheet.select(1); continue;}
    }
    if (ch == K_ENTER || ch == K_DEL) 
      cursor = sheet.selected();
    
    switch(ch)
    {
    case K_ENTER:  // edit fields
      svrec = curr;
      if (edit_record(curr, FALSE) == K_ENTER)
      {
        TString s(svrec.key(1));

        if (s != curr.key(1))
        {
          cursor.file().write();
          if (cursor.file().good())
          {
            curr = svrec;
            cursor.file().remove();
          }
        }
        else
          cursor.file().rewrite();
      }
      break;
    case K_DEL:    // delete record
      if (yesno_box("Confermare la cancellazione del record"))
        cursor.file().remove();
      break;
    case K_INS:    // insert new record
      curr.zero();
      if (edit_record(curr,FALSE) == K_ENTER)
        cursor.file().write();
      break;
    }
    sheet.force_update();
  }
  return K_ESC;
}

KEY  TEdit_file::edit_record(TRectype& rec, bool readonly)
{
  for(int recs = 0; rec.fieldname(recs) != NULL; recs++);
  
  int rows  = recs > 15 ? 20 : recs+4;
  int pages = recs/15 + (recs % 15 ? 1 : 0);
  TMask m("Modifica record", pages, 78, rows);
  
  int curpage = 0, currow = 1, nid = 100;
  const char* cp;
  
  for(int i = 0; (cp = rec.fieldname(i)) != NULL; i++)
  {
    // add fields ONE by ONE
    TString16 s(cp); s.left_just(16);
    int len = rec.length(cp);
    switch (rec.type(cp))
    {
    case _alfafld:
      m.add_string(nid++,curpage, s, 3, currow, len, "", 
                   len > 50 ? 50 : len);
      break;
    case _intfld:
    case _longfld:
    case _wordfld:
    case _realfld:
      m.add_number(nid++, curpage, s, 3, currow, len, "", 
                   rec.ndec(cp));
      break;
    case _datefld:
      m.add_date(nid++, curpage, s, 3, currow);
      break;
    case _charfld:
    case _boolfld:
      m.add_string(nid++,curpage, s, 3, currow, 1);
      break;
    case _intzerofld:
    case _longzerofld:
      m.add_number(nid++, curpage, s, 3, currow, len, "Z", 
                   rec.ndec(cp));
      break;
    default:
      break;
    }
    
    m.field(nid-1).set(rec.get(cp));
    
    if (((i+1) % 15) == 0 || i == recs-1) 
    { 
      m.add_button(DLG_OK, curpage, "", -12, -1, 9, 2);
      m.add_button(DLG_CANCEL, curpage, "", -22, -1, 9, 2);
      curpage++; currow = 1;
    }
    else currow++;
  }

  if (readonly)
  {
    const int max = m.fields();
    for (int i = 0; i < max; i++)
    {
      TMask_field& f = m.fld(i);
      if (f.class_id() != CLASS_BUTTON_FIELD)
        f.disable();
    }
  }

  const KEY k = m.run();

  if (k == K_ENTER && !readonly)
  {
    nid = 100;
    for(i = 0; (cp = rec.fieldname(i)) != NULL; i++)
      rec.put(cp, m.get(nid++));
  }

  return k;
}



