Imports System.Net
Imports System.IO
Imports System.Data

Partial Class admin_Format
    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 Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        'プログラムの始まり
        If Not IsPostBack Then
            'PC or Mobile
            If Request.Browser.IsMobileDevice Then
                Response.Redirect("../NotAllowed.aspx?msg=スマホではなく、PCに限定されます")
                Response.End()
            Else

            End If

            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 "System"
                            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 "DBのリセット"
                            If LCase(xFormat.Item(i).Row(2).ToString) = "true" Then
                                PanelReset.Visible = True
                            Else
                                PanelReset.Visible = False
                            End If
                    End Select
                Next
            End If

            '認証
            Dim xCode As String() = IsStatus.Split(",")
            Dim n As Integer = Integer.Parse(xCode(xCode.Length - 1))
            If n >= Integer.Parse(xApprove) And 7 Then '
            Else
                Response.Redirect("../NotAllowed.aspx?msg=認証されていません DBに無し")
                Response.End()
            End If

            '
            Dim xIP As String = System.Web.HttpContext.Current.Request.ServerVariables("REMOTE_ADDR")
            LabelIP.Text = xIP
        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 ListViewFormatData_ItemCommand(sender As Object, e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles ListViewFormatData.ItemCommand
        If IsPostBack Then
            Dim xName As String = e.CommandName
            Dim xComm As String = e.CommandArgument
            If xName = "Sel" Then
                Session("id") = xComm
                Dim dt As Data.DataView = SqlDataSourceFormatID.Select(DataSourceSelectArguments.Empty)
                If dt IsNot Nothing AndAlso dt.Count <> 0 Then
                    LabelID.Text = dt.Item(0).Row(0).ToString
                    LabelItem.Text = dt.Item(0).Row(1).ToString
                    TextBoxDescription.Text = HttpUtility.HtmlDecode(dt.Item(0).Row(2).ToString & "")
                    TextBoxChoice.Text = HttpUtility.HtmlDecode(dt.Item(0).Row(3).ToString & "")
                End If
            Else
                LabelID.Text = ""
                LabelItem.Text = ""
                TextBoxDescription.Text = ""
                TextBoxChoice.Text = ""
            End If
        End If

    End Sub

    Protected Sub Button5_Click(sender As Object, e As System.EventArgs) Handles Button5.Click
        If IsPostBack Then
            If Len(LabelID.Text) = 0 Then Exit Sub
            Session("id") = LabelID.Text
            Session("item") = LabelItem.Text
            Session("description") = HttpUtility.HtmlEncode(TextBoxDescription.Text & "")
            Session("choice") = HttpUtility.HtmlEncode(TextBoxChoice.Text & "")
            SqlDataSourceFormatData.Update()
            ListViewFormatData.DataBind()
            LabelID.Text = ""
            LabelItem.Text = ""
            TextBoxDescription.Text = ""
            TextBoxChoice.Text = ""
        End If

    End Sub

    Protected Sub ButtonReset_Click(sender As Object, e As System.EventArgs) Handles ButtonReset.Click
        If IsPostBack Then
            Dim cn As New SqlClient.SqlConnection
            Dim cd As New SqlClient.SqlCommand
            Dim SQL As String

            'データベース接続
            cn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"
            cn.Open()
            'SQL文生成
            SQL = "TRUNCATE TABLE Novel"
            cd.CommandText = SQL
            cd.Connection = cn

            cd.ExecuteNonQuery()
            'クローズ解放
            cd.Dispose()
            cn.Close()
            cn.Dispose()
        End If

    End Sub

End Class
