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

#include <vcl.h>
#include <stdio.h>
#pragma hdrstop

#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
#define     PORT1       1
#define     PORT2       2

TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
    StringGrid1->Cells[0][0]= "Menu" ;
    StringGrid1->Cells[1][0]= "Q'ty" ;
    StringGrid1->Cells[2][0]= "Price" ;
 
    StringGrid2->Cells[0][0]= "No." ;
    StringGrid2->Cells[1][0]= "Menu" ;
    StringGrid2->Cells[2][0]= "Q'ty" ;
    StringGrid2->Cells[3][0]= "Price" ;
 
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Display1(void)
{
    unsigned int amount = 0 ;
    int price=0 ;
    

    for(int i=0 ; i<m_iMenuCnt1 ; i++)
    {
        if(member_flag!=1 && stOrder1[i].Menu == 13) {
           StringGrid1->Cells[0][i+1] = stMenu[stOrder1[i].Menu].Name ;
           member_flag=1;
        }
        if(dis_flag1!=1 && stOrder1[i].Menu == 15) {
           StringGrid1->Cells[0][i+1] = stMenu[stOrder1[i].Menu].Name ;
           discount+=stMenu[stOrder1[i].Menu].Price;
           dis_flag1=1;

        }
        if(dis_flag2!=1 && stOrder1[i].Menu == 14) {
           StringGrid1->Cells[0][i+1] = stMenu[stOrder1[i].Menu].Name ;
           discount+=stMenu[stOrder1[i].Menu].Price;
           dis_flag2=1;

        }
        else if(stOrder1[i].Menu == 0) {
           order_flag=1;
           StringGrid1->Cells[0][i+1] = stMenu[stOrder1[i].Menu].Name ;
           if(discount!=0) StringGrid4->Cells[0][0] = amount *(1-(discount*0.01));
           return;
        }
        else if(stOrder1[i].Menu != 13 && stOrder1[i].Menu != 15 && stOrder1[i].Menu != 14 && stMenu[stOrder1[i].Menu].Name != "" && stOrder1[i].Qty !=0 && stMenu[stOrder1[i].Menu].Price !=0){
        StringGrid1->Cells[0][i+1] = stMenu[stOrder1[i].Menu].Name ;         // Menu Name Write
        StringGrid1->Cells[1][i+1] = stOrder1[i].Qty ;                       // Menu Q'ty
        price = stMenu[stOrder1[i].Menu].Price * stOrder1[i].Qty ;           // Menu Price
        StringGrid1->Cells[2][i+1] = price ;
        amount += price ;
        }
        if(stMenu[stOrder1[i].Menu].Name == "" && stOrder1[i].Qty ==0 && stMenu[stOrder1[i].Menu].Price ==0)
        {
           m_iMenuCnt1-- ;
           m_iMenuCnt2--;
        }

    }
    StringGrid4->Cells[0][0] = amount ;
    
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Display2(void)
{
    Number++;
    int j=0,i=0;
    int price=0 ;
    unsigned int numprice=0;

    for(i= m_iMenuCnt2-m_iMenuCnt1; i<m_iMenuCnt2 ; i++)
    {


        if(stOrder1[j].Menu == 15) {
           StringGrid2->Cells[0][i+1] = Number ;
           StringGrid2->Cells[1][i+1] = stMenu[stOrder1[j].Menu].Name ;
           j++;
        }
        else if(stOrder1[j].Menu == 14) {
           StringGrid2->Cells[0][i+1] = Number ;
           StringGrid2->Cells[1][i+1] = stMenu[stOrder1[j].Menu].Name ;
           j++;
        }
        else if(stOrder1[j].Menu == 13) {
           StringGrid2->Cells[0][i+1] = Number ;
           StringGrid2->Cells[1][i+1] = stMenu[stOrder1[j].Menu].Name ;
           j++;
        }
        else if(stOrder1[j].Menu != 13 && stOrder1[j].Menu != 0 && stOrder1[j].Menu != 15 && stOrder1[j].Menu != 14 && stMenu[stOrder1[j].Menu].Name != "" && stOrder1[j].Qty !=0 && stMenu[stOrder1[j].Menu].Price !=0) {
        StringGrid2->Cells[0][i+1] = Number ;
        StringGrid2->Cells[1][i+1] = stMenu[stOrder1[j].Menu].Name ;         // Menu Name Write
        StringGrid2->Cells[2][i+1] = stOrder1[j].Qty ;                       // Menu Q'ty
        price = stMenu[stOrder1[j].Menu].Price * stOrder1[j].Qty ;
        StringGrid2->Cells[3][i+1] = price;

        j++;
        numprice+=price;
        }
        if(stMenu[stOrder1[j].Menu].Name == "" && stOrder1[j].Qty ==0 && stMenu[stOrder1[j].Menu].Price ==0)
        {
           m_iMenuCnt1-- ;
           m_iMenuCnt2--;
        }   

    }

    StringGrid2->Cells[0][i] = "소 계" ;
    StringGrid2->Cells[1][i] = Time();

    if(discount!=0) {
      StringGrid2->Cells[3][i] = numprice* (1-(discount*0.01));
      TotalAmount+=numprice* (1-(discount*0.01))+1;
    }
    else {
      StringGrid2->Cells[3][i] = numprice;
      TotalAmount+=numprice;
    }
    i++;
    m_iMenuCnt2++;
    if(member_flag==1) {
      StringGrid2->Cells[1][i] = "적립 마일리지" ;
      StringGrid2->Cells[3][i] =  StringGrid2->Cells[3][i-1].ToInt()*0.1;
    }
    m_iMenuCnt2++;
    StringGrid3->Cells[0][0] = TotalAmount ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Menu_Cancel1(int Menu)
{
    int i ;
    if(m_iMenuCnt1 == 0) return ;
    for(i=0 ; i<m_iMenuCnt1 ; i++)
    {
        if(stOrder1[i].Menu == Menu) break ;
    }
    if(i == m_iMenuCnt1) return ;

    for(int j=i ; j<m_iMenuCnt1-1 ; j++)
    {
        stOrder1[j].Menu = stOrder1[j+1].Menu ;
        stOrder1[j].Qty = stOrder1[j+1].Qty ;
    }
    m_iMenuCnt1 -- ;
    for(int i=0 ; i<100 ; i++)
    {
        StringGrid1->Cells[0][i+1] = "" ;
        StringGrid1->Cells[1][i+1] = "" ;
        StringGrid1->Cells[2][i+1] = "" ;
    }

    Display1() ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Menu_Cancel2(int Menu)
{
   /* int i ;
    if(m_iMenuCnt2 == 0) return ;
    for(i=0 ; i<m_iMenuCnt2 ; i++)
    {
        if(stOrder2[i].Menu == Menu) break ;
    }
    if(i == m_iMenuCnt2) return ;

    for(int j=i ; j<m_iMenuCnt2-1 ; j++)
    {
        stOrder2[j].Menu = stOrder2[j+1].Menu ;
        stOrder2[j].Qty = stOrder2[j+1].Qty ;
    }
    m_iMenuCnt2 -- ;
    for(int i=0 ; i<15 ; i++)
    {
        StringGrid2->Cells[0][i+1] = "" ;
        StringGrid2->Cells[1][i+1] = "" ;
        StringGrid2->Cells[2][i+1] = "" ;
    }

    Display2() ;       */
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
    char data[50] ;
    int Cnt ;
    memset(data, 0x00, sizeof(data)) ;
    StatusBar1->Panels->Items[2]->Text = Time() ;

    pSerialComm->Read(data, Cnt) ;

    if(data[1] == 0x61)
    {
        if(m_bOrderOK1 == TRUE)
        {
              Display2();
              m_iMenuCnt1 = 0 ;
              dis_flag2=0;
              dis_flag1=0;
              discount=0;
              member_flag=0;
            for(int i=0 ; i<100 ; i++)
            {
                StringGrid1->Cells[0][i+1] = "" ;
                StringGrid1->Cells[1][i+1] = "" ;
                StringGrid1->Cells[2][i+1] = "" ;
            }
        }
        if(data[3] == 99) Menu_Cancel1(data[2]) ;
        else
        {
            stOrder1[m_iMenuCnt1].Menu = data[2] ;
            stOrder1[m_iMenuCnt1].Qty  = data[3] ;
            if(stOrder1[m_iMenuCnt1-1].Menu ==0 && m_iMenuCnt1!=0 ) {
              m_iMenuCnt1 -- ;
              m_iMenuCnt2 --;
            }
            if(data[2]==15 && dis_flag1==1) {
              m_iMenuCnt1 -- ;
              m_iMenuCnt2 --;
            }
            if(data[2]==14 && dis_flag2==1) {
              m_iMenuCnt1 -- ;
              m_iMenuCnt2 --;
            }
            if(data[2]==13 && member_flag==1) {
              m_iMenuCnt1 -- ;
              m_iMenuCnt2 --;
            }
            m_iMenuCnt1 ++;
            m_iMenuCnt2 ++;

            Display1() ;


        }
        m_bOrderOK1 = FALSE ;
    }
    
    else {}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{
    StatusBar1->Panels->Items[0]->Text = "System Disconnect" ;
    m_bOrderOK1 = FALSE ;
    m_bOrderOK2 = FALSE ;
    m_iMenuCnt1 = 0 ;
    m_iMenuCnt2 = 0 ;
    TotalAmount = 0;
    Number = 0;
    dis_flag1=0;
    dis_flag2=0;
    discount=0;
    order_flag=0;
    member_flag=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCloseQuery(TObject *Sender, bool &CanClose)
{
    if(Application->MessageBox("프로그램을 종료합니다.","종료",MB_YESNO) == IDYES)
    {
        delete pSerialComm ;
        CanClose = true ;
    }
    else CanClose = false ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
    pSerialComm = new SerialComm(PORT1) ;
    if(pSerialComm->PortIOCtrl(_B4800,_DATA8,_STOP1,NONE) == TRUE)
    {
        Timer1->Enabled = true ;
        StatusBar1->Panels->Items[0]->Text = "System Connect" ;
    }
    else { delete pSerialComm ; }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{
    Form2->ShowModal() ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn4Click(TObject *Sender)
{
    char data[3] = {0xaa, 0x11, 0xbb} ;
    char data1[3] = {0xaa, 0x22, 0xbb};

    if(order_flag==1) {
    if(Application->MessageBox("주문을 승낙하시겠습니까?","주문응답",MB_YESNO) == IDYES)
    {
        pSerialComm->Write(data, 3) ;
        m_bOrderOK1 = TRUE ;
    }
    else {
        pSerialComm->Write(data1, 3) ;
          m_iMenuCnt2-=m_iMenuCnt1;
          m_iMenuCnt1 = 0 ;
          dis_flag2=0;
          dis_flag1=0;
          discount=0;
          member_flag=0;
            for(int i=0 ; i<100 ; i++)
            {
                StringGrid1->Cells[0][i+1] = "" ;
                StringGrid1->Cells[1][i+1] = "" ;
                StringGrid1->Cells[2][i+1] = "" ;
            }
           StringGrid4->Cells[0][0] = 0;
    }
    }
    else return;


}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn5Click(TObject *Sender)
{
    char data[3] = {0xaa, 0x22, 0xbb} ;
    pSerialComm->Write(data, 3) ;

    m_bOrderOK2 = TRUE ;

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




















