unit findbookinf;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, DBGrids, DB, ADODB, Mask, DBCtrls, ExtCtrls,
  DBClient;

type
  TfrmFindBook = class(TForm)
    DBGrid1: TDBGrid;
    btnExit: TButton;
    ComboBox1: TComboBox;
    ComboBox2: TComboBox;
    btnSeek: TButton;
    GroupBox1: TGroupBox;
    Edit1: TEdit;
    btnBoroow: TButton;
    DataSource1: TDataSource;
    ClientDataSet1: TClientDataSet;
    ClientDataSet2: TClientDataSet;
    DBGrid2: TDBGrid;
    ComboBox3: TComboBox;
    Button1: TButton;
    DataSource2: TDataSource;
    Panel1: TPanel;
    procedure btnExitClick(Sender: TObject);
    procedure ComboBox2Change(Sender: TObject);
    procedure btnSeekClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnBoroowClick(Sender: TObject);
    procedure ComboBox3Change(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmFindBook: TfrmFindBook;
  arrInf:TStringList;
implementation

uses service;

{$R *.dfm}

procedure TfrmFindBook.btnExitClick(Sender: TObject);
begin
  close;
end;

procedure TfrmFindBook.ComboBox2Change(Sender: TObject);
begin
  if combobox2.itemindex=0 then  begin
    combobox1.Visible:=false;
    edit1.Visible:=false;
  end
  else  begin
    combobox1.Visible:=true;
    edit1.Visible:=true;
  end;
end;

procedure TfrmFindBook.btnSeekClick(Sender: TObject);
var
  strSql:string;
begin
  if combobox2.ItemIndex>0 then  begin
     try
        case  combobox2.ItemIndex-1 of
          0,1,2,3,4,10,11:
          if combobox1.Text=' like ' then
             strSql:='select * from booksinf where '+arrInf[combobox2.ItemIndex-1]+combobox1.Text+quotedstr('%'+edit1.Text+'%')
          else
            strSql:='select * from booksinf where '+arrInf[combobox2.ItemIndex-1]+combobox1.Text+quotedstr(edit1.Text);
          5:
             strSql:='select * from booksinf where '+arrInf[combobox2.ItemIndex-1]+combobox1.Text+quotedstr(edit1.Text);
          6,7,8,9:
             strSql:='select * from booksinf where '+arrInf[combobox2.ItemIndex-1]+combobox1.Text+edit1.Text;
         end;
         with  clientdataset1 do   begin
           close;
           commandtext:=strsql;
           Open;
         end;
     except
        showmessage('SQL语句错误：'+strSql);
        with  clientdataset1 do begin
          close;
          commandtext:=strsql;
          Open;
        end;
     end
    end
  else  begin
      with  clientdataset1 do  begin
        close;
        commandtext:='select * from booksinf';
        Open;
      end;
    end;
end;

procedure TfrmFindBook.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  frmservice.Visible:=true;
  clientdataset1.Close;
  clientdataset2.Close;
end;

procedure TfrmFindBook.btnBoroowClick(Sender: TObject);
var
  strBookid,strBookname:string;
begin
    with frmFindBook.clientdataset1 do begin
      strBookid:=fieldvalues['book_id'];
      strBookname:=fieldValues['book_name'];
      if FieldValues['book_num']-FieldValues['booking_num']-FieldValues['borrow_num']>0 then begin
        with frmFindBook.clientdataset2 do begin
          append;                  //添加新的借书记录
          fieldvalues['libcard_id']:= strLibCard_id;
          fieldvalues['reader_name']:= strName;
          fieldvalues['book_id']:= strBookid;
          fieldvalues['book_Name']:= strBookName;
          fieldvalues['borrow_date']:= date();
          fieldValues['status']:= '预约';
          applyupdates(-1);     //将修改提交到服务端
          refresh;  //刷新客户端数据集
        end;
      refresh;  //刷新客户端数据集
      end
      else
        showmessage('该图书已被借出或预约，请选择其他书籍。');
   end;
end;

procedure TfrmFindBook.ComboBox3Change(Sender: TObject);
begin
  with clientdataset2 do  begin
    case combobox3.ItemIndex of
      0:
        if active then   begin
          filtered:=false;     //取消过滤
        end;
      1:
        begin
          filtered:=false;
          filter:='status='+quotedstr('预约');   //设置过滤条件
          filtered:=true;            //执行过滤
        end;
      2:
        begin
          filtered:=false;
          filter:='status='+quotedstr('借书');
          filtered:=true;
        end;
      3:
        begin
          filtered:=false;
          filter:='status='+quotedstr('还书');
          filtered:=true;
        end;
    end;
  end;
end;

procedure TfrmFindBook.Button1Click(Sender: TObject);
var
  ret:integer;
begin
  ret := MessageBox(handle, '确实要删除该记录？',
               '删除记录', MB_YESNO+MB_ICONQUESTION	);
  if ret=IDYES then
    with clientdataset2 do  begin
      if FieldValues['status']='预约' then  begin
          delete;
          ApplyUpdates(-1);
        end
      else
        showmessage('没有权限删除该记录.');
    end;
end;

procedure TfrmFindBook.FormShow(Sender: TObject);
begin
  arrInf:=TStringList.Create;
  arrInf.commatext:=('book_id,book_name,book_type,author,publisher,pub_date,borrow_num,booking_num,book_num,price,locality,mem');
  with  clientdataset1 do begin
      close;
      commandtext:='select * from booksinf';
      Open;
  end;
  with clientdataset2 do begin
      close;
      commandtext:='select * from records where libcard_id='+quotedstr(strLibCard_id)+' order by records.no asc';
      open;
  end;
end;

end.
