Imports System.Data
Imports AppVariable
Imports AppVariable_QA
Imports AppCommon

Partial Class faculty_QAList
    Inherits System.Web.UI.Page

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

    Private _search As QASearch
    Private S_QA_Search As String = "S_QA_Search"
#End Region

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

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then

            gv_問合せ履歴.DataSource = GetData()
            gv_問合せ履歴.DataBind()

            '---検索条件をセット---
            txt_検索_問合せ者氏名.Text = _search.問合せ氏名
            txt_検索_問合せ日.Text = _search.問合せ日
            txt_検索_回名.Text = _search.回名
            '----------------------  

            'ページの位置を復元
            If Not Session(S_QA_PAGE_INDEX) Is Nothing Then
                gv_問合せ履歴.SetPageIndex(Session(S_QA_PAGE_INDEX))
            End If

            button_area.Visible = True
            search_area.Visible = False
            ViewState("mode") = "Init"
            Return
        End If

        If ViewState("mode").ToString() = "Confirm" Then
            button_area.Visible = False
            search_area.Visible = True
            btn_戻る.Visible = True
        ElseIf ViewState("mode").ToString() = "Init" Then
            button_area.Visible = True
            search_area.Visible = False
            btn_戻る.Visible = False
        End If
    End Sub

#End Region

#Region "***************** コントロールイベント *****************"
    Protected Sub btn_ToInsertPage_Click(sender As Object, e As EventArgs) Handles btn_ToInsertPage.Click

        '遷移先でリストが残るため、遷移前にTMP宛先から削除
        AppQA.宛先削除(strTo区分:="",
                      strTo履修年度:="",
                      strTo成績年度:="",
                      strTo分野CD:="",
                      strTo科目CD:="",
                      strTo講座CD:="",
                      str処理区分:="一括")

        '問い合わせ入力フォームへ
        Response.Redirect(Com_Get_Content("menu_kj_qa03"))
    End Sub

    Protected Sub btn_ToConfirmPage_Click(sender As Object, e As EventArgs) Handles btn_ToConfirmPage.Click

        search_area.Visible = True
        button_area.Visible = False

        btn_戻る.Visible = True

        ViewState("mode") = "Confirm"

        gv_問合せ履歴.DataSource = GetData()
        gv_問合せ履歴.DataBind()
    End Sub

    Protected Sub btn_戻る_Click(sender As Object, e As EventArgs) Handles btn_戻る.Click

        search_area.Visible = False
        button_area.Visible = True

        '戻るボタン
        btn_戻る.Visible = False

        '検索結果をクリア
        _search = New QASearch
        Session(S_QA_Search) = _search
        ViewState("mode") = "Init"

        gv_問合せ履歴.DataSource = GetData()
        gv_問合せ履歴.DataBind()

    End Sub

    Protected Sub btn_検索_Click(sender As Object, e As EventArgs)
        'ページインデックスを初期化
        gv_問合せ履歴.PageIndex = 0
        Session.Remove(S_QA_PAGE_INDEX)

        '検索条件をクリア
        _search = New QASearch
        Session(S_QA_Search) = _search

        '検索条件をセッションに保存
        _search.問合せ氏名 = txt_検索_問合せ者氏名.Text
        _search.問合せ日 = txt_検索_問合せ日.Text
        _search.回名 = txt_検索_回名.Text

        Session(S_QA_Search) = _search
        ViewState("mode") = "Confirm"
        gv_問合せ履歴.DataSource = GetData()
        gv_問合せ履歴.DataBind()
    End Sub

    Protected Sub btn_検索クリア_Click(sender As Object, e As EventArgs)
        '検索条件をクリア
        _search = New QASearch
        Session(S_QA_Search) = _search
        txt_検索_問合せ者氏名.Text = _search.問合せ氏名
        txt_検索_問合せ日.Text = _search.問合せ日
        txt_検索_回名.Text = _search.回名

        'フィルタなしで再取得
        gv_問合せ履歴.DataSource = GetData()
        gv_問合せ履歴.DataBind()
    End Sub

    Protected Sub gv_問合せ履歴_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles gv_問合せ履歴.RowCommand
        If e.CommandName = "詳細" Then
            'IDをセッションに保存
            Session(S_QA_Selected_ID) = e.CommandArgument.ToString() '問合せID
            '遷移元保存
            Session(S_QA_Detail_RedirectFrom) = "お問合せ一覧"
            '詳細画面へ
            Response.Redirect(Com_Get_Content("menu_kj_qa02"))
        End If
    End Sub

    Protected Sub gv_問合せ履歴_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles gv_問合せ履歴.PageIndexChanging
        gv_問合せ履歴.PageIndex = e.NewPageIndex

        'ページの位置を保持
        Session(S_QA_PAGE_INDEX) = gv_問合せ履歴.PageIndex.ToString

        gv_問合せ履歴.DataSource = GetData()
        gv_問合せ履歴.DataBind()

    End Sub

    Protected Sub gv_問合せ履歴_Sorting(sender As Object, e As GridViewSortEventArgs) Handles gv_問合せ履歴.Sorting
        If Session(S_QA_SORT_COLUMN) Is Nothing Then
            Session(S_QA_SORT_COLUMN) = e.SortExpression

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

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

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

                        Session(S_QA_SORT_COLUMN) = "既定SORT順"
                        Session(S_QA_SORT_DIR) = "ASC"

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

                End Select
            Else
                Session(S_QA_SORT_COLUMN) = e.SortExpression

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

            End If
        End If

        gv_問合せ履歴.DataSource = GetData()
        gv_問合せ履歴.DataBind()

    End Sub

    Protected Sub gv_問合せ履歴_RowCreated(sender As Object, e As GridViewRowEventArgs) Handles gv_問合せ履歴.RowCreated
        If e.Row.RowType = DataControlRowType.Header Then
            Dim strDir As String = ""
            If Not Session(S_QA_SORT_DIR) Is Nothing Then
                If Session(S_QA_SORT_DIR) = "Default" Then strDir = ""
                If Session(S_QA_SORT_DIR) = "ASC" Then strDir = " ▲"
                If Session(S_QA_SORT_DIR) = "DESC" Then strDir = " ▼"
            End If

            If Not Session(S_QA_SORT_COLUMN) Is Nothing Then

                If Session(S_QA_SORT_COLUMN) = "登録者名" Then
                    e.Row.Cells(0).Controls.Add(New LiteralControl(strDir))

                ElseIf Session(S_QA_SORT_COLUMN) = "問合せ日時" Then
                    e.Row.Cells(1).Controls.Add(New LiteralControl(strDir))

                ElseIf Session(S_QA_SORT_COLUMN) = "件名" Then
                    e.Row.Cells(2).Controls.Add(New LiteralControl(strDir))

                ElseIf Session(S_QA_SORT_COLUMN) = "宛先名前表示" Then
                    e.Row.Cells(3).Controls.Add(New LiteralControl(strDir))

                ElseIf Session(S_QA_SORT_COLUMN) = "最新返信日時" Then
                    e.Row.Cells(4).Controls.Add(New LiteralControl(strDir))

                End If
            End If

        End If
    End Sub
