Imports System.Data
Imports AppCommon
Imports AppInfo
Imports AppVariable

Partial Class InfoClass
    Inherits System.Web.UI.Page

#Region "************************* 定数 *************************"

    'SORT
    Public Const S_LIST_SORT_COLUMN As String = "InfoClass_SORT_COLUMN"
    Public Const S_LIST_SORT_DIR As String = "InfoClass_SORT_DIR"
    'ページインデックス
    Public Const S_LIST_PAGE_INDEX As String = "InfoClass_PAGE_INDEX"

#End Region

#Region "********************** メンバ変数 **********************"

    Private Read_Command As String '既読処理 OR 未読処理フラグ

#End Region

#Region "******************** ページイベント ********************"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        '---------------------------------------------------------------
        'HTMLタグを追加
        '---------------------------------------------------------------
        Dim ctl As Control = Page.Master.FindControl("msp_header")
        'CSS
        SetTagCss(ctl, "info.css")

        If Not IsPostBack Then
            gv_info.DataSource = GetData()
            gv_info.DataBind()

            '検索項目に値をセット
            If Request.QueryString("m") IsNot Nothing AndAlso Request.QueryString("m").ToString = "1" Then
                cb_自分宛て検索.Checked = True
            Else
                cb_自分宛て検索.Checked = False
            End If

            'ページインデックスを復元
            If Not Session(S_LIST_PAGE_INDEX) Is Nothing Then
                gv_info.SetPageIndex(Session(S_LIST_PAGE_INDEX))
            End If

            '---------------------------------------------------------------
            '指定したinfoをセレクト状態にする
            '---------------------------------------------------------------
            If Not Session(S_Info_Selected_Index) Is Nothing Then
                gv_info.SelectedIndex = Session(S_Info_Selected_Index)
            End If

            Try '※外すと落ちる
                If Not String.IsNullOrEmpty(Request.QueryString("w")) Then
                    tb_WORD検索.Text = HttpUtility.UrlDecode(Request.QueryString("w"))
                Else
                    tb_WORD検索.Text = String.Empty
                End If
                'tb_WORD検索.Text = HttpUtility.UrlDecode(Request.QueryString("w").ToString)
            Catch ex As Exception
            End Try
        End If
    End Sub

#End Region

