unit View.Tasks;

interface

uses
  System.SysUtils, System.Classes, System.Variants,
  JS, Web, WEBLib.Graphics, WEBLib.Controls, WEBLib.Forms, WEBLib.Dialogs,
  Vcl.Controls, WebLib.JSON,
  VCL.TMSFNCTypes, VCL.TMSFNCUtils, VCL.TMSFNCGraphics, VCL.TMSFNCGraphicsTypes,
  VCL.TMSFNCDataGridCell, VCL.TMSFNCDataGridData, VCL.TMSFNCDataGridBase,
  VCL.TMSFNCDataGridCore, VCL.TMSFNCDataGridRenderer, VCL.TMSFNCCustomControl,
  VCL.TMSFNCDataGrid,
  XData.Web.Client,
  Utils, System.Rtti, WEBLib.ExtCtrls;

type
  TFTasks = class(TWebForm)
    xdwcTasks: TXDataWebClient;
    pnlGrid: TWebPanel;
    grdTasks: TTMSFNCDataGrid;
    procedure WebFormCreate(Sender: TObject);
  private
    procedure SetupGrid;
    [async] procedure LoadTasks(const AProjectId: string);
    procedure AddComboColumn(ACol: Integer; const Items: array of string);
    procedure AddDateColumn(ACol: Integer);
  public
  end;

var
  FTasks: TFTasks;

implementation

uses
  ConnectionModule;

{$R *.dfm}

const
  COL_TASK_ITEM_ID = 0;
  COL_TASK_ID      = 1;
  COL_APP          = 2;
  COL_VERSION      = 3;
  COL_TASK_DATE    = 4;
  COL_REPORTED_BY  = 5;
  COL_ASSIGNED_TO  = 6;
  COL_STATUS       = 7;
  COL_STATUS_DATE  = 8;
  COL_FIXED_VER    = 9;
  COL_FORM_SECTION = 10;
  COL_ISSUE        = 11;
  COL_NOTES        = 12;

procedure TFTasks.WebFormCreate(Sender: TObject);
begin
  xdwcTasks.Connection := DMConnection.ApiConnection;

  SetupGrid;
  LoadTasks('WPR0001');
end;

procedure TFTasks.SetupGrid;
begin
  grdTasks.BeginUpdate;
  try
    grdTasks.Options.IO.StartColumn := 1;
    grdTasks.Options.IO.StartRow := 1;

    grdTasks.RowCount := 1;
    grdTasks.ColumnCount := 13;

    grdTasks.DefaultRowHeight := 100;

    grdTasks.Options.Keyboard.TabKeyDirectEdit := True;
    grdTasks.Options.Keyboard.ArrowKeyDirectEdit := True;
    grdTasks.Options.Keyboard.EnterKeyDirectEdit := True;

    grdTasks.Options.Editing.Enabled := True;

    grdTasks.Columns[COL_TASK_ITEM_ID].Width := 110;
    grdTasks.Columns[COL_TASK_ID].Width := 90;
    grdTasks.Columns[COL_APP].Width := 220;
    grdTasks.Columns[COL_VERSION].Width := 80;
    grdTasks.Columns[COL_TASK_DATE].Width := 110;
    grdTasks.Columns[COL_REPORTED_BY].Width := 110;
    grdTasks.Columns[COL_ASSIGNED_TO].Width := 110;
    grdTasks.Columns[COL_STATUS].Width := 140;
    grdTasks.Columns[COL_STATUS_DATE].Width := 110;
    grdTasks.Columns[COL_FIXED_VER].Width := 110;
    grdTasks.Columns[COL_FORM_SECTION].Width := 160;
    grdTasks.Columns[COL_ISSUE].Width := 650;
    grdTasks.Columns[COL_NOTES].Width := 450;

    grdTasks.Cells[COL_TASK_ITEM_ID, 0] := 'Task Item ID';
    grdTasks.Cells[COL_TASK_ID, 0]      := 'Task ID';
    grdTasks.Cells[COL_APP, 0]          := 'Application';
    grdTasks.Cells[COL_VERSION, 0]      := 'Version';
    grdTasks.Cells[COL_TASK_DATE, 0]    := 'Date';
    grdTasks.Cells[COL_REPORTED_BY, 0]  := 'Reported By';
    grdTasks.Cells[COL_ASSIGNED_TO, 0]  := 'Assigned To';
    grdTasks.Cells[COL_STATUS, 0]       := 'Status';
    grdTasks.Cells[COL_STATUS_DATE, 0]  := 'Status Date';
    grdTasks.Cells[COL_FIXED_VER, 0]    := 'Fixed Version';
    grdTasks.Cells[COL_FORM_SECTION, 0] := 'Form (Section)';
    grdTasks.Cells[COL_ISSUE, 0]        := 'Issue';
    grdTasks.Cells[COL_NOTES, 0]        := 'Notes';

    grdTasks.Columns[COL_TASK_ITEM_ID].ReadOnly := True;
    grdTasks.Columns[COL_TASK_ID].ReadOnly := True;

    AddComboColumn(COL_APP, ['webPoliceReports - Client', 'webPoliceReports - Server']);
    AddComboColumn(COL_VERSION, ['0.9.4', '0.9.5']);
    AddComboColumn(COL_REPORTED_BY, ['Mark', 'Mac']);
    AddComboColumn(COL_ASSIGNED_TO, ['Mac', 'Mark']);
    AddComboColumn(COL_STATUS, ['Open', 'In Progress', 'Blocked', 'Fixed', 'Fixed - Verified']);
    AddComboColumn(COL_FIXED_VER, ['0.9.4', '0.9.5']);

    AddDateColumn(COL_TASK_DATE);
    AddDateColumn(COL_STATUS_DATE);
  finally
    grdTasks.EndUpdate;
  end;