#End Region

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

    Function GetData() As DataTable

        _search = Session(S_QA_Search)
        If _search Is Nothing Then
            _search = New QASearch
        End If

        'modeを取得
        Dim modeVal As String = ""
        Try
            If Not IsNothing(ViewState("mode")) AndAlso
               Not String.IsNullOrEmpty(ViewState("mode").ToString()) Then
                If ViewState("mode").ToString() = "Confirm" Then
                    modeVal = "1"
                End If
            End If
        Catch ex As Exception
            ' エラーハンドリング
        End Try

        Using spm As New StoredProcedureManager("QA_問合せ一覧_職員用_SELECT")

            spm.AddPrmIn("@問合せ区分", Data.SqlDbType.VarChar, 20, Session(S_QA_Search_kubun))
            spm.AddPrmIn("@問合せ者氏名", Data.SqlDbType.NVarChar, 100, _search.問合せ氏名)
            spm.AddPrmIn("@問合せ日", Data.SqlDbType.VarChar, 20, _search.問合せ日)
            spm.AddPrmIn("@回名", Data.SqlDbType.NVarChar, 100, _search.回名)
            spm.AddPrmIn("@ユーザCD", Data.SqlDbType.VarChar, 200, Session(S_COM_UserCD))
            spm.AddPrmIn("@MODE", Data.SqlDbType.VarChar, 2, modeVal)

            'spm.Fill("Contact")
            spm.Fill()

            'Dim dt As DataTable = spm.ds.Tables("Contact")
            Dim dt As DataTable = spm.ds.Tables(0)

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

            Return dt.DefaultView.ToTable

        End Using
    End Function

#End Region





End Class
