unit unitCleanerAss;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, StdCtrls, Buttons, ComCtrls;

type
  TfrmCleanerAssign = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    lvAss: TListView;
    GroupBox1: TGroupBox;
    txtSearch: TEdit;
    btnSearch: TBitBtn;
    btnAdd: TBitBtn;
    RightClick: TPopupMenu;
    EditAssignment1: TMenuItem;
    DeleteAssignment1: TMenuItem;
    procedure FormShow(Sender: TObject);
    procedure btnSearchClick(Sender: TObject);
    procedure DeleteAssignment1Click(Sender: TObject);
    procedure EditAssignment1Click(Sender: TObject);
    procedure btnAddClick(Sender: TObject);
  private
    { Private declarations }
  public
      assID : Integer;
      procedure loadAss;
      procedure loadSearch;
      procedure delAss;
    { Public declarations }
  end;

var
  frmCleanerAssign: TfrmCleanerAssign;

implementation

uses DM, unitAddAssignment;

{$R *.dfm}
procedure TfrmCleanerAssign.loadAss;
var newState : TListItem;
begin
    if DataModule1.tr.InTransaction then
        DataModule1.tr.Commit;

     DataModule1.tr.StartTransaction;
     DataModule1.query.SQL.Clear;

     DataModule1.query.SQL.Add('SELECT a.RM_ASSID,b.DESCRIPTION,a.RM_NO,a.NAME,a.DESIGN_AREA,a.DATE_CLEANED');
     DataModule1.query.SQL.Add(' FROM ROOM_ASSIGNMENT a, BUILDING b WHERE b.BUIL_ID = a.BUIL_ID ORDER BY a.RM_ASSID');
     DataModule1.query.Prepare;
     DataModule1.query.Open();
     lvAss.Items.BeginUpdate;
     lvAss.Items.Clear;

     if DataModule1.query.Fields.IsNull[0] then Exit;
     while not DataModule1.query.Eof do begin

         newState := lvAss.Items.Add;
         newState.Caption := DataModule1.query.Fields.AsString[0];
         newState.SubItems.Add(DataModule1.query.Fields.AsString[1]);
         newState.SubItems.Add(DataModule1.query.Fields.AsString[2]);
         newState.SubItems.Add(DataModule1.query.Fields.AsString[3]);
         newState.SubItems.Add(DataModule1.query.Fields.AsString[4]);
         newState.SubItems.Add(DataModule1.query.Fields.AsString[5]);

         DataModule1.query.Next;
end;
        if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;
          lvAss.Items.EndUpdate;
end;

procedure TfrmCleanerAssign.FormShow(Sender: TObject);
begin
      loadAss;
end;

procedure TfrmCleanerAssign.loadSearch;
var newState : TListItem;
begin
    if DataModule1.tr.InTransaction then
        DataModule1.tr.Commit;

     DataModule1.tr.StartTransaction;
     DataModule1.query.SQL.Clear;

     DataModule1.query.SQL.Add('SELECT a.RM_ASSID,b.DESCRIPTION,a.RM_NO,a.NAME,a.DESIGN_AREA,a.DATE_CLEANED');
     DataModule1.query.SQL.Add(' FROM ROOM_ASSIGNMENT a, BUILDING b WHERE b.BUIL_ID = a.BUIL_ID And a.RM_NO LIKE :C OR b.DESCRIPTION LIKE :D ORDER BY a.RM_ASSID');
     DataModule1.query.Params.AsString[0] := trim(txtSearch.Text);
     DataModule1.query.Params.AsString[1] := trim(txtSearch.Text);
     DataModule1.query.Prepare;
     DataModule1.query.Open();
     lvAss.Items.BeginUpdate;
     lvAss.Items.Clear;

     if DataModule1.query.Fields.IsNull[0] then Exit;
     while not DataModule1.query.Eof do begin

         newState := lvAss.Items.Add;
         newState.Caption := DataModule1.query.Fields.AsString[0];
         newState.SubItems.Add(DataModule1.query.Fields.AsString[1]);
         newState.SubItems.Add(DataModule1.query.Fields.AsString[2]);
         newState.SubItems.Add(DataModule1.query.Fields.AsString[3]);
         newState.SubItems.Add(DataModule1.query.Fields.AsString[4]);
         newState.SubItems.Add(DataModule1.query.Fields.AsString[5]);

         DataModule1.query.Next;
end;
        if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;
          lvAss.Items.EndUpdate;
end;

procedure TfrmCleanerAssign.btnSearchClick(Sender: TObject);
begin
           if Length(txtSearch.Text) > 0 then
     begin
          loadSearch;
     end
        else  begin
            MessageDlg('Please input first the textbox when searching.',mtInformation,[mbOK],0);
        end;
end;

procedure TfrmCleanerAssign.delAss;
begin
if (lvAss.ItemFocused <> nil) then
      begin
      assID := strtoInt(lvAss.ItemFocused.Caption);
      if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;

          DataModule1.query.SQL.Clear;
          DataModule1.query.SQL.Add('DELETE FROM ROOM_ASSIGNMENT WHERE RM_ASSID LIKE :A');
          DataModule1.query.Params.AsInteger[0] := assID;
          DataModule1.query.Prepare;
          DataModule1.query.Open();

          if DataModule1.tr.InTransaction then
              DataModule1.tr.Commit;

              ShowMessage('Assignment successfully deleted.');
              loadAss;
                end
                  else
                    ShowMessage('Nothing Selected');
end;
procedure TfrmCleanerAssign.DeleteAssignment1Click(Sender: TObject);
begin
      delAss;
end;

procedure TfrmCleanerAssign.EditAssignment1Click(Sender: TObject);
begin
     // frmEditAss.assid.Text := lvAss.ItemFocused.Caption;
      //frmEditAss.ShowModal;
end;

procedure TfrmCleanerAssign.btnAddClick(Sender: TObject);
begin
    frmAddCleaner.ShowModal;
end;

end.