#Region "***************** コントロールイベント *****************"

    Protected Sub gv_info_PageIndexChanging(sender As Object, e As GridViewPageEventArgs)
        gv_info.SelectedIndex = -1

        gv_info.PageIndex = e.NewPageIndex

        'ページの位置を保持
        Session(S_LIST_PAGE_INDEX) = gv_info.PageIndex.ToString

        gv_info.DataSource = GetData()
        gv_info.DataBind()
    End Sub

    Protected Sub gv_info_Sorting(sender As Object, e As GridViewSortEventArgs)
        If Session(S_LIST_SORT_COLUMN) Is Nothing Then
            Session(S_LIST_SORT_COLUMN) = e.SortExpression

            e.SortDirection = SortDirection.Ascending
            Session(S_LIST_SORT_DIR) = "ASC"

        Else
            If e.SortExpression = Session(S_LIST_SORT_COLUMN) Then
                Select Case Session(S_LIST_SORT_DIR)
                    Case "ASC"
                        e.SortDirection = SortDirection.Descending
                        Session(S_LIST_SORT_DIR) = "DESC"

                    Case "DESC"
                        Session(S_LIST_SORT_DIR) = "Default"

                        Session(S_LIST_SORT_COLUMN) = "既定SORT順"
                        Session(S_LIST_SORT_DIR) = "ASC"

                    Case "Default"
                        e.SortDirection = SortDirection.Ascending
                        Session(S_LIST_SORT_DIR) = "ASC"

                End Select
            Else
                Session(S_LIST_SORT_COLUMN) = e.SortExpression

                e.SortDirection = SortDirection.Ascending
                Session(S_LIST_SORT_DIR) = "ASC"

            End If
        End If

        gv_info.DataSource = GetData()
        gv_info.DataBind()

    End Sub

    Protected Sub gv_info_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gv_info.SelectedIndexChanged
        '未読既読処理
        If Read_Command = "read" Then

            Info_既読処理(gv_info.SelectedDataKey.Item(0).ToString, Session(S_COM_PortalID))
            'ユーザコントロールを開く
            Com_UC1_Visible("slideopen")

        Else
            Info_未読処理(gv_info.SelectedDataKey.Item(0).ToString, Session(S_COM_PortalID))
        End If

        Session(S_Info_Selected_Index) = gv_info.SelectedIndex
        Session(S_Info_Selected_No) = gv_info.SelectedDataKey.Item(0).ToString

        'ユーザコントロールの変更
        If CType(gv_info.Rows.Item(Session(S_Info_Selected_Index)).FindControl("対象予定区分"), Label).Text = "変更" Then
            '授業変更用UC呼び出し
            Com_Change_UC1("menu_kj_004")
        Else
            '授業変更以外のUC呼び出し
            Com_Change_UC1("menu_kj_003")
        End If
        Response.Redirect(Request.RawUrl.ToString)
    End Sub

    Protected Sub gv_info_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv_info.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            If CType(e.Row.FindControl("lb_添付"), Label).Text = "添付" Then
                CType(e.Row.FindControl("img_添付"), Image).Visible = True
            Else
                CType(e.Row.FindControl("img_添付"), Image).Visible = False
            End If

            If CType(e.Row.FindControl("lb_new"), Label).Text = "新着" Then
                CType(e.Row.FindControl("img_new"), Image).Visible = True
            Else
                CType(e.Row.FindControl("img_new"), Image).Visible = False
            End If

            If CType(e.Row.FindControl("lb_未読"), Label).Text = "既読" Then
                CType(e.Row.FindControl("img_未読"), Image).Visible = False
            Else
                CType(e.Row.FindControl("img_未読"), Image).Visible = True
            End If

            If Not CType(e.Row.FindControl("lb_img_path"), Label) Is Nothing And CType(e.Row.FindControl("lb_img_path"), Label).Text <> "" Then
                CType(e.Row.FindControl("img_lv"), Image).ImageUrl = CType(e.Row.FindControl("lb_img_path"), Label).Text
            Else
                CType(e.Row.FindControl("img_lv"), Image).Visible = False
            End If
        End If
    End Sub

    Protected Sub gv_info_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv_info.RowCommand
        Read_Command = e.CommandArgument
    End Sub

    Protected Sub btn_検索_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_検索.Click
        'URLパラメータを作成
        Dim strSearch As String
        strSearch = "?"

        If cb_自分宛て検索.Checked = True Then
            strSearch = strSearch & "m=1"
        End If

        If Len(tb_WORD検索.Text) > 0 Then

            If strSearch <> "?" Then
                strSearch = strSearch & "&"
            End If

            strSearch = strSearch & "w=" & HttpUtility.UrlEncode(tb_WORD検索.Text)
        End If

        'URLパラメータをセットしてリダイレクト
        Response.Redirect(Request.FilePath.ToString & strSearch)
    End Sub

#End Region

#Region "******************* Sub及びFunction ********************"
    Private Function GetData() As DataTable
        Using spm As New StoredProcedureManager("掲示_掲示一覧_授業_SELECT")
            Dim m As String
            Dim w As String
            Dim n As String

            If Request.QueryString("m") Is Nothing Then
                m = ""
            Else
                m = Request.QueryString("m").ToString
            End If

            If Request.QueryString("w") Is Nothing Then
                w = ""
            Else
                w = Request.QueryString("w").ToString
            End If

            If Request.QueryString("n") Is Nothing Then
                n = ""
            Else
                n = Request.QueryString("n").ToString
            End If

            spm.AddPrmIn("@PORTALID", Data.SqlDbType.VarChar, 200, Session(S_COM_PortalID))
            spm.AddPrmIn("@自分宛て", Data.SqlDbType.VarChar, 20, m.ToString)
            spm.AddPrmIn("@WORD", Data.SqlDbType.VarChar, 200, w.ToString)
            spm.AddPrmIn("@新着", Data.SqlDbType.Int, n.ToString)

            spm.Fill("Class")

            Dim dt As DataTable = spm.ds.Tables("Class")

            If Not Session(S_LIST_SORT_COLUMN) Is Nothing And Not Session(S_LIST_SORT_DIR) Is Nothing Then
                dt.DefaultView.Sort = String.Format("{0} {1}", Session(S_LIST_SORT_COLUMN).ToString(), Session(S_LIST_SORT_DIR).ToString())
            End If

            Return dt.DefaultView.ToTable

        End Using
    End Function

#End Region

End Class
