unit uProductFrame;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, Dialogs, uniGUITypes, uniGUIAbstractClasses,
  uniGUIClasses, uniGUIFrame, uniButton, uniBitBtn, uniLabel, uniGUIBaseClasses, uniImage, uniPanel, uniCanvas,
  uniImageList,UniGUIJSInterface;

type
  TProductFrame = class(TUniFrame)
    UniCanvas: TUniCanvas;
    bSelect: TUniBitBtn;
    UniImageProductFrame: TUniImage;
    pLabelsPanel: TUniPanel;
    lProductName: TUniLabel;
    lCountry: TUniLabel;
    lPrice: TUniLabel;
    UniImageList: TUniImageList;
    procedure UniFrameCreate(Sender: TObject);
    procedure lProductNameMouseEnter(Sender: TObject);
    procedure bSelectClick(Sender: TObject);
  private
    { Private declarations }

    FShortProductName:string;
    FFullProductName:string;

    FOriginColor:TColor;

    FSelectedColor:Boolean;

    FIndexInObjectList:integer;

    FidDataBase:Integer;
    
    procedure CenterElements;
    procedure TogglePaintUniCanvas;
    procedure PaintUniCanvas;


  public
    { Public declarations }

    property ShortProductName:string read FShortProductName write FShortProductName;
    property FullProductName:string read FFullProductName write FFullProductName;

    property IndexInObjectList:integer read FIndexInObjectList write FIndexInObjectList;

    property idDataBase:Integer read FidDataBase write FidDataBase;

    procedure PaintBorder;
  end;

implementation

{$R *.dfm}

uses uUniShowCaseFrame,MainModule;

var UniShowCaseFrame_UnitVar:TUniShowCaseFrame;

procedure TProductFrame.bSelectClick(Sender: TObject);
begin
TogglePaintUniCanvas;
end;

procedure TProductFrame.CenterElements;


begin

 bSelect.Left:=Trunc(Self.Width/2-bSelect.Width/2);
 pLabelsPanel.Left:=Trunc(Self.Width/2-pLabelsPanel.Width/2);

 UniImageProductFrame.Left:=Trunc(Self.Width/2-UniImageProductFrame.Width/2);




 lProductName.Left:=Trunc(pLabelsPanel.Width/2-lProductName.Width/2);
 lCountry.Left:=Trunc(pLabelsPanel.Width/2-lCountry.Width/2);
 lPrice.Left:=Trunc(pLabelsPanel.Width/2-lPrice.Width/2);


end;

procedure TProductFrame.lProductNameMouseEnter(Sender: TObject);
begin
//

lProductName.Hint:=FFullProductName;
lProductName.ShowHint:=true;

end;


procedure TProductFrame.PaintUniCanvas;

begin


UniCanvas.BitmapCanvas.Brush.Color:=FOriginColor;




UniCanvas.BitmapCanvas.FillRect(

Rect(

UniCanvas.Left+5,
UniCanvas.Top+5,
UniCanvas.Left+UniCanvas.Width-5,
UniCanvas.Top+UniCanvas.Height-5

)

);


IUniJSInterface(UniCanvas).JSCall('submitData', []);

end;


procedure TProductFrame.TogglePaintUniCanvas;

var index:Integer;

begin


if FSelectedColor then

// Deselecting
begin
FSelectedColor:=false;
UniCanvas.BitmapCanvas.Brush.Color:=FOriginColor;

// deleting index

  index:=UniMainModule.SelectedProductsIdList.IndexOf(FidDataBase.ToString());
  if index<>-1 then UniMainModule.SelectedProductsIdList.Delete(index);


end else


 // Selecting
begin
FSelectedColor:=true;
UniCanvas.BitmapCanvas.Brush.Color:=$A4A0A0;

// adding index
UniMainModule.SelectedProductsIdList.Add(
FidDataBase.ToString()
);

end;



UniCanvas.BitmapCanvas.FillRect(

Rect(

UniCanvas.Left+5,
UniCanvas.Top+5,
UniCanvas.Left+UniCanvas.Width-5,
UniCanvas.Top+UniCanvas.Height-5

)

);


//IUniJSInterface(UniCanvas).JSCall('submitData', []);

end;

procedure TProductFrame.PaintBorder;
begin
  UniCanvas.BitmapCanvas.Pen.Color := $A4A0A0;
  UniCanvas.BitmapCanvas.MoveTo(5, 5);
  UniCanvas.BitmapCanvas.LineTo(5, (UniCanvas.Height - 5));
  UniCanvas.BitmapCanvas.MoveTo(5, 5);
  UniCanvas.BitmapCanvas.LineTo(UniCanvas.width - 5, 5);
  UniCanvas.BitmapCanvas.MoveTo(UniCanvas.width - 5, UniCanvas.Height - 5);
  UniCanvas.BitmapCanvas.LineTo(5, UniCanvas.Height - 5);
  UniCanvas.BitmapCanvas.MoveTo(UniCanvas.width - 5, UniCanvas.Height - 5);
  UniCanvas.BitmapCanvas.LineTo(UniCanvas.width - 5, 5);
  IUniJSInterface(UniCanvas).JSCall('submitData', []);
end;

procedure TProductFrame.UniFrameCreate(Sender: TObject);
begin

//

CenterElements;

PaintBorder;  // <<<

UniImageList.GetBitmap(0,UniImageProductFrame.Picture.Bitmap);

if (Sender is TUniShowCaseFrame) then
  UniShowCaseFrame_UnitVar:=(Sender as TUniShowCaseFrame);


FOriginColor:=UniCanvas.Color;
FSelectedColor:=false;


PaintUniCanvas;
end;

end.
