unit EntInfo;

interface

uses
  XMLTag, iq,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Dockable, StdCtrls, TntStdCtrls, ExtCtrls, RxGIF, ComCtrls;

type
  TEnterpriseInfo = record
    CID: widestring;
    Company: widestring;
    Service: widestring;
    Mode: widestring;
    Trade: widestring;
    Subtrade: widestring;
    Person: widestring;
    Tel: widestring;
    Fax: widestring;
    Province: widestring;
    City: widestring;
    Street: widestring;
    Postcode: widestring;
    url: widestring;
    Content: widestring;
    Domain: Widestring;
  end;

  TfrmEntInfo = class(TfrmDockable)
    Panel1: TPanel;
    Panel2: TPanel;
    btnModify: TButton;
    btnClose: TButton;
    btnRefresh: TButton;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    coboProvince: TTntComboBox;
    TabSheet2: TTabSheet;
    coboMode: TTntComboBox;
    coboTrade: TTntComboBox;
    coboSubtrade: TTntComboBox;
    lbledtCompany: TLabeledEdit;
    lbledtTel: TLabeledEdit;
    lbledtFax: TLabeledEdit;
    lbledtPerson: TLabeledEdit;
    lbledtCity: TLabeledEdit;
    lbledtStreet: TLabeledEdit;
    lbledtPostcode: TLabeledEdit;
    lbledtDomain: TLabeledEdit;
    Label1: TLabel;
    lbledtService: TLabeledEdit;
    lbledtURL: TLabeledEdit;
    TntMemContent: TTntMemo;
    Image1: TImage;
    Label7: TLabel;
    Bevel1: TBevel;
    Image2: TImage;
    Label2: TLabel;
    Bevel2: TBevel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    procedure btnRefreshClick(Sender: TObject);
    procedure btnModifyClick(Sender: TObject);
    procedure btnCloseClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure coboTradeChange(Sender: TObject);
    procedure coboChange(Sender: TObject);
  private
    { Private declarations }
    cur_iq: TJabberIQ;
    procedure RegCallback(event: string; tag: TXMLTag);
  public
    { Public declarations }
    HYFL: TXMLTag;
    EI: TEnterpriseInfo;
    procedure SendRefresh;
    procedure ItemsCallback(event: string; tag: TXMLTag);
    procedure DispyInfo(info: TEnterpriseInfo);
  end;

var
  frmEntInfo: TfrmEntInfo;

procedure showentinfo(info: TEnterpriseInfo);
procedure Regentinfo;
procedure showMyentinfo;

resourcestring
  sEntServer = 'enterprise.anyq.net';

implementation

uses Session, JabberConst, RiserWindow, Unicode, XMLParser, Jabber1;

{$R *.dfm}

procedure showMyentinfo;
begin
  with TfrmEntInfo.create(Application) do
  try
    EI.CID := MainSession.BareJid;
    caption := '本企业信息';
    btnRefreshClick(btnRefresh);
    ShowModal;
  finally
    Free;
  end;
end;

procedure Regentinfo;
begin
  with TfrmEntInfo.create(Application) do
  try
    EI.CID := MainSession.BareJid;
    btnModify.Visible := false;
    btnRefresh.Visible := false;
    btnClose.Caption := '确定';
    btnClose.OnClick := btnModifyClick;
    caption := '请注册企业信息';
    ShowModal;
  finally
    Free;
  end;
end;

procedure showentinfo(info: TEnterpriseInfo);
var
  i: Integer;
begin
  with TfrmEntInfo.create(Application) do
  try
    btnModify.Visible := false;
    lbledtCompany.ReadOnly := true;
    lbledtService.ReadOnly := true;
    lbledtPerson.ReadOnly := true;
    lbledtTel.ReadOnly := true;
    lbledtFax.ReadOnly := true;
    lbledtCity.ReadOnly := true;
    lbledtStreet.ReadOnly := true;
    lbledtPostcode.ReadOnly := true;
    lbledtURL.ReadOnly := true;
    TntMemContent.ReadOnly := true;

    coboProvince.Enabled := false;
    coboMode.Enabled := false;
    coboTrade.Enabled := false;
    coboSubtrade.Enabled := false;

    DispyInfo(info);

    EI := info;
    ShowModal;
  finally
    Free;
  end;
