<%
Class Cls_Type
    Public Cid, ChannelId, Pid, ChildId, ChildIdAll, DeepPath, CnName, Px
    Public CnTemplateList, EnTemplateList, HtmlFilePath, CnTemplateView, EnTemplateView
    Public ViewHtmlPath, ViewHtmlPrefix
    Public TypeImg, MetaKey, MetaDesc, IsShow, MarkVal, UrlLink, DataNum

    Private TableName

    Private Sub Class_Initialize()
        TableName = TablePrefix & "Type"
        Initialize()
    End Sub

    Private Sub Class_Terminate()
        Initialize()
    End Sub

    Private Function Initialize()
        ChannelId = 0
        Cid = 0
        Pid = 0
        ChildId = ""
        DeepPath = 0
        CnName = ""
        Px = 0
        CnTemplateList = "cnlist.html"
        EnTemplateList = "enlist.html"
        HtmlFilePath = "/list/"
        CnTemplateView = "cninfo.html"
        EnTemplateView = "eninfo.html"
        ViewHtmlPath = "/html/"
        ViewHtmlPrefix = ""
        TypeImg = ""
        MetaKey = ""
        MetaDesc = ""
        IsShow = 1
        MarkVal = ""
        UrlLink = ""
        DataNum = 0
    End Function

    Private Sub ReplaceVal()
        CnTemplateList = Replace(Replace(Replace(CnTemplateList, "\", "/"), "//", "/"), "//", "/")
        EnTemplateList = Replace(Replace(Replace(EnTemplateList, "\", "/"), "//", "/"), "//", "/")
        HtmlFilePath = Replace(Replace(Replace(Replace(HtmlFilePath, "\", "/"), "//", "/"), "//", "/"), ".", "")
        CnTemplateView = Replace(Replace(Replace(CnTemplateView, "\", "/"), "//", "/"), "//", "/")
        EnTemplateView = Replace(Replace(Replace(EnTemplateView, "\", "/"), "//", "/"), "//", "/")
        ViewHtmlPath = Replace(Replace(Replace(Replace(ViewHtmlPath, "\", "/"), "//", "/"), "//", "/"), ".", "")
        If Not Easp.IsN(HtmlFilePath) Then
            If Left(HtmlFilePath, 1) <> "/" Then
                HtmlFilePath = "/" & HtmlFilePath
            End If
            If Right(HtmlFilePath, 1) <> "/" Then
                HtmlFilePath = HtmlFilePath & "/"
            End If
        End If
        If Left(ViewHtmlPath, 1) <> "/" Then
            ViewHtmlPath = "/" & ViewHtmlPath
        End If
        If Right(ViewHtmlPath, 1) <> "/" Then
            ViewHtmlPath = ViewHtmlPath & "/"
        End If
    End Sub

    Public Function ShowSelectList(ByVal TitleName, ByVal ChannelId, ByVal Id, ByVal IsMultiple)
        Dim strHtml, strMultiple
        strMultiple = " style=""width:350px; height:150px"" multiple=""multiple"""
        strHtml = "<select name=""ClassId"" id=""ClassId"" class=""select2"""& Easp.IIf(IsMultiple = 1, strMultiple, "") &" datatype=""select"">" & vbCrLf
        strHtml = strHtml & "<option value=""0"">"& TitleName &"</option>" & vbCrLf
        strHtml = strHtml & ClassList(ChannelId, Id)
        strHtml = strHtml & "</select>" & vbCrLf
        ShowSelectList = strHtml
    End Function

    Private Function ClassList(ByVal ChannelId, ByVal Id)
        Dim arr, SelectList
        arr = TypeList(ChannelId)
        SelectList = ShowOption(0, arr, Id)
        ClassList = SelectList
    End Function

    Public Function TypeList(ByVal Cid)
        Dim oRs, SQLText, arrList
        SQLText = "Id,Pid,ChildId,DeepPath,CnName,Px,CnTemplateList,CnTemplateView,HtmlFilePath,IsShow,ViewHtmlPath,DataNum,UrlLink"
        Set oRs = Easp.db.GR(TableName & ":" & SQLText, " ChannelId = "& Cid &" ", " Px Asc,Id Desc ")
        If Easp.IsN(oRs) Then
            arrList = Null
        Else
            arrList = oRs.GetRows()
        End If
        Easp.C oRs
        TypeList = arrList
    End Function

    Public Function ShowOption(ByVal id, ByVal arr, ByVal Val)
        If IsArray(arr) Then
            Dim i, j, arrList
            Dim ClassId, CnName, DeepPath
            For i = 0 To UBound(arr, 2)
                If arr(1, i) = id Then
                    ClassId = arr(0, i)
                    CnName = arr(4, i)
                    DeepPath = ""
                    For j = 1 To arr(3, i)
                        DeepPath = DeepPath & "　"
                    Next
                    If id > 0 Then
                        DeepPath = DeepPath & "├ "
                    End If
                    arrList = arrList & "<option value=""" & ClassId & """"& Easp.IIf(Trim(arr(0, i)) = Trim(Val), " selected=""selected""", "") &">" & DeepPath & CnName & "</option>" & vbCrLf
                    arrList = arrList & ShowOption(ClassId, arr, Val)
                End If
            Next
            ShowOption = arrList
        End If
    End Function

    Private Function ShowVal()
        Dim oRs, ResultVal
        ResultVal = 0
        Set oRs = Easp.db.GR(TableName & ":1", " Id = "& Cid &" ", "")
        If Easp.IsN(oRs) Then
            ResultVal = 0
        Else
            ResultVal = 1
            Pid = oRs("Pid")
            ChildId = oRs("ChildId")
            ChildIdAll = oRs("ChildIdAll")
            DeepPath = oRs("DeepPath")
            CnName = oRs("CnName")
            Px = oRs("Px")
            CnTemplateList = oRs("CnTemplateList")
            EnTemplateList = oRs("EnTemplateList")
            HtmlFilePath = oRs("HtmlFilePath")
            CnTemplateView = oRs("CnTemplateView")
            EnTemplateView = oRs("EnTemplateView")
            ViewHtmlPath = oRs("ViewHtmlPath")
            ViewHtmlPrefix = oRs("ViewHtmlPrefix")
            TypeImg = oRs("TypeImg")
            MetaKey = oRs("MetaKey")
            MetaDesc = oRs("MetaDesc")
            IsShow = oRs("IsShow")
            MarkVal = oRs("MarkVal")
            UrlLink = oRs("UrlLink")
        End If
        Easp.C oRs
        ShowVal = ResultVal
    End Function

    Public Function Add()
        Dim List, ResultVal
        ResultVal = 0
        ' 计算深度
        If Pid > 0 Then
            DeepPath = Easp.db.RT(TableName, " Id = "& Pid &" ", "DeepPath") + 1
        Else
            DeepPath = 0
        End If
        ReplaceVal()
        Easp.Use "list"
        Set List = Easp.List.New
        List("ChildId") = ""
        List("ChildIdAll") = ""
        List("ChannelId") = ChannelId
        List("Pid") = Pid
        List("DeepPath") = DeepPath
        List("CnName") = CnName
        List("Px") = Px
        List("CnTemplateList") = CnTemplateList
        List("EnTemplateList") = EnTemplateList
        List("HtmlFilePath") = HtmlFilePath
        List("CnTemplateView") = CnTemplateView
        List("EnTemplateView") = EnTemplateView
        List("ViewHtmlPath") = ViewHtmlPath
        List("ViewHtmlPrefix") = ViewHtmlPrefix
        List("TypeImg") = TypeImg
        List("MetaKey") = MetaKey
        List("MetaDesc") = MetaDesc
        List("IsShow") = IsShow
        List("MarkVal") = MarkVal
        List("UrlLink") = UrlLink
        List("DataNum") = DataNum
        List("AddTime") = Now()
        ResultVal = Easp.db.AR(TableName, List.Hash)
        Easp.C List
        Easp.db.UR TableName, " 1 = 1 ", Array("Cid:Id")
        ReloadChildId() ' 更新子类
        Add = ResultVal
    End Function

    Public Function Edit()
        Dim oRs, ChildDeepPath, Childs, i, ResultVal, List
        ResultVal = 0
        Set oRs = Easp.db.GR(TableName, " Id = "& Cid &" ", "")
        If Easp.IsN(oRs) Then
            Easp.C oRs
            ResultVal = 0
            Exit Function
        End If
        ' 重新计算深度
        If Pid > 0 Then
            DeepPath = Easp.db.RT(TableName, " Id = "& Pid &" ", "DeepPath") + 1
        Else
            DeepPath = 0
        End If
        If Len(oRs("ChildIdAll")) - Len(Cid & ",") > 0 Then
            Childs = Right(oRs("ChildIdAll"), Len(oRs("ChildIdAll")) - Len(Cid & ","))
            If Len(Childs) > 0 Then
                ChildDeepPath = oRs("DeepPath") - DeepPath
                If CmsFun.ChkNumEric(ChildDeepPath) > 0 Then
                    Easp.db.UR TableName, " Id In (" & oRs("ChildIdAll") & ") ", Array("DeepPath:DeepPath-" & ChildDeepPath)
                End If
            End If
        End If
        ReplaceVal()
        Easp.Use "list"
        Set List = Easp.List.New
        List("DeepPath") = DeepPath
        List("Pid") = Pid
        List("CnName") = CnName
        List("Px") = Px
        List("CnTemplateList") = CnTemplateList
        List("EnTemplateList") = EnTemplateList
        List("HtmlFilePath") = HtmlFilePath
        List("CnTemplateView") = CnTemplateView
        List("EnTemplateView") = EnTemplateView
        List("ViewHtmlPath") = ViewHtmlPath
        List("ViewHtmlPrefix") = ViewHtmlPrefix
        List("TypeImg") = TypeImg
        List("MetaKey") = MetaKey
        List("MetaDesc") = MetaDesc
        List("IsShow") = IsShow
        List("MarkVal") = MarkVal
        List("UrlLink") = UrlLink
        ResultVal = Easp.db.UR(TableName, " ChannelId = "& ChannelId &" And Id = "& Cid &" ", List.Hash)
        Easp.C List
        Easp.db.UR TableName, " 1 = 1 ", Array("Cid:Id")
        ReloadChildId() ' 更新子类
        Edit = ResultVal
    End Function

    Public Function Del()
        Dim ResultVal, i, oRs
        i = ShowVal()
        If i > 0 Then
            '            Set oRs = Easp.db.GR(TableName & ":Id,HtmlFilePath", " Id in("& ChildIdAll &") ", "")
            '            If Not Easp.IsN(oRs) Then
            '                Easp.Use "fso"
            '                Do While Not oRs.EOF
            '                    If Easp.Fso.IsFolder(oRs(1)) Then
            '                        Easp.Fso.DelFolder oRs(1)
            '                    End If
            '                    oRs.MoveNext
            '                Loop
            '            End If
            '            Easp.C oRs
            '== 删除该类的数据
            DelInfo ChildIdAll, ChannelId
            '==
            Easp.db.DR TableName, "Id:" & ChildIdAll
            ReloadChildId() ' 更新子类
            ResultVal = 1
        Else
            ResultVal = 0
        End If
        Del = ResultVal
    End Function

    Private Sub DelInfo(ByVal Id, ByVal ChannelId)
        If Easp.IsN(id) Then
            Exit Sub
        End If
        ' 读取频道信息
        Dim ArrayChannel, TabName
        ArrayChannel = GetCaCheChannelInfo("Channel", ChannelId, ChannelField)
        If Easp.IsN(ArrayChannel) Then
            Exit Sub
        Else
            TabName = ArrayChannel(2)
        End If
        '===================================================================== 删除html文件
        Dim ResultVal, oSQL, oRs, TempFile
        oSQL = "Select a.Id,a.HtmlFile,b.ViewHtmlPath,b.ViewHtmlPrefix,a.Tag From ["& TabName &"] a,["& TableName &"] b Where b.ChannelId = "& ChannelId &" "
        oSQL = oSQL & " And a.ClassId = b.id And a.ClassId in("& Id &") "
        Set oRs = Easp.db.Exec(oSQL)
        If Not Easp.IsN(oRs) Then
            Easp.Use "fso"
            Do While Not oRs.EOF
                TempFile = CmsFun.HtmlFileUrl(oRs("ViewHtmlPath") & oRs("ViewHtmlPrefix"), oRs("HtmlFile"), oRs("id"))
                CmsAdmin.UpdateTag ChannelId, TabName, oRs("Tag")
                If Easp.Fso.IsFile(TempFile) Then
                    Easp.Fso.DelFile TempFile
                End If
                oRs.MoveNext
            Loop
        End If
        Easp.db.C oRs
        '===================================================================== 删除数据
        ResultVal = Easp.db.DR(TabName, "ClassId:" & Id)
    End Sub

    Private Function ReloadChildId()
        Dim oRs, strTemp, MaxDeepPath
        Set oRs = Easp.db.GR(TableName & ":DeepPath:1", "", " DeepPath Desc ")
        If Not Easp.IsN(oRs) Then
            MaxDeepPath = oRs(0)
        Else
            MaxDeepPath = 0
        End If
        Easp.C oRs
        Set oRs = Easp.db.GR(TableName & ":Id,ChildId,ChildIdAll,DeepPath", "", "")
        If Not Easp.IsN(oRs) Then
            Do While Not oRs.EOF
                Dim List, ResultVal
                Easp.Use "list"
                Set List = Easp.List.New
                If oRs(3) = MaxDeepPath Then
                    List("ChildId") = ""
                    List("ChildIdAll") = oRs(0)
                Else

                    strTemp = GetChildId(oRs(0), 0)
                    If MaxDeepPath - 1 = oRs(3) Then
                        List("ChildId") = strTemp
                        If Len(strTemp) = 0 Then strTemp = oRs(0) Else strTemp = oRs(0) & "," & strTemp
                        List("ChildIdAll") = strTemp
                    Else
                        List("ChildId") = strTemp
                        If Len(strTemp) = 0 Then
                            strTemp = oRs(0)
                        Else
                            strTemp = oRs(0) & "," & GetChildId(oRs(0), 1)
                        End If
                        List("ChildIdAll") = strTemp
                    End If
                End If
                List("ChildNum") = GetChildNum(oRs(1))
                ResultVal = Easp.db.UR(TableName, " Id = "& oRs(0) &" ", List.Hash)
                Easp.C List
                oRs.MoveNext
            Loop
        End If
        Easp.C oRs
    End Function

    Private Function GetChildId(Byval Id, Byval Deep)
        Dim oRs
        Set oRs = Easp.db.GR(TableName & ":Id", " Pid = "& Id &" ", " Px Desc ")
        If Not Easp.IsN(oRs) Then
            Do While Not oRs.EOF
                If Deep = 0 Then
                    GetChildId = GetChildId & "," & oRs(0) ' 仅下级栏目
                Else
                    GetChildId = GetChildId & "," & oRs(0) & "," & GetChildId(oRs(0), 1) ' 所有子栏目
                End If
                oRs.MoveNext
            Loop
        End If
        Easp.C oRs
        GetChildId = Replace(GetChildId, ",,", ",")
        GetChildId = Replace(GetChildId, ",,", ",")
        If Right(GetChildId, 1) = "," Then GetChildId = Left(GetChildId, Len(GetChildId) -1)
        If Left(GetChildId, 1) = "," Then GetChildId = Right(GetChildId, Len(GetChildId) -1)
    End Function


    Private Function GetChildNum(Byval Val)
        If Not Easp.IsN(Val) Then
            GetChildNum = UBound(Split(Val, ",")) + 1
        Else
            GetChildNum = 0
        End If
    End Function

End Class
%>