unit U_staffSeList;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, cxGraphics, cxControls, cxLookAndFeels,
  dxCustomTileControl, dxTileBar, cxClasses, dxListView, Data.DB, Data.Win.ADODB,
  dxTileControl, cxContainer, cxEdit, cxGroupBox, cxGeometry, dxFramedControl,
  dxPanel, System.ImageList, Vcl.ImgList, cxImageList, Vcl.ToolWin, Vcl.ActnMan,
  Vcl.ActnCtrls, cxImage,  Vcl.Menus, Vcl.StdCtrls, cxButtons,
  Vcl.ExtCtrls, cxLookAndFeelPainters;

type
  TfrmStaffSelList = class(TForm)
    ADOQueryStaff: TADOQuery;
    dxPanel1: TdxPanel;
    dxTileBar2: TdxTileBar;
    dxTileControlGroup1: TdxTileControlGroup;
    dxTileControlGroup3: TdxTileControlGroup;
    dxTileBarItem1: TdxTileBarItem;
    dxTileBarItem2: TdxTileBarItem;
    ADOQueryTmp: TADOQuery;
    ilSmall: TcxImageList;
    dxTileControl1: TdxTileControl;
    dxTileControl1Item1: TdxTileControlItem;
    dxTileControl1Item2: TdxTileControlItem;
    dxTileControl1Item3: TdxTileControlItem;
    dxTileControl1Item4: TdxTileControlItem;
    dxTileControl1Item5: TdxTileControlItem;
    dxTileControl1Group1: TdxTileControlGroup;
    dxTileControl1Item6: TdxTileControlItem;
    dxTileControl1Item7: TdxTileControlItem;
    dxTileControl1Item8: TdxTileControlItem;
    dxTileControl1Item9: TdxTileControlItem;
    dxTileControl1Item10: TdxTileControlItem;
    dxTileControl1Item11: TdxTileControlItem;
    dxTileControl1Item12: TdxTileControlItem;
    dxTileControl1Item13: TdxTileControlItem;
    dxTileControl1Item14: TdxTileControlItem;
    dxTileControl1Item15: TdxTileControlItem;
    dxTileControl1Item16: TdxTileControlItem;
    dxTileControl1Item17: TdxTileControlItem;
    dxTileControl1Item18: TdxTileControlItem;
    dxTileControl1Item19: TdxTileControlItem;
    dxTileControl1Item20: TdxTileControlItem;
    dxTileControl1Group2: TdxTileControlGroup;
    dxTileControl1Item21: TdxTileControlItem;
    ADOQueryCmd: TADOQuery;
    ilMedium: TcxImageList;
    Image_tmp: TImage;
    procedure dxTileBar1dxTileBarItem2Click(Sender: TdxTileControlItem);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure dxTileBarItem1Click(Sender: TdxTileControlItem);
  private
    fcurUserId:string;

    fcurItem:TdxTileControlItem;
    procedure createGroup();
    procedure createGroupItem(group:TdxTileControlGroup;groupName:String);
    procedure dxTileControl1Item1Click(Sender: TdxTileControlItem);
    function addStaff():boolean;
  public
    fCurDept:string;
    fCurGroupId:string;
    fSelUsersName:string;
    fCurGroupName:string;
  end;

var
  frmStaffSelList: TfrmStaffSelList;

implementation
uses
  spmain,U_iniParam;
{$R *.dfm}

procedure TfrmStaffSelList.dxTileBar1dxTileBarItem2Click(
  Sender: TdxTileControlItem);
begin
  close;
end;

procedure TfrmStaffSelList.dxTileBarItem1Click(Sender: TdxTileControlItem);
begin
  if fcurItem=nil then
  begin
    application.MessageBox('请先选择人员！','警告信息',0) ;
    exit;
  end;
//  if fCurGroupId='' then
//  begin
//    application.MessageBox('班组id不能为空！','警告信息',0) ;
//    exit;
//  end;

  if addStaff() then
  begin
    modalresult:=1;;
  end;
end;

procedure TfrmStaffSelList.FormClose(Sender: TObject; var Action: TCloseAction);
begin
   action:=cafree;
end;

procedure TfrmStaffSelList.FormCreate(Sender: TObject);
begin
  dxTileControl1.Align:=alClient;


end;

procedure TfrmStaffSelList.FormDestroy(Sender: TObject);
begin
  frmStaffSelList:=nil;
end;

procedure TfrmStaffSelList.FormShow(Sender: TObject);
var
  item:TdxTileBarItem;
begin
    createGroup();
