
//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop
#include <stdlib.h>
#include "SkemaFrameUnit.h"
//#include "rand.h"
#include "mainframeUnit.h"
//---------------------------------------------------------------------------
#pragma link "Grids"
#pragma resource "*.dfm"
TSkemaFrame *SkemaFrame;
//---------------------------------------------------------------------------
__fastcall TSkemaFrame::TSkemaFrame(TComponent* Owner)
	: TForm(Owner)
{
    ListeIndlaest = false;
    SkemaFrame->Caption = "Skema - <tom>";
}
void __fastcall TSkemaFrame::FormClose(TObject *Sender, TCloseAction &Action)
{
	Action = caFree;
}
void __fastcall TSkemaFrame::FormCreate(TObject *Sender)
{
   Height = 225;
   Width = 464;
   string s;
   NulstilStatusBar();
   SFStringGrid1->Cells[1][0] = "Mandag";
   SFStringGrid1->Cells[2][0] = "Tirsdag";
   SFStringGrid1->Cells[3][0] = "Onsdag";
   SFStringGrid1->Cells[4][0] = "Torsdag";
   SFStringGrid1->Cells[5][0] = "Fredag";

   for(int i=1; i<9; i++)
	   SFStringGrid1->Cells[0][i] = i;

   for(int i=0; i<3; i++)
   {  AnsiString AnSt[3];
      AnSt[0] = "Hold";   AnSt[1] = "Laerer";   AnSt[2] = "Lokale";

      SFComboBox1->Items->Clear();

      for(int i=0; i<3; i++)
	   	SFComboBox1->Items->Add(AnSt[i]);
   }
}
//---------------------------------------------------------------------------
void TSkemaFrame::SletSkema()
{
    for(int i = 1; i < 6; i++)
      for(int j = 1; j < 9; j++)
        SFStringGrid1->Cells[i][j] = "";
}
void TSkemaFrame::NulstilStatusBar()
{
	StatusBar->Panels->Items[1]->Text = 0;
    StatusBar->Panels->Items[3]->Text = 0;
    StatusBar->Panels->Items[5]->Text = 0;
    StatusBar->Panels->Items[7]->Text = 0;
}
//----------------------Funktioner til Combobox1------------------------------//
void __fastcall TSkemaFrame::SFComboBox1Change(TObject *Sender)
{
   SFComboBox2->Items->Clear();
   SletSkema();
   NulstilStatusBar();
   ListeIndlaest = false;

   SFComboBox2->Enabled = true;
}
//---------------------------------------------------------------------------//--------------------------Combobox2 funktioner------------------------------//
///Funktion der fylder de rette ID'er ind i comboboxen alt efter om det
///er lokale, hold eller lærer som ønskes.. Angives i combobox1.
void __fastcall TSkemaFrame::SFComboBox2DropDown(TObject *Sender)
{
   string s2;
   char type;
   string HoldID, FagID, LaererID, LokaleID, temp;
    int col(0), row(0);
   if(SFComboBox1->Text == "Hold")
   {
     if(!ListeIndlaest)
     {
       for(int i = 0; i < MainFrame->AntalHold; i++)
         SFComboBox2->Items->Add(MainFrame->HoldListe[i].c_str());
       ListeIndlaest = true;
     }
   }
   else if(SFComboBox1->Text == "Laerer")
   {
     if(!ListeIndlaest)
     {
       for(int i = 0; i < MainFrame->AntalLaerer; i++)
         SFComboBox2->Items->Add(MainFrame->LaererListe[i].c_str());
       ListeIndlaest = true;
     }
   }
   else if(SFComboBox1->Text == "Lokale")
   {
     if(!ListeIndlaest)
     {
       for(int i = 0; i < MainFrame->AntalLokaler; i++)
         SFComboBox2->Items->Add(MainFrame->LokaleListe[i].c_str());
       ListeIndlaest = true;
     }
   }
}//
///Funktionen aktiveres i det øjeblik hvor brugeren har valgt et objekt fra
///combo box'en. Herefter begynder indlæsningen af det dertilhørende skema.
void __fastcall TSkemaFrame::SFComboBox2Click(TObject *Sender)
{
    double Procent(0);
    comm << "inde";
    string HoldID, FagID, LaererID, LokaleID, temp;
    int col(0), row(0);

    SletSkema();
    if(SFComboBox1->Text == "Hold" && SFComboBox2->Text != "")
    {
      //Hold ID findes.
      temp = MainFrame->HoldListe[SFComboBox2->ItemIndex];
      //hentning starter.
      MainFrame->e->StartHentningAfSkema(temp.c_str(), 'h');
      while(MainFrame->e->HentNaesteLektion(HoldID, FagID, LaererID, LokaleID, col, row))
      {
        temp = FagID + " " + LaererID + " " + LokaleID;
        SFStringGrid1->Cells[col+1][row+1] = temp.c_str();
      }

      //status baren opdateres.
      temp = MainFrame->HoldListe[SFComboBox2->ItemIndex];
      int PL = MainFrame->e->PlanlagteLektioner(temp, 'h');
      int LL = MainFrame->e->LagteLektioner(temp, 'h');
      if(PL > 0)
        Procent = (double(LL)/double(PL)) * 100;
      StatusBar->Panels->Items[1]->Text = PL;
      StatusBar->Panels->Items[3]->Text = LL;
      StatusBar->Panels->Items[5]->Text = FloatToStrF(Procent, ffNumber, 3, 2);
      double CV = MainFrame->e->VariansFor(temp, 'h');
      StatusBar->Panels->Items[7]->Text = FloatToStrF(CV, ffNumber, 3, 2);
    }
    else if(SFComboBox1->Text == "Laerer" && SFComboBox2->Text != "")
    {
      //Lærer ID hentes.
      temp = MainFrame->LaererListe[SFComboBox2->ItemIndex];
      //Lærer skema hentes.
      MainFrame->e->StartHentningAfSkema(temp.c_str(), 'l');
      while(MainFrame->e->HentNaesteLektion(HoldID, FagID, LaererID, LokaleID, col, row))
      {
        temp = HoldID + " " + FagID + " " + LokaleID;
        SFStringGrid1->Cells[col+1][row+1] = temp.c_str();
      }

      //status bar opdateres.
      temp = MainFrame->LaererListe[SFComboBox2->ItemIndex];
      int PL = MainFrame->e->PlanlagteLektioner(temp, 'l');
      int LL = MainFrame->e->LagteLektioner(temp, 'l');
      if(PL > 0)
        Procent = (double(LL)/double(PL)) * 100;
      StatusBar->Panels->Items[1]->Text = PL;
      StatusBar->Panels->Items[3]->Text = LL;
      StatusBar->Panels->Items[5]->Text = FloatToStrF(Procent, ffNumber, 3, 2);
      double CV = MainFrame->e->VariansFor(temp, 'l');
      StatusBar->Panels->Items[7]->Text = FloatToStrF(CV, ffNumber, 3, 2);
    }
    else if(SFComboBox1->Text == "Lokale" && SFComboBox2->Text != "")
    {
      //LOkale ID hentes.
      temp = MainFrame->LokaleListe[SFComboBox2->ItemIndex];
      //Lokale skema indlæses.
      MainFrame->e->StartHentningAfSkema(temp.c_str(), 'k');
      while(MainFrame->e->HentNaesteLektion(HoldID, FagID, LaererID, LokaleID, col, row))
      {
        temp = HoldID + " " + FagID + " " + LaererID;
        SFStringGrid1->Cells[col+1][row+1] = temp.c_str();
      }

      //status bar opdateres.
      temp = MainFrame->LokaleListe[SFComboBox2->ItemIndex];
      int PL = MainFrame->e->PlanlagteLektioner(temp, 'k');
      int LL = MainFrame->e->LagteLektioner(temp, 'k');
      if(PL > 0)
        Procent = (double(LL)/double(PL)) * 100;
      StatusBar->Panels->Items[1]->Text = PL;
      StatusBar->Panels->Items[3]->Text = LL;
      StatusBar->Panels->Items[5]->Text = FloatToStrF(Procent, ffNumber, 3, 2);
      double CV = MainFrame->e->VariansFor(temp, 'k');
      StatusBar->Panels->Items[7]->Text = FloatToStrF(CV, ffNumber, 3, 2);
    }
    //nye navn skrives som over skrift i framen.!
    string Navn ="Skema - ";
    Navn += temp;
    Caption = Navn.c_str();
}//
//------------------------------Print funktion--------------------------------//
bool TSkemaFrame::Print()
{
    char type('0');
	string ID = SFComboBox1->Text.c_str();
    if(ID == "Hold")
      type = 'h';
    else if(ID == "Laerer")
      type = 'l';
    else if(ID == "Lokale")
      type = 'k';

    if(type != '0')
    {
      if(strlen(SFComboBox2->Text.c_str()) > 0)
      {
        MainFrame->e->PrintSkema(SFComboBox2->Text.c_str(), type);
        comm.MeddelelseBox("Printer Skema");
      }
      else
        comm.FejlBox("Intet at printe, gået tidligt hjem idag");
    }
    else
      comm.FejlBox("Vælge venligst en type førend der printes!");
}
//---------------------------------------------------------------------------

