Imports System.Data
Imports AppCommon
Imports AppInfo
Imports AppVariable

Partial Class mobile_info_PublicSP
    Inherits System.Web.UI.Page
#Region "************************* 定数 *************************"
    'SORT
    Public Const S_LIST_SORT_COLUMN As String = "InfoPublic_SORT_COLUMN"
    Public Const S_LIST_SORT_DIR As String = "InfoPublic_SORT_DIR"
    'ページインデックス
    Public Const S_LIST_PAGE_INDEX As String = "InfoPublic_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_public.DataSource = GetInfoData()
            gv_info_public.DataBind()

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

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

#End Region

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

    Protected Sub gv_info_public_PageIndexChanging(sender As Object, e As GridViewPageEventArgs)
        gv_info_public.SelectedIndex = -1

        gv_info_public.PageIndex = e.NewPageIndex

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

        gv_info_public.DataSource = GetInfoData()
        gv_info_public.DataBind()
    End Sub

    Protected Sub gv_info_public_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_public.DataSource = GetInfoData()
        gv_info_public.DataBind()

    End Sub

    Protected Sub gv_info_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gv_info_public.SelectedIndexChanged
        '未読既読処理
        Session(S_Info_Selected_No) = gv_info_public.SelectedDataKey.Item(0).ToString
        Session(S_Info_Selected_Index) = gv_info_public.SelectedIndex

        If Read_Command = "read" Then
            Info_既読処理(gv_info_public.SelectedDataKey.Item(0).ToString, Session(S_COM_PortalID))
            'ユーザコントロールを開く
            Session(S_Info_遷移元) = "連絡事項"
            Com_Change_UC1("menu_kj_002_mb")
            Response.Redirect("~/mobile/info/DetailSP.aspx")
        Else
            Info_未読処理(gv_info_public.SelectedDataKey.Item(0).ToString, Session(S_COM_PortalID))

            Response.Redirect(Request.RawUrl.ToString)
        End If
    End Sub

    Protected Sub gv_info_public_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv_info_public.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 CType(e.Row.FindControl("lb_レベル"), Label).Text = "通常" Then
                CType(e.Row.FindControl("lb_レベル"), Label).Visible = False
            Else
                CType(e.Row.FindControl("lb_レベル"), Label).Visible = True
            End If

            If CType(e.Row.FindControl("lb_レベル"), Label).Text = "重要" Then
                CType(e.Row.FindControl("lb_レベル"), Label).ForeColor = Drawing.Color.FromName("#ff0000")
            Else
                CType(e.Row.FindControl("lb_レベル"), Label).ForeColor = Drawing.Color.FromName("#0000ff")
            End If
        End If
    End Sub

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


#End Region

#Region "******************* Sub及びFunction ********************"

    Private Function GetData() As DataSet
        Using spm As New StoredProcedureManager("掲示_掲示所属_CMB_SELECT")
            spm.Fill()

            Return spm.ds
        End Using
    End Function

    Private Function GetInfoData() As DataTable
        Using spm As New StoredProcedureManager("掲示_掲示一覧_連絡_SELECT")
            Dim m As String
            Dim w As String
            Dim n As String
            Dim t 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

            If Request.QueryString("t") Is Nothing Then
                t = ""
            Else
                t = Request.QueryString("t").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("@掲示対象CD", Data.SqlDbType.VarChar, 200, t.ToString)
            spm.AddPrmIn("@新着", Data.SqlDbType.Int, n.ToString)

            spm.Fill("Public")

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

            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
