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

#include <vcl.h>
#pragma hdrstop

#include "Customer.h"
#include "sCustomer.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TCustomerForm *CustomerForm;
//---------------------------------------------------------------------------
__fastcall TCustomerForm::TCustomerForm(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TCustomerForm::ShBtClick(TObject *Sender)
{       String sh = ShEd->Text;
        Query1->Close();
        Query1->SQL->Clear();
        Query1->SQL->Add("select * from point where gbarcord like '%"+sh+"%' or name like '%"+sh+"%' or jumin like '%"+sh+"%'");
        Query1->Prepare();
        if(Query1->Prepared)
                Query1->Open();

        if(Query1->RecordCount > 0)
        {
                ReSG->RowCount = 2;
                ReSG->Rows[1]->Clear();

                
                for(int s=1;!Query1->Eof;s++)
                {
                        if(ReSG->Cells[0][ReSG->RowCount-1] != "")
                        {
                                ReSG->RowCount++;
                                ReSG->Row = ReSG->RowCount-1;
                        }
                        ReSG->Cells[0][s]=Query1->FieldByName("Gbarcord")->AsString;
                        ReSG->Cells[1][s]=Query1->FieldByName("name")->AsString;
                        ReSG->Cells[2][s]=Query1->FieldByName("jumin")->AsString;
                        ReSG->Cells[3][s]=Query1->FieldByName("point")->AsString;
                        ReSG->Cells[4][s]=Query1->FieldByName("phone")->AsString;
                        ReSG->Cells[5][s]=Query1->FieldByName("indate")->AsString;
                        ReSG->Cells[6][s]=Query1->FieldByName("useday")->AsString;
                        ReSG->Cells[7][s]=Query1->FieldByName("usepoint")->AsString;
                        Query1->Next();

                }

        }
        else
        {
                ShowMessage("등록되지 않았습니다.");
        }

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

void __fastcall TCustomerForm::FormActivate(TObject *Sender)
{
        ReSG->Cells[0][0]="바코드";
        ReSG->Cells[1][0]="이름";
        ReSG->Cells[2][0]="주민번호";
        ReSG->Cells[3][0]="포인트";
        ReSG->Cells[4][0]="전화번호";
        ReSG->Cells[5][0]="가입일";
        ReSG->Cells[6][0]="사용일";
        ReSG->Cells[7][0]="사용액";
}
//---------------------------------------------------------------------------


void __fastcall TCustomerForm::ReSGKeyPress(TObject *Sender, char &Key)
{
        if(Key==0x0D)
        {       
                sCustomForm = new TsCustomForm(this);

                sCustomForm->sBarEd->Text = ReSG->Cells[0][ReSG->Row];
                sCustomForm->sNameEd->Text = ReSG->Cells[1][ReSG->Row];
                sCustomForm->sJuminEd->Text = ReSG->Cells[2][ReSG->Row];
                sCustomForm->sPointEd->Text = ReSG->Cells[3][ReSG->Row];
                sCustomForm->sPhonEd->Text = ReSG->Cells[4][ReSG->Row];
                sCustomForm->sIndateEd->Text = ReSG->Cells[5][ReSG->Row];
                sCustomForm->ShowModal();
                delete sCustomForm;
        }
        
}
//---------------------------------------------------------------------------

void __fastcall TCustomerForm::ReSGKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
        if(Key==0x2e)
        {
                if(MessageDlg ("정말 삭제 할까요?", mtWarning, TMsgDlgButtons () << mbOK<<mbCancel, 0)==1)
                {
                        Query1->Close();
                        Query1->SQL->Clear();
                        Query1->SQL->Add("delete from point where gbarcord=:barcord");
                        Query1->ParamByName("barcord")->AsString = ReSG->Cells[0][ReSG->Row];
                        Query1->Prepare();
                        if(Query1->Prepared)
                                Query1->ExecSQL();

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


                }

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

void __fastcall TCustomerForm::ReSGDblClick(TObject *Sender)
{
        ReSG->OnKeyPress(this,0x0D);
}
//---------------------------------------------------------------------------

void __fastcall TCustomerForm::FormResize(TObject *Sender)
{
//        ReSG->Width = CustomerForm->Width - 40;
//        ReSG->Height = CustomerForm->Height - 90;
}
//---------------------------------------------------------------------------

void __fastcall TCustomerForm::GBtClick(TObject *Sender)
{
        if(GBarEd->Text==""||GNameEd->Text==""||GJuminEd->Text==""||GPhoneEd->Text=="")
        {
                ShowMessage("모두 입력하세요.");
        }
        else if(GBarEd->Text.Length() != 13|| GJuminEd->Text.Length() !=13)
        {
                ShowMessage("바코드 또는 주민번호를 확인해 주세요.");
        }
        else
        {
                Query1->Close();

                Query1->SQL->Clear();
                Query1->SQL->Add("select * from point");
                Query1->SQL->Add(" where gbarcord = :barcord or (name = :name and jumin=:jumin)");

                Query1->ParamByName("barcord")->AsString= GBarEd->Text;
                Query1->ParamByName("name")->AsString= GNameEd->Text;
                Query1->ParamByName("jumin")->AsString= GJuminEd->Text;
                

                Query1->Prepare();

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


                if(Query1->RecordCount < 1)
                {

                        Query1->Close();
                        Query1->SQL->Clear();
                        Query1->SQL->Add("insert into Point");
                        Query1->SQL->Add(" (jumin, GBarcord, name, indate, phone, point)");
                        Query1->SQL->Add(" values (:jumin, :GBarcord, :name, :indate, :phone, :point)");

                        Query1->ParamByName("jumin")->AsString= GJuminEd->Text;
                        Query1->ParamByName("GBarcord")->AsString = GBarEd->Text;
                        Query1->ParamByName("name")->AsString =  GNameEd->Text;
                        Query1->ParamByName("indate")->AsString = Date();
                        Query1->ParamByName("phone")->AsString = GPhoneEd->Text;
                        Query1->ParamByName("point")->AsInteger = 0;

                        Query1->Prepare();

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

                        if(ReSG->Cells[0][ReSG->RowCount-1]!="")
                                ReSG->RowCount++;

                        ReSG->Cells[0][ReSG->RowCount-1]=GBarEd->Text;
                        ReSG->Cells[1][ReSG->RowCount-1]=GNameEd->Text;
                        ReSG->Cells[2][ReSG->RowCount-1]=GJuminEd->Text;
                        ReSG->Cells[3][ReSG->RowCount-1]= 0;
                        ReSG->Cells[4][ReSG->RowCount-1]=GPhoneEd->Text;
                        ReSG->Cells[5][ReSG->RowCount-1]=Date();

                }
                else
                {
                        ShowMessage("이미 등록된 고객입니다.");
                }
        }        
}
//---------------------------------------------------------------------------

void __fastcall TCustomerForm::GBarEdChange(TObject *Sender)
{
        Label1->Caption =  IntToStr(GBarEd->Text.Length())+"자리";         
}
//---------------------------------------------------------------------------

void __fastcall TCustomerForm::GJuminEdChange(TObject *Sender)
{
        Label2->Caption =  IntToStr(GJuminEd->Text.Length())+"자리";
}
//---------------------------------------------------------------------------

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

