const
  ListCount=6;

var
  SkinRes:TxspSkinRes;
  http:TxsHttp;
  lstResult,lstHint:TStrings;
  lblDescs;
  PageIndex,PageCount:integer;//当前页，当前总页数
  isSearchCheCi:boolean; //当前查询为站站查询还是车次查询
  sStartAdd,sEndAdd,sTrainNo:string;
//设置显示位置
procedure ShowDetail(value:boolean);
const
  POSITION=294;
var
  iPos:integer;//组件的偏移设置
begin
  if value then ipos:=POSITION
    else ipos:=0;
  if (imgbg.left=ipos) then exit;
  BeginUpdate;
  imgDetail.visible:=value;
  lblTitle.visible:=value;
  for i:=0 to ListCount-1 do
    lblDescs[i].visible:=value;
  imgbg.left:=ipos;
  lblCaption.left:=ipos+11;
  lblNo.left:=ipos+68;
  lbl1.left:=ipos+18;
  edt1.left:=ipos+18;
  lbl2.left:=ipos+18;
  edt2.left:=ipos+18;
  if value then
    imgtab.left:=imgtab.left+ipos
  else
    imgtab.left:=imgtab.left-POSITION;
  btn.left:=ipos+52;
  if value then
    Widget.Width:=Widget.Width+ipos
  else
    Widget.Width:=Widget.Width-POSITION;
  EndUpdateAll;  
  if value then
  begin
    Widget.Left:=Widget.Left-ipos;
  end
  else
  begin
    Widget.Left:=Widget.Left+POSITION;
  end
end;

//读取当前Widget的INI设置
procedure InitIni;
begin

end;

//--- Widget移入边栏事件，当Widget被移到边栏上时会触发此事件 ---
procedure OnWidgetDock;
begin
  BeginUpdate;
  Widget.Width:=138;
  EndUpdateall;
end;

//--- Widget移出边栏事件，当Widget被移出边栏时会触发此事件 ---
procedure OnWidgetUnDock;
begin
  BeginUpdate;
  Widget.Width:=148;
  EndUpdate;
end;

//--- Widget初始化事件，当打开Widget时会触发此事件 ---
procedure OnWidgetInit;
begin
  http:=nil;
  isSearchCheCi:=false;
  PageIndex:=1;
  PageCount:=1;
  lblDescs:=[lblDesc0,lblDesc1,lblDesc2,lblDesc3,lblDesc4,lblDesc5];
  BeginUpdate;
  SkinRes:=TxspSkinRes.create(Widget);
  SkinRes.EditFileName:=WidgetPath+'images\btn.png';
  SkinRes.EditGlyphCount:=2;
  SkinRes.ButtonFileName:=WidgetPath+'images\btn.png';
  edt1.SkinRes:=SkinRes;
  edt2.SkinRes:=SkinRes;
  btn.SkinRes:=SkinRes;
  lstResult:=TStringList.create;
  lstHint:=TStringList.create;

  InitIni;
  lblCaption.OnClick:='lblClick';
  lblNo.OnClick:='lblClick';
  EndUpdate;
  for i:=0 to ListCount-1 do
  begin
    lblDescs[i].tag:=i;
    lblDescs[i].HotFontStyle:=',,#FEBD06';
    lblDescs[i].Cursor:=crHandPoint;
    lblDescs[i].OnClick:='lblDescClick';
  end;
  edt1.OnEnter:='EditEnter';
  edt2.OnEnter:='EditEnter';
end;

//--- Widget关闭事件，当关闭Widget时会触发此事件 ---
procedure OnWidgetClose;
begin
  lstResult.free;
  lstHint.free;
  SkinRes.free;
  SkinRes:=nil;
end;

//--- Widget失去焦点事件，当Widget窗口失去焦点时触发此事件 ---
procedure OnWidgetDeactivate;
begin
  ShowDetail(false);
end;

//居中格式化字符串
function GetCenterText(value:string; len:integer):boolean;
begin
  i:=length(value);
  if i>len then
    result:=copy(value,1,len)
  else
    result:=value;
  i:=length(result);
  if i<len then
  begin
    j:=len-i;
    for i:=1 to j do
    begin
      result:=' '+result+' ';
    end;
  end;
end;

//显示页面
procedure ShowPage(index:integer);
var
  ibase:integer;