//  with ADOQueryTmp do
//  begin
//    close;
//    sql.Clear;
//    sql.Add('select distinct mctype as groupName from Bs_Machine');
//    sql.Add('where isnull(mctype,'''')<>'''' ');
//    open;
//
//    first;
//    while not eof do
//    begin
//      item:=dxTileBar2.CreateItem(tbisRegular,dxTileControlGroup4) ;
//
//      item.Text2.Value:=trim(fieldByName('groupName').AsString);
//
//      next;
//    end;
//  end;
end;
procedure TfrmStaffSelList.createGroup();
var
  group:TdxTileControlGroup;
  mCaption:string;
  i:integer;
begin
  dxTileControl1.Groups.Clear;
  with ADOQueryTmp do
  begin
    close;
    filtered:=false;
    sql.clear;
    sql.Add('select distinct a.dept as groupName');
    sql.Add('from SY_Employee a');
    //if fCurDept<>'' then
    //  sql.Add('where a.dept='+quotedstr(fCurDept));
   // sql.Add('order by a.orderNo,a.mcName');
    open;
  end;
  ADOQueryTmp.first;
  i:=0;
  while not ADOQueryTmp.eof  do
  begin
    mCaption:=trim( ADOQueryTmp.fieldByname('groupName').asstring);
    group:=dxTileControl1.Groups.Add;
    group.Caption.Font.Name:='微软雅黑';
    group.Caption.Font.Size:=12;
    group.Caption.Font.Color:=clWhite;
    group.Caption.Text:=mCaption;
    group.Caption.Font.Style:= [fsBold] ;
    group.Tag:=i;
    createGroupItem(group,mCaption);
    inc(i);
    ADOQueryTmp.Next;
  end;
end;

///////////////////////////////////////////////
/////////////////////////////////////////////////////
///
procedure TfrmStaffSelList.createGroupItem(group:TdxTileControlGroup;groupName:String);
var
  item:TdxTileControlItem;
  i:integer;
  Stream:TMemoryStream ;
begin
  Stream := TMemoryStream.Create;
  try
  with ADOQueryStaff do
  begin
    close;
    sql.Clear;
    sql.Add('select a.*');
    sql.Add('from SY_Employee a');
    sql.Add('where a.dept='''+groupName+'''');
   // sql.Add(' and not exists (SELECT x.user_id FROM bs_group_staff x where x.user_id=a.eeCode and group_id='+quotedstr(fCurGroupId)+')');
   // sql.Add('order by a.mcno,a.mcName');
    open;
    if recordcount=0 then
    begin
      group.Visible:=false;
      exit;
    end;
  end;
  //
  ADOQueryStaff.First;
  i:=1;
  while not ADOQueryStaff.eof do
  begin
    item:= dxTileControl1.CreateItem(tcisRegular,group);  // tcisSmall  tcisRegular
    item.OnClick:=dxTileControl1Item1Click;
    item.Style.GradientBeginColor:=$20998200;
    //item.Glyph.ImageIndex:=13;

//      if not ADOQueryStaff.FieldByName('pic').IsNull then
//      begin
//        TBlobField(ADOQueryStaff.FieldByName('pic')).SaveToStream(Stream) ;
//        Stream.Position:=0;
//        item.Glyph.Image.LoadFromStream(Stream) ;
//      end;


    item.Text1.Value:=trim(ADOQueryStaff.FieldByName('EEName').AsString);
    item.Text1.Font.Style:=[fsBold];
    item.Text1.Font.Name:='微软雅黑';
    item.Text1.Font.Size:=12;
    item.Text2.Value:=trim(ADOQueryStaff.FieldByName('EECode').AsString);
    item.Text3.Value:=trim(ADOQueryStaff.FieldByName('sex').AsString);
    item.Text2.Font.Color:= $20998200;
   // item.Text3.Font.Color:= $20998200;
    item.Text4.Value:= trim(ADOQueryStaff.FieldByName('post').AsString);

   inc(i);
   ADOQueryStaff.Next;
  end;
  finally
      Stream.Free;
    end;
end;


procedure TfrmStaffSelList.dxTileControl1Item1Click(Sender: TdxTileControlItem);
begin
   fcurUserId:=sender.Text2.Value;
   fcurItem:= Sender ;
   Sender.Checked:=not sender.Checked;
end;

//////////////////////////////////////////////
function TfrmStaffSelList.addStaff():boolean;
var
  i:integer;
begin
  try
     result:=false;
     fSelUsersName:='';
     for i := 0 to dxTileControl1.Items.Count-1 do
     begin
       if dxTileControl1.Items[i].Checked then
       begin
          if fSelUsersName='' then
          begin
            fSelUsersName:= dxTileControl1.Items[i].Text1.Value;
          end
          else
          begin
            fSelUsersName:=fSelUsersName+','+ dxTileControl1.Items[i].Text1.Value;
          end;
       end;
     end;

     result:=true;
  except


  end;
end;



end.
