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

#include <vcl.h>
#pragma hdrstop

#include "SavePoint.h"
#include "Calc.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TSavePointForm *SavePointForm;
int jukrip=0,chk=0 ;
double p,card,cash;
String st="";
//---------------------------------------------------------------------------
__fastcall TSavePointForm::TSavePointForm(TComponent* Owner)
        : TForm(Owner)
{
        ComboBox1->Left = 32;
        ComboBox1->Top = 24;

  TStringList *pBuffer = new TStringList() ;
  pBuffer->LoadFromFile(ExtractFilePath(ParamStr(0))+"\data\\config.bin") ;

  cash = StrToFloat(pBuffer->Strings[1]);
  card = StrToFloat(pBuffer->Strings[2]);
    delete pBuffer ;
    
}
//---------------------------------------------------------------------------
void __fastcall TSavePointForm::SBarEdKeyPress(TObject *Sender, char &Key)
{
        if(Key == 0x1b)//esc키
        {
                SavePointForm->Close();

        }
        if(Key==0x0D)
        {
                st  = SBarEd->Text.Trim();
                Query1->Close();

                Query1->SQL->Clear();
                Query1->SQL->Add("select * from point");
                Query1->SQL->Add(" where jumin like '%"+ st +"%' or gbarcord like '%"+ st +"%' or name like '%"+ st +"%'");



                Query1->Prepare();

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

                if(Query1->RecordCount == 1)
                {
                        SBarEd->Text  = Query1->FieldByName("gbarcord")->AsString;
                        STotalEd->Text = Query1->FieldByName("point")->AsInteger;

                        Label1->Caption =  Query1->FieldByName("name")->AsString + " 고객님";

                        chk=1;
                }
                else if(Query1->RecordCount > 1)
                {
                        ComboBox1->Visible=true;
                        ComboBox1->Items->Clear();
                        while(!Query1->Eof)
                        {
                                ComboBox1->Items->Add(Query1->FieldByName("name")->AsString+"/"+Query1->FieldByName("Gbarcord")->AsString);
                                Query1->Next();
                        }
                        
                }
                else
                {
                        chk=0;
                        ShowMessage("등록되지 않은 고객입니다.");
                }
        }
}
//---------------------------------------------------------------------------
void __fastcall TSavePointForm::SOkBtClick(TObject *Sender)
{
        if(SBarEd->Text !="" && chk==1)
        {
                Query1->Close();
                Query1->SQL->Clear();

                if(STotalEd->Text == "" ||STotalEd->Text == 0)
                Query1->SQL->Add("update point set point=:point ");
                else
                Query1->SQL->Add("update point set point=point+:point ");

                Query1->SQL->Add("where gbarcord = :gbarcord or jumin = :gbarcord");

                Query1->ParamByName("gbarcord")->AsString=SBarEd->Text;
                Query1->ParamByName("point")->AsInteger=StrToInt(SPointEd->Text);

                Query1->Prepare();

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

                TCalForm* parent = dynamic_cast<TCalForm*>(Owner);

                if(RG->ItemIndex ==0)
                parent->SavePoint(true,"cash*");
                else if(RG->ItemIndex ==1)
                parent->SavePoint(true,"card*");

                SavePointForm->Close();
        }
        else
        {
                ShowMessage("등록된 고객이 아닙니다.");
        }
}
//---------------------------------------------------------------------------

void __fastcall TSavePointForm::RGClick(TObject *Sender)
{
        if(RG->ItemIndex == 0)
        {
                p = StrToInt(SBuyEd->Text) * (cash/100);
                jukrip = p;
                SPointEd->Text = jukrip ;
        }
        else if  (RG->ItemIndex == 1)
        {
                p = StrToInt(SBuyEd->Text) * (card/100);
                jukrip = p;
                SPointEd->Text = jukrip ;
        }


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

void __fastcall TSavePointForm::FormActivate(TObject *Sender)
{

        p = StrToInt(SBuyEd->Text) * (cash/100);
        jukrip = p;
        SPointEd->Text = jukrip ;
}
//---------------------------------------------------------------------------


void __fastcall TSavePointForm::ComboBox1Change(TObject *Sender)
{
        for(int a=0;a < ComboBox1->Text.Length();a++)
        {
                if(ComboBox1->Text.SubString(a,1)=="/")
                {
                        SBarEd->Text = ComboBox1->Text.SubString(a+1,13);
                        SBarEd->OnKeyPress(this,0x0D);
                        
                }
        } ComboBox1->Visible=false;
}
//---------------------------------------------------------------------------

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


void __fastcall TSavePointForm::SBuyEdKeyPress(TObject *Sender, char &Key)
{
        if(Key==0x0D)
        {
                if(RG->ItemIndex == 0)
                {
                        p = StrToInt(SBuyEd->Text) * (cash/100);
                        jukrip = p;
                        SPointEd->Text = jukrip ;
                }
                else if  (RG->ItemIndex == 1)
                {
                        p = StrToInt(SBuyEd->Text) * (card/100);
                        jukrip = p;
                        SPointEd->Text = jukrip ;
                }
        }
}
//---------------------------------------------------------------------------