begin
  BeginUpdate;
  try
    if (index<1) or (index>PageCount) then exit;
    PageIndex:=index;
    imgPrev.Enabled:=PageIndex>1;
    imgNext.Enabled:=PageIndex<PageCount;
    lblPage.caption:=format('%d / %d',[PageIndex,PageCount]);
    
    if lstResult.count>0 then
    begin
      if isSearchCheCi then
        lblTitle.caption:='站次   站名       到达       开车       运行时间     里程'
      else
        lblTitle.caption:='车次    出发站    开车    到达站     到达       用时';
      ibase:=(PageIndex-1)*ListCount;
      for i:=0 to ListCount-1 do
      begin
        if ibase+i<lstResult.count then
        begin
          lblDescs[i].caption:=lstResult.ValueFromIndex[ibase+i];
          lblDescs[i].hint:=StrToStrings(lstHint.strings[ibase+i],#1);
        end
        else
        begin
          lblDescs[i].caption:='';
          lblDescs[i].hint:='';
        end;
      end;
      for i:=0 to ListCount-1 do
      begin
        if isSearchCheCi then
          lblDescs[i].Cursor:=crDefault
        else
          lblDescs[i].Cursor:=crHandPoint;
      end;
      
      ShowDetail(true);
    end;
  finally
    EndUpdate;
  end;
end;

procedure PrevPage;
begin
  ShowPage(PageIndex-1);
end;

procedure NextPage;
begin
  ShowPage(PageIndex+1);
end;

//解析站站网页
procedure ParseHtml(value:string);
var
  lst:TStrings;
  i,j,k:integer;
  s,s1,
  sNo,sBegin,sBeginTime,sEnd,sEndTime,sTime,sLen,sPrice1,sPrice2,sPrice3,sPrice4:string;
begin
  lst:=TStringList.create;
  try
    lst.text:=value;
    //showtrayhint('','save html');
    //lst.savetofile(WidgetPath+'data.htm');
    //lst.loadfromfile(WidgetPath+'data.htm');
    if lst.count<=0 then exit;
    lstResult.clear;
    lstHint.clear;
    for i:=0 to lst.count-1 do
    begin
      if pos('到达时间',lst.strings[i])>0 then
        break;
    end;
    if i+3<lst.count then//查到数据
      for j:=i+3 to lst.count-1 do
      begin
        //  showmsg(inttostr(j)+':'+lst.strings[j]);
        if pos('resultc.asp?txtCheCi=',lst.strings[j])>0 then
        begin//找到一条记录
          sNo:=StrBetween(lst.strings[j],'resultc.asp?txtCheCi=','">');
          sBegin:=StrBetween(lst.strings[j+1],'.html">','</a>');
          sBeginTime:=StrBetween(lst.strings[j+2],'"center">','</td>');
          sEnd:=StrBetween(lst.strings[j+3],'.html">','</a>');
          sEndTime:=StrBetween(lst.strings[j+5],'"center">','</td>');
          sTime:=StrBetween(lst.strings[j+6],'"center">','</td>');
          sLen:=StrBetween(lst.strings[j+7],'"center">','</td>');
          sPrice1:=StrBetween(lst.strings[j+8],'"center">','</td>');
          sPrice2:=StrBetween(lst.strings[j+9],'"center">','</td>');
          sPrice3:=StrBetween(lst.strings[j+10],'"center">','</td>');
          sPrice4:=StrBetween(lst.strings[j+11],'"center">','</td>');
          //提示
          s:='车　　次：'+sNo+#13#10;
          s:=s+'出发站　：'+sBegin+#13#10;
          s:=s+'开车时间：'+sBeginTime+#13#10;
          s:=s+'到达站　：'+sEnd+#13#10;
          s:=s+'到达时间：'+sEndTime+#13#10;
          s:=s+'用　　时：'+sTime+#13#10;
          s:=s+'里　　程：'+sLen+' 公里'+#13#10;
          s:=s+'票价(硬座)：'+sPrice1+' 元'+#13#10;
          if sPrice2<>'0' then
            s:=s+'票价(软座)：'+sPrice2+' 元'+#13#10;
          s:=s+'票价(硬卧)：'+sPrice3+' 元'+#13#10;
          s:=s+'票价(软卧)：'+sPrice4+' 元';
          lstHint.add(StringsToStr(s,#1));

          //列表
          sNo:=GetCenterText(sNo,4);
          sBegin:=GetCenterText(sBegin,8);
          sBeginTime:=GetCenterText(sBeginTime,5);
          sEnd:=GetCenterText(sEnd,8);
          sEndTime:=GetCenterText(sEndTime,5);
          sTime:=GetCenterText(sTime,10);
          //s:=sNo+format('=%s %8s %5s %8s %5s  %10s',[sNo,sBegin,sBeginTime,sEnd,sEndTime,sTime]);
          s:=sNo+format('=%s %s %s %s %s  %s',[sNo,sBegin,sBeginTime,sEnd,sEndTime,sTime]);
          lstResult.add(s);
        end;
      end;
      PageCount:=lstResult.count div ListCount;
      if lstResult.count mod ListCount>0 then PageCount:=PageCount+1;
      if lstResult.count<=0 then
        showtrayhint('提示','没有查询到可用的数据！');
      else
        ShowPage(1);
  finally
    lst.free;
  end;
end;

//解析车次网页
procedure ParseHtmlCheCi(value:string);
var
  lst:TStrings;
  i,j,k:integer;
  s,s1,
  sNo,sStation,sToTime,sStartTime,sTime,sLen,sPrice1,sPrice2,sPrice3,sPrice4:string;
begin
  lst:=TStringList.create;
  try
    lst.text:=value;
    //lst.savetofile(WidgetPath+'data1.htm');
    //lst.loadfromfile(WidgetPath+'data1.htm');
    if lst.count<=0 then exit;
    lstResult.clear;
    lstHint.clear;
    for i:=0 to lst.count-1 do
    begin
      if pos('全程票价参考',lst.strings[i])>0 then
        break;
    end;
    if i+2<lst.count then//查到数据
      for j:=i+2 to lst.count-1 do
      begin
        if pos('<td align="center"><a href="http://www.huochepiao.com/search/chezhan',lst.strings[j])>0 then
        begin//站次    站名    到达    开车     运行时间       里程
          sNo:=IntToStr(lstResult.count+1);
          sStation  :=StrBetween(lst.strings[j],'.html">','</a>');
          sToTime   :=StrBetween(lst.strings[j+1],'<td align="center">','</td>');
          sStartTime   :=StrBetween(lst.strings[j+2],'<td align="center">','</td>');
          sTime:=StrBetween(lst.strings[j+3],'<td align="center">','</td>');
          sLen :=StrBetween(lst.strings[j+4],'<td align="center">','</td>');

          sPrice1:=StrBetween(lst.strings[j+5],'<td align="center">','</td>');
          if pos('a href',lst.strings[j+6])>0 then
            sPrice2:='0'
          else
            sPrice2:=StrAfter('"center">',lst.strings[j+6]);
          sPrice3:=StrBetween(lst.strings[j+8],'<td align="center">','</td>');
          sPrice4:=StrBetween(lst.strings[j+9],'bgcolor="#ffffff">','</td>');
          
          //提示
          s:='站　　次：'+sNo+#13#10;
          s:=s+'站　　名：'+sStation+#13#10;
          s:=s+'到达时间：'+sToTime+#13#10;
          s:=s+'开车时间：'+sStartTime+#13#10;
          s:=s+'运行时间：'+sTime+#13#10;
          s:=s+'里　　程：'+sLen+' 公里'+#13#10;
          s:=s+'票价(硬座)：'+sPrice1+' 元'+#13#10;
          if sPrice2<>'0' then
            s:=s+'票价(软座)：'+sPrice2+' 元'+#13#10;
          s:=s+'票价(硬卧)：'+sPrice3+' 元'+#13#10;
          s:=s+'票价(软卧)：'+sPrice4+' 元';
          lstHint.add(StringsToStr(s,#1));

          //列表
          sNo:=GetCenterText(sNo,2);
          sStation:=GetCenterText(sStation,8);
          sToTime:=GetCenterText(sToTime,6);
          sStartTime:=GetCenterText(sStartTime,6);
          sTime:=GetCenterText(sTime,10);
          sLen:=GetCenterText(sLen,4);
          s:=sNo+format('=%s  %s %s   %s   %s  %s',[sNo,sStation,sToTime,sStartTime,sTime,sLen]);
          lstResult.add(s);
        end;
      end;
      PageCount:=lstResult.count div ListCount;
      if lstResult.count mod ListCount>0 then PageCount:=PageCount+1;
      if lstResult.count<=0 then
        showtrayhint('提示','没有查询到可用的数据！');
      else
        ShowPage(1);
  finally
    lst.free;
  end;
end;

procedure HttpDone(url,result,isString);
begin
  if http<>nil then
  begin
  	http.free;
  	http:=nil;
  end;
  if result>'' then
    ParseHtml(result)
  else
    showtrayhint('提示','没有查询到可用的数据！');
  btn.caption:='查询';
end;

procedure HttpDoneCheCi(url,result,isString);
begin
  if http<>nil then
  begin
  	http.free;
  	http:=nil;
  end;
  if result>'' then
    ParseHtmlCheCi(result);
  btn.caption:='查询';
end;

procedure HttpGetError(url, isString);
begin
  if http<>nil then
  begin
  	http.free;
  	http:=nil;
  end;
  showtrayhint('警告','获取信息失败！');
  btn.caption:='查询';
end;

//站站查询
procedure Search1(sBegin,sEnd:string);
begin
  if http<>nil then
  begin
    showmsg('正在查询中，请稍候再试！');
    exit;
  end;
  http:=TxsHttp.Create(nil);
  http.OnHTTPGetFailed:='HttpGetError';
  http.OnHttpGetDone:='HttpDone';

  aurl:=format('http://www.huochepiao.com/search/chaxun/result.asp?txtChuFa=%s&txtDaoDa=%s',[sBegin,sEnd]);
  //showtrayhint('url',aurl);
  btn.caption:='查询中...';
  http.GetHTTP(aurl,true,'');
end;

//车次查询
procedure SearchCheCi(sNo:string);
begin
  if http<>nil then
  begin
    showmsg('正在查询中，请稍候再试！');
    exit;
  end;
  http:=TxsHttp.Create(nil);
  http.OnHTTPGetFailed:='HttpGetError';
  http.OnHttpGetDone:='HttpDoneCheCi';

  aurl:='http://www.huochepiao.com/search/chaxun/resultc.asp?txtCheCi='+trim(sNo);
  //showmsg('begin get url:'+aurl);
  btn.caption:='查询中...';
  http.GetHTTP(aurl,true,'');
end;

//点击搜索按钮触发的事件
procedure Search;
var
  s1,s2:string;
begin
  //ParseHtmlCheCi('');
  //ParseHtml('');
  //exit;
  if isSearchCheCi then
  begin//车次查询
    s1:=trim(edt1.text);
    if s1='' then
    begin
      showmsg('请输入车次！');
      exit;
    end;
    SearchCheCi(s1);
  end
  else
  begin//车站查询
    s1:=trim(edt1.text);
    if s1='' then
    begin
      showmsg('请输入起始站！');
      exit;
    end;
    s2:=trim(edt2.text);
    if s2='' then
    begin
      showmsg('请输入终点站！');
      exit;
    end;
    Search1(s1,s2);
  end;
end;

//站站查询或车次查询的Label点击事件
procedure lblClick(sender);
begin
  BeginUpdate;
  imgtab.left:=sender.left-4;
  isSearchCheCi:=sender.tag=1;
  if isSearchCheCi then
	begin
    lbl1.caption:='车次：';
		sStartAdd:=edt1.text;								{保存当前的起始站字符}
		sEndAdd:=edt2.text;                 {保存当前的终点站字符}
    edt1.text:=sTrainNo;  							{获取上次保存的车次}
  end
	else
  begin
    sTrainNo:=edt1.text;                {保存当前车次字符}
    lbl1.caption:='起始站：';
    lbl2.caption:='终点站：';
		edt1.text:=sStartAdd;								{获取上次保存的起始，终点信息}
		edt2.text:=sEndAdd;
  end
  lbl2.visible:=not isSearchCheCi;
  edt2.visible:=not isSearchCheCi;
  EndUpdateAll;
end;

//点击列表Label
procedure lblDescClick(sender);
var
  i:integer;
begin
  if isSearchCheCi then exit;

  i:=(PageIndex-1)*ListCount+sender.tag;
  if i<lstResult.count then
    SearchCheCi(lstResult.Names[i]);
end;

procedure EditEnter(sender);
begin
  Search;
end;