end;

procedure TfrmEntInfo.btnRefreshClick(Sender: TObject);
begin
  SendRefresh;
end;

procedure TfrmEntInfo.ItemsCallback(event: string; tag: TXMLTag);
var
  cidx, i, c: integer;
  items, cols: TXMLTagList;
  cur, CurItem: TXMLTag;
  CurEI: TEnterpriseInfo;
  XState: Boolean;
//    col: TListColumn;
//    jid_fld: Widestring;
//    clist: TWideStringList;
  tmps, tmpV: Widestring;

  procedure GetValue;
  begin
    CurItem := cur.GetFirstTag('company');
    if CurItem <> nil then
      CurEI.Company := CurItem.Data;
    CurItem := cur.GetFirstTag('service');
    if CurItem <> nil then
      CurEI.Service := CurItem.Data;
    CurItem := cur.GetFirstTag('mode');
    if CurItem <> nil then
      CurEI.mode := CurItem.Data;
    CurItem := cur.GetFirstTag('trade');
    if CurItem <> nil then
      CurEI.trade := CurItem.Data;
    CurItem := cur.GetFirstTag('subtrade');
    if CurItem <> nil then
      CurEI.subtrade := CurItem.Data;
    CurItem := cur.GetFirstTag('person');
    if CurItem <> nil then
      CurEI.person := CurItem.Data;
    CurItem := cur.GetFirstTag('tel');
    if CurItem <> nil then
      CurEI.tel := CurItem.Data;
    CurItem := cur.GetFirstTag('fax');
    if CurItem <> nil then
      CurEI.fax := CurItem.Data;
    CurItem := cur.GetFirstTag('province');
    if CurItem <> nil then
      CurEI.province := CurItem.Data;
    CurItem := cur.GetFirstTag('city');
    if CurItem <> nil then
      CurEI.city := CurItem.Data;
    CurItem := cur.GetFirstTag('street');
    if CurItem <> nil then
      CurEI.street := CurItem.Data;
    CurItem := cur.GetFirstTag('postcode');
    if CurItem <> nil then
      CurEI.postcode := CurItem.Data;
    CurItem := cur.GetFirstTag('url');
    if CurItem <> nil then
      CurEI.url := CurItem.Data;
    CurItem := cur.GetFirstTag('content');
    if CurItem <> nil then
      CurEI.content := CurItem.Data;
    CurItem := cur.GetFirstTag('domain');
    if CurItem <> nil then
      CurEI.Domain := CurItem.Data;
  end;

  procedure GetXValue(s, v: widestring);
  begin
    if (s = 'jid') or (s = 'jid-single') then begin
      CurEI.CID := v;
      exit;
    end;
    if (s = 'company') then begin
      CurEI.Company := v;
      exit;
    end;

    if (s = 'service') then begin
      CurEI.Service := v;
      exit;
    end;
    if (s = 'mode') then begin
      CurEI.mode := v;
      exit;
    end;
    if (s = 'trade') then begin
      CurEI.trade := v;
      exit;
    end;
    if (s = 'subtrade') then begin
      CurEI.subtrade := v;
      exit;
    end;
    if (s = 'person') then begin
      CurEI.person := v;
      exit;
    end;
    if (s = 'tel') then begin
      CurEI.tel := v;
      exit;
    end;
    if (s = 'fax') then begin
      CurEI.fax := v;
      exit;
    end;
    if (s = 'province') then begin
      CurEI.province := v;
      exit;
    end;
    if (s = 'city') then begin
      CurEI.city := v;
      exit;
    end;
    if (s = 'street') then begin
      CurEI.street := v;
      exit;
    end;
    if (s = 'postcode') then begin
      CurEI.postcode := v;
      exit;
    end;
    if (s = 'url') then begin
      CurEI.url := v;
      exit;
    end;
    if (s = 'content') then begin
      CurEI.content := v;
      exit;
    end;
    if (s = 'domain') then begin
      CurEI.Domain := v;
      exit;
    end;
  end;
