unit ProjectPropertiesFrm;

interface

uses
  ProjectFme,
  ProjectData, ProjectProperty, StaffDm,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, JimPages, ComCtrls, ToolWin, MScrollBox,
  XPMenu, JimCombos, Mask, ImgList, ZJGrid, ZjGridDBA;

type
  TProjectPropertiesForm = class(TForm)
    btnOk: TButton;
    btnCancel: TButton;
    xpm: TXPMenu;
    pnlTop: TPanel;
    tbTop: TToolBar;
    tobtnBase: TToolButton;
    Images: TImageList;
    pnlBlank: TPanel;
    tobtnStaff: TToolButton;
    tobtnView: TToolButton;
    jpsProperties: TJimPages;
    jpsPropertiesBase: TJimPage;
    jpsPropertiesStaff: TJimPage;
    jpsPropertiesView: TJimPage;
    pnlBase: TPanel;
    tbContents: TToolBar;
    tobtnDealInfo: TToolButton;
    tobtnTechParameters: TToolButton;
    tobtnDealPayment: TToolButton;
    msbBase: TMScrollBox;
    lblDealInfo: TLabel;
    lblBuilder: TLabel;
    lblConstructor: TLabel;
    lblSupervisor: TLabel;
    lblTechParameters: TLabel;
    lblLoadLevel: TLabel;
    lblDealPayTitle: TLabel;
    pnlDealInfoBar: TPanel;
    leProjectName: TLabeledEdit;
    leDealName: TLabeledEdit;
    leDealIndex: TLabeledEdit;
    leBName: TLabeledEdit;
    leBLegal: TLabeledEdit;
    leBDate: TLabeledEdit;
    leCName: TLabeledEdit;
    leCLegal: TLabeledEdit;
    leCDate: TLabeledEdit;
    leSName: TLabeledEdit;
    leSLegal: TLabeledEdit;
    leSDate: TLabeledEdit;
    pnlTechParametersBar: TPanel;
    leStartPeg: TLabeledEdit;
    leEndPeg: TLabeledEdit;
    leLength: TLabeledEdit;
    leLaneCount: TLabeledEdit;
    cbLoadLevel: TComboBox;
    pnlDealPayBar: TPanel;
    leContractPrice: TLabeledEdit;
    leStartedSubsist: TLabeledEdit;
    leMaterialSubsist: TLabeledEdit;
    pnlViewBar: TPanel;
    leView: TLabel;
    zgStaff: TZJGrid;
    zaStaff: TZjGridDBA;
    jcbPhase: TJimComboBox;
    tobtnSafe: TToolButton;
    lblSafeTitel: TLabel;
    pnlSafeBar: TPanel;
    lblUnlockPassword: TLabel;
    imgValid: TImageList;
    pnlUnlockPassword: TPanel;
    pnlOrgPW: TPanel;
    leUnlockPassword: TLabeledEdit;
    imgOrg: TImage;
    pnlNewPW: TPanel;
    leNewUnlockPasswordAgain: TLabeledEdit;
    leNewUnLockPassword: TLabeledEdit;
    imgNew: TImage;
    imgNewAgain: TImage;
    lblPasswordHint: TLabel;
    leDealType: TLabeledEdit;
    pnlAllView: TPanel;
    pnlAlias: TPanel;
    pnlPriceChange: TPanel;
    cbShowPriceChange: TCheckBox;
    pnlBGL: TPanel;
    cbShowBGLCode: TCheckBox;
    pnlDesignQuantity: TPanel;
    cbShowDesignQuantity: TCheckBox;
    cbShowAlias: TCheckBox;
    tobtnCalcParameters: TToolButton;
    pnlCalcParametersBar: TPanel;
    lblCalcParametersTitel: TLabel;
    leQuantityDigit: TLabeledEdit;
    Label1: TLabel;
    leTotalPriceDigit: TLabeledEdit;
    udQuantity: TUpDown;
    udTotalPrice: TUpDown;
    gbPM_Digit: TGroupBox;
    lePM_PriceDigit: TLabeledEdit;
    udPM_PriceDigit: TUpDown;
    procedure btnOkClick(Sender: TObject);
    procedure leContractPriceClick(Sender: TObject);
    procedure msbBaseMouseWheel(Sender: TObject; Shift: TShiftState;
      WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
    procedure msbBaseMouseWheelDown(Sender: TObject;
      Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
    procedure msbBaseMouseWheelUp(Sender: TObject; Shift: TShiftState;
      MousePos: TPoint; var Handled: Boolean);
    procedure msbBaseVScroll(Sender: TObject);
    procedure tobtnDealInfoClick(Sender: TObject);
    procedure tobtnBaseClick(Sender: TObject);
    procedure jcbPhaseChanged(Sender: TObject);
    procedure zgStaffCellGetColor(Sender: TObject; ACoord: TPoint;
      var AColor: TColor);
    procedure zgStaffCellGetFont(Sender: TObject; ACoord: TPoint;
      AFont: TFont);
    procedure zgStaffCellCanEdit(Sender: TObject; const ACoord: TPoint;
      var Allow: Boolean);
    procedure leUnlockPasswordKeyPress(Sender: TObject; var Key: Char);
    procedure leNewUnlockPasswordAgainExit(Sender: TObject);
    procedure leNewUnLockPasswordExit(Sender: TObject);
    procedure leUnlockPasswordExit(Sender: TObject);
  private
    FProjectData: TProjectData;
    FStaffData: TStaffData;
    FProjProperties: TProjProperties;
    FProjectFrame: TProjectFrame;

    procedure InitAllPage;
    procedure InitDealInfo;
    procedure InitDealPay;
    procedure InitTechParameters;
    procedure InitProjectView;
    procedure InitStaffRela;
    procedure InitSafe;
    procedure InitCalcParameters;

    procedure SaveDealInfo;
    procedure SaveDealPay;
    procedure SaveTechParameters;
    procedure SaveSafe;
    procedure SaveCalcParameters;
    procedure SaveProjectView;

    procedure ResetContentsCheck; overload;
    procedure ResetContentsCheck(ATag: Integer); overload;
    procedure ResetVertScrollBarPosition(ATag: Integer);

    procedure CheckValidKey(var AKey: Char);

    procedure CheckOrgPassword;
    procedure CheckNewPassword;
    procedure CheckNewPasswordAgain;

    procedure SetProjectData(const Value: TProjectData);
  public
    procedure Save;

    property ProjectData: TProjectData read FProjectData write SetProjectData;
    property ProjProperties: TProjProperties read FProjProperties;
    property ProjectFrame: TProjectFrame read FProjectFrame write FprojectFrame;
  end;

procedure ModifyProjectProperties(ProjectFrame: TProjectFrame);

implementation

uses
  PhaseProperty, DB, UtilMethods, ZhAPI;

{$R *.dfm}

procedure ModifyProjectProperties(ProjectFrame: TProjectFrame);
var
  PropertiesForm: TProjectPropertiesForm;
begin
  PropertiesForm := TProjectPropertiesForm.Create(nil);
  try
    PropertiesForm.ProjectFrame := ProjectFrame;
    PropertiesForm.ProjectData := ProjectFrame.ProjectData;
    PropertiesForm.zgStaff.OnCellCanEdit := PropertiesForm.zgStaffCellCanEdit;
    if PropertiesForm.ShowModal = mrOk then
      PropertiesForm.Save;
  finally
    PropertiesForm.Free;
  end;
end;

{ TProjectPropertiesForm }

procedure TProjectPropertiesForm.InitAllPage;
begin
  InitDealInfo;
  InitTechParameters;
  InitDealPay;
  InitSafe;
  InitCalcParameters;
  InitStaffRela;
  InitProjectView;
end;

procedure TProjectPropertiesForm.InitProjectView;
begin
  cbShowPriceChange.Checked := FProjProperties.ShowPriceChange;
  cbShowBGLCode.Checked := FProjProperties.ShowBGLCode;
  cbShowDesignQuantity.Checked := FProjProperties.ShowDesignQuantity;
  cbShowAlias.Checked := FProjProperties.ShowAlias;
end;

procedure TProjectPropertiesForm.btnOkClick(Sender: TObject);
begin
  ModalResult := mrOk;
end;

procedure TProjectPropertiesForm.Save;
begin
  SaveDealPay;
  SaveDealInfo;
  SaveTechParameters;
  SaveSafe;
  SaveCalcParameters;
  SaveProjectView;
end;

procedure TProjectPropertiesForm.InitDealPay;
begin
  leContractPrice.Text := FloatToStr(FProjProperties.ContractPrice);
  leContractPrice.ReadOnly := FProjProperties.PhaseCount > 0;
  SetWindowLong(leContractPrice.Handle, GWL_STYLE, GetWindowLong(leContractPrice.Handle, GWL_STYLE) or ES_RIGHT);

  leStartedSubsist.Text := FloatToStr(FProjProperties.StartedSubsisit);
  leStartedSubsist.ReadOnly := FProjProperties.PhaseCount > 0;
  SetWindowLong(leStartedSubsist.Handle, GWL_STYLE, GetWindowLong(leStartedSubsist.Handle, GWL_STYLE) or ES_RIGHT);

  leMaterialSubsist.Text := FloatToStr(FProjProperties.MaterialSubsist);
  leMaterialSubsist.ReadOnly := FProjProperties.PhaseCount > 0;
  SetWindowLong(leMaterialSubsist.Handle, GWL_STYLE, GetWindowLong(leMaterialSubsist.Handle, GWL_STYLE) or ES_RIGHT);
end;

procedure TProjectPropertiesForm.SaveDealPay;
begin
  if FProjProperties.PhaseCount > 0 then Exit;
  FProjProperties.ContractPrice := StrToFloatDef(leContractPrice.Text, 0);
  FProjProperties.StartedSubsisit := StrToFloatDef(leStartedSubsist.Text, 0);
  FProjProperties.MaterialSubsist := StrToFloatDef(leMaterialSubsist.Text, 0);
  FProjectData.DealPaymentData.CalcStarted_RangePrice;
end;

procedure TProjectPropertiesForm.leContractPriceClick(Sender: TObject);
begin
  if not TLabeledEdit(Sender).ReadOnly then
    TLabeledEdit(Sender).SelectAll;
end;

procedure TProjectPropertiesForm.msbBaseMouseWheel(Sender: TObject;
  Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint;
  var Handled: Boolean);
begin
  ResetContentsCheck;
end;

procedure TProjectPropertiesForm.msbBaseMouseWheelDown(Sender: TObject;
  Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
begin
  SendMessage(msbBase.Handle,WM_VSCROLL,SB_LINEDOWN, 0);
end;

procedure TProjectPropertiesForm.msbBaseMouseWheelUp(Sender: TObject;
  Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
begin
  SendMessage(msbBase.Handle,WM_VSCROLL,SB_LINEUP, 0);
end;

procedure TProjectPropertiesForm.ResetContentsCheck(ATag: Integer);
begin
  tobtnDealInfo.Down := tobtnDealInfo.Tag = ATag;
  tobtnTechParameters.Down := tobtnTechParameters.Tag = ATag;
  tobtnDealPayment.Down := tobtnDealPayment.Tag = ATag;
  tobtnSafe.Down := tobtnSafe.Tag = ATag;
  tobtnCalcParameters.Down := tobtnCalcParameters.Tag = ATag;
end;

procedure TProjectPropertiesForm.msbBaseVScroll(Sender: TObject);
begin
  ResetContentsCheck;
end;

procedure TProjectPropertiesForm.ResetContentsCheck;
begin
  if msbBase.VertScrollBar.Position < 355 then
    ResetContentsCheck(tobtnDealInfo.Tag)
  else if msbBase.VertScrollBar.Position < 515 then
    ResetContentsCheck(tobtnTechParameters.Tag)
  else if msbBase.VertScrollBar.Position < 628 then
    ResetContentsCheck(tobtnDealPayment.Tag)
  else if msbBase.VertScrollBar.Position < 758 then
    ResetContentsCheck(tobtnSafe.Tag)
  else
    ResetContentsCheck(tobtnCalcParameters.Tag);
end;

procedure TProjectPropertiesForm.tobtnDealInfoClick(Sender: TObject);
begin
  ResetContentsCheck(TToolButton(Sender).Tag);
  ResetVertScrollBarPosition(TToolButton(Sender).Tag);
end;

procedure TProjectPropertiesForm.ResetVertScrollBarPosition(ATag: Integer);
begin
  if ATag = tobtnDealInfo.Tag then
    msbBase.VertScrollBar.Position := 0
  else if ATag = tobtnTechParameters.Tag then
    msbBase.VertScrollBar.Position := 355
  else if ATag = tobtnDealPayment.Tag then
    msbBase.VertScrollBar.Position := 515
  else if ATag = tobtnSafe.Tag then
    msbBase.VertScrollBar.Position := 628
  else if ATag = tobtnCalcParameters.Tag then
    msbBase.VertScrollBar.Position := 758;
end;

procedure TProjectPropertiesForm.InitDealInfo;
begin
  leProjectName.Text := FProjProperties.ProjectName;
  leDealName.Text := FProjProperties.DealName;
  leDealIndex.Text := FProjProperties.DealIndex;
  leDealType.Text := FProjProperties.DealType;
  leBName.Text := FProjProperties.BName;
  leBLegal.Text := FProjProperties.BLegal;
  leBDate.Text := FProjProperties.BDate;
  leCName.Text := FProjProperties.CName;
  leCLegal.Text := FProjProperties.CLegal;
  leCDate.Text := FProjProperties.CDate;
  leSName.Text := FProjProperties.SName;
  leSLegal.Text := FProjProperties.SLegal;
  leSDate.Text := FProjProperties.SDate;
end;

procedure TProjectPropertiesForm.InitTechParameters;
begin
  cbLoadLevel.ItemIndex := FProjProperties.LoadLevel;
  leStartPeg.Text := FProjProperties.StartPeg;
  leEndPeg.Text := FProjProperties.EndPeg;
  leLength.Text := FloatToStr(FProjProperties.LoadLength);
  leLaneCount.Text := IntToStr(FProjProperties.LaneCount);
end;

procedure TProjectPropertiesForm.SaveDealInfo;
begin
  FProjProperties.ProjectName := leProjectName.Text;
  FProjProperties.DealName := leDealName.Text;
  FProjProperties.DealIndex := leDealIndex.Text;
  FProjProperties.DealType := leDealType.Text;
  FProjProperties.BName := leBName.Text;
  FProjProperties.BLegal := leBLegal.Text;
  FProjProperties.BDate := leBDate.Text;
  FProjProperties.CName := leCName.Text;
  FProjProperties.CLegal := leCLegal.Text;
  FProjProperties.CDate := leCDate.Text;
  FProjProperties.SName := leSName.Text;
  FProjProperties.SLegal := leSLegal.Text;
  FProjProperties.SDate := leSDate.Text;
end;

procedure TProjectPropertiesForm.SaveTechParameters;
begin
  FProjProperties.LoadLevel := cbLoadLevel.ItemIndex;
  FProjProperties.StartPeg := leStartPeg.Text;
  FProjProperties.EndPeg := leEndPeg.Text;
  FProjProperties.LoadLength := StrToFloatDef(leLength.Text, 0);
  FProjProperties.LaneCount := StrToIntDef(leLaneCount.Text, 0);
end;

procedure TProjectPropertiesForm.tobtnBaseClick(Sender: TObject);

  procedure ResetTopButtonDown(ATag: Integer);
  begin
    tobtnBase.Down := tobtnBase.Tag = ATag;
    tobtnStaff.Down := tobtnStaff.Tag = ATag;
    tobtnView.Down := tobtnView.Tag = ATag;
  end;

begin
  ResetTopButtonDown(TToolButton(Sender).Tag);
  jpsProperties.ActivePageIndex := TToolButton(Sender).Tag;
  jcbPhase.Visible := TToolButton(Sender).Tag = tobtnStaff.Tag;
end;

procedure TProjectPropertiesForm.InitStaffRela;
var
  I: Integer;
begin
  tobtnStaff.Enabled := FProjectData.PhaseIndex > 0;
  zaStaff.DataSet := FProjectData.StaffData.cdsStaffView;
  jcbPhase.Items.Clear;
  for I := 1 to FProjectData.ProjProperties.PhaseCount do
    jcbPhase.Items.Add(Format('第 %d 期', [I]));
  jcbPhase.ItemIndex := FProjectData.ProjProperties.PhaseCount - 1;
end;

procedure TProjectPropertiesForm.SetProjectData(const Value: TProjectData);
begin
  FProjectData := Value;
  FStaffData := FProjectData.StaffData;
  FProjProperties := FProjectData.ProjProperties;
  InitAllPage;
end;

procedure TProjectPropertiesForm.jcbPhaseChanged(Sender: TObject);
begin
  FStaffData.ResetViewFilter(jcbPhase.ItemIndex + 1);
end;

procedure TProjectPropertiesForm.zgStaffCellGetColor(Sender: TObject;
  ACoord: TPoint; var AColor: TColor);
begin
  if FStaffData.cdsStaff.FindKey([jcbPhase.ItemIndex + 1, ACoord.Y - zgStaff.FixedRowCount]) then
    if FStaffData.cdsStaffEnable.AsBoolean then
      AColor := clWindow
    else
      AColor := $DCDCDC;
end;

procedure TProjectPropertiesForm.zgStaffCellGetFont(Sender: TObject;
  ACoord: TPoint; AFont: TFont);
begin
  if FStaffData.cdsStaff.FindKey([jcbPhase.ItemIndex + 1, ACoord.Y - zgStaff.FixedRowCount]) then
    if not FStaffData.cdsStaffLocked.AsBoolean and FStaffData.cdsStaffEnable.AsBoolean then
      AFont.Color := clWindowText
    else
      AFont.Color := $A9A9A9;
end;

procedure TProjectPropertiesForm.zgStaffCellCanEdit(Sender: TObject;
  const ACoord: TPoint; var Allow: Boolean);
begin
  if ACoord.X < 2 then Allow := False;
  if FStaffData.cdsStaff.FindKey([jcbPhase.ItemIndex + 1, ACoord.Y - zgStaff.FixedRowCount]) then
    Allow := Allow and not FStaffData.cdsStaffLocked.AsBoolean and FStaffData.cdsStaffEnable.AsBoolean;
end;

procedure TProjectPropertiesForm.InitSafe;
var
  bHasPassword: Boolean;
begin
  bHasPassword := FProjProperties.UnlockInfoPassword <> '';
  pnlOrgPW.Visible := bHasPassword;
  leNewUnLockPassword.ReadOnly := bHasPassword;
end;

procedure TProjectPropertiesForm.SaveSafe;
begin
  if leUnlockPassword.Text = FProjProperties.UnlockInfoPassword then
    if leNewUnLockPassword.Text = leNewUnlockPasswordAgain.Text then
      FProjProperties.UnlockInfoPassword := leNewUnLockPassword.Text
    else ErrorMessage('两次密码输入不一致，密码修改不成功！')
  else if leUnlockPassword.Text <> '' then
    ErrorMessage('原密码输入错误，密码修改不成功！');
end;

procedure TProjectPropertiesForm.leUnlockPasswordKeyPress(Sender: TObject;
  var Key: Char);
begin
  CheckValidKey(Key);
end;

procedure TProjectPropertiesForm.CheckValidKey(var AKey: Char);
begin
  if not (AKey in ['A'..'Z', 'a'..'z', '0'..'9', #8]) then
    AKey := #0;
end;

procedure TProjectPropertiesForm.leNewUnlockPasswordAgainExit(
  Sender: TObject);
begin
  CheckNewPasswordAgain;
end;

procedure TProjectPropertiesForm.leNewUnLockPasswordExit(Sender: TObject);
begin
  CheckNewPassword;
  if leNewUnlockPasswordAgain.Text <> '' then
    CheckNewPasswordAgain;
end;

procedure TProjectPropertiesForm.CheckNewPassword;
var
  bValid: Boolean;
begin
  bValid := CheckValidPassword(leNewUnLockPassword.Text);
  imgNew.Visible := not bValid;
  if not bValid then
    lblPasswordHint.Caption := leNewUnLockPassword.Hint;
  lblPasswordHint.Visible := not bValid;
  leNewUnlockPasswordAgain.ReadOnly := not bValid;
end;

procedure TProjectPropertiesForm.CheckNewPasswordAgain;
var
  bValid: Boolean;
begin
  bValid := not(CheckValidPassword(leNewUnLockPassword.Text)
      and (leNewUnlockPasswordAgain.Text <> leNewUnLockPassword.Text));
  imgNewAgain.Visible := not bValid;
  if not bValid then
    lblPasswordHint.Caption := leNewUnlockPasswordAgain.Hint;
  lblPasswordHint.Visible := not bValid;
end;

procedure TProjectPropertiesForm.leUnlockPasswordExit(Sender: TObject);
begin
  CheckOrgPassword;
end;

procedure TProjectPropertiesForm.CheckOrgPassword;
var
  bValid: Boolean;
begin
  bValid := CheckValidPassword(leUnlockPassword.Text);
  imgOrg.Visible := not bValid;
  if not bValid then
    lblPasswordHint.Caption := leUnlockPassword.Hint;
  lblPasswordHint.Visible := not bValid;
  leNewUnLockPassword.ReadOnly := not bValid;
end;

procedure TProjectPropertiesForm.SaveCalcParameters;
begin
  if FProjProperties.PhaseCount = 0 then
  begin
    FProjProperties.QuantityDigit := udQuantity.Position;
    FProjProperties.TotalPriceDigit := udTotalPrice.Position;
    FProjectData.ResetFloatDigitView;
  end;
  if (FProjProperties.PhaseCount = 1) and not FProjectData.PriceMarginReadOnly then
    FProjProperties.DecimalManager.PriceMargin.Price.Digit := udPM_PriceDigit.Position;
end;

procedure TProjectPropertiesForm.InitCalcParameters;
begin
  udQuantity.Position := FProjProperties.QuantityDigit;
  leQuantityDigit.ReadOnly := FProjProperties.PhaseCount > 0;
  udQuantity.Enabled := FProjProperties.PhaseCount = 0;
  udTotalPrice.Position := FProjProperties.TotalPriceDigit;
  leTotalPriceDigit.ReadOnly := FProjProperties.PhaseCount > 0;
  udTotalPrice.Enabled := FProjProperties.PhaseCount = 0;

  udPM_PriceDigit.Position := FProjProperties.DecimalManager.PriceMargin.Price.Digit;
  lePM_PriceDigit.ReadOnly := FProjectData.PriceMarginReadOnly and (FProjProperties.PhaseCount > 1);
  udPM_PriceDigit.Enabled := not lePM_PriceDigit.ReadOnly;
end;

procedure TProjectPropertiesForm.SaveProjectView;
begin
  FProjProperties.ShowPriceChange := cbShowPriceChange.Checked;
  FProjProperties.ShowBGLCode := cbShowBGLCode.Checked;
  FProjProperties.ShowDesignQuantity := cbShowDesignQuantity.Checked;
  FProjProperties.ShowAlias := cbShowAlias.Checked;
  // 刷新界面显示
  FProjectFrame.RefreshColumnDisplay;
end;

end.
