unit uAddProductForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, Dialogs, uniGUITypes, uniGUIAbstractClasses,
  uniGUIClasses, uniGUIForm, uniLabel, uniPanel, uniPageControl, uniGUIBaseClasses, uniImage, uniEdit, uniButton,
  uniBitBtn, uniImageList,uUniShowCaseFrame,uProductFrame,Main;

type
  TfAddProductForm = class(TUniForm)
    UniPageControl1: TUniPageControl;
    tsMainFeature: TUniTabSheet;
    tsUploadPhoto: TUniTabSheet;
    lProductName: TUniLabel;
    lCountry: TUniLabel;
    lPrice: TUniLabel;
    UniImage: TUniImage;
    bUploadImage: TUniBitBtn;
    eName: TUniEdit;
    eCountry: TUniEdit;
    ePrice: TUniEdit;
    eImageLinkOnServer: TUniEdit;
    UniLabel1: TUniLabel;
    bAdd: TUniButton;
    UniImageList: TUniImageList;
    procedure bAddClick(Sender: TObject);
    procedure UniFormCreate(Sender: TObject);
    procedure eNameClick(Sender: TObject);
    procedure eCountryClick(Sender: TObject);
    procedure ePriceClick(Sender: TObject);
    procedure bUploadImageClick(Sender: TObject);
  private
    { Private declarations }

    FUnishowCaseFrame:TUniShowCaseFrame;
    FMainForm:TMainForm;

    FMaxInsertedID:integer;

    FOpenLastPage:TNotifyEvent;

    function IsItDigit(someChar: char): Boolean;



  public
    { Public declarations }


    constructor Create(AOwner: TComponent); override;

    property MaxInsertedID:Integer read FMaxInsertedID
    write FMaxInsertedID;

    property UnishowCaseFrame:TUniShowCaseFrame read
    FUnishowCaseFrame write FUnishowCaseFrame;

    property MainForm:TMainForm read FMainForm write FMainForm;

    property OpenLastPage:TNotifyEvent read FOpenLastPage write FOpenLastPage;


  end;

function fAddProductForm: TfAddProductForm;

implementation

{$R *.dfm}

uses
  MainModule, uniGUIApplication,uDataBaseUnit,
  uDiminishPic,uUploadPicture;

function fAddProductForm: TfAddProductForm;
begin
  Result := TfAddProductForm(UniMainModule.GetFormInstance(TfAddProductForm));
end;

procedure TfAddProductForm.bAddClick(Sender: TObject);
var
  I: Integer;
begin



if eName.Text='Enter some product name here...' then
begin

 ShowMessage('Enter Product Name');
 Exit;


end;



if eCountry.Text='Enter some Country here...' then
 begin
 ShowMessage('Enter Country');
 Exit;
 end;



if ePrice.Text='Enter some Price here... (only digits)' then
begin

 ShowMessage('Enter Price');
 Exit;

end;



for I := Low(ePrice.Text) to High(ePrice.Text) do
if not IsItDigit(ePrice.Text[i]) then
 begin
 ShowMessage('Price entered incorrectly');
 Exit;
 end;



//Updating text fields in data base



with  DataBaseUnit.qUpdateName do begin

      Params.ParamValues['Name']:=Trim(eName.Text);
      Params.ParamValues['id']:=fAddProductForm.MaxInsertedID;
      ExecSQL;

end;

with  DataBaseUnit.qUpdateCountry do begin

      Params.ParamValues['Country']:=Trim(eCountry.Text);
      Params.ParamValues['id']:=fAddProductForm.MaxInsertedID;
      ExecSQL;

end;

with  DataBaseUnit.qUpdatePrice_begin do begin

      Params.ParamValues['Price_begin']:=Trim(ePrice.Text);
      Params.ParamValues['id']:=fAddProductForm.MaxInsertedID;
      ExecSQL;

end;



ShowMessage('New Product Successfully added');


FUniShowCaseFrame.MaxWidth:=FMainForm.Width;
FUniShowCaseFrame.Width:=FMainForm.Width;
FUniShowCaseFrame.ProductsOnPage:=10;



FUniShowCaseFrame.AddProductFrameMultiPages;

with
TProductFrame(
FUniShowCaseFrame.ProductsOL[FUniShowCaseFrame.ProductsOL.Count-1]
) do begin

lProductName.Text:='';
lCountry.Text:='';
lPrice.Text:='';

lProductName.Left:=10;
lCountry.Left:=10;
lPrice.Left:=10;

lProductName.Text:=Trim(eName.Text);
lCountry.Text:=Trim(eCountry.Text);
lPrice.Text:=Trim(ePrice.Text);

UniImageProductFrame.Assign(UniImage);

end;




FUniShowCaseFrame.OpenLastPage;


//firing Event

if Assigned(FOpenLastPage)  then FOpenLastPage(UniApplication);



Close;
//ModalResult:=mrOk;

end;



function TfAddProductForm.IsItDigit(someChar:char): Boolean;
begin
 if not (someChar in ['0'..'9']) then Result:=false else Result:=true;
end;




procedure TfAddProductForm.bUploadImageClick(Sender: TObject);
var uniimageSource:tuniimage;
    uniimageDest:tuniimage;

begin
//


UploadPicture.UniFileUpload.Execute;

{
uniimageSource:=uniimage.Create(UniApplication);
uniimageDest:=uniimage.Create(UniApplication);


DiminishPic.ProportionalDiminishPicSize(
OpenDialog.FileName,
uniimageSource,
uniimageDest,
128,
128

);


UniImage.Assign(uniimageDest);
}



end;

constructor TfAddProductForm.Create(AOwner: TComponent);
begin
  inherited;


// adding new product to database
DataBaseUnit.qAddSomeNewProduct.ExecSQL;

with DataBaseUnit.qGetMaxID do begin
Open();
FMaxInsertedID:=FieldByName('MaxID').AsInteger;
Close;
end;


ShowMessage('added new product with id='+FMaxInsertedID.ToString());

end;

procedure TfAddProductForm.eCountryClick(Sender: TObject);
begin

if eCountry.Text='Enter some Country here...' then
eCountry.Text:='';


end;

procedure TfAddProductForm.eNameClick(Sender: TObject);
begin

if eName.Text='Enter some product name here...' then
eName.Text:='';

end;

procedure TfAddProductForm.ePriceClick(Sender: TObject);
begin

if ePrice.Text='Enter some Price here... (only digits)' then
ePrice.Text:='';


end;

procedure TfAddProductForm.UniFormCreate(Sender: TObject);
begin
UniImageList.GetBitmap(0,UniImage.Picture.Bitmap);


end;

end.
