unit OptionFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, XPMenu;

type
  TOptionForm = class(TForm)
    pcOptions: TPageControl;
    tsCommon: TTabSheet;
    btnOk: TButton;
    btnCancel: TButton;
    lblCommon: TLabel;
    pnlUserInfo: TPanel;
    xpm: TXPMenu;
    cbAutoSave: TCheckBox;
    lblSaveInterval: TLabel;
    cbbSaveIntervall: TComboBox;
    lblMinutes: TLabel;
    gbOverRange: TGroupBox;
    lblTitleOverRange: TLabel;
    rbLedger: TRadioButton;
    rbDeal: TRadioButton;
    rbBoth: TRadioButton;
    GroupBox1: TGroupBox;
    cbExcelWithMis: TCheckBox;
    Label1: TLabel;
    edtOverRangePercent: TEdit;
    Label2: TLabel;
    procedure cbAutoSaveClick(Sender: TObject);
    procedure rbLedgerClick(Sender: TObject);
    procedure edtOverRangePercentKeyPress(Sender: TObject; var Key: Char);
    procedure edtOverRangePercentExit(Sender: TObject);
    procedure edtOverRangePercentKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    procedure SetSaveIntervalEnable(AEnable: Boolean);
    function GetOverRangeType: Integer;
    procedure SetOverRangeType(const Value: Integer);
  public
    procedure Load;
    procedure Save;

    property OverRangeType: Integer read GetOverRangeType write SetOverRangeType;
  end;

procedure ModifiedOptions;

implementation

{$R *.dfm}

uses
  Globals, ConfigDoc;

procedure ModifiedOptions;
var
  OptionForm: TOptionForm;
begin
  OptionForm := TOptionForm.Create(nil);
  try
    OptionForm.Load;
    OptionForm.ClientHeight := 338;
    OptionForm.ClientWidth := 479;
    if OptionForm.ShowModal = mrOk then
      OptionForm.Save;
  finally
    OptionForm.Free;
  end;
end;

{ TOptionForm }

procedure TOptionForm.Load;
begin
  with SupportManager.ConfigInfo do
  begin
    cbAutoSave.Checked := AutoSave;
    SetSaveIntervalEnable(cbAutoSave.Checked);
    cbbSaveIntervall.Text := IntToStr(AutoSaveInterval);
    Self.OverRangeType := OverRangeType;
    edtOverRangePercent.Text := IntToStr(OverRangePercent);
    cbExcelWithMis.Checked := ExcelWithMis;
  end;
end;

procedure TOptionForm.Save;
begin
  with SupportManager.ConfigInfo do
  begin
    AutoSave := cbAutoSave.Checked;
    AutoSaveInterval := StrToIntDef(cbbSaveIntervall.Text, 15);
    OverRangeType := Self.OverRangeType;
    OverRangePercent := StrToIntDef(edtOverRangePercent.Text, 100);
    ExcelWithMis := cbExcelWithMis.Checked;
  end;
end;

procedure TOptionForm.cbAutoSaveClick(Sender: TObject);
begin
  SetSaveIntervalEnable(cbAutoSave.Checked);
end;

procedure TOptionForm.SetSaveIntervalEnable(AEnable: Boolean);
begin
  lblSaveInterval.Enabled := AEnable;
  cbbSaveIntervall.Enabled := AEnable;
  lblMinutes.Enabled := AEnable;
end;

function TOptionForm.GetOverRangeType: Integer;
begin
  if rbLedger.Checked then
    Result := rbLedger.Tag
  else if rbDeal.Checked then
    Result := rbDeal.Tag
  else if rbBoth.Checked then
    Result := rbBoth.Tag;
end;

procedure TOptionForm.SetOverRangeType(const Value: Integer);
begin
  rbLedger.Checked := rbLedger.Tag = Value;
  rbDeal.Checked := rbDeal.Tag = Value;
  rbBoth.Checked := rbBoth.Tag = Value;
end;

procedure TOptionForm.rbLedgerClick(Sender: TObject);
begin
  TRadioButton(Sender).Checked := True;
end;

procedure TOptionForm.edtOverRangePercentKeyPress(Sender: TObject;
  var Key: Char);
begin
  if not (Key in ['0'..'9', #8, #13]) then
    Key := #0;
end;

procedure TOptionForm.edtOverRangePercentExit(Sender: TObject);
var
  fPer: Double;
begin
  fPer := StrToIntDef(TEdit(Sender).text, 0);
  if (fPer < 50) then
    TEdit(Sender).text := '50'
  else if (fPer > 100) then
    TEdit(Sender).text := '100';
end;

procedure TOptionForm.edtOverRangePercentKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);                          
var
  fPer: Double;
begin
  if (Key = 13) then
  begin
    fPer := StrToIntDef(TEdit(Sender).text, 0);
    if (fPer < 50) then
      TEdit(Sender).text := '50'
    else if (fPer > 100) then
      TEdit(Sender).text := '100';
  end;
end;

end.
