//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "mainFrm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "LP1C_OCX"
#pragma link "LP1C_OCX"
#pragma resource "*.dfm"

TMainForm *MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ExitClick(TObject *Sender)
{
   //  
   Application->Terminate();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::PagePropertiesClick(TObject *Sender)
{
   //   
   LP451->ShowProperties();
   UpdateForm();
}
//---------------------------------------------------------------------------

void TMainForm::UpdateForm ()
{
   //  
   Enable->Checked = LP451->DeviceEnabled;
   Cache->Checked = LP451->CacheReport;
   FirstRecord->ItemIndex = LP451->FirstRecord - 1;
   LastRecord->ItemIndex = LP451->LastRecord - 1;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::EnableClick(TObject *Sender)
{
   try
   {
      // / 
      LP451->DeviceEnabled = Enable->Checked;
   }
   catch (...)
   {
      //  
      DisplayError();
   }
   UpdateForm();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::CacheClick(TObject *Sender)
{
   try
   {
      // /  
      LP451->CacheReport = Cache->Checked;
   }
   catch (...)
   {
      DisplayError();
   }
   UpdateForm();
}
//---------------------------------------------------------------------------
void TMainForm::DisplayError()
{
   AnsiString stData;

   stData = LP451->ResultDescription;
   Application->MessageBoxA(stData.c_str(), "", MB_OK);
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FormCreate(TObject *Sender)
{
   //   
   List->ColCount = 10;
   List->Cells[0][0] = "PLU";
   List->Cells[1][0] = "";
   List->Cells[2][0] = " ";
   List->Cells[3][0] = "";
   List->Cells[4][0] = " 2";
   List->Cells[5][0] = " ";
   List->Cells[6][0] = " ";
   List->Cells[7][0] = " ";
   List->Cells[8][0] = " ";
   List->Cells[9][0] = " ";

   //     
   for (int i=1; i<=1000; i++)
   {
        FirstRecord->Items->Add(IntToStr(i));
        LastRecord->Items->Add(IntToStr(i));
   }

   UpdateForm();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::AddRowClick(TObject *Sender)
{
   int i, rows;
   //  
   rows = List->RowCount;
   List->RowCount = List->RowCount + 1;

   //    
   for (int i=0; i<10 ;i++)
      List->Cells[i][rows] = "1";

   //   
   List->FixedRows = 1;
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::DeleteRowClick(TObject *Sender)
{
   //   
   if (List->RowCount > 1)
      List->RowCount = List->RowCount - 1;
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::ReadDataClick(TObject *Sender)
{
   int RowIndex = 1;

   LP451->RaiseException = False;

   //  
   LP451->BeginReport();
   if (LP451->ResultCode)
   {
      DisplayError();
      return;
   }

   for (int i=LP451->FirstRecord; i<=LP451->LastRecord; i++)
   {
      LP451->GetRecord();
      if (LP451->ResultCode == -2001)
         break;
      else if (LP451->ResultCode)
      {
         DisplayError();
         return;
      }

      List->RowCount = RowIndex + 1;
      List->Cells[0][RowIndex] = IntToStr(LP451->PLU);
      List->Cells[1][RowIndex] = CurrToStr(LP451->Price);
      List->Cells[2][RowIndex] = IntToStr(LP451->ICode);
      List->Cells[3][RowIndex] = LP451->Name;
      List->Cells[4][RowIndex] = LP451->Name2;
      List->Cells[5][RowIndex] = IntToStr(LP451->MsgNo);
      List->Cells[6][RowIndex] = IntToStr(LP451->Life);
      List->Cells[7][RowIndex] = FloatToStr(LP451->Tare);
      List->Cells[8][RowIndex] = IntToStr(LP451->GCode);
      List->Cells[9][RowIndex] = IntToStr(LP451->WareType);

      RowIndex ++;
   }
   //   
   LP451->EndReport();
   if (LP451->ResultCode)
      DisplayError();

   LP451->RaiseException = True;

   if (List->RowCount > 1)
      List->FixedRows = 1;

   UpdateForm();
}

//---------------------------------------------------------------------------

void __fastcall TMainForm::WriteDataClick(TObject *Sender)
{
   // ,      ?
   if (List->RowCount <= 1)
   {
      Application->MessageBoxA ("   ", "", MB_OK);
      return;
   }

   LP451->RaiseException = False;
   //   
   LP451->BeginAdd();
   if (LP451->ResultCode)
   {
      DisplayError();
      return;
   }

   for (int i=1; i < List->RowCount; i++)
   {
      //   
      LP451->PLU = StrToInt (List->Cells[0][i]);
      LP451->Price = StrToCurr (List->Cells[1][i]);
      LP451->ICode = StrToInt (List->Cells[2][i]);
      LP451->Name = List->Cells[3][i];
      LP451->Name2 = List->Cells[4][i];
      LP451->MsgNo = StrToInt (List->Cells[5][i]);
      LP451->Life = StrToInt (List->Cells[6][i]);
      LP451->Tare = StrToFloat (List->Cells[7][i]);
      LP451->GCode = StrToInt (List->Cells[8][i]);
      LP451->WareType = StrToInt (List->Cells[8][i]);

      //   
      LP451->SetRecord();
      if (LP451->ResultCode)
      {
         DisplayError();
         return;
      }
   }

   //   
   LP451->EndAdd();
   if (LP451->ResultCode)
      DisplayError();

   LP451->RaiseException = True;
   UpdateForm ();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FirstRecordChange(TObject *Sender)
{
   //     
   try
   {
        LP451->FirstRecord = FirstRecord->ItemIndex + 1;
   }
   catch (...)
   {
      //  
      DisplayError();
   }

   UpdateForm();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::LastRecordChange(TObject *Sender)
{
   //     
   try
   {
        LP451->LastRecord = LastRecord->ItemIndex + 1;
   }
   catch (...)
   {
      //  
      DisplayError();
   }

   UpdateForm();

}
//---------------------------------------------------------------------------

