unit U_DyeCardRoll;

interface

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

type
  TfrmDyeCardRoll = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    GPM_1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    OrderNo: TEdit;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    PM_1: TPopupMenu;
    N2: TMenuItem;
    Label12: TLabel;
    MPRTKZ: TEdit;
    DS_2: TDataSource;
    cxTabControl1: TcxTabControl;
    ADOQueryImage: TADOQuery;
    DSImage: TDataSource;
    IdFTP1: TIdFTP;
    cxGridPopupMenu2: TcxGridPopupMenu;
    ToolButton2: TToolButton;
    N1: TMenuItem;
    CDS_2: TClientDataSet;
    Tv1OrderNo: TcxGridDBColumn;
    Tv1QtyOrder: TcxGridDBColumn;
    Tv1PCID: TcxGridDBColumn;
    Tv1KaZu: TcxGridDBColumn;
    Tv1KaZuPiece: TcxGridDBColumn;
    Tv1KaZuQty: TcxGridDBColumn;
    Tv1RollNumber: TcxGridDBColumn;
    Tv1C_Name: TcxGridDBColumn;
    Tv1F_Name: TcxGridDBColumn;
    Tv1C_PlanQty: TcxGridDBColumn;
    Tv1F_description: TcxGridDBColumn;
    Tv1C_description: TcxGridDBColumn;
    Tv1PCType: TcxGridDBColumn;
    Tv1PCTime: TcxGridDBColumn;
    Tv1C_Width: TcxGridDBColumn;
    Tv1C_GramWeight: TcxGridDBColumn;
    Tv1DTMPSlist: TcxGridDBColumn;
    Tv1CINote: TcxGridDBColumn;
    Tv1C_Color: TcxGridDBColumn;
    Tv1finishDescription: TcxGridDBColumn;
    Tv1GlideNo: TcxGridDBColumn;
    Tv1GlideName: TcxGridDBColumn;
    Tv1RollMeter: TcxGridDBColumn;
    Label2: TLabel;
    KaZu: TEdit;
    Label4: TLabel;
    C_Name: TEdit;
    Tv1Column1: TcxGridDBColumn;
    Label6: TLabel;
    GlideNo: TEdit;
    TbEditMeter: TToolButton;
    ToolButton3: TToolButton;
    N3: TMenuItem;
    N4: TMenuItem;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);

    procedure TBExportClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure Tv1StylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure v1DeliveryDateCustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Label3DblClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure TbEditMeterClick(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure N4Click(Sender: TObject);

  private
    DQdate: TDateTime;
    procedure InitGrid();
    function DelData(): Boolean;
    procedure SetStatus();
    procedure InitImage(fwbid: string);
    { Private declarations }
  public
    FFInt, FCloth: Integer;
    canshu1, canshu2, FWorkshop: string;
    fFlileFlag: string;
    { Public declarations }
  end;

var
  frmDyeCardRoll: TfrmDyeCardRoll;

implementation

uses
  U_DataLink, U_RTFun, U_LabelPrint;
{$R *.dfm}

procedure TfrmDyeCardRoll.InitImage(fwbid: string);
begin
  ADOQueryImage.close;
  if fwbid = '' then
    exit;
  with ADOQueryImage do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    sql.Add('where WBID=' + quotedstr(trim(fwbid)));
    open;
  end;
end;

procedure TfrmDyeCardRoll.SetStatus();
begin

  if Trim(canshu1) = '管理' then
  begin

    case cxTabControl1.TabIndex of
      0:
        begin

        end;
      1:
        begin

        end;
      2:
        begin

        end;
      3:
        begin

        end;
    end;
  end;

  if Trim(canshu1) = '录入' then
  begin

    case cxTabControl1.TabIndex of
      0:
        begin

        end;
      1:
        begin
      //  tnochk.Visible:=true;
        end;
      2:
        begin
        end;
      3:
        begin
    //    TBNOQX.Visible:=true;
        end;
    end;
  end;
end;

procedure TfrmDyeCardRoll.Label3DblClick(Sender: TObject);
var
  I: Integer;
  Column: TcxGridDBColumn;
  FieldNames: TStringList;
begin
  FieldNames := TStringList.Create;
  try
    for I := 0 to Tv1.ColumnCount - 1 do
    begin
      Column := Tv1.Columns[I];
      FieldNames.Add(Column.Caption + ':' + Column.DataBinding.FieldName);
    end;
    ShowMessage(FieldNames.Text);
  finally
    FieldNames.Free;
  end;
end;

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

procedure TfrmDyeCardRoll.FormCreate(Sender: TObject);
begin
  inherited;
  cxgrid1.Align := alClient;
  DQdate := SGetServerDate(ADOQueryTemp);
  canshu1 := trim(Self.fParameters1);
  canshu2 := trim(Self.fParameters2);
  FWorkshop := trim(Self.fParameters3);
end;

procedure TfrmDyeCardRoll.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 TfrmDyeCardRoll.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmDyeCardRoll.TbEditMeterClick(Sender: TObject);
var
  InputStr: string;
  MID: string;
begin
  if not Order_Main.IsEmpty then
    MID := Trim(Order_Main.FieldByName('ID').AsString)
  else
    MID := '';
// 最后尝试定位回原记录



  // 弹出输入框
  InputStr := InputBox('编辑米数', '请输入新的米数:', '');

  if InputStr <> '' then  // 如果用户输入了内容
  begin
    // 处理输入的值
    with ADOQueryCmd do
    begin
      Close;
      sql.clear;
      sql.Add(' update Dye_ClothCardRoll set RollMeter = ' + QuotedStr(Trim(InputStr)));
      sql.add(' where ID = ' + Order_Main.FieldByName('ID').AsString);
      ExecSQL;
    end;
  end;
  InitGrid();
  if MID <> '' then
    Order_Main.Locate('ID', MID, []);
end;

procedure TfrmDyeCardRoll.InitGrid();
begin

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      Filtered := False;
      sql.Clear;
      sql.Add('select  A.* ');
      sql.Add('from V_Dye_Card_Roll A');
      sql.Add(' where 1=1');

//      case cxTabControl1.TabIndex of
//        0:
//          begin
//            sql.Add('  and  isnull(A.status,''0'') in(''已计划'') ');
//          end;
//        1:
//          begin
//            sql.Add('  and  isnull(A.status,''0'')in(''已下达'' )');
//          end;
//        2:
//          begin
//            sql.Add('  and  isnull(A.status,''0'')in(''已结算'' )');
//          end;
//        3:
//          begin
//            sql.Add('  and  isnull(A.status,''0'')in(''已取消'' )');
//          end;
//        4:
//          begin
//            sql.Add('  and  isnull(A.status,''0'')in(''已结算'' )');
//          end;
//      end;
      sql.Add(' and fillTime>=''' + FormatDateTime('yyyy-MM-dd', begdate.DateTime) + ''' ');
      sql.Add(' and fillTime<''' + FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1) + ''' ');
////      sql.Add(' and A.Workshop=''' + trim(FWorkshop) + ''' ');
//      if Trim(canshu1) = '录入' then
//        sql.Add('and Filler=' + QuotedStr(Trim(DName)));
      sql.Add(' order By KaZu ,GlideNo');
////      ShowMessage(sql.Text);
///
///
///
      Open;
    end;
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, Order_Main);
    SInitCDSData(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

function TfrmDyeCardRoll.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Dye_Plan_Main where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
      sql.Add('delete Dye_Plan_Sub where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('指示单删除')));
      sql.Add(',' + quotedstr(trim('指示单号：' + trim(Order_Main.FieldByName('OrderNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      ExecSQL;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result := False;
    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
end;

procedure TfrmDyeCardRoll.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
end;

procedure TfrmDyeCardRoll.TBPrintClick(Sender: TObject);
var
  WSql: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  WSql := QuotedStr(Trim(Order_Main.fieldbyname('MainId').AsString));

  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'DyePlanPrint';
      FFiltration1 := WSql;
      if ShowModal = 1 then
      begin
//        Self.InitGrid();
      end;
    end;
  finally
    frmLabelPrint.Free;
  end;
end;

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

procedure TfrmDyeCardRoll.OrderNoChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, Order_Main);
  SInitCDSData(ADOQueryMain, Order_Main);
end;

procedure TfrmDyeCardRoll.FormShow(Sender: TObject);
begin
  inherited;
  fFlileFlag := UserDataFlag + 'HX';
  canshu1 := self.fParameters1;
  canshu2 := self.fParameters2;
  FWorkshop := self.fParameters3;
  BegDate.DateTime := SGetServerDate(ADOQueryTemp) - 15;
  EndDate.DateTime := SGetServerDate(ADOQueryTemp);
  ReadCxGrid(self.Caption + 'TV1', Tv1, '大卷桩管理');

  SetStatus();
  InitGrid();
  KeyPreview := True;  // 确保表单可以预览按键
end;

procedure TfrmDyeCardRoll.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmDyeCardRoll.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmDyeCardRoll.Tv1StylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
var
  id, id10: Integer;
begin

end;

procedure TfrmDyeCardRoll.v1DeliveryDateCustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
 { Id:=TV1.GetColumnByFieldName('DeliveryDate').Index;//;-TV1.GroupedItemCount;
  Id10:=TV1.GetColumnByFieldName('SubStatus').Index;
  if Id<0 then Exit;
  if AViewInfo.GridRecord.Values[Id10]='完成' then Exit;
  if AViewInfo.GridRecord.Values[Id]-SGetServerDate(ADOQueryTemp)>=4 then Exit;
  if ((AViewInfo.GridRecord.Values[id]-SGetServerDate(ADOQueryTemp))>=0) and ((AViewInfo.GridRecord.Values[id]-SGetServerDate(ADOQueryTemp))<4) then
    ACanvas.Brush.Color:=clYellow
  else
  if (AViewInfo.GridRecord.Values[id])-(SGetServerDate(ADOQueryTemp)<0) then
  begin
    ACanvas.Brush.Color:=clRed;
  end;
  begin
    ACanvas.Brush.Color:=clRed;
  end else
  if AViewInfo.GridRecord.Values[Id]='Purple' then
  begin
    ACanvas.Brush.Color:=clPurple;
  end else
  if AViewInfo.GridRecord.Values[Id]='Olive' then
  begin
    ACanvas.Brush.Color:=clOlive;
  end else
  if AViewInfo.GridRecord.Values[Id]='Teal' then
  begin
    ACanvas.Brush.Color:=clTeal;
  end else
  if AViewInfo.GridRecord.Values[Id]='Background' then
  begin
    ACanvas.Brush.Color:=clBackground;
  end;   }
end;

procedure TfrmDyeCardRoll.N1Click(Sender: TObject);
begin
  SelOKNoFiler(Tv1, false);
end;

procedure TfrmDyeCardRoll.N2Click(Sender: TObject);
begin

  SelOKNoFiler(Tv1, true);
end;

procedure TfrmDyeCardRoll.N4Click(Sender: TObject);
var
  InputValues: array[0..1] of string;
  StartRow, EndRow, i: Integer;
  topIndex: Integer;
begin
  topIndex := tv1.Controller.TopRowIndex;

  // 初始化为空字符串
  InputValues[0] := '';
  InputValues[1] := '';

  if InputQuery('选择行范围', ['起始行:', '结束行:'], InputValues) then
  begin
    // 检查是否为空
    if InputValues[0] = '' then
    begin
      ShowMessage('请输入起始行');
      exit;
    end;

    if InputValues[1] = '' then
    begin
      ShowMessage('请输入结束行');
      exit;
    end;

    // 转换输入值
    StartRow := StrToIntDef(InputValues[0], 0);
    EndRow := StrToIntDef(InputValues[1], 0);

    // 验证范围
    if StartRow <= 0 then
    begin
      ShowMessage('起始行必须大于0');
      exit;
    end;

    if EndRow < StartRow then
    begin
      ShowMessage('结束行不能小于起始行');
      exit;
    end;

//    if EndRow > Order_Main.RecordCount then
//    begin
//      ShowMessage(Format('结束行不能超过总行数(%d)', [Order_Main.RecordCount]));
//      Exit;
//    end;

    // 循环设置指定范围内的行
    Order_Main.DisableControls;
    try
      Order_Main.First;
      for i := 1 to Order_Main.RecordCount do
      begin
        if (i >= StartRow) and (i <= EndRow) then
        begin
          Order_Main.Edit;
          Order_Main.FieldByName('ssel').Value := True;
          Order_Main.post;
        end;

        Order_Main.Next;
      end;
    finally
      Order_Main.EnableControls;
    end;

  end;

  tv1.Controller.TopRowIndex := topIndex;
end;

procedure TfrmDyeCardRoll.ToolButton2Click(Sender: TObject);
begin
  WriteCxGrid(self.Caption + 'TV1', Tv1, '大卷桩管理');
end;

procedure TfrmDyeCardRoll.ToolButton3Click(Sender: TObject);
begin
  if Order_Main.Locate('ssel', true, []) = False then
  begin
    Application.MessageBox('请选择数据', '提示信息', MB_OK);
    Exit;
  end;

  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    with Order_Main do
    begin
      DisableControls;
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean then
        begin
          try
            ADOQueryCmd.Connection.BeginTrans;

            with ADOQueryCmd do
            begin
              Close;
              Sql.Clear;
              Sql.Add('delete from Dye_ClothCardRoll where ID = ' + quotedstr(Trim(Order_Main.fieldbyname('ID').AsString)));
              ExecSQL;
            end;

            ADOQueryCmd.Connection.CommitTrans;
          except
            ADOQueryCmd.Connection.RollbackTrans;
            raise Exception.Create(PChar(Exception(ExceptObject).Message));
          end;
        end;

        Next;
      end;
      EnableControls;
    end;

    TBRafresh.Click;
  except
    Order_Main.EnableControls;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

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

end.

