unit U_DyeTechnicsList;

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, dxSkinsCore, dxSkinsDefaultPainters,
  dxDateRanges, dxScrollbarAnnotations;

type
  TfrmDyeTechnicsList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    Label1: TLabel;
    DTMName: TEdit;
    ADOQueryMain: TADOQuery;
    Label3: TLabel;
    DTMNo: 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;
    ToolButton3: TToolButton;
    ToolButton1: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    TV1PSID: TcxGridDBColumn;
    TV1All_Detail: TcxGridDBColumn;
    TV1ALL_NOTE: TcxGridDBColumn;
    TV1All_zhuji: TcxGridDBColumn;
    Tv2Editer: TcxGridDBColumn;
    Tv2Column1: TcxGridDBColumn;
    Tv2GYNote: TcxGridDBColumn;
    cxSplitter1: TcxSplitter;
    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 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);
    procedure ToolButton5Click(Sender: TObject);
    procedure cxGridDBColumn1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure DTMNoKeyPress(Sender: TObject; var Key: Char);
    procedure DTMNameKeyPress(Sender: TObject; var Key: Char);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  private
    { Private declarations }

    procedure InitGrid();
    procedure SetStatus();
    procedure InitGlide();

    procedure WriteSpliterPosition();
    procedure ReadSpliterPosition();
  public
    fmanage: string;
  end;

var
  frmDyeTechnicsList: TfrmDyeTechnicsList;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_DyeTechnicsinput, U_DyeProcessDefin;

