unit SeasonListEd;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Mask, ToolEdit, CurrEdit, ExtCtrls,data, RXCtrls, Buttons,
  Placemnt;


{
 Κατά την μετακίνηση σε περίδο
 χάνονται τα στοιχεία ...
}

resourcestring
  SPriceListFilter = 'Τιμές Πώλησης|*.ssn';

type
  TfmSeasonListEd = class(TForm)
    HeadPanel: TPanel;
    FootPanel: TPanel;
    lbDescription: TLabel;
    edDescription: TEdit;
    edStartDay: TMaskEdit;
    lbStartDay: TLabel;
    edStopDay: TMaskEdit;
    Label1: TLabel;
    btnOK: TButton;
    EditorsPanel: TPanel;
    Panel1: TPanel;
    Panel2: TPanel;
    lbDays: TListBox;
    cbAllDaysSame: TCheckBox;
    btNewSeason: TButton;
    btPriorSsn: TButton;
    btNextSsn: TButton;
    btDeleteSsn: TButton;
    sbFill: TSpeedButton;
    FormStorage: TFormStorage;
    btnSave: TButton;
    btnOpen: TButton;
    procedure ValueChange(Sender: TObject);
    procedure cbAllDaysSameClick(Sender: TObject);
    procedure lbDaysClick(Sender: TObject);
    procedure SelectNextEd(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure edDescriptionChange(Sender: TObject);
    procedure btNewSeasonClick(Sender: TObject);
    procedure btPriorSsnClick(Sender: TObject);
    procedure btNextSsnClick(Sender: TObject);
    procedure edStopDayChange(Sender: TObject);
    procedure btDeleteSsnClick(Sender: TObject);
    procedure sbFillClick(Sender: TObject);
    procedure ValidateDates(Sender: TObject);
    procedure edStopDayExit(Sender: TObject);
    procedure btnOKClick(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
    procedure btnOpenClick(Sender: TObject);
  private
    { Private declarations }
    FSeasonChanged : boolean;
    FSeasonEdited : integer;
    FSeasonList : TSeasonList;
    FValueEdits : array[0..23] of TRxCalcEdit;
    procedure SetSeasonEdited(const Value: integer);
  public
    { Public declarations }
    constructor Create(ASeasonList : TSeasonList); reintroduce;
    property SeasonEdited : integer read FSeasonEdited write SetSeasonEdited;
  end;

var
  fmSeasonListEd: TfmSeasonListEd;

implementation

uses wbMath, Dates;

{$R *.DFM}

{ TfmSeasonListEd }

constructor TfmSeasonListEd.Create(ASeasonList: TSeasonList);
var
 i   : integer;
 lbl : TRxLabel;
begin
  inherited Create(Application);
  FSeasonList := ASeasonList;
  FSeasonEdited :=0;

  for i := 0 to 23 do
  begin
    FValueEdits[i] := TRxCalcEdit.Create(Self);
    lbl := TRxLabel.Create(Self);
    lbl.Caption := Format('%0.2d:00-%0.2d:00',[i,i+1]);
    with FValueEdits[i] do
    begin
      Parent := EditorsPanel;
      if i<=11 then
        Left :=  110
      else
        Left := EditorsPanel.Width - Width - 10;

      Top := (i mod 12)*Height +10;
      Tag := i;
      Visible := True;
      Value := FSeasonList[0].Value[0,i];
      onChange  := ValueChange;
      onKeyDown := SelectNextEd;
      lbl.Left := Left-100;
      lbl.Top :=  Top;
      lbl.Parent := EditorsPanel;
    end;
  end;
  Height := FValueEdits[23].Top+
            FValueEdits[23].Height+
            HeadPanel.Height+
            FootPanel.Height+40;
  for i:=1 to 7 do
    lbDays.Items.Add(LongDayNames[i]);



  edDescription.Left  := EditorsPanel.Left;
  edDescription.Width := FValueEdits[23].Left+FValueEdits[23].Width;
  {
  lbDays.ItemIndex := 0;
  lbDaysClick(Self);

  cbAllDaysSame.Checked:= FSeasonList[FSeasonEdited].AllDaysSame;
  }
  FSeasonEdited := -1;
  SeasonEdited := 0;
end;

procedure TfmSeasonListEd.ValueChange(Sender: TObject);
var
 eid : TRxCalcEdit;
 val : float;
begin
  if not FSeasonChanged then
  begin
    eid := (Sender as TRxCalcEdit);
    val := eid.Value;
    FSeasonList[SeasonEdited].Value[lbDays.ItemIndex,eid.Tag] := val;
  end;
end;

procedure TfmSeasonListEd.cbAllDaysSameClick(Sender: TObject);
begin
  if not FSeasonChanged then
  begin
    FSeasonList[FSeasonEdited].AllDaysSame := cbAllDaysSame.Checked;
    if cbAllDaysSame.Checked then
       lbDays.ItemIndex := 0;
    lbDays.Enabled := not cbAllDaysSame.Checked;
  end;
end;

procedure TfmSeasonListEd.lbDaysClick(Sender: TObject);
var
 h : integer;
begin
  if Not FSeasonChanged then
  begin
    edStartDay.Text := sDate(FSeasonList[FSeasonEdited].StartDay*24);
    edStopDay.Text  := sDate(FSeasonList[FSeasonEdited].StopDay*24);
    for h:=0 to 23 do
        FValueEdits[h].Value := FSeasonList[FSeasonEdited].Value[lbDays.ItemIndex,h];
  end;  
end;

procedure TfmSeasonListEd.SelectNextEd(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
 ed : TRxCalcEdit;
 pv : byte;
begin
  ed := Sender as TRxCalcEdit;
  case key of
    VK_DOWN :  FValueEdits[(ed.Tag+1) mod 24].SetFocus;
    VK_UP   :  begin
                 if ed.Tag=0 then
                    pv := 23
                 else
                    pv := (ed.Tag-1) mod 24;
                 FValueEdits[pv].SetFocus;
               end;
  end;
  // Αν δεν γίνει αυτό
  // δεν δουλεύει ..
  if key in [VK_DOWN,VK_UP] then
     key := 0;
end;

procedure TfmSeasonListEd.edDescriptionChange(Sender: TObject);
begin
  FSeasonList[FSeasonEdited].Description := edDescription.Text;
end;

procedure TfmSeasonListEd.btNewSeasonClick(Sender: TObject);
var
 ns  : TSeason;
 irg ,
 idt : integer;
begin
  idt := (iDate(edStopDay.Text,24)+24 ) div 24;
  irg := (DatePtr(31,12,24)-idt*24) div 24;
  ns  := FSeasonList.Add;
  ns.StartDay := idt;
  ns.DayRange := irg;
   //:= TSeason.Create(idt,irg);
  //FSeasonList.Add(ns);
  SeasonEdited := FSeasonList.Count-1;
end;


procedure TfmSeasonListEd.SetSeasonEdited(const Value: integer);
var
 ssn : TSeason;
 h   : byte;
begin
  if Value<0 then
     Exit;
  if Value>=FSeasonList.Count then
     Exit;
  if (Value <> FSeasonEdited) then
  begin
    FSeasonChanged := True;
    try
      FSeasonEdited := Value;
      Ssn := FSeasonList[FSeasonEdited];
      edDescription.Text    := Ssn.Description;
      edStartDay.Text       := sDate(ssn.StartDay*24);
      edStopDay.Text        := sDate(ssn.StopDay*24);
      cbAllDaysSame.Checked := Ssn.AllDaysSame;
      lbDays.ItemIndex      := 0;
      lbDays.Enabled        := not ssn.AllDaysSame;
      for h:=0 to 23 do
        FValueEdits[h].Value := ssn[0,h];
    finally
      FSeasonChanged := False
    end;
  end;
  //edStartDay.Enabled  := FSeasonEdited>0;
  btNextSsn.Enabled   := FSeasonEdited<FSeasonList.Count-1;
  btPriorSsn.Enabled  := FSeasonEdited>0;
  btDeleteSsn.Enabled := FSeasonList.Count>1;
end;

procedure TfmSeasonListEd.btPriorSsnClick(Sender: TObject);
begin
  SeasonEdited := SeasonEdited-1;
end;

procedure TfmSeasonListEd.btNextSsnClick(Sender: TObject);
begin
  SeasonEdited := SeasonEdited+1;
end;

procedure TfmSeasonListEd.edStopDayChange(Sender: TObject);
var
 idt : integer;
 DayRange : integer;
begin
  btNewSeason.Enabled := edStopDay.Text<>'31/12';
  if (not FSeasonChanged) and ValidDate(edStopDay.Text) then
  begin
    iDt := iDate(edStopDay.Text,24) div 24;
    DayRange :=  idt-FSeasonList[SeasonEdited].StartDay;
    FSeasonList[SeasonEdited].DayRange := DayRange;
  end;
end;

procedure TfmSeasonListEd.btDeleteSsnClick(Sender: TObject);
begin
  if FSeasonList.Count>1 then
  begin
    //FSeasonList.Delete(SeasonEdited);
    FSeasonList[SeasonEdited].Free;
    FSeasonList.Normalize;
    btDeleteSsn.Enabled := FSeasonList.Count>1;
    SeasonEdited := FSeasonList.Count-1;
  end;
end;

procedure TfmSeasonListEd.sbFillClick(Sender: TObject);
var
 h : integer;
 s : string;
 val : Float;
begin
  s:=FValueEdits[0].Text;
  if InputQuery('Γέμισμα','Τιμή:',s) then
  begin
    val := StrToFloat(s);
    for h:=0 to 23 do
      FValueEdits[h].Value := val;
  end;
end;

procedure TfmSeasonListEd.ValidateDates(Sender: TObject);
begin
  iDate(TCustomEdit(Sender).Text,0);
  FSeasonList.Normalize;
end;

procedure TfmSeasonListEd.edStopDayExit(Sender: TObject);
var
 dy : integer;
 dr : integer;
begin
  try
    dy := iDate(edStopDay.Text,24) div 24;
    dr := dy-FSeasonList[SeasonEdited].StartDay;
    if dy>0 then
       FSeasonList[SeasonEdited].DayRange := dr
    else
       raise Exception.CreateFmt('Ακατάλληλη Περιόδος Ημερομηνιών %s - %s',
                                 [edStartDay.Text,edStopDay.Text]);
    //FSeasonList.Normalize;
  except
    edStopDay.SetFocus;
    raise;
  end;
end;

procedure TfmSeasonListEd.btnOKClick(Sender: TObject);
begin
  FSeasonList.Normalize;
  ModalResult := mrOK;
end;

procedure TfmSeasonListEd.btnSaveClick(Sender: TObject);
var
 FNm : string;
begin
  FNm := SaveDialogExec(SPriceListFilter,'');
  if FNm<>'' then
     FSeasonList.SaveToFile(FNm);
end;

procedure TfmSeasonListEd.btnOpenClick(Sender: TObject);
var
 FNm : string;
begin
  FNm := OpenDialogExec(SPriceListFilter,'');
  if FNm<>'' then
  begin
    FSeasonList.LoadFromFile(FNm);
    FSeasonEdited := -1;
    SeasonEdited := 0;
  end;
end;

end.
