<!--#include file="../Inc/Cls_Main.Asp"-->
<!--#include file="../Inc/Cms/Cms.Asp"-->
<%
Dim Action
'======================================================= 搜索需要的
Dim Keywords, KeyTag
Keywords = Trim(Easp.Get("key:s"))
KeyTag = Trim(Easp.Get("tag:s"))
'=======================================================
Select Case Action
    Case "pageload"
        Call PageLoad()
    Case Else
        Call PageLoad()
End Select
Set Cms = Nothing
Set Easp = Nothing

Sub PageLoad()
    Dim ChannelId, ArrayChannel, TabName, CnTemplateSearch
    ChannelId = Easp.Get("ChannelId:n:0")
    If ChannelId = 0 Then
        Easp.Alert "搜索值缺少频道ID"
    End If
    ' 读取频道信息
    ArrayChannel = GetCaCheChannelInfo("Channel", ChannelId, ChannelField)
    If Easp.IsN(ArrayChannel) Then
        Easp.Alert "不存在的频道"
    Else
        TabName = ArrayChannel(2)
        CnTemplateSearch = ArrayChannel(9)
    End If
    If Easp.IsN(CnTemplateSearch) Then
        Easp.Alert "频道搜索模板未设置……"
    End If
    ShowSearchHtml ChannelId, TabName, CnTemplateSearch
End Sub