begin
  btnRefresh.Enabled := true;
  btnModify.Enabled := true;

  XState := false;
    // callback when we get our search results back
  cur_iq := nil;

  if (event <> 'xml') then begin
        // timeout
    ShowRiserWindow(nil, '查询超时', ico_info);
        //MessageBox(Handle,'查询超时', '错误', MB_OK OR MB_ICONERROR);
    exit;
  end
  else begin
        // tag
        {
        <iq from='users.jabber.org' id='jcl_6' to='pgm-foo@jabber.org/Exodus' type='result'>
        <query xmlns='jabber:iq:search'>
        <item jid='clever@jabber.org'>
            <nick>Clever</nick>
            <first>Hennie</first>
            <email>clever@jabber.com</email>
            <last/>
        </item></query></iq>
        }

        // get all the returned items
    items := nil;
    items := tag.QueryXPTags('/iq/query/item');

    if ((items = nil) or (items.Count = 0)) then
    begin
      items := tag.QueryXPTags('//x[@xmlns="jabber:x:data"]/item');
      XState := true;
    end;

    if ((items = nil) or (items.Count = 0)) then begin
      items.Free();
      ShowRiserWindow(nil, '没有找到符合条件的企业！', ico_info);
            //MessageBox(Handle,'没有找到符合条件的企业！', '信息', MB_OK OR MB_ICONINFORMATION);
      exit;
    end;


    for i := 0 to 0 do begin //items.count - 1 do begin
      cur := items[i];

      if not XState then begin
        CurEI.CID := cur.GetAttribute('jid');
        GetValue;
        EI := CurEI;
      end
      else begin // xitems
        cols := cur.QueryTags('field');
        for c := 0 to cols.count - 1 do begin
          tmps := cols[c].getAttribute('var');
          tmpv := cols[c].GetBasicText('value');
          GetXValue(tmps, tmpv);
        end;
        cols.Free();
        EI := CurEI;
      end;
    end;

    DispyInfo(EI);
    if (items <> nil) then Items.Free();

  end;
end;

procedure TfrmEntInfo.SendRefresh;
var
  i: integer;
  valid: boolean;
begin
  cur_iq := TJabberIQ.Create(MainSession, MainSession.generateID(), ItemsCallback, 30);
  with cur_iq do begin
    iqType := 'set';
    Namespace := XMLNS_SEARCH;
    toJid := sEntServer;

    qTag.AddBasicTag('cid', EI.CID);
  end;
  btnRefresh.Enabled := false;
  btnModify.Enabled := false;
  cur_iq.Send();
end;

procedure TfrmEntInfo.btnModifyClick(Sender: TObject);
begin
  if lbledtCompany.Text = '' then
  begin
    ShowRiserWindow(nil, '企业名称不能为空！', ico_info);
    //MessageBox(handle,'企业名称不能为空！','错误', MB_OK or MB_ICONERROR);
    exit;
  end;
  cur_iq := TJabberIQ.Create(MainSession, MainSession.generateID(), RegCallback, 30);
  with cur_iq do begin
    iqType := 'set';
    Namespace := XMLNS_REGISTER;
    toJid := sEntServer;

    qTag.AddBasicTag('cid', MainSession.BareJid);

    qTag.AddBasicTag('company', lbledtCompany.Text);
    qTag.AddBasicTag('service', lbledtService.Text);
    qTag.AddBasicTag('person', lbledtPerson.Text);
    qTag.AddBasicTag('tel', lbledtTel.Text);
    qTag.AddBasicTag('fax', lbledtFax.Text);
    qTag.AddBasicTag('city', lbledtCity.Text);
    qTag.AddBasicTag('street', lbledtStreet.Text);
    qTag.AddBasicTag('postcode', lbledtPostcode.Text);
    qTag.AddBasicTag('url', lbledtURL.Text);
    qTag.AddBasicTag('content', TntMemContent.Text);
    qTag.AddBasicTag('domain', MainSession.Server);

    if coboProvince.ItemIndex > 0 then
      qTag.AddBasicTag('province', coboProvince.Text);
    if coboMode.ItemIndex > 0 then
      qTag.AddBasicTag('mode', coboMode.Text);
    if coboTrade.ItemIndex > 0 then
      qTag.AddBasicTag('trade', coboTrade.Text);
    if coboSubtrade.ItemIndex > 0 then
      qTag.AddBasicTag('subtrade', coboSubtrade.Text);
  end;
  btnRefresh.Enabled := false;
  btnModify.Enabled := false;
  cur_iq.Send();
