unit U_WeChatBindingCompany;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, DBClient, cxCalendar, cxButtonEdit, cxSplitter,
  RM_Common, RM_Class, RM_e_Xls, RM_Dataset, RM_System, RM_GridReport,
  cxTextEdit, cxPC, cxCheckBox;

type
  TfrmWeChatBindingCompany = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    UserID: TEdit;
    Label5: TLabel;
    UserName: TEdit;
    Order_Main: TClientDataSet;
    ScrollBox1: TScrollBox;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1Column5: TcxGridDBColumn;
    CDS_PRT: TClientDataSet;
    v1Column1: TcxGridDBColumn;
    Label1: TLabel;
    wxNickName: TEdit;
    ToolButton1: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure UserIDChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
  private
    FInt,PFInt:Integer;
    procedure InitGrid();
    procedure InitForm();
    { Private declarations }
  public
    FType:string;
    { Public declarations }
  end;

var
  frmWeChatBindingCompany: TfrmWeChatBindingCompany;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp,U_WeiXinHelp; //,U_ClothContractInPutPB   ,U_ProductOrderList

{$R *.dfm}

procedure TfrmWeChatBindingCompany.FormDestroy(Sender: TObject);
begin
  frmWeChatBindingCompany:=nil;
end;

procedure TfrmWeChatBindingCompany.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TfrmWeChatBindingCompany.FormCreate(Sender: TObject);
begin
  ScrollBox1.Align:=alClient;
end;

procedure TfrmWeChatBindingCompany.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid(self.Caption+tv1.Name,Tv1,'指示单管理');
end;

procedure TfrmWeChatBindingCompany.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select *  ');
      sql.Add(' from KH_Zdy_Attachment where Type=''KHName'' ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
    TBFind.Click;
  end;
end;

procedure TfrmWeChatBindingCompany.InitForm();
begin
  ReadCxGrid(self.Caption+tv1.Name,Tv1,'指示单管理');
  InitGrid();
end;

procedure TfrmWeChatBindingCompany.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmWeChatBindingCompany.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmWeChatBindingCompany.UserIDChange(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmWeChatBindingCompany.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmWeChatBindingCompany.TBAddClick(Sender: TObject);
var
  FUserId:string;
begin
  if Order_Main.IsEmpty then Exit;
  FUserId:=Trim(Self.Order_Main.FieldByName('ATID').AsString);
  try
    frmWeiXinHelp:=TfrmWeiXinHelp.Create(Self);
    with frmWeiXinHelp do
    begin
//      FGsName:='SW';
      if ShowModal=1 then
      begin
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          SQL.Add(' update KH_Zdy_Attachment set wxid='+QuotedStr(Trim(frmWeiXinHelp.CDS_Help.FieldByName('OPENID').AsString)));
          SQL.Add(',wxNickName='+QuotedStr(Trim(frmWeiXinHelp.CDS_Help.FieldByName('NICKNAME').AsString)));
          SQL.Add(' where ATID='+QuotedStr(Trim(FUserId)));
          ExecSQL;
        end;
        Self.InitGrid();
        Self.Order_Main.Locate('ATID',Trim(FUserId),[]);
      end;
    end;
  finally
    frmWeiXinHelp.Free;
  end;
end;

procedure TfrmWeChatBindingCompany.ToolButton1Click(Sender: TObject);
var
  FUserId:string;
begin
  if Order_Main.IsEmpty then Exit;
  FUserId:=Trim(Self.Order_Main.FieldByName('ATID').AsString);
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    SQL.Add(' update KH_Zdy_Attachment set wxNickName=NULL ');
    SQL.Add(' , wxid=null ');
    SQL.Add(' where ATID='+QuotedStr(Trim(FUserId)));
    ExecSQL;
  end;
  Self.InitGrid();
  Self.Order_Main.Locate('ATID',Trim(FUserId),[]);
end;

end.
