{*******************************************************************************
  单元名称：  FindUserFrm.pas

  单元说明：  设置游客帐号，标段关注人。

  作者时间：  Chenshilong, 2017-06-19
*******************************************************************************}

unit SetGuestFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ZJGrid, ZjGridDBA, DB, DBClient, sdDB;

type
  TSetGuestForm = class(TForm)
    btnAdd: TButton;
    zgGuest: TZJGrid;
    btnOK: TButton;
    zaGuest: TZjGridDBA;
    cdsGuest: TClientDataSet;
    cdsGuestID: TIntegerField;
    cdsGuestName: TWideStringField;
    cdsGuestAccount: TWideStringField;
    cdsGuestOperate: TWideStringField;
    procedure btnAddClick(Sender: TObject);
    procedure zgGuestCellGetFont(Sender: TObject; ACoord: TPoint;
      AFont: TFont);
    procedure zgGuestMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
    FWebID: Integer;
    FOwner: TObject;

    procedure GetGuests;
    function Col(AGridDBA: TZjGridDBA; FieldName: string): Integer;
    procedure SetOwner(const Value: TObject);

  public
    { Public declarations }
    constructor Create(AWebID: Integer);
    procedure AddGuest(AID: Integer; AName, AEmail: string);
    function HasGuest(AID: Integer): Boolean;
    property Owner: TObject read FOwner write SetOwner;
  end;

var
  SetGuestForm: TSetGuestForm;

implementation

uses FindUserFrm, PHPWebDm, CSLJson;

{$R *.dfm}

procedure TSetGuestForm.AddGuest(AID: Integer; AName, AEmail: string);
begin
  cdsGuest.Append;
  cdsGuestID.AsInteger := AID;
  cdsGuestName.AsString := AName;
  cdsGuestAccount.AsString := AEmail;
  cdsGuestOperate.AsString := '取消关注';
  cdsGuest.Post;
end;

procedure TSetGuestForm.btnAddClick(Sender: TObject);
var
  FindUserForm: TFindUserForm;
begin
  FindUserForm := TFindUserForm.Create(self, 1, [FWebID, -1]);
  try
    FindUserForm.ShowModal;
  finally
    FindUserForm.Free;
  end;
end;

function TSetGuestForm.Col(AGridDBA: TZjGridDBA;
  FieldName: string): Integer;
begin
  Result := AGridDBA.ColumnIndex(FieldName) + 1;
end;

constructor TSetGuestForm.Create(AWebID: Integer);
var i: Integer;
begin
  inherited Create(nil);
  FWebID := AWebID;
  GetGuests;

// for test
//  for i := 1 to 5 do
//  begin
//    cdsGuest.Append;
//    cdsGuestID.AsString := IntToStr(i);
//    cdsGuestName.AsString := 'aaa' + IntToStr(i);
//    cdsGuestAccount.AsString := 'aaaa@qq.com';
//    cdsGuestOperate.AsString := '取消关注';
//    cdsGuest.Post;
//  end;
end;

procedure TSetGuestForm.GetGuests;
var sURL: string;
  UserArr: TOVArr;
  i: Integer;
begin
  Screen.Cursor := crHourGlass;
  try
    sURL := Format('%stender/%s/concernaudit/list', [PHPWeb.MeasureURL, InttoStr(FwebID)]);   // AAAAAA 查询标段的关注人
    case PHPWeb.Search(sURL, [], [], UserArr) of
      1:
      begin
        if cdsGuest.RecordCount > 0 then
          cdsGuest.EmptyDataSet;

        for i := Low(UserArr) to High(UserArr) do
        begin
          cdsGuest.Append;
          cdsGuestID.AsString := UserArr[i, 0];
          cdsGuestName.AsString := UserArr[i, 1];
          cdsGuestAccount.AsString := UserArr[i, 2];
          cdsGuestOperate.AsString := '取消关注';
          cdsGuest.Post;
        end;

        cdsGuest.First;
      end;
      -1:
      begin
        Application.MessageBox(PChar(PHPWeb.NetError('无法查询到关注人')),
            '警告', MB_OK + MB_ICONWARNING);
        Exit;
        close;
      end;
      0:
      begin
        Application.MessageBox(PChar(PHPWeb.PageError('无法查询到关注人')),
            '警告', MB_OK + MB_ICONWARNING);
        Exit;
        close;
      end;
    end;
  finally
    Screen.Cursor := crDefault;
  end;
end;

function TSetGuestForm.HasGuest(AID: Integer): Boolean;
begin
  Result := False;
  cdsGuest.First;
  while not cdsGuest.Eof do
  begin
    if cdsGuestID.AsInteger = AID then
    begin
      Result := True;
      Break;
    end;

    cdsGuest.Next;
  end;
end;

procedure TSetGuestForm.SetOwner(const Value: TObject);
begin
  FOwner := Value;
end;

procedure TSetGuestForm.zgGuestCellGetFont(Sender: TObject; ACoord: TPoint;
  AFont: TFont);
begin
  if ACoord.X = Col(zaGuest, 'Operate') then
    AFont.Color := clBlue;
end;

procedure TSetGuestForm.zgGuestMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  var s, sURL: string;
  vArr: array of string;
begin

  if ((zgGuest.CurCol = Col(zaGuest, 'Operate')) and (zgGuest.CurRow <= cdsGuest.RecordCount)) then
  begin
    s := Format('确定要取消用户“%s”对该标段的关注吗？', [cdsGuestName.AsString]);
    if Application.MessageBox(PChar(s), '询问', MB_YESNO + MB_ICONQUESTION) = ID_No then
       Exit;

    Screen.Cursor := crHourGlass;
    vArr := VarArrayOf(['msg']);
    try
      sURL := PHPWeb.MeasureURL + 'tender/concernaudit/del';   // 取消关注人

      case PHPWeb.Search(sURL, ['tenderid', 'uid'], [InttoStr(FwebID), cdsGuestID.AsString], vArr) of
        1:
        begin
          cdsGuest.Delete;
        end;
        -1:
        begin
          Application.MessageBox(PChar(PHPWeb.NetError('无法取消关注人')),
              '警告', MB_OK + MB_ICONWARNING);
          Exit;
        end;
        0:
        begin
          Application.MessageBox(PChar(PHPWeb.PageError('无法取消关注人')),
              '警告', MB_OK + MB_ICONWARNING);
          Exit;
        end;
      end;
    finally
      Screen.Cursor := crDefault;
    end;
  end;
end;

end.
