Imports System.Data
Imports AppCommon
Imports AppInfo
Imports AppVariable

Partial Class mobile_info_ClassSP
    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")
        SetTagCss(ctl, "mobile_info.css")

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

            'ページインデックスを復元
            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
           
        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
        '未読既読処理
        Session(S_Info_Selected_No) = gv_info.SelectedDataKey.Item(0).ToString
        Session(S_Info_Selected_Index) = gv_info.SelectedIndex
        If Read_Command = "read" Then

            Info_既読処理(gv_info.SelectedDataKey.Item(0).ToString, Session(S_COM_PortalID))
            'ユーザコントロールを開く
            Session(S_Info_遷移元) = "授業関連"

            'ユーザコントロールの変更
            If CType(gv_info.Rows.Item(Session(S_Info_Selected_Index)).FindControl("対象予定区分"), Label).Text = "変更" Then
                '授業変更用UC呼び出し
                Com_Change_UC1("menu_kj_004_mb")
            Else
                '授業変更以外のUC呼び出し
                Com_Change_UC1("menu_kj_003_mb")
            End If
            Response.Redirect("~/mobile/info/DetailSP.aspx")
        Else
            Info_未読処理(gv_info.SelectedDataKey.Item(0).ToString, Session(S_COM_PortalID))
            Response.Redirect(Request.RawUrl.ToString)
        End If
        
    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

    

#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
