unit U_ClothInfoTechnicsList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin, StdCtrls,
  cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, ShellAPI, IniFiles, cxCheckBox, cxCalendar, cxButtonEdit,
  cxTextEdit, cxPC, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxBarBuiltInMenu, Menus, U_BaseList,  
  dxDateRanges, cxContainer, dxSkinsCore, dxSkinsDefaultPainters,
  dxScrollbarAnnotations, cxProgressBar;

type
  TfrmClothInfoTechnicsList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    Label1: TLabel;
    CTMName: TEdit;
    ADOQueryMain: TADOQuery;
    Label3: TLabel;
    CTMNo: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    GPM_2: TcxGridPopupMenu;
    DS_2: TDataSource;
    CDS_2: TClientDataSet;
    cxTabControl1: TcxTabControl;
    Tchk: TToolButton;
    TNochk: TToolButton;
    v2GYGX: TcxGridDBColumn;
    v2Ssel: TcxGridDBColumn;
    v2GYName: TcxGridDBColumn;
    v2GYNO: TcxGridDBColumn;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    XUHAO: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    ADOQuery1: TADOQuery;
    GPM_1: TcxGridPopupMenu;
    cxSplitter1: TcxSplitter;
    ToolButton3: TToolButton;
    ToolButton1: TToolButton;
    ToolButton4: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure CTMNoChange(Sender: TObject);
    procedure TchkClick(Sender: TObject);
    procedure TNochkClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    { Private declarations }

    procedure InitGrid();
    procedure SetStatus();
    procedure InitGlide();
  public
    fmanage: string;
  end;

var
  frmClothInfoTechnicsList: TfrmClothInfoTechnicsList;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_ClothInfoTechnicsinput;

{$R *.dfm}
procedure TfrmClothInfoTechnicsList.InitGlide();
begin
  try
    ADOQueryTemp.DisableControls;
    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.*,B.* FROM BS_Cloth_Technics_Sub A inner join Bs_Cloth_Process B on A.PSId=B.PSId ');
      sql.Add(' Where A.CTMID=''' + trim(CDS_2.fieldbyname('CTMID').AsString) + '''');
      SQL.Add(' ORDER BY A.PSNO');
      Open;
    end;
    SCreateCDS(ADOQueryTemp, CDS_1);
    SInitCDSData(ADOQueryTemp, CDS_1);
  finally
    ADOQueryTemp.EnableControls;
  end;
end;

procedure TfrmClothInfoTechnicsList.SetStatus();
var
  i: integer;
begin

  Tchk.Visible := false;
  TNochk.Visible := false;
  TBDel.Visible := false;
  for i := 0 to tv2.ColumnCount - 1 do
  begin
    if uppercase(tv2.Columns[i].DataBinding.FieldName) <> 'SSEL' then
      tv2.Columns[i].Options.Editing := false;
  end;
  if cxTabControl1.TabIndex = 0 then
  begin
    Tchk.Visible := true;
    TBDel.Visible := true;

  end;
  if cxTabControl1.TabIndex = 1 then
  begin
    TNochk.Visible := true;
  end;
  if cxTabControl1.TabIndex = 2 then
  begin

  end;
end;

procedure TfrmClothInfoTechnicsList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select * FROM BS_Cloth_Technics_Main');
      if cxTabControl1.TabIndex < 2 then
        sql.Add(' where isnull(status,''0'')=''' + inttostr(cxTabControl1.TabIndex) + ''' ');
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_2);
    SInitCDSData(ADOQueryMain, CDS_2);
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
    //CDS_1.EmptyDataSet;
  end;
end;

procedure TfrmClothInfoTechnicsList.FormDestroy(Sender: TObject);
begin
  inherited;
  frmClothInfoTechnicsList := nil;
end;

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

procedure TfrmClothInfoTechnicsList.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmClothInfoTechnicsList.TBDelClick(Sender: TObject);
begin
  if CDS_2.IsEmpty then
    Exit;
  if CDS_2.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_2 do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        if Fieldbyname('ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('delete BS_Cloth_Technics_Main where CTMID=''' + Trim(CDS_2.fieldbyname('CTMID').AsString) + '''');
            sql.Add('delete BS_Cloth_Technics_Sub where  CTMID=''' + Trim(CDS_2.fieldbyname('CTMID').AsString) + '''');
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmClothInfoTechnicsList.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '工艺管理');
  ReadCxGrid(trim(self.Caption) + 'Tv2', Tv2, '工艺管理');
  SetStatus();
  InitGrid();
end;

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

procedure TfrmClothInfoTechnicsList.ToolButton1Click(Sender: TObject);
var
  maxno: string;
begin
  try
    frmDyeTechnicsinput := TfrmDyeTechnicsinput.Create(Application);
    with frmDyeTechnicsinput do
    begin
      PState := '新增';
      FCTMID := '';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmDyeTechnicsinput.Free;
  end;
end;

procedure TfrmClothInfoTechnicsList.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_2);
    SInitCDSData(ADOQueryMain, CDS_2);
  end;
end;

procedure TfrmClothInfoTechnicsList.ToolButton3Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, '工艺管理');
  WriteCxGrid(trim(self.Caption) + 'Tv2', Tv2, '工艺管理');
end;

procedure TfrmClothInfoTechnicsList.ToolButton4Click(Sender: TObject);
begin
  try
    frmDyeTechnicsinput := TfrmDyeTechnicsinput.Create(Application);
    with frmDyeTechnicsinput do
    begin
      PState := '修改';
      FCTMID := Trim(self.CDS_2.fieldbyname('CTMID').AsString);
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmDyeTechnicsinput.Free;
  end;
end;

procedure TfrmClothInfoTechnicsList.CTMNoChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmClothInfoTechnicsList.TchkClick(Sender: TObject);
begin
  if CDS_2.IsEmpty then
    exit;

  if CDS_2.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_2 do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        if Fieldbyname('ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update BS_Cloth_Technics_Main SET status=''1'' ');
            sql.Add('where CTMID =' + quotedstr(trim(CDS_2.fieldbyname('CTMID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;

    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmClothInfoTechnicsList.TNochkClick(Sender: TObject);
begin
  if CDS_2.IsEmpty then
    exit;

  if CDS_2.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_2 do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        if Fieldbyname('ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update BS_Cloth_Technics_Main SET status=''0'' ');
            sql.Add('where CTMID =' + quotedstr(trim(CDS_2.fieldbyname('CTMID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmClothInfoTechnicsList.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  InitGrid();
end;

procedure TfrmClothInfoTechnicsList.Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitGlide();

end;

procedure TfrmClothInfoTechnicsList.FormCreate(Sender: TObject);
begin
  inherited;
  fmanage := Trim(DParameters1);
end;

end.

