unit U_DyePCMachineList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridCustomTableView, cxGridTableView,
  cxGridBandedTableView, cxGridDBBandedTableView, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridDBTableView, cxGrid, StdCtrls, ComCtrls,
  ExtCtrls, ToolWin, cxGridCustomPopupMenu, cxGridPopupMenu, ADODB, DBClient,
  cxDropDownEdit, MovePanel, cxButtonEdit, cxCalendar, RM_System, RM_Common,
  RM_Class, RM_GridReport, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxSkinsCore, dxSkinsDefaultPainters, dxDateRanges, dxBarBuiltInMenu,
  U_BaseList, dxScrollbarAnnotations;

type
  TfrmDyePCMachineList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label3: TLabel;
    CDS_Main: TClientDataSet;
    MovePanel2: TMovePanel;
    RMGridReport1: TRMGridReport;
    customerNoName: TEdit;
    Label13: TLabel;
    ywy: TEdit;
    Label17: TLabel;
    lbHx: TEdit;
    Label7: TLabel;
    ToolButton1: TToolButton;
    dbGrid1: TcxGrid;
    tv1: TcxGridDBTableView;
    tv1CraftIdx: TcxGridDBColumn;
    tv1mcName: TcxGridDBColumn;
    tv1CCQX1: TcxGridDBColumn;
    tv1DX1: TcxGridDBColumn;
    tv1DX2: TcxGridDBColumn;
    tv1DX3: TcxGridDBColumn;
    tv1DX4: TcxGridDBColumn;
    tv1DX5: TcxGridDBColumn;
    tv1DX6: TcxGridDBColumn;
    tv1JG1: TcxGridDBColumn;
    tv1JRG1: TcxGridDBColumn;
    tv1GC_BatchNo: TcxGridDBColumn;
    tv1CurGlideName: TcxGridDBColumn;
    tv1Note: TcxGridDBColumn;
    dbGrid1Level1: TcxGridLevel;
    ToolButton2: TToolButton;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    Label2: TLabel;
    tv1PCKey: TcxGridDBColumn;
    BegTime: TDateTimePicker;
    EndTime: TDateTimePicker;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    MCNO: TComboBox;
    TbAdd: TToolButton;
    MCName: TEdit;
    TbDel: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure pcidChange(Sender: TObject);
    procedure PRTColorChange(Sender: TObject);

    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);

    procedure tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure tv1mcNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure MCNOChange(Sender: TObject);
    procedure TbAddClick(Sender: TObject);
    procedure TbDelClick(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);


//    procedure tv1CustomDrawCell(Sender: TcxCustomGridTableView;
//      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
//      var ADone: Boolean);

  private
    FLeft, FTop: Integer;
    procedure InitGrid();
    { Private declarations }
  public
    fsyrName, canshu1: string;

    { Public declarations }
  end;

var
  frmDyePCMachineList: TfrmDyePCMachineList;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_MachTaskList;

{$R *.dfm}

procedure TfrmDyePCMachineList.FormDestroy(Sender: TObject);
begin
  inherited;
  frmDyePCMachineList := nil;
end;

procedure TfrmDyePCMachineList.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 TfrmDyePCMachineList.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  Action := caFree;
end;

procedure TfrmDyePCMachineList.FormCreate(Sender: TObject);
begin
  inherited;

//  EndDate.DateTime := SGetServerDateTime(ADOQueryTemp);
//  BegDate.DateTime := EndDate.DateTime;
  fsyrName := trim(DdataBase);
  canshu1 := trim(DParameters1);
//  SInitComBoxBySql(ADOQueryTemp, GlideName, false, 'select PSName as Name from Dye_Process');

end;

procedure TfrmDyePCMachineList.InitGrid();
var
  mBegDate, mendDate: string;
  mAction: string;
  rs: _Recordset;
  r: integer;
begin
  // 任务时间

  try
    tv1.BeginUpdate;
    ADOQueryMain.DisableControls;

    with ADOQueryMain do
    begin
      close;
      filtered := false;
      sql.Clear;
      SQL.Add('select * from V_Dye_Cloth_PaiGang where (BegTime between :StartTime and :EndTime) and MCNO = :MCNO order By BegTime');
      Parameters.ParamByName('StartTime').Value := Trunc(BegDate.Date) + Frac(BegTime.Time);
      Parameters.ParamByName('EndTime').Value := Trunc(EndDate.Date) + Frac(EndTime.Time);
      Parameters.ParamByName('MCNO').Value := MCNO.Text;
      Open;

    end;
    SCreateCDS(ADOQueryMain, CDS_main);
    SInitCDSData(ADOQueryMain, CDS_main);
  finally
    ADOQueryMain.EnableControls;
    tv1.EndUpdate;
   // tv1.ApplyBestFit();
  end;
end;

procedure TfrmDyePCMachineList.MCNOChange(Sender: TObject);
begin
  InitGrid();
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * from Bs_Machine where MCNO = ' + quotedStr(Trim(MCNO.Text)));
    Open;
  end;
  MCName.Text := ADOQueryCmd.FieldByName('MCName').AsString;
