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

#include <vcl.h>
#pragma hdrstop

#include "InUser.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TInUserForm *InUserForm;
//---------------------------------------------------------------------------
__fastcall TInUserForm::TInUserForm(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TInUserForm::FormActivate(TObject *Sender)
{
        CuSG->Cells[0][0]= "아이디";
        CuSG->Cells[1][0]= "이름";
        CuSG->Cells[2][0]= "비밀번호";
        CuSG->Cells[3][0]= "계산하기";
        CuSG->Cells[4][0]= "상품입력";
        CuSG->Cells[5][0]= "정산";
        CuSG->Cells[6][0]= "분류등록";
        CuSG->Cells[7][0]= "거래처등록";
        CuSG->Cells[8][0]= "사용자등록";
        CuSG->Cells[9][0]= "환경설정";
        CuSG->Cells[10][0]= "고객관리";
        CuSG->Cells[11][0]= "전화번호";
        CuSG->Cells[12][0]= "핸드폰";
        CuSG->Cells[13][0]= "주소";
        CuSG->Cells[14][0]= "등록일";

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


void __fastcall TInUserForm::SaveBtClick(TObject *Sender)
{
        if(IdLE->Text != "" && NameLE->Text != "" && PasswdLE->Text != "")
        {
                Query1->Close();
                Query1->SQL->Clear();
                Query1->SQL->Add("Select * from posuser where user_id = :id order by user_name asc");
                Query1->ParamByName("id")->AsString = IdLE->Text;
                Query1->Prepare();
                if(Query1->Prepared)
                        Query1->Open();

                if(Query1->RecordCount > 0)
                {
                        ShowMessage("이미 등록된 아이디 입니다.");
                }
                else
                {
                        CuSG->RowCount++;
                        CuSG->Cells[0][CuSG->RowCount-1]= IdLE->Text;
                         CuSG->Cells[1][CuSG->RowCount-1]= NameLE->Text;
                         CuSG->Cells[2][CuSG->RowCount-1]= PasswdLE->Text;
                         CuSG->Cells[11][CuSG->RowCount-1]= PhonLE->Text;
                         CuSG->Cells[12][CuSG->RowCount-1]= HpLE->Text;
                         CuSG->Cells[13][CuSG->RowCount-1]= AddLE->Text;
                         CuSG->Cells[14][CuSG->RowCount-1]= Date();
                        Query1->Close();
                        Query1->SQL->Clear();
                        Query1->SQL->Add("insert into posuser (user_id,user_name,passwd,telephon,handphon,addres,indate,cal,datain,jungsan,partin,sellerin,userin,config,buyer)");
                        Query1->SQL->Add(" values (:user_id,:user_name,:passwd,:telephon,:handphon,:addres,:indate,:cal,:datain,:jungsan,:partin,:sellerin,:userin,:config,:buyer)");
                        Query1->ParamByName("user_id")->AsString = IdLE->Text ;
                        Query1->ParamByName("user_name")->AsString = NameLE->Text;
                        Query1->ParamByName("passwd")->AsString = PasswdLE->Text;
                        Query1->ParamByName("telephon")->AsString = PhonLE->Text;
                        Query1->ParamByName("handphon")->AsString = HpLE->Text;
                        Query1->ParamByName("addres")->AsString = AddLE->Text;
                        Query1->ParamByName("indate")->AsString = Date();
                        
                        if(CalRG->ItemIndex==0)
                        {
                                Query1->ParamByName("cal")->AsString = "true";
                                CuSG->Cells[3][CuSG->RowCount-1]= "허용";
                        }
                        else
                        {
                                Query1->ParamByName("cal")->AsString = "false";
                                CuSG->Cells[3][CuSG->RowCount-1]= "불가";
                        }
                        if(DataRG->ItemIndex==0)
                        {
                                Query1->ParamByName("datain")->AsString = "true";
                                CuSG->Cells[4][CuSG->RowCount-1]= "허용";
                        }
                        else
                        {
                                Query1->ParamByName("datain")->AsString = "false";
                                CuSG->Cells[4][CuSG->RowCount-1]= "불가";
                        }
                        if(JsRG->ItemIndex==0)
                        {
                                Query1->ParamByName("jungsan")->AsString = "true";
                                CuSG->Cells[5][CuSG->RowCount-1]= "허용";
                        }
                        else
                        {
                                Query1->ParamByName("jungsan")->AsString = "false";
                                CuSG->Cells[5][CuSG->RowCount-1]= "불가";
                        }
                        if(PartRG->ItemIndex==0)
                        {
                                Query1->ParamByName("partin")->AsString = "true";
                                CuSG->Cells[6][CuSG->RowCount-1]= "허용";
                        }
                        else
                        {
                                Query1->ParamByName("partin")->AsString = "false";
                                CuSG->Cells[6][CuSG->RowCount-1]= "불가";
                        }
                        if(SellerRG->ItemIndex==0)
                        {
                                Query1->ParamByName("sellerin")->AsString = "true";
                                CuSG->Cells[7][CuSG->RowCount-1]= "허용";
                        }
                        else
                        {
                                Query1->ParamByName("sellerin")->AsString = "false";
                                CuSG->Cells[7][CuSG->RowCount-1]= "불가";
                        }
                        if(UserRG->ItemIndex==0)
                        {
                                Query1->ParamByName("userin")->AsString = "true";
                                CuSG->Cells[8][CuSG->RowCount-1]= "허용";
                        }
                        else
                        {
                                Query1->ParamByName("userin")->AsString = "false";
                                CuSG->Cells[8][CuSG->RowCount-1]= "불가";
                        }
                        if(ConfigRG->ItemIndex==0)
                        {
                                Query1->ParamByName("config")->AsString = "true";
                                CuSG->Cells[9][CuSG->RowCount-1]= "허용";
                        }
                        else
                        {
                                Query1->ParamByName("config")->AsString = "false";
                                CuSG->Cells[9][CuSG->RowCount-1]= "불가";
                        }
                        if(CustomRG->ItemIndex==0)
                        {
                                Query1->ParamByName("buyer")->AsString = "true";
                                CuSG->Cells[10][CuSG->RowCount-1]= "허용";
                        }
                        else
                        {
                                Query1->ParamByName("buyer")->AsString = "false";
                                CuSG->Cells[10][CuSG->RowCount-1]= "불가";
                        }

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


                }
        }
        else
        {
                ShowMessage("모두 입력해 주세요..");
        }
}
//---------------------------------------------------------------------------

void __fastcall TInUserForm::CuSGDblClick(TObject *Sender)
{
        CuSG->OnKeyPress(this,0x0D);
}
//---------------------------------------------------------------------------


void __fastcall TInUserForm::CuSGKeyPress(TObject *Sender, char &Key)
{
        if(Key==0x0D)
        {
                IdLE->Text=CuSG->Cells[0][CuSG->Row];
                NameLE->Text=CuSG->Cells[1][CuSG->Row];
                PasswdLE->Text=CuSG->Cells[2][CuSG->Row];
                if(CuSG->Cells[3][CuSG->Row]=="허용")
                        CalRG->ItemIndex = 0;
                else
                        CalRG->ItemIndex = 1;
                if(CuSG->Cells[4][CuSG->Row]=="허용")
                        DataRG->ItemIndex = 0;
                else
                        DataRG->ItemIndex = 1;
                if(CuSG->Cells[5][CuSG->Row]=="허용")
                        JsRG->ItemIndex = 0;
                else
                        JsRG->ItemIndex = 1;
                if(CuSG->Cells[6][CuSG->Row]=="허용")
                        PartRG->ItemIndex = 0;
                else
                        PartRG->ItemIndex = 1;
                if(CuSG->Cells[7][CuSG->Row]=="허용")
                        SellerRG->ItemIndex = 0;
                else
                        SellerRG->ItemIndex = 1;
                if(CuSG->Cells[8][CuSG->Row]=="허용")
                        UserRG->ItemIndex = 0;
                else
                        UserRG->ItemIndex = 1;
                if(CuSG->Cells[9][CuSG->Row]=="허용")
                        ConfigRG->ItemIndex = 0;
                else
                        ConfigRG->ItemIndex = 1;
                if(CuSG->Cells[10][CuSG->Row]=="허용")
                        CustomRG->ItemIndex = 0;
                else
                        CustomRG->ItemIndex = 1;

                PhonLE->Text=CuSG->Cells[11][CuSG->Row];
                HpLE->Text=CuSG->Cells[12][CuSG->Row];
                AddLE->Text=CuSG->Cells[13][CuSG->Row];
        }
}
//---------------------------------------------------------------------------

void __fastcall TInUserForm::ReBtClick(TObject *Sender)
{
        for(int i=1;i < CuSG->RowCount-1;i++)
        {
                if(CuSG->Cells[0][i]==IdLE->Text)
                        CuSG->Row = i;
        }

                         CuSG->Cells[1][CuSG->Row]= NameLE->Text;
                         CuSG->Cells[2][CuSG->Row]= PasswdLE->Text;
                         CuSG->Cells[11][CuSG->Row]= PhonLE->Text;
                         CuSG->Cells[12][CuSG->Row]= HpLE->Text;
                         CuSG->Cells[13][CuSG->Row]= AddLE->Text;

                        Query1->Close();
                        Query1->SQL->Clear();
                        Query1->SQL->Add("update posuser set user_name=:user_name,passwd=:passwd,telephon=:telephon,handphon=:handphon,addres=:addres,cal=:cal,datain=:datain,jungsan=:jungsan,partin=:partin,sellerin=:sellerin,userin=:userin,config=:config,buyer=:buyer");
                        Query1->SQL->Add("where user_id=:user_id");
                        Query1->ParamByName("user_id")->AsString = IdLE->Text ;
                        Query1->ParamByName("user_name")->AsString = NameLE->Text;
                        Query1->ParamByName("passwd")->AsString = PasswdLE->Text;
                        Query1->ParamByName("telephon")->AsString = PhonLE->Text;
                        Query1->ParamByName("handphon")->AsString = HpLE->Text;
                        Query1->ParamByName("addres")->AsString = AddLE->Text;

                        if(CalRG->ItemIndex==0)
                        {
                                Query1->ParamByName("cal")->AsString = "true";
                                CuSG->Cells[3][CuSG->Row]= "허용";
                        }
                        else
                        {
                                Query1->ParamByName("cal")->AsString = "false";
                                CuSG->Cells[3][CuSG->Row]= "불가";
                        }
                        if(DataRG->ItemIndex==0)
                        {
                                Query1->ParamByName("datain")->AsString = "true";
                                CuSG->Cells[4][CuSG->Row]= "허용";
                        }
                        else
                        {
                                Query1->ParamByName("datain")->AsString = "false";
                                CuSG->Cells[4][CuSG->Row]= "불가";
                        }
                        if(JsRG->ItemIndex==0)
                        {
                                Query1->ParamByName("jungsan")->AsString = "true";
                                CuSG->Cells[5][CuSG->Row]= "허용";
                        }
                        else
                        {
                                Query1->ParamByName("jungsan")->AsString = "false";
                                CuSG->Cells[5][CuSG->Row]= "불가";
                        }
                        if(PartRG->ItemIndex==0)
                        {
                                Query1->ParamByName("partin")->AsString = "true";
                                CuSG->Cells[6][CuSG->Row]= "허용";
                        }
                        else
                        {
                                Query1->ParamByName("partin")->AsString = "false";
                                CuSG->Cells[6][CuSG->Row]= "불가";
                        }
                        if(SellerRG->ItemIndex==0)
                        {
                                Query1->ParamByName("sellerin")->AsString = "true";
                                CuSG->Cells[7][CuSG->Row]= "허용";
                        }
                        else
                        {
                                Query1->ParamByName("sellerin")->AsString = "false";
                                CuSG->Cells[7][CuSG->Row]= "불가";
                        }
                        if(UserRG->ItemIndex==0)
                        {
                                Query1->ParamByName("userin")->AsString = "true";
                                CuSG->Cells[8][CuSG->Row]= "허용";
                        }
                        else
                        {
                                Query1->ParamByName("userin")->AsString = "false";
                                CuSG->Cells[8][CuSG->Row]= "불가";
                        }
                        if(ConfigRG->ItemIndex==0)
                        {
                                Query1->ParamByName("config")->AsString = "true";
                                CuSG->Cells[9][CuSG->Row]= "허용";
                        }
                        else
                        {
                                Query1->ParamByName("config")->AsString = "false";
                                CuSG->Cells[9][CuSG->Row]= "불가";
                        }
                        if(CustomRG->ItemIndex==0)
                        {
                                Query1->ParamByName("buyer")->AsString = "true";
                                CuSG->Cells[10][CuSG->Row]= "허용";
                        }
                        else
                        {
                                Query1->ParamByName("buyer")->AsString = "false";
                                CuSG->Cells[10][CuSG->Row]= "불가";
                        }

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


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

void __fastcall TInUserForm::CuSGKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
        if(Key==0x2e)//delete key
        {
                if(MessageDlg ("정말 삭제 할까요?", mtWarning, TMsgDlgButtons () << mbOK<<mbCancel, 0)==1)
                {
                Query1->Close();
                Query1->SQL->Clear();
                Query1->SQL->Add("delete from posuser where user_id = :id");
                Query1->ParamByName("id")->AsString = CuSG->Cells[0][CuSG->Row];
                Query1->Prepare();
                if(Query1->Prepared)
                        Query1->ExecSQL();

                for(int a=CuSG->Row;a < CuSG->RowCount-1;a++)
                        CuSG->Rows[a]->Assign(CuSG->Rows[a+1]);

                if(CuSG->RowCount > 2)
                        CuSG->RowCount--;
                else
                        CuSG->Rows[CuSG->Row]->Clear();
                }
        }
}
//---------------------------------------------------------------------------


void TInUserForm::DBRead(String id)
{
        //TODO: Add your source code here
        Query1->Close();
        Query1->SQL->Clear();
        Query1->SQL->Add("Select * from posuser ");
        if(id != "")
                Query1->SQL->Add("where user_id like '%"+ id +"%' or user_name like '%"+ id +"%' ");
        Query1->SQL->Add(" order by user_name asc");
        Query1->Prepare();
        if(Query1->Prepared)
                Query1->Open();

                CuSG->RowCount = 2;
                CuSG->Rows[1]->Clear();
                for(int i=1;!Query1->Eof;i++)
                {
                        if(CuSG->Cells[0][CuSG->RowCount-1] != "")
                        {
                                CuSG->RowCount++;
                                CuSG->Row = CuSG->RowCount-1;
                        }
                        CuSG->Cells[0][i]= Query1->FieldByName("user_id")->AsString;
                        CuSG->Cells[1][i]= Query1->FieldByName("user_name")->AsString;
                        CuSG->Cells[2][i]= Query1->FieldByName("passwd")->AsString;

                        if(Query1->FieldByName("cal")->AsString == "True" || Query1->FieldByName("cal")->AsString=="true")
                                CuSG->Cells[3][i]= "허용";
                        else
                                CuSG->Cells[3][i]= "불가";
                        if(Query1->FieldByName("datain")->AsString == "True" || Query1->FieldByName("datain")->AsString=="true")
                                CuSG->Cells[4][i]= "허용";
                        else
                                CuSG->Cells[4][i]= "불가";
                        if(Query1->FieldByName("jungsan")->AsString == "True" || Query1->FieldByName("jungsan")->AsString=="true")
                                CuSG->Cells[5][i]= "허용";
                        else
                                CuSG->Cells[5][i]= "불가";
                        if(Query1->FieldByName("partin")->AsString == "True" || Query1->FieldByName("partin")->AsString=="true")
                                CuSG->Cells[6][i]= "허용";
                        else
                                CuSG->Cells[6][i]= "불가";
                        if(Query1->FieldByName("sellerin")->AsString == "True" || Query1->FieldByName("sellerin")->AsString=="true")
                                CuSG->Cells[7][i]= "허용";
                        else
                                CuSG->Cells[7][i]= "불가";
                        if(Query1->FieldByName("userin")->AsString == "True" || Query1->FieldByName("userin")->AsString=="true")
                                CuSG->Cells[8][i]= "허용";
                        else
                                CuSG->Cells[8][i]= "불가";
                        if(Query1->FieldByName("config")->AsString == "True" || Query1->FieldByName("config")->AsString=="true")
                                CuSG->Cells[9][i]= "허용";
                        else
                                CuSG->Cells[9][i]= "불가";
                        if(Query1->FieldByName("buyer")->AsString == "True" || Query1->FieldByName("buyer")->AsString=="true")
                                CuSG->Cells[10][i]= "허용";
                        else
                                CuSG->Cells[10][i]= "불가"; 

                        CuSG->Cells[11][i]= Query1->FieldByName("telephon")->AsString;
                        CuSG->Cells[12][i]= Query1->FieldByName("handphon")->AsString;
                        CuSG->Cells[13][i]= Query1->FieldByName("addres")->AsString;
                        CuSG->Cells[14][i]= Query1->FieldByName("indate")->AsString;

                        Query1->Next();
                }


}
//---------------------------------------------------------------------------
void __fastcall TInUserForm::ReReadBtClick(TObject *Sender)
{
        DBRead("");
}
//---------------------------------------------------------------------------

void __fastcall TInUserForm::SearchBtClick(TObject *Sender)
{
        DBRead(SearchLE->Text);
}
//---------------------------------------------------------------------------

