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

  单元说明：  查找审核人。

  作者时间：  Chenshilong, 2014-07-11
*******************************************************************************}

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;
    lbl1: TLabel;
    lbl2: 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 FormCreate(Sender: TObject);
    procedure edtMailKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure btnAddClick(Sender: TObject);
    procedure edtMailClick(Sender: TObject);
  private
    FProjectData: TObject;
    FOwner: TObject;
    procedure SetProjectData(const Value: TObject);
    procedure SetOwner(const Value: TObject);
    { Private declarations }
  public
    { Public declarations }
     property ProjectData: TObject read FProjectData write SetProjectData;
     property Owner: TObject read FOwner write SetOwner;
  end;

implementation

uses PHPWebDm, ConstUnit, ProjectData, CheckerFme, ProjectFme,
  OrderCheckerFme;

{$R *.dfm}

var
  UserArr: array of string;
  PicPath: string;

procedure TFindUserForm.btnSearchClick(Sender: TObject);
var bUsed, bSelf, bFull: Boolean;
begin
  Screen.Cursor := crHourGlass;
  try
    UserArr := VarArrayOf(['avatar', 'name', 'jobs', 'company', 'uid']);
    case PHPWeb.Search(PHPWeb.MeasureURL + 'user/get/search', ['uemail'], [Trim(edtMail.Text)], UserArr) of
      1:
      begin
        lblHint.Caption := '';
        lblUserName.Caption := TOrderCheckerFrame(Owner).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;
        bUsed := TCheckerFrame(TOrderCheckerFrame(Owner).Owner).InCheckerList(StrToInt(UserArr[4]));
        bSelf := StrToInt(UserArr[4]) = PHPWeb.UserID;
        bFull := (TCheckerFrame(TOrderCheckerFrame(Owner).Owner).List.Count >= 15);   // AddFrame包含在List.Count中，等效于原报不在List.Count中
        btnAdd.Enabled := (not bUsed) and (not bSelf) and (not bFull);

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

      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('无法查询用户' + '[' + UserArr[0] + ']')),
            '警告', MB_OK + MB_ICONWARNING);
        lblHint.Caption := '没有找到该用户的相关信息！';
        pnlUser.Visible := False;
        btnAdd.Enabled := False;
        Exit;
      end;
    end;
  finally
    Screen.Cursor := crDefault;
  end;
end;

procedure TFindUserForm.FormCreate(Sender: TObject);
begin
  lblHint.Caption := '';
  pnlUser.Visible := False;

  if G_IsTest then
    edtMail.Text := 'sp1@qq.com';
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;
  vProj: TProjectData;
  vOwner: TCheckerFrame;
begin
  sURL := PHPWeb.MeasureURL + 'user/add/audit/measure';
  vArr := VarArrayOf(['msg']);
  vProj := TProjectData(ProjectData);
  // 标段ID（网络）、谁添加的、添加的谁、期号
  if PHPWeb.Search(sURL, ['pmid', 'creatoruid', 'auditoruid', 'numpname'],
    [IntToStr(vProj.WebID), IntToStr(PHPWeb.UserID), UserArr[4],
    IntToStr(vProj.PhaseIndex)], vArr) = 1 then
  begin
    btnAdd.Enabled := False;
    btnAdd.Caption := '添加成功';
    vOwner := TCheckerFrame(TOrderCheckerFrame(Owner).Owner);
    vOwner.AddNewChecker(cftChecker, StrToInt(UserArr[4]), UserArr[1], UserArr[2], UserArr[3], PicPath, '', csNotBegin);
    vOwner.RepairOrder;
    vOwner.RepairDelete(True);
  end;
end;

procedure TFindUserForm.SetProjectData(const Value: TObject);
begin
  FProjectData := Value;
end;

procedure TFindUserForm.SetOwner(const Value: TObject);
begin
  FOwner := Value;
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;

end.