end;

procedure TfrmDyePCMachineList.TBRafreshClick(Sender: TObject);
begin
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  InitGrid();
  MovePanel2.Visible := False;
end;

procedure TfrmDyePCMachineList.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(self.Caption + tv1.Name, Tv1, '排产信息');
end;

procedure TfrmDyePCMachineList.ToolButton2Click(Sender: TObject);
begin
//  if ADOQuerymain.IsEmpty then
//    exit;

  frmMachTaskList := TfrmMachTaskList.create(self);
  with frmMachTaskList do
  begin
//    fcurMachId := trim(self.adoquerymain.FieldByName('mcid').AsString);
//    machName.text := trim(self.adoquerymain.FieldByName('mcName').AsString);
    showModal;
    free;
  end;
  initgrid();
end;

procedure TfrmDyePCMachineList.tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
//  if ADOQuerymain.IsEmpty then
//    exit;

//  frmMachTaskList := TfrmMachTaskList.create(self);
//  with frmMachTaskList do
//  begin
//    fBegTime := Self.CDS_Main.FieldByName('time').AsString;
//    fMCName := Self.Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
//    fText := self.CDS_Main.FieldByName(fMCName).AsString;
//    showModal;
//    free;
//  end;
//  initgrid();
end;

procedure TfrmDyePCMachineList.tv1mcNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
//  if ADOQuerymain.IsEmpty then
//    exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.add('select * from PCAboutTime where time = ' + QuotedStr(Trim(CDS_Main.FieldByName('begtime').AsString)));
    Open;
  end;

  frmMachTaskList := TfrmMachTaskList.create(self);
  with frmMachTaskList do
  begin
    fBegTime := Self.CDS_Main.FieldByName('Begtime').AsString;
    fMCName := Self.MCName.Text;
    fText := self.ADOQueryCmd.FieldByName(fMCName).AsString;
    showModal;
    free;
  end;
  initgrid();
end;

//procedure TfrmDyePCMachineList.tv1CustomDrawCell(Sender: TcxCustomGridTableView;
//  ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
//  var ADone: Boolean);
//begin
//  inherited;
//  ACanvas.Font.Size := 7;
//end;

procedure TfrmDyePCMachineList.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  end;
end;

procedure TfrmDyePCMachineList.TbAddClick(Sender: TObject);
var
  mTime: TDateTime;
begin
  if CDS_Main.IsEmpty then
  begin

    mTime := Trunc(SGetServerDateTime(ADOQueryTemp) * 144 + 1) / 144;
  end
  else
  begin
    // 移动到上一条记录获取BegTime
//    CDS_Main.Prior;
    CDS_Main.Last;
    mTime := CDS_Main.FieldByName('EndTime').AsDateTime;
//    CDS_Main.Next;  // 移回当前位置
  end;

  with CDS_Main do
  begin
    Append;
    FieldByName('BegTime').Value := mTime;
    Post;  // 保存记录
  end;
end;

procedure TfrmDyePCMachineList.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmDyePCMachineList.TbDelClick(Sender: TObject);
begin
  if (CDS_Main.FieldByName('PCID').Value = null) or (CDS_Main.FieldByName('PCID').Value = '') then
  begin
    CDS_Main.Delete;
  end
  else
  begin
    Application.MessageBox('已排产工序不能删除！','提示信息',MB_OK);
  end;


end;

procedure TfrmDyePCMachineList.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(self.Caption + tv1.Name, Tv1, '排产信息');
  canshu1 := self.fParameters1;
//  BegDate.DateTime := strToDateTime(FormatDateTime('yyyy-MM-dd 00:00:00', SGetServerDate(ADOQueryTemp))) - 1;
//  EndDate.DateTime := strToDateTime(FormatDateTime('yyyy-MM-dd 00:00:00', SGetServerDate(ADOQueryTemp))) + 1;
  var ServerDate: TDateTime := SGetServerDate(ADOQueryTemp);

  BegDate.Date := Trunc(ServerDate) - 1;  // 前一天
  EndDate.Date := Trunc(ServerDate) + 1;  // 后一天
  BegTime.Time := EncodeTime(0, 0, 0, 0);   // 早上8点
  endTime.Time := EncodeTime(0, 0, 0, 0);  // 晚上6点

  MCNO.Text := '长车清洗机';
  MCName.Text := 'CCQX1';
  initgrid();
  KeyPreview := True;
end;

procedure TfrmDyePCMachineList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
//  SelExportData(tv1,ADOQueryMain,self.Caption);
  TcxGridToExcel(self.Caption, DBGrid1);
end;

procedure TfrmDyePCMachineList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_Main);
    SInitCDSData(ADOQueryMain, CDS_Main);
  end;
end;

procedure TfrmDyePCMachineList.pcidChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmDyePCMachineList.PRTColorChange(Sender: TObject);
begin
  TBFind.Click;
end;

end.