end;

procedure TfrmEntInfo.RegCallback(event: string; tag: TXMLTag);
begin
  btnRefresh.Enabled := true;
  btnModify.Enabled := true;
  cur_iq := nil;
  if ((event = 'xml') and (tag.getAttribute('type') = 'result')) then begin
      // normal result
    ShowRiserWindow(nil, '修改企业信息成功！', ico_info);
    //MessageBox(handle,'修改企业信息成功！','成功',MB_OK or MB_ICONINFORMATION);
  end
  else begin
      // some kind of error
    ShowRiserWindow(nil, '无法修改企业信息！', ico_info);
    //MessageBox(handle,'无法修改企业信息！','失败',MB_OK or MB_ICONERROR);
  end;
end;

procedure TfrmEntInfo.btnCloseClick(Sender: TObject);
begin
  inherited;
  close;
end;

procedure TfrmEntInfo.DispyInfo(info: TEnterpriseInfo);
begin
  lbledtCompany.Text := info.Company;
  lbledtService.Text := info.Service;
  lbledtPerson.Text := info.Person;
  lbledtTel.Text := info.Tel;
  lbledtFax.Text := info.Fax;
  lbledtCity.Text := info.City;
  lbledtStreet.Text := info.Street;
  lbledtPostcode.Text := info.Postcode;
  lbledtURL.Text := info.url;
  TntMemContent.Text := info.Content;
  if btnModify.Visible then
    lbledtDomain.Text := MainSession.Server
  else
    lbledtDomain.Text := info.Domain;

  coboProvince.ItemIndex := coboProvince.Items.IndexOf(info.Province);
  coboChange(coboProvince);
  coboMode.ItemIndex := coboMode.Items.IndexOf(info.Mode);
  coboChange(coboMode);
  coboTrade.ItemIndex := coboTrade.Items.IndexOf(info.Trade);
  coboTradeChange(coboTrade);
  coboSubtrade.ItemIndex := coboSubtrade.Items.IndexOf(info.Subtrade);
  coboChange(coboSubtrade);
end;

procedure TfrmEntInfo.FormCreate(Sender: TObject);
var
  Parser: TXMLTagParser;
  i: integer;
  lst: TXMLTagList;
begin
  inherited;

  HYFL := nil;
  Parser := TXMLTagParser.Create;
  Parser.ParseFile(ExtractFilePath(Application.ExeName) + 'hyfl.xml');
  if Parser.Count > 0 then
  begin
    HYFL := Parser.popTag;
    lst := HYFL.ChildTags;
    for i := 0 to lst.Count - 1 do
      coboTrade.Items.AddObject(lst[i].GetAttribute('name'), lst[i]);
    lst.Free;
  end;
  Parser.Free;
end;

procedure TfrmEntInfo.FormDestroy(Sender: TObject);
begin
  if HYFL <> nil then
    HYFL.Free;

  inherited;
end;

procedure TfrmEntInfo.coboTradeChange(Sender: TObject);
var
  lst: TXMLTagList;
  i: integer;
begin
  coboSubtrade.Items.Clear;
  coboSubtrade.Items.Add('请选择合适您的行业小类');
  if (coboTrade.ItemIndex > 0) and (coboTrade.Items.Objects[coboTrade.ItemIndex] <> nil) then
  begin
    lst := TXMLTag(coboTrade.Items.Objects[coboTrade.ItemIndex]).ChildTags;
    for i := 0 to lst.Count - 1 do
      coboSubtrade.Items.Add(lst[i].GetAttribute('name'));
  end;
  coboSubtrade.ItemIndex := 0;
  with TTntComboBox(sender) do
    if Enabled then
      if ItemIndex < 0 then
        ItemIndex := 0;
end;

procedure TfrmEntInfo.coboChange(Sender: TObject);
begin
  with TTntComboBox(sender) do
    if Enabled then
      if ItemIndex < 0 then
        ItemIndex := 0;
end;

end.