{$R *.dfm}
procedure TfrmDyeTechnicsList.InitGlide();
begin
  try
    ADOQueryTemp.DisableControls;
    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.* FROM V_Dye_Technics_Details A');
      sql.Add(' Where A.DTMID=''' + trim(CDS_2.fieldbyname('DTMID').AsString) + '''');
      SQL.Add(' ORDER BY A.PSNO');
      Open;
    end;
    SCreateCDS(ADOQueryTemp, CDS_1);
    SInitCDSData(ADOQueryTemp, CDS_1);
  finally
    ADOQueryTemp.EnableControls;
  end;
end;

procedure TfrmDyeTechnicsList.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 TfrmDyeTechnicsList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select * FROM Dye_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 TfrmDyeTechnicsList.WriteSpliterPosition();
var
  IniFile: TIniFile;
  leftPos: Integer;
begin
  try
    IniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'SpliterPosition.INI');
    IniFile.WriteInteger('DyeTechnicsList_Spliter', 'Position', cxSplitter1.Control.Width);
  finally
    IniFile.Free;
  end;
end;

procedure TfrmDyeTechnicsList.ReadSpliterPosition();
var
  IniFile: TIniFile;
  leftPos1: Integer;
begin
  try
    IniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'SpliterPosition.INI');
    leftPos1 := IniFile.ReadInteger('DyeTechnicsList_Spliter', 'Position', 848);
    cxSplitter1.Control.Width := leftPos1;
  finally
    IniFile.Free;
  end;
end;


procedure TfrmDyeTechnicsList.FormDestroy(Sender: TObject);
begin
  inherited;
  frmDyeTechnicsList := nil;
end;

procedure TfrmDyeTechnicsList.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_ESCAPE then  // VK_ESCAPE 是 ESC 键的常量
  begin
    Key := 0;  // 清除按键，防止继续传递
    Close;     // 或者 ModalResult := mrCancel;
  end;
end;

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

procedure TfrmDyeTechnicsList.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmDyeTechnicsList.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 Dye_Technics_Main where DTMID=''' + Trim(CDS_2.fieldbyname('DTMID').AsString) + '''');
            sql.Add(' delete Dye_Technics_Sub where  DTMID=''' + Trim(CDS_2.fieldbyname('DTMID').AsString) + '''');
            sql.Add(' delete Bs_Cloth_Dye_Technics where  DTMID=''' + Trim(CDS_2.fieldbyname('DTMID').AsString) + '''');
            sql.add(' update Bs_Cloth_Info set DTMID=null,DTMPSlist=null where  DTMID=''' + Trim(CDS_2.fieldbyname('DTMID').AsString) + '''');
            execsql;
          end;
          CDS_1.First;
          while not CDS_1.Eof do
          begin
            with ADOQueryCmd do
            begin
              close;
              sql.Clear;
              sql.Add('delete Dye_Process where PSID=''' + Trim(CDS_1.fieldbyname('PSID').AsString) + '''');
              sql.Add('delete BS_Cloth_GY_Sub where  PSID=''' + Trim(CDS_1.fieldbyname('PSID').AsString) + '''');
              sql.Add('delete BS_Cloth_GY_Main where  PSID=''' + Trim(CDS_1.fieldbyname('PSID').AsString) + '''');

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

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

  ReadSpliterPosition();
  SetStatus();
  InitGrid();
  KeyPreview := True;
end;

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

procedure TfrmDyeTechnicsList.ToolButton1Click(Sender: TObject);
var
  maxno: string;
begin
  try
    frmDyeTechnicsinput := TfrmDyeTechnicsinput.Create(Application);
    with frmDyeTechnicsinput do
    begin
      PState := '新增';
      FDTMID := '';
      if ShowModal = 1 then
      begin
        TBRafresh.Click;
      end;
    end;
  finally
    frmDyeTechnicsinput.Free;
  end;
end;

procedure TfrmDyeTechnicsList.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 TfrmDyeTechnicsList.ToolButton3Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, '工艺管理');
  WriteCxGrid(trim(self.Caption) + 'Tv2', Tv2, '工艺管理');
  WriteSpliterPosition();
end;

procedure TfrmDyeTechnicsList.ToolButton4Click(Sender: TObject);
begin
  try
    frmDyeTechnicsinput := TfrmDyeTechnicsinput.Create(Application);
    with frmDyeTechnicsinput do
    begin
      PState := '修改';
      FDTMID := Trim(self.CDS_2.fieldbyname('DTMID').AsString);
      if ShowModal = 1 then
      begin
        TBRafresh.Click;
      end;
    end;
  finally
    frmDyeTechnicsinput.Free;
  end;
end;

procedure TfrmDyeTechnicsList.ToolButton5Click(Sender: TObject);
begin
  try
    frmDyeTechnicsinput := TfrmDyeTechnicsinput.Create(Application);
    with frmDyeTechnicsinput do
    begin
      PState := '复制';
      FDTMID := Trim(self.CDS_2.fieldbyname('DTMID').AsString);
      if ShowModal = 1 then
      begin
        TBRafresh.Click;
      end;
    end;
  finally
    frmDyeTechnicsinput.Free;
  end;
end;



procedure TfrmDyeTechnicsList.DTMNameKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    ToolButton2.Click;
  end;

end;

procedure TfrmDyeTechnicsList.DTMNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    ToolButton2.Click;
  end;

end;

procedure TfrmDyeTechnicsList.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 Dye_Technics_Main SET status=''1'' ');
            sql.Add('where DTMID =' + quotedstr(trim(CDS_2.fieldbyname('DTMID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;

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

procedure TfrmDyeTechnicsList.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 Dye_Technics_Main SET status=''0'' ');
            sql.Add('where DTMID =' + quotedstr(trim(CDS_2.fieldbyname('DTMID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmDyeTechnicsList.cxGridDBColumn1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmDyeProcessDefin := TfrmDyeProcessDefin.Create(Application);
    with frmDyeProcessDefin do
    begin
      FPSType := self.CDS_1.FieldByName('PSType').Value;
      FPSID := Self.CDS_1.FieldByName('PSID').Value;
      BtnInt := 3;//查看
      TbSave.visible := false;
      TbUpFile.Visible := False;
      if ShowModal = 1 then
      begin

      end;
    end;

  finally
    frmDyeProcessDefin.Free;
  end;
end;

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

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

end;

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

end.