Sub ShowSearchHtml(ByVal ChannelId, ByVal TabName, ByVal ChannelFile)
    Dim TempFilePath, ShowNum, OrderBy, ShowPage, CountNum, TagName
    CountNum = 0
    ShowPage = ""
    TagName = "SearchList"
    TempFilePath = CmsTemplates & CmsTempSkin & ChannelFile
    Easp.Use "fso"
    If Easp.Fso.IsFile(TempFilePath) Then
        Cms.Tpl.Path = CmsTemplates & CmsTempSkin
        Cms.Tpl.File = ChannelFile
        Cms.Fun.Init()
        '==========================================================================
        Dim strHtml, oSQLText, TempWhere, oRs
        Dim Blocks, block, html, i, ii, n, matchs
        Set Blocks = cms.tpl.getBlocks(TagName)
        For Each block In Blocks
            TempWhere = ""
            Dim PageMode, PageHtmlList, PageTipName, objDic, ClassId
            Set objDic = Server.CreateObject("Scripting.Dictionary")
            '=====================================================================
            ShowNum = CmsFun.ChkNumEric(Cms.Tpl.Attr(block.SubMatches(0), "num"))
            ShowNum = Easp.IIf(ShowNum = 0, 10, ShowNum)
            OrderBy = CmsFun.ChkNumEric(Cms.Tpl.Attr(block.SubMatches(0), "order"))
            '=====================================================================
            PageMode = Trim(Cms.Tpl.Attr(block.SubMatches(0), "pagemode"))
            PageTipName = Trim(Cms.Tpl.Attr(block.SubMatches(0), "pagetip"))
            If Easp.IsN(PageMode) Then
                PageHtmlList = "<span class=""disableds"">总数：{recordcount}</span><span class=""disableds"">总页数：{pagecount}</span>{first}{prev}{list}{next}{last}{jump}"
            Else
                PageHtmlList = Replace(Replace(Replace(PageMode & "", "|", """"), "[", "{"), "]", "}")
            End If
            If Not Easp.IsN(PageTipName) Then
                PageTipName = Split(PageTipName, "|")
                objDic("First") = PageTipName(0)
                objDic("Last") = PageTipName(1)
                objDic("Prev") = PageTipName(2)
                objDic("Next") = PageTipName(3)
            Else
                objDic("First") = "首　页"
                objDic("Last") = "尾　页"
                objDic("Prev") = "上一页"
                objDic("Next") = "下一页"
            End If
            '=====================================================================
            Easp.db.PageSize = ShowNum
            Easp.db.PageParam = "p"
            Easp.db.SetPager "default", PageHtmlList, Array("first:" & objDic("First"), "last:" & objDic("Last"), "prev:" & objDic("Prev"), "next:" & objDic("Next"), "list:*")
            '===================================================================== 搜索SQL
            If ClassId > 0 Then
                TempWhere = TempWhere & " And a.ClassId = "& ClassId &" "
            End If
            If Not Easp.IsN(Keywords) Then
                TempWhere = TempWhere & " And (a.Title Like '%"& Keywords &"%' Or a.Content Like '%"& Keywords &"%') "
            End If
            If Not Easp.IsN(KeyTag) Then
                Dim objRs
                Set objRs = Easp.db.GR(TablePrefix & "Tags:Name:1", " ChannelId = "& ChannelId &" And Tag = '"& KeyTag &"' ", "")
                If Easp.IsN(objRs) Then
                    TempWhere = TempWhere & " And ',' + a.Tag +',' Like '%," & KeyTag & ",%' "
                Else
                    KeyTag = objRs(0)
                    TempWhere = TempWhere & " And ',' + a.Tag +',' Like '%," & KeyTag & ",%' "
                End If
                Easp.C objRs
            End If
            '===================================================================== 组合SQL
            oSQLText = GetSearchSQL(ChannelId, TabName, TempWhere, OrderBy)
            Set oRs = Easp.db.GPR(1, oSQLText)
            '=====================================================================
            html = block.SubMatches(1)
            strHtml = ""
            Set Matchs = Easp.RegMatch(html, "\{((?:empty|null|eof|nodata)\:"&TagName&")\s*?(?:[\s\S.]*?)\/?\}(?:([\s\S.]*?)\{/\1\})")
            If Easp.IsN(oRs) Then
                If Matchs.Count > 0 Then
                    strHtml = Matchs(0).SubMatches(1)
                End If
            Else
                If Matchs.Count > 0 Then
                    html = Easp.RegReplace(html, Matchs(0).Value, "")
                End If
            End If
            If Not oRs.EOF Then
                ShowPage = Easp.db.GetPager("default")
                CountNum = Easp.db.PageRecordCount
                i = 1
                ii = 1
                n = 0
                Do While Not oRs.EOF And (n < oRs.PageSize)
                    strHtml = strHtml & html
                    strHtml = Cms.Fun.DataListReturnRs(ChannelId, strHtml, TagName, oRs)
                    strHtml = Cms.Tpl.Assign(strHtml, TagName, oRs)
                    Cms.Tpl.blockTagd strHtml, TagName & ".i", i
                    Cms.Tpl.blockTagd strHtml, TagName & ".ii", CmsFun.ZeroFill(ii, 2)
                    n = n + 1
                    i = i + 1
                    ii = ii + 1
                    oRs.MoveNext
                Loop
            End If
            Easp.C oRs
            Cms.Tpl.d("ChannelId") = ChannelId
            Cms.Tpl.d("CountNum") = CountNum
            Cms.Tpl.d("PageTitle") = Easp.IIf(Easp.IsN(CmsFun.HtmlRCdoe(Keywords)), CmsFun.HtmlRCdoe(KeyTag), CmsFun.HtmlRCdoe(Keywords))
            Cms.Tpl.d("Key") = Easp.IIf(Easp.IsN(CmsFun.HtmlRCdoe(Keywords)), CmsFun.HtmlRCdoe(KeyTag), CmsFun.HtmlRCdoe(Keywords))
            Cms.Tpl.d("ShowPage") = ShowPage
            Cms.Tpl.blockTag(block) = strHtml
        Next
        Set Blocks = Nothing
        Cms.Tpl.Show()
    Else
        Easp.WE Info & "模版文件："& TempFilePath &",未找到!"
    End If
End Sub

Function GetSearchSQL(ByVal ChannelId, ByVal TabName, ByVal TempWhere, ByVal OrderId)
    Dim oSQL
    oSQL = "Select "
    oSQL = oSQL & "a.*,b.ChannelId,b.Cid,b.Pid,b.ChildId,b.ChildIdAll,b.ChildNum,b.DeepPath,b.CnName,b.Px,"
    oSQL = oSQL & "b.CnTemplateList,b.EnTemplateList,b.CnTemplateView,b.EnTemplateView,"
    oSQL = oSQL & "b.HtmlFilePath,b.ViewHtmlPath,b.ViewHtmlPrefix,b.TypeImg,b.MarkVal,b.DataNum"
    oSQL = oSQL & " From ["& TabName &"] AS a Left Join ["& TablePrefix & "Type] AS b ON a.ClassId=b.Id "
    oSQL = oSQL & " Where b.ChannelId="& ChannelId &" "
    oSQL = oSQL & TempWhere
    Select Case OrderId
        Case 1
            oSQL = oSQL & " Order By a.UpTime Desc,a.Id Desc "
        Case 2
            oSQL = oSQL & " Order By a.Px Asc,a.Id Desc "
        Case 3
            oSQL = oSQL & " Order By a.Hits Desc,a.Id Desc "
        Case Else
            oSQL = oSQL & " Order By a.AddTime Desc,a.Id Desc "
    End Select
    GetSearchSQL = oSQL
End Function
%>