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

#include <vcl.h>
#pragma hdrstop

#include "PridejUT.h"
#include "Katalog.h"
#include "string.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmPridejUT *frmPridejUT;
//---------------------------------------------------------------------------
__fastcall TfrmPridejUT::TfrmPridejUT(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TfrmPridejUT::btnKonecUTClick(TObject *Sender)
{


Edit1UT -> Clear();
Edit2UT -> Clear();
Edit3UT -> Clear();
Edit4UT -> Clear();
Memo1UT -> Clear();

frmPridejUT -> Close();
}
//---------------------------------------------------------------------------

void __fastcall TfrmPridejUT::btnPridejUTClick(TObject *Sender)
{
AnsiString a,b,c,d,e,Nazev;
AnsiString TypUT = ComboBox1PridejUT -> Items -> Strings[ComboBox1PridejUT -> ItemIndex];
AnsiString Typ = Form1 -> ComboBox1UT -> Items -> Strings[Form1 -> ComboBox1UT -> ItemIndex];
AnsiString edity[] = {Edit2UT -> Text, Edit3UT -> Text, Edit4UT -> Text};
AnsiString promenne[] = {b,c,d};
AnsiString i;

a = Edit1UT -> Text;
b = Edit2UT -> Text;
c = Edit3UT -> Text;
d = Edit4UT -> Text;
e = Memo1UT -> Text;


for (int i=0; i<3; i++)
 {
 if(edity[i] == "")
   {
   promenne[i] = -10;
   }
 else
   promenne[i] = edity[i];
 }


if(Edit1UT -> Text == "")
 ShowMessage("Nezadali jste název součástky.");
else
{
Query1 -> SQL -> Clear();
Query1 -> SQL -> Add("SELECT Nazev_soucastky FROM Unipolarni_tranzistory WHERE Nazev_soucastky = '"+a+"' ");
Query1 -> Open();

Nazev = DataSource1->DataSet->FieldByName("Nazev_soucastky")->AsString;

if(a == Nazev)
 {
ShowMessage("Součástka "+Nazev+" je již v databázi obsažena");
 }
else
 {
Query1 -> SQL -> Clear();
Query1 -> SQL -> Add("INSERT INTO Unipolarni_tranzistory (Nazev_soucastky, Napeti_drain_source, Proud_Id, Max_vykonova_ztrata, Popis, Typ) VALUES ('"+a+"', "+promenne[0]+", "+promenne[1]+", "+promenne[2]+", '"+e+"', '"+Typ+"')");
Query1 -> ExecSQL();


Query1 -> SQL -> Clear();
Query1 -> SQL -> Add("SELECT Nazev_soucastky FROM Unipolarni_tranzistory WHERE Typ = '"+TypUT+"'");
Query1 -> Open();

Form1->DBListBox2->Items->Clear();
Form1->DataSource2->DataSet->FindFirst();
  do
      {
Form1->DBListBox2->Items->Add(Form1->DataSource2->DataSet->FieldByName("Nazev_soucastky")->AsString);
Form1->DataSource2->DataSet->Next();
      }
  while (!Form1->DataSource2->DataSet->Eof);
  }
 }
Form1 -> Memo2 -> Clear(); 
}
//---------------------------------------------------------------------------
void __fastcall TfrmPridejUT::FormShow(TObject *Sender)
{
Memo1UT -> Clear();
}
//---------------------------------------------------------------------------

