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

#include <vcl.h>
#pragma hdrstop

#include "UsePoint.h"
#include "Calc.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TUsePointForm *UsePointForm;
//---------------------------------------------------------------------------
__fastcall TUsePointForm::TUsePointForm(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TUsePointForm::UBarEdKeyPress(TObject *Sender, char &Key)
{
        if(Key == 0x1b)//esc키
        {
                UsePointForm->Close();

        }
        if(Key==0x0D)
        {
                Query1->Close();

                Query1->SQL->Clear();
                Query1->SQL->Add("select * from point.db ");
                Query1->SQL->Add("where gbarcord=:gbarcord");

                Query1->ParamByName("gbarcord")->AsString=UBarEd->Text;

                Query1->Prepare();

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

                USavePointEd->Text = Query1->FieldByName("point")->AsInteger;
                UNameLb->Caption = Query1->FieldByName("name")->AsString + "  고객님";
        }
}
//---------------------------------------------------------------------------

void __fastcall TUsePointForm::UUsePointEdChange(TObject *Sender)
{
        if(UUsePointEd->Text != "" && UCashEd->Text != "")
        {
                URemainPointEd->Text = StrToInt(USavePointEd->Text) - StrToInt(UUsePointEd->Text);
                UTotalEd->Text = StrToInt(UUsePointEd->Text) + StrToInt(UCashEd->Text);
        }

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

void __fastcall TUsePointForm::UCashEdChange(TObject *Sender)
{
        if(UUsePointEd->Text != "" && UCashEd->Text != "")
                UTotalEd->Text = StrToInt(UUsePointEd->Text) + StrToInt(UCashEd->Text);
}
//---------------------------------------------------------------------------

void __fastcall TUsePointForm::Button1Click(TObject *Sender)
{

               Query1->Close();

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

                Query1->ParamByName("gbarcord")->AsString= UBarEd->Text;

                Query1->Prepare();

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

                if(Query1->RecordCount == 1)
                {

                Query1->SQL->Clear();
                Query1->SQL->Add("update point set point=point-:point,useday=:useday, usepoint=:point ");
                Query1->SQL->Add("where gbarcord=:gbarcord or jumin=:gbarcord");

                Query1->ParamByName("gbarcord")->AsString=UBarEd->Text;
                Query1->ParamByName("useday")->AsString=Date();
                Query1->ParamByName("point")->AsInteger=StrToInt(UUsePointEd->Text);

                Query1->Prepare();

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


               

                TCalForm* parent = dynamic_cast<TCalForm*>(Owner);
                parent->GetED->Text = UTotalEd->Text;
                parent->SavePoint(true,"usepoint");
                parent->GetED->OnKeyPress(this,0x0D);
                UsePointForm->Close();
                }
                else
                {
                        ShowMessage("등록되지 않은 고객입니다.");
                }
        
}
//---------------------------------------------------------------------------

void __fastcall TUsePointForm::FormKeyPress(TObject *Sender, char &Key)
{
        if(Key == 0x1b)//esc키
        {
                UsePointForm->Close();

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

void __fastcall TUsePointForm::Button2Click(TObject *Sender)
{
        UsePointForm->Close();
}
//---------------------------------------------------------------------------

