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

#include <vcl.h>
#pragma hdrstop

#include "Part.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TPartForm *PartForm;
//---------------------------------------------------------------------------
__fastcall TPartForm::TPartForm(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------



void __fastcall TPartForm::InBtClick(TObject *Sender)
{
        Query1->Close();
        Query1->SQL->Clear();
        Query1->SQL->Add("select count(groups) cnt from groups where groups = :groups");
        Query1->ParamByName("groups")->AsString = pNameEd->Text;
        Query1->Prepare();
        if(Query1->Prepared)
                Query1->Open();
        if(Query1->FieldByName("cnt")->AsInteger > 0)
        {
                ShowMessage("이미 등록된 분류명 입니다.");
        }
        else
        {
                Query1->SQL->Clear();
                Query1->SQL->Add("insert into groups (groups) values (:groups)");
                Query1->ParamByName("groups")->AsString = pNameEd->Text;
                Query1->Prepare();
                if(Query1->Prepared)
                        Query1->ExecSQL();

                 ListBox1->Clear();
                        RoadGroups();
        }
}
//---------------------------------------------------------------------------

void __fastcall TPartForm::MdBtClick(TObject *Sender)
{
        if(reEd1->Text !="" && reEd2->Text !="")
        {
                Query1->Close();
                Query1->SQL->Clear();
                Query1->SQL->Add("update groups set groups=:groups where groups = :selcitem");
                Query1->ParamByName("groups")->AsString = reEd2->Text;
                Query1->ParamByName("selcitem")->AsString = reEd1->Text;
                Query1->Prepare();
                if(Query1->Prepared)
                        Query1->ExecSQL();

                 ListBox1->Clear();
                 RoadGroups();

        }
        else
        {
                ShowMessage("분류명을 입력하세요");
        }

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

void __fastcall TPartForm::DelBtClick(TObject *Sender)
{
        if( delEd->Text != "")
        {
                Query1->Close();
                Query1->SQL->Clear();
                Query1->SQL->Add("select count(a.part) cnt from goodslist a, groups b where a.part = b.numb and b.groups = :groups");
                Query1->ParamByName("groups")->AsString = delEd->Text;
                Query1->Prepare();
                if(Query1->Prepared)
                        Query1->Open();
                if(Query1->FieldByName("cnt")->AsInteger > 0)
                {
                        ShowMessage("사용하고 있는 상품이 있어 삭제할 수 없습니다.");
                }
                else
                {

                        Query1->SQL->Clear();
                        Query1->SQL->Add("delete from groups where groups = :groups");
                        Query1->ParamByName("groups")->AsString = delEd->Text;

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

                        ListBox1->Clear();
                        RoadGroups();
                }
        }
        else
        {
                ShowMessage("삭제할 분류명을 입력하세요.");
        }
}
//---------------------------------------------------------------------------

void __fastcall TPartForm::FormActivate(TObject *Sender)
{
        RoadGroups();
       
}
//---------------------------------------------------------------------------


void TPartForm::RoadGroups()
{
        //TODO: Add your source code here
        Query1->Close();
        Query1->SQL->Clear();
        Query1->SQL->Add("select groups from groups order by groups");
        Query1->Prepare();
        if(Query1->Prepared)
                Query1->Open();

        while(!Query1->Eof)
        {
                ListBox1->Items->Add(Query1->FieldByName("groups")->AsString);
                Query1->Next();
        }
}

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

void __fastcall TPartForm::ListBox1Click(TObject *Sender)
{
        reEd1->Text = ListBox1->Items[0][ListBox1->ItemIndex] ;
        delEd->Text = ListBox1->Items[0][ListBox1->ItemIndex] ;
        mvEd1->Text = ListBox1->Items[0][ListBox1->ItemIndex] ;
        mvEd2->Text = ListBox1->Items[0][ListBox1->ItemIndex] ;


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

void __fastcall TPartForm::Button1Click(TObject *Sender)
{
        if(mvEd1->Text != "" && mvEd2->Text != "")
        {
                Query1->Close();
                Query1->SQL->Clear();
                Query1->SQL->Add("update goodslist set part=(select numb from groups where gropus=:groups) where part = :selcitem");
                Query1->ParamByName("groups")->AsString = reEd2->Text;
                Query1->ParamByName("selcitem")->AsString = ListBox1->Items->IndexOf(reEd1->Text);
                Query1->Prepare();
                if(Query1->Prepared)
                        Query1->ExecSQL();

                ShowMessage("상품의 분류명이 변경 되었습니다.");
        }
        else
        {
                ShowMessage("분류명을 입력하세요..");
        }
}
//---------------------------------------------------------------------------

