Imports System.Net
Imports System.IO

Partial Class user_Default
    Inherits System.Web.UI.Page
    '初期設定用変数の初まり
    Protected xApprove As String = "管理者認証"
    Protected xTitle As String = "小説フォーマット"
    Protected xTab As String = "小説フォーマット"
    Protected xListTitle As String = "タイトルのリスト"
    Protected xPageTitle As String = "ページのタイトル"
    Protected xWidth As String = "width: 715px;"
    Protected xDisplay As String = "全体"
    '初期設定用変数の終わり

    Protected Function levelChar(ByVal x As String) As String
        levelChar = ""
        Dim n As Integer = Integer.Parse(x)
        Dim y As String = ""
        Select Case n
            Case 0
                levelChar = ""
            Case 1
                levelChar = "　：… "
            Case Else
                y = "　：… "
                For i = 0 To n - 1
                    y = "　" & y
                Next
                levelChar = y
        End Select
    End Function

    Protected Function buttonVisible(ByVal xID As String) As Boolean
        buttonVisible = True
        If xID = "0" Then
            buttonVisible = False
        End If
    End Function

    Protected Function xBackColor(ByVal xBcolor As String) As String
        If xBcolor = "True" Then
            xBackColor = "background-color: #F7F6F3; color: #7C6F57; overflow: auto; border-left:solid 3px; border-right: solid 3px;"
        Else
            xBackColor = "background-color: Transparent; color: #7C6F57; overflow: auto; border-left:none; border-right: none; border-top: solid 3px; border-bottom: solid 3px;"
        End If
    End Function

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        'スマホ or PC
        If Request.Browser.IsMobileDevice Then
            PanelMenuPC.Visible = False
            PanelMenuMobile.Visible = True
         Else
            PanelMenuPC.Visible = True
            PanelMenuMobile.Visible = False
        End If


        'プログラムの始まり
        If Not IsPostBack Then
            Dim xFormat As Data.DataView = SqlDataSourceFormat.Select(DataSourceSelectArguments.Empty)
            If xFormat IsNot Nothing AndAlso xFormat.Count <> 0 Then
                For i = 0 To xFormat.Count - 1
                    Select Case xFormat.Item(i).Row(1).ToString
                        Case "User"
                            xApprove = xFormat.Item(i).Row(2).ToString
                        Case "タイトル"
                            xTitle = xFormat.Item(i).Row(2).ToString
                        Case "タブ"
                            xTab = xFormat.Item(i).Row(2).ToString
                        Case "並び"
                            LabelOrder.Text = xFormat.Item(i).Row(2).ToString
                        Case "リストのタイトル"
                            xListTitle = xFormat.Item(i).Row(2).ToString
                        Case "ページのタイトル"
                            xPageTitle = xFormat.Item(i).Row(2).ToString
                        Case "表示選択"
                            xDisplay = xFormat.Item(i).Row(2).ToString
                    End Select
                Next
            End If

            '認証
            Dim xCode As String() = IsStatus.Split(",")  'id,date,name,mail,IP,status,cookie,
            Dim n As Integer = Integer.Parse(xCode(xCode.Length - 1))
            If n >= Integer.Parse(xApprove) Then '1:会員 3:幹事 7:管理者
            Else
                Response.Redirect("../NotAllowed.aspx?msg=認証されていません DBに無し")
                Response.End()
            End If

            Dim x As String() = xDisplay.Split(",")
            Session("menu") = x(0)
            If LabelOrder.Text = "上に挿入" Then
                ListViewList.DataSourceID = "SqlDataSourceList"
            Else
                ListViewList.DataSourceID = "SqlDataSourceList2"
            End If
        End If
    End Sub

    Protected Function IsStatus() As String
        '管理者確認の問合せ
        Dim xCookie As String = ""
        If Request.Cookies("Local") IsNot Nothing Then
            xCookie = Request.Cookies("Local").Value
            Dim xTime As String = Format(Now, "hhmmss").ToString
            Dim x As String() = xCookie.Split(",")
            Dim enc As Encoding = Encoding.GetEncoding("UTF-8")
            Dim wc As WebClient = New WebClient()
            Dim host As String = System.Net.Dns.GetHostName
            Dim xPath As String = "http://" & host
            Dim st As Stream = wc.OpenRead(xPath & "/Approve/IsStatus.aspx?code=" & xTime & "," & xCookie)
            Dim sr As StreamReader = New StreamReader(st, enc)
            Dim html As String = sr.ReadToEnd()
            sr.Close()
            st.Close()
            Return html
        Else
            Return "不明,0,0,0,0,0,0,0,0,0,0"
        End If
    End Function

    Protected Sub ButtonSearch_Click(sender As Object, e As System.EventArgs) Handles ButtonSearch.Click
        If IsPostBack Then
            If Len(TextBoxSearch.Text) = 0 Then
                If LabelOrder.Text = "上に挿入" Then
                    ListViewList.DataSourceID = "SqlDataSourceList"
                Else
                    ListViewList.DataSourceID = "SqlDataSourceList2"
                End If
            Else
                Session("body") = HttpUtility.HtmlEncode(TextBoxSearch.Text)
                Session("subject") = HttpUtility.HtmlEncode(TextBoxSearch.Text)
                If LabelOrder.Text = "上に挿入" Then
                    ListViewList.DataSourceID = "SqlDataSourceSearch"
                Else
                    ListViewList.DataSourceID = "SqlDataSourceSearch2"
                End If
            End If
        End If
    End Sub

    Protected Function txtBox() As String
        Return TextBoxSearch.Text & ""
    End Function

    Protected Function textLength(ByVal x As String) As String
        If InStr(x, "<") = 0 Then
            If Len(x) >= 42 Then
                Return Mid(x, 1, 39) & "。。。"
            Else
                Return x
            End If
        Else
            Return x
        End If
    End Function

End Class