end;

procedure TFTasks.AddComboColumn(ACol: Integer; const Items: array of string);
var
  I: Integer;
begin
  grdTasks.Columns[ACol].AddSetting(gcsEditor);
  grdTasks.Columns[ACol].AddSetting(gcsEditorItems);
  grdTasks.Columns[ACol].Editor := getComboBox;

  grdTasks.Columns[ACol].EditorItems.Clear;
  for I := Low(Items) to High(Items) do
    grdTasks.Columns[ACol].EditorItems.Add(Items[I]);
end;

procedure TFTasks.AddDateColumn(ACol: Integer);
begin
  grdTasks.Columns[ACol].AddSetting(gcsEditor);
  grdTasks.Columns[ACol].Editor := getDatePicker;

  grdTasks.Columns[ACol].AddSetting(gcsFormatting);
  grdTasks.Columns[ACol].Formatting.&Type := gdftDateTime;
end;

[async] procedure TFTasks.LoadTasks(const AProjectId: string);
var
  response: TXDataClientResponse;
  resultObj: TJSObject;
  tasksArray: TJSArray;
  taskObj: TJSObject;
  itemsArray: TJSArray;
  itemObj: TJSObject;
  taskIndex, itemIndex: Integer;
  gridRow: Integer;
  statusDateVal: JSValue;
  r: Integer;
begin
  Utils.ShowSpinner('spinner');
  try
    try
      response := await(xdwcTasks.RawInvokeAsync(
        'IApiService.GetProjectTasks', [AProjectId]
      ));

      if not Assigned(response.Result) then
        Exit;

      resultObj := TJSObject(response.Result);
      tasksArray := TJSArray(resultObj['data']);

      grdTasks.BeginUpdate;
      try
        grdTasks.RowCount := 1;
        gridRow := 1;

        for taskIndex := 0 to tasksArray.Length - 1 do
        begin
          taskObj := TJSObject(tasksArray[taskIndex]);
          itemsArray := TJSArray(taskObj['items']);

          for itemIndex := 0 to itemsArray.Length - 1 do
          begin
            itemObj := TJSObject(itemsArray[itemIndex]);

            grdTasks.RowCount := gridRow + 1;

            grdTasks.Cells[COL_TASK_ITEM_ID, gridRow] := string(itemObj['taskItemId']);
            grdTasks.Cells[COL_TASK_ID, gridRow]      := string(itemObj['taskId']);
            grdTasks.Cells[COL_APP, gridRow]          := string(itemObj['application']);
            grdTasks.Cells[COL_VERSION, gridRow]      := string(itemObj['version']);
            grdTasks.Cells[COL_TASK_DATE, gridRow]    := string(itemObj['taskDate']);
            grdTasks.Cells[COL_REPORTED_BY, gridRow]  := string(itemObj['reportedBy']);
            grdTasks.Cells[COL_ASSIGNED_TO, gridRow]  := string(itemObj['assignedTo']);
            grdTasks.Cells[COL_STATUS, gridRow]       := string(itemObj['status']);

            statusDateVal := itemObj['statusDate'];

            if JS.isNull(statusDateVal) or JS.isUndefined(statusDateVal) then
              grdTasks.Cells[COL_STATUS_DATE, gridRow] := ''
            else
              grdTasks.Cells[COL_STATUS_DATE, gridRow] := string(statusDateVal);

            grdTasks.Cells[COL_FIXED_VER, gridRow]    := string(itemObj['fixedVersion']);
            grdTasks.Cells[COL_FORM_SECTION, gridRow] := string(itemObj['formSection']);
            grdTasks.Cells[COL_ISSUE, gridRow]        := string(itemObj['issue']);
            grdTasks.Cells[COL_NOTES, gridRow]        := string(itemObj['notes']);

            Inc(gridRow);
          end;
        end;
      finally
        grdTasks.EndUpdate;
      end;

      grdTasks.Height := Trunc((grdTasks.RowCount * grdTasks.DefaultRowHeight) + 2);

    except
      on E: EXDataClientRequestException do
        Utils.ShowErrorModal(E.ErrorResult.ErrorMessage);
    end;
  finally
    Utils.HideSpinner('spinner');
  end;
end;

end.

