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

#include <vcl.h>
#pragma hdrstop

#include "Seller.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TSellerForm *SellerForm;

//String *list;
int stay = 0;//buffsize=0;
//---------------------------------------------------------------------------
__fastcall TSellerForm::TSellerForm(TComponent* Owner)
        : TForm(Owner)
{
        stay=0;
}
//---------------------------------------------------------------------------

void __fastcall TSellerForm::FormActivate(TObject *Sender)
{
        SellerList->Cells[0][0] =  "거래처명";
        SellerList->Cells[1][0] =  "전화번호";
        SellerList->Cells[2][0] =  "팩스";
        SellerList->Cells[3][0] =  "메모";

        DBRead("");
}
//---------------------------------------------------------------------------



void __fastcall TSellerForm::SellerListKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{


        if(Key== 0x2e && stay==0)//delete key
        {
                if(MessageDlg ("정말 삭제 하시겠습니까?", mtWarning, TMsgDlgButtons () << mbOK<<mbCancel, 0)==1)
                {
                        if(stay > 0 && stay < SellerList->Row)
                                stay--;
                        else if(stay > 0 && stay == SellerList->Row)
                                stay=0;

                        SellerList->EditorMode=false;

                        Query1->Close();
                        Query1->SQL->Clear();
                        Query1->SQL->Add("delete from seller where seller=:seller");
                        Query1->ParamByName("seller")->AsString = SellerList->Cells[0][SellerList->Row];

                        Query1->Prepare();
                        if(Query1->Prepared)
                                Query1->ExecSQL();


                        if(SellerList->RowCount > 2)
                        {
                                for(int i = SellerList->Row;i < SellerList->RowCount-1;i++)
                                        SellerList->Rows[i]->Assign(SellerList->Rows[i+1]);

                                SellerList->RowCount--;
                        }
                        else if(SellerList->RowCount == 2)
                        {
                                SellerList->Rows[SellerList->Row]->Clear();
                        }

                }
        }

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

void __fastcall TSellerForm::SaveBtClick(TObject *Sender)
{




        if(SanghoLE->Text == "")
        {
                ShowMessage("거래처명을 입력하세요.");


        }
        else
        {
                Query1->Close();
                Query1->SQL->Clear();
                Query1->SQL->Add("select seller from seller where seller=:seller") ;
                Query1->ParamByName("seller")->AsString = SanghoLE->Text;
                Query1->Prepare();

                if(Query1->Prepared)
                        Query1->Open();

                if(Query1->RecordCount > 0)
                {
                        ShowMessage("이미 등록된 거래처 입니다.");
                }
                else
                {
                        Query1->Close();
                                Query1->SQL->Clear();
                        Query1->SQL->Add("insert  into seller (seller,phon,fax,ref) values (:seller,:phon,:fax,:ref)");
                        Query1->ParamByName("seller")->AsString = SanghoLE->Text;
                        Query1->ParamByName("phon")->AsString = PhoneLE->Text;
                        Query1->ParamByName("fax")->AsString = FaxLE->Text;
                        Query1->ParamByName("ref")->AsString = Memo1->Text;
                        Query1->Prepare();
                        if(Query1->Prepared)
                                Query1->ExecSQL();

                        if( SellerList->Cells[0][SellerList->RowCount-1]!="")
                        {

                                SellerList->RowCount++;
                                SellerList->Cells[0][SellerList->RowCount-1]= SanghoLE->Text;
                                SellerList->Cells[1][SellerList->RowCount-1]= PhoneLE->Text;
                                SellerList->Cells[2][SellerList->RowCount-1]= FaxLE->Text;
                                SellerList->Cells[3][SellerList->RowCount-1]= Memo1->Text;
                                SellerForm->ReReadBt->Click();
                        }
                        else
                        {
                                SellerList->Cells[0][SellerList->RowCount-1]= SanghoLE->Text;
                                SellerList->Cells[1][SellerList->RowCount-1]= PhoneLE->Text;
                                SellerList->Cells[2][SellerList->RowCount-1]= FaxLE->Text;
                                SellerList->Cells[3][SellerList->RowCount-1]= Memo1->Text;
                                SellerForm->ReReadBt->Click();

                        }
                }
        }

              

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


void __fastcall TSellerForm::FormDestroy(TObject *Sender)
{
     //   delete list;

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

void __fastcall TSellerForm::ReReadBtClick(TObject *Sender)
{

        ReBt->Visible=false;
        SaveBt->Visible=true;
        SanghoLE->Text="";
        PhoneLE->Text="";
        FaxLE->Text="";
        Memo1->Text="";
        SanghoLE->ReadOnly=false;
}
//---------------------------------------------------------------------------



TSellerForm::DBRead(String name)
{
        //TODO: Add your source code here
        
    //    delete list;

        Query1->Close();
        Query1->SQL->Clear();
        Query1->SQL->Add("select * from seller ");
        if(name != "")
                Query1->SQL->Add("where seller like '%"+ name +"%' ");
        Query1->SQL->Add("order by seller asc");
        Query1->Prepare();
        if(Query1->Prepared)
                Query1->Open();

    //    buffsize = Query1->RecordCount;
    //    list = new String[buffsize];

        SellerList->RowCount = 2;
        SellerList->Rows[1]->Clear();

        for(int i=1;!Query1->Eof;i++)
        {

                if(SellerList->Cells[0][SellerList->RowCount-1] != "")
                {
                        SellerList->RowCount++;
                        SellerList->Row = SellerList->RowCount-1;
                }
                SellerList->Cells[0][i] = Query1->FieldByName("seller")->AsString;
                SellerList->Cells[1][i] = Query1->FieldByName("phon")->AsString;
                SellerList->Cells[2][i] = Query1->FieldByName("fax")->AsString;
                SellerList->Cells[3][i] = Query1->FieldByName("ref")->AsString;

              //  list[i-1] = SellerList->Cells[0][i];

                Query1->Next();
        }
        stay=0;
        SellerForm->FocusControl(SellerList);
}
void __fastcall TSellerForm::SearchBtClick(TObject *Sender)
{
        DBRead(SearchLE->Text);
}
//---------------------------------------------------------------------------

void __fastcall TSellerForm::SearchLEKeyPress(TObject *Sender, char &Key)
{
        if(Key==0x0D)
        {
                SearchBt->OnClick(this);
        }
}
//---------------------------------------------------------------------------

void __fastcall TSellerForm::SellerListDblClick(TObject *Sender)
{
        ReBt->Top= SaveBt->Top;
        ReBt->Left= SaveBt->Left;
        ReBt->Visible=true;
        SaveBt->Visible=false;
        SanghoLE->Text=SellerList->Cells[0][SellerList->Row];
        PhoneLE->Text=SellerList->Cells[1][SellerList->Row];
        FaxLE->Text=SellerList->Cells[2][SellerList->Row];
        Memo1->Text=SellerList->Cells[3][SellerList->Row];
        SanghoLE->ReadOnly=true;
        stay=SellerList->Row;
}
//---------------------------------------------------------------------------


void __fastcall TSellerForm::ReBtClick(TObject *Sender)
{
        Query1->Close();
        Query1->SQL->Clear();
        Query1->SQL->Add("update seller set phon=:phon, fax=:fax, ref=:ref where seller=:seller");
        Query1->ParamByName("seller")->AsString =SanghoLE->Text;
        Query1->ParamByName("phon")->AsString = PhoneLE->Text;
        Query1->ParamByName("fax")->AsString = FaxLE->Text;
        Query1->ParamByName("ref")->AsString = Memo1->Text;
        Query1->Prepare();

        if(Query1->Prepared)
                Query1->ExecSQL();

        SellerList->Cells[1][stay]= PhoneLE->Text;
        SellerList->Cells[2][stay]= FaxLE->Text;
        SellerList->Cells[3][stay]= Memo1->Text;
        SellerForm->ReReadBt->Click();
}
//---------------------------------------------------------------------------


