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

  单元说明：  查找审核人。

  作者时间：  Chenshilong, 2014-07-11
  
              2017.06.19 该窗口定制太深，不通用，改成通用查询窗口。
*******************************************************************************}

unit FindUserFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, JimPages, StdCtrls, cxLookAndFeelPainters, cxButtons,
  cxControls, cxContainer, cxEdit, cxTextEdit, ExtCtrls;

type
  TFindUserForm = class(TForm)
    JimPageControl1: TJimPageControl;
    JimPageControl1Page1: TJimPage;
    JimPageControl1Page2: TJimPage;
    lblHint1: TLabel;
    lblHint2: TLabel;
    edtMail: TcxTextEdit;
    btnSearch: TcxButton;
    pnlUser: TPanel;
    bvl1: TBevel;
    lbl3: TLabel;
    imgUserPic: TImage;
    lblUserName: TLabel;
    lblUserRole: TLabel;
    lblUserCompany: TLabel;
    shp2: TShape;
    btnAdd: TcxButton;
    lblHint: TLabel;
    procedure btnSearchClick(Sender: TObject);
    procedure edtMailKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure btnAddClick(Sender: TObject);
    procedure edtMailClick(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    FOwner: TObject;
    FType: Integer;    // 0审批人, 1关注人。
    FWebID: Integer;
    FPhaseIndex: Integer;
    FAccount: string;
    { Private declarations }
  public
    { Public declarations }
     // AType调用类型: 0审批人, 1关注人。 AValuesArr：第一个值WebID，第二个值PhaseIndex。
     constructor Create(AOwner: TObject; AType: Integer; AValuesArr: array of Integer);
  end;

implementation

uses PHPWebDm, ConstUnit, CheckerFme, ProjectFme, ProjectManagerFme,
  OrderCheckerFme, SetGuestFrm;

{$R *.dfm}

var
  UserArr: array of string;
  PicPath: string;

procedure TFindUserForm.btnSearchClick(Sender: TObject);
var bChecker, bSelf, bFull, bGuest: Boolean;
begin
  Screen.Cursor := crHourGlass;
  try
    FAccount := Trim(edtMail.Text);
    UserArr := VarArrayOf(['avatar', 'name', 'jobs', 'company', 'uid']);
    case PHPWeb.Search(PHPWeb.MeasureURL + 'user/get/search', ['uemail'], [AnsiToUtf8(FAccount)], UserArr) of
      1:
      begin
        lblHint.Caption := '';
        lblUserName.Caption := TOrderCheckerFrame(FOwner).IsMe(StrToInt(UserArr[4]), UserArr[1]) ;
        lblUserRole.Caption := UserArr[2];
        lblUserCompany.Caption := UserArr[3];
        PicPath := PHPWeb.UserPath + '1_' + UserArr[4] + '.jpg';
        if PHPWeb.DownFile(UserArr[0], PicPath) then
          imgUserPic.Picture.LoadFromFile(PicPath);
        pnlUser.Visible := True;
        bSelf := StrToInt(UserArr[4]) = PHPWeb.UserID;

        case FType of
          0:
          begin
            bChecker := TCheckerFrame(TOrderCheckerFrame(FOwner).Owner).InCheckerList(StrToInt(UserArr[4]));
            // AddFrame包含在List.Count中，等效于原报不在List.Count中
            bFull := (TCheckerFrame(TOrderCheckerFrame(FOwner).Owner).List.Count >= 15);
            btnAdd.Enabled := (not bChecker) and (not bSelf) and (not bFull);

            if bFull then
              btnAdd.Caption := '限添加14人'
            else if bSelf then
              btnAdd.Caption := '不能添加自己'
            else if bChecker then
              btnAdd.Caption := '已使用'
            else
              btnAdd.Caption := '使用Ta';
          end;

          1:
          begin
            bChecker := TProjectManagerFrame(TSetGuestForm(FOwner).Owner).UserIsChecker(StrToInt(UserArr[4]));     // 判断是否是参与人
            bGuest := TSetGuestForm(FOwner).HasGuest(StrToInt(UserArr[4]));
            btnAdd.Enabled := (not bChecker) and (not bGuest);

            if bChecker then
              btnAdd.Caption := '已是参与人'
            else if bGuest then
              btnAdd.Caption := '已是关注人'
            else
              btnAdd.Caption := '使用Ta';
          end;
        else ;
        end;
      end;
      -1:
      begin
//        Application.MessageBox(PChar(PHPWeb.NetError('无法查询用户')),
//            '警告', MB_OK + MB_ICONWARNING);
        lblHint.Caption := '没有找到该用户的相关信息！';
        pnlUser.Visible := False;
        btnAdd.Enabled := False;
        Exit;
      end;
      0:
      begin
//        Application.MessageBox(PChar(PHPWeb.PageError('无法查询用户' + '[' + edtMail.Text + ']')),
//            '警告', MB_OK + MB_ICONWARNING);
        lblHint.Caption := '没有找到该用户的相关信息！';
        pnlUser.Visible := False;
        btnAdd.Enabled := False;
        Exit;
      end;
    end;
  finally
    Screen.Cursor := crDefault;
  end;
end;

procedure TFindUserForm.edtMailKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_Return then
    btnSearch.Click;
end;

procedure TFindUserForm.btnAddClick(Sender: TObject);
var sURL: string;
  vArr: array of string;
  vOwner: TCheckerFrame;
begin
  vArr := VarArrayOf(['msg']);

  case FType of
    0:
    begin
      sURL := PHPWeb.MeasureURL + 'user/add/audit/measure';    // 添加审批人
      // 标段ID（网络）、谁添加的、添加的谁、期号
      if PHPWeb.Search(sURL, ['pmid', 'creatoruid', 'auditoruid', 'numpname'],
        [IntToStr(FWebID), IntToStr(PHPWeb.UserID), UserArr[4], IntToStr(FPhaseIndex)], vArr) = 1 then
      begin
        btnAdd.Enabled := False;
        btnAdd.Caption := '添加成功';
        vOwner := TCheckerFrame(TOrderCheckerFrame(FOwner).Owner);
        vOwner.AddNewChecker(cftChecker, StrToInt(UserArr[4]), UserArr[1], UserArr[2], UserArr[3], PicPath, '', csNotBegin, '', -1);
        vOwner.RepairOrder;
        vOwner.RepairDelete(True);

        // 如果他也是关注人，则移除关注人
        sURL := PHPWeb.MeasureURL + 'tender/concernaudit/del';   // 取消关注人
        case PHPWeb.Search(sURL, ['tenderid', 'uid'], [InttoStr(FwebID), UserArr[4]], vArr) of
          1:
          begin

          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;
      end;
    end;
    1:
    begin
      sURL := PHPWeb.MeasureURL + 'tender/concernaudit/create';    // 添加关注人
      // 标段ID（网络）、添加的谁
      if PHPWeb.Search(sURL, ['tenderid', 'uid'], [IntToStr(FWebID), UserArr[4]], vArr) = 1 then
      begin
        btnAdd.Enabled := False;
        btnAdd.Caption := '添加成功';
        TSetGuestForm(FOwner).AddGuest(StrToInt(UserArr[4]), UserArr[1], FAccount);
      end;
    end;
  end;  
end;

procedure TFindUserForm.edtMailClick(Sender: TObject);
begin
  if G_IsTest then
  begin
    if edtMail.Text = '1' then
    begin
      edtMail.Text := '2636698008@qq.com';
    end
    else if edtMail.Text = '2' then
    begin
      edtMail.Text := '1971614655@qq.com';
    end
    else if edtMail.Text = '3' then
    begin
      edtMail.Text := '1835082984@qq.com';
    end
    else if edtMail.Text = '4' then
    begin
      edtMail.Text := '2417587264@qq.com';
    end
    else if edtMail.Text = '5' then
    begin
      edtMail.Text := '2609827960@qq.com';
    end
    else if edtMail.Text = '6' then
    begin
      edtMail.Text := '1240621850@qq.com';
    end
    else if edtMail.Text = '7' then
    begin
      edtMail.Text := '1014149875@qq.com';
    end
    else if edtMail.Text = '8' then
    begin
      edtMail.Text := '1525739553@qq.com';
    end
    else if edtMail.Text = '9' then
    begin
      edtMail.Text := '1391010261@qq.com';
    end
    else if edtMail.Text = '10' then
    begin
      edtMail.Text := '916960227@qq.com';
    end;
  end;
end;

constructor TFindUserForm.Create(AOwner: TObject; AType: Integer; AValuesArr: array of Integer);
var sName: string;
begin
  inherited Create(nil);
  FOwner := AOwner;
  FType := AType;
  FWebID := AValuesArr[0];
  FPhaseIndex := AValuesArr[1];

  lblHint.Caption := '';
  pnlUser.Visible := False;

  case AType of
    0: sName := '审批人';
    1: sName := '关注人';
  else sName := '用户';
  end;

  self.Caption := '添加' + sName;
  JimPageControl1Page1.Caption := Format('查询新%s', [sName]);
  lblHint1.Caption := Format('输入%s的通行帐号注册邮箱，查询并确认后，点击“使用Ta”完成添加%s。', [sName, sName]);
  lblHint2.Caption := Format('添加新%s', [sName]);
end;

procedure TFindUserForm.FormActivate(Sender: TObject);
begin
  Self.Top := Self.Top + 60;
end;

end.
