Imports System.Data
Imports AppCommon
Imports AppVariable

Partial Class teacher_uc_SearchTraining
    Inherits System.Web.UI.UserControl

#Region "******************** メンバ変数 ********************"
    Private _search As TrainingSearch
    Private _param As TrainingParam
    Private _user As LoginUser

    Private Const SESSION_LIST_PAGE_INDEX As String = "SearchTraining_PageIndex"             'ページの位置
    Private Const SESSION_QA_TEACHER_DEFAULT_YEAR As String = "qa_teacher_default_year"
#End Region

#Region "******************** ページイベント ********************"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        _search = DirectCast(Session(S_Training_Search), TrainingSearch)
        _user = DirectCast(Session(S_COM_LoginUser), LoginUser)

        If Not IsPostBack() Then
            CreateControls()

            '検索条件の初期化、開講年度は初期値に西暦今年を設定
            _search = New TrainingSearch()
            Dim seireki As String = Com_Get_変数("西暦今年")
            Dim seirekiValue As Short
            If Short.TryParse(seireki, seirekiValue) Then
                dd_検索_開講年度.SelectedValue = seireki
                _search.開講年度 = seireki
                Session(SESSION_QA_TEACHER_DEFAULT_YEAR) = seireki
                Session(S_Training_Search) = _search
            End If
        End If


        '検索条件を復元
        If Not IsNothing(_search) Then
            'dd_検索_開講年度.SelectedValue = _search.開講年度
            'txt_検索_科目名.Text = _search.科目名
            'txt_検索_回名.Text = _search.回名

            If _search.ForceSearch Then '検索ボタン押下時
                Search研修()
                'ユーザコントロールを開く
                Com_UC1_Visible("slideopen")
            Else '参照ボタン押下時等
                Dim seireki As String = Com_Get_変数("西暦今年")
                Dim seirekiValue As Short
                If Short.TryParse(seireki, seirekiValue) Then
                    dd_検索_開講年度.SelectedValue = seireki
                    _search.開講年度 = seireki
                End If
                _search.ForceSearch = False
            End If
        End If
        Session(S_Training_Search) = _search

        '---------------------------------------------------------------
        'ダイアログセット
        '---------------------------------------------------------------
        btn_宛先に追加.Attributes("onclick") = "return Dialog_YN(""宛先に追加します。よろしいですか？"")"

    End Sub
#End Region

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

    Protected Sub btn_検索_Click(ByVal sender As Object, ByVal e As EventArgs)
        gv_研修一覧.PageIndex = 0
        Session.Remove(SESSION_LIST_PAGE_INDEX)

        _search.開講年度 = dd_検索_開講年度.SelectedValue
        _search.科目名 = txt_検索_科目名.Text
        _search.回名 = txt_検索_回名.Text
        _search.ForceSearch = True
        Session(S_Training_Search) = _search
        Search研修()
    End Sub

    Protected Sub btn_クリア_Click(sender As Object, e As EventArgs) Handles btn_クリア.Click

        '検索条件をクリア
        _search = New TrainingSearch
        _search.開講年度 = Session(SESSION_QA_TEACHER_DEFAULT_YEAR).ToString()
        Session(S_Training_Search) = _search
        Session.Remove(SESSION_LIST_PAGE_INDEX)
        '検索条件を復元
        'If Not _search.開講年度 = "" Then
        'dd_検索_開講年度.SelectedValue = _search.開講年度
        'End If
        dd_検索_開講年度.SelectedValue = Session(SESSION_QA_TEACHER_DEFAULT_YEAR).ToString()
        txt_検索_科目名.Text = _search.科目名
        txt_検索_回名.Text = _search.回名


        Create研修一覧()
        gv_研修一覧.DataBind()
    End Sub

    Protected Sub sql_研修一覧_Init(sender As Object, e As EventArgs) Handles sql_研修一覧.Init

        If IsNothing(Session(S_Training_Search)) Then
            _search = New TrainingSearch()
            Dim seireki As String = Com_Get_変数("西暦今年")
            Dim seirekiValue As Short
            If Short.TryParse(seireki, seirekiValue) Then
                dd_検索_開講年度.SelectedValue = seireki
                _search.開講年度 = seireki
                Session(S_Training_Search) = _search
                'Search研修()
            End If
        End If
    End Sub

    Protected Sub gv_研修一覧_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gv_研修一覧.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim chk As CheckBox = CType(e.Row.FindControl("cb_MailTo"), CheckBox)

            _param = New TrainingParam
            _param.履修年度 = DirectCast(e.Row.FindControl("lb_履修年度"), Label).Text
            _param.成績年度 = DirectCast(e.Row.FindControl("lb_成績年度"), Label).Text
            _param.分野CD = DirectCast(e.Row.FindControl("lb_分野CD"), Label).Text
            _param.科目CD = DirectCast(e.Row.FindControl("lb_科目CD"), Label).Text
            _param.講座CD = DirectCast(e.Row.FindControl("lb_回CD"), Label).Text
            Session(S_Training_Seleted_Param) = _param

            ' value属性の設定 (履修年度_成績年度_分野CD_科目CD_講座CD)
            chk.InputAttributes("value") = _param.履修年度 & "_" & _param.成績年度 & "_" &
                                  _param.分野CD & "_" & _param.科目CD & "_" & _param.講座CD

        End If
    End Sub

    Protected Sub gv_研修一覧_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles gv_研修一覧.PageIndexChanging
        '選択行を解除
        gv_研修一覧.SelectedIndex = -1
        'ページを移動
        gv_研修一覧.PageIndex = e.NewPageIndex

        'ページの位置を保持
        Session(SESSION_LIST_PAGE_INDEX) = gv_研修一覧.PageIndex.ToString

        ' 保存した検索条件で再バインド
        If Not IsNothing(_search) Then
            Search研修()
        End If

    End Sub

    Protected Sub gv_研修一覧_DataBound(sender As Object, e As EventArgs) Handles gv_研修一覧.DataBound
        'ページの位置を復元
        gv_研修一覧.PageIndex = Session(SESSION_LIST_PAGE_INDEX)
    End Sub

    Protected Sub btn_宛先に追加_Click(sender As Object, e As EventArgs) Handles btn_宛先に追加.Click

        Dim gv_宛先 As GridView = CType(Page.Master.FindControl("msp_asp_cph1").FindControl("gv_宛先"), GridView)
        Dim btn_宛先削除 As Button = CType(Page.Master.FindControl("msp_asp_cph1").FindControl("btn_宛先削除"), Button)

        Clear宛先(gv_宛先)
        If hKenshuCd.Value.Length > 0 Then  '1人も選択していない場合、エラーになるため/Liu

            Dim array研修CD As String() = hKenshuCd.Value.Substring(0, hKenshuCd.Value.Length - 1).Split(",")
            Dim pnl_入力確認 As Panel = CType(Page.Master.FindControl("msp_asp_cph1").FindControl("pnl_入力確認"), Panel)

            For Each str研修CD In array研修CD
                Dim arrCDs As String() = str研修CD.ToString().Split("_")
                Dim strTo履修年度 As String = arrCDs(0)
                Dim strTo成績年度 As String = arrCDs(1)
                Dim strTo分野CD As String = arrCDs(2)
                Dim strTo科目CD As String = arrCDs(3)
                Dim strTo講座CD As String = arrCDs(4)

                AppQA.宛先追加(strTo履修年度:=strTo履修年度,
                           strTo成績年度:=strTo成績年度,
                           strTo分野CD:=strTo分野CD,
                           strTo科目CD:=strTo科目CD,
                           strTo講座CD:=strTo講座CD,
                           strTo区分:="研修")
            Next
        End If

        gv_宛先.DataBind()
        btn_宛先削除.Visible = IIf(gv_宛先.Rows.Count > 0, True, False)

        ' 保存した検索条件で再バインド
        If Not IsNothing(_search) Then
            Search研修()
        End If

    End Sub

    Protected Sub SelectButton_Click(sender As Object, e As EventArgs)
        Dim gv_宛先 As GridView = CType(Page.Master.FindControl("msp_asp_cph1").FindControl("gv_宛先"), GridView)
        Dim btn_宛先削除 As Button = CType(Page.Master.FindControl("msp_asp_cph1").FindControl("btn_宛先削除"), Button)

        Clear宛先(gv_宛先)

        Dim btn As Button = DirectCast(sender, Button)
        Dim row As GridViewRow = DirectCast(btn.NamingContainer, GridViewRow)
        Dim gridView As GridView = DirectCast(row.NamingContainer, GridView)

        Dim dataItemIndex As Integer = Integer.Parse(btn.CommandArgument)
        If dataItemIndex >= 0 AndAlso dataItemIndex < gridView.DataKeys.Count Then

            Dim strTo履修年度 As String = gridView.DataKeys(dataItemIndex).Values("履修年度").ToString()
            Dim strTo成績年度 As String = gridView.DataKeys(dataItemIndex).Values("成績年度").ToString()
            Dim strTo分野CD As String = gridView.DataKeys(dataItemIndex).Values("分野CD").ToString()
            Dim strTo科目CD As String = gridView.DataKeys(dataItemIndex).Values("科目CD").ToString()
            Dim strTo講座CD As String = gridView.DataKeys(dataItemIndex).Values("回CD").ToString()


            AppQA.宛先追加(strTo履修年度:=strTo履修年度,
                           strTo成績年度:=strTo成績年度,
                           strTo分野CD:=strTo分野CD,
                           strTo科目CD:=strTo科目CD,
                           strTo講座CD:=strTo講座CD,
                           strTo区分:="研修")

            gv_宛先.DataBind()
            btn_宛先削除.Visible = IIf(gv_宛先.Rows.Count > 0, True, False)
        Else

        End If


        ' 保存した検索条件で再バインド
        If Not IsNothing(_search) Then
            Search研修()
        End If
        Com_UC1_Visible("slideclose")
    End Sub

#End Region

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

    Private Sub CreateControls()
        Create検索_開講年度()
    End Sub

    Private Sub Create検索_開講年度()
        Using spm As New StoredProcedureManager("共通_教務_授業年度_DDL")
            spm.Fill()

            dd_検索_開講年度.DataSource = spm.ds.Tables(0)
        End Using

        dd_検索_開講年度.DataBind()
    End Sub


    Private Sub Search研修()
        If IsNothing(_search) Then
            _search = New TrainingSearch
        End If
        '_search.開講年度 = dd_検索_開講年度.SelectedValue
        '_search.科目名 = txt_検索_科目名.Text
        '_search.回名 = txt_検索_回名.Text
        Session(S_Training_Search) = _search

        Create研修一覧()
    End Sub

    Private Sub Create研修一覧()
        '初期値を設定
        If IsNothing(_search) Then
            _search = New TrainingSearch
        End If

        Dim param As TrainingSearch = DirectCast(Session(S_Training_Search), TrainingSearch)

        Using spm As New StoredProcedureManager("QA_研修一覧_SELECT")
            spm.AddPrmIn("@開講年度", SqlDbType.SmallInt, param.開講年度)
            spm.AddPrmIn("@科目名", SqlDbType.VarChar, param.科目名)
            spm.AddPrmIn("@回名", SqlDbType.VarChar, param.回名)

            spm.Fill()

            gv_研修一覧.DataSource = spm.ds.Tables(0)
        End Using

        gv_研修一覧.DataBind()
    End Sub

    Private Sub Clear宛先(gv_宛先 As GridView)

        If gv_宛先.Rows.Count > 0 Then

            For Each grvRow In gv_宛先.Rows

                Dim strTo区分 As String = CType(grvRow.FindControl("lbl_To区分"), Label).Text
                Dim strTo履修年度 As String = CType(grvRow.Cells(0).FindControl("lbl_To履修年度"), Label).Text
                Dim strTo成績年度 As String = CType(grvRow.Cells(0).FindControl("lbl_To成績年度"), Label).Text
                Dim strTo分野CD As String = CType(grvRow.Cells(0).FindControl("lbl_To分野CD"), Label).Text
                Dim strTo科目CD As String = CType(grvRow.Cells(0).FindControl("lbl_To科目CD"), Label).Text
                Dim strTo講座CD As String = CType(grvRow.Cells(0).FindControl("lbl_To講座CD"), Label).Text

                'TMP宛先から削除
                AppQA.宛先削除(strTo区分:=strTo区分,
                      strTo履修年度:=strTo履修年度,
                      strTo成績年度:=strTo成績年度,
                      strTo分野CD:=strTo分野CD,
                      strTo科目CD:=strTo科目CD,
                      strTo講座CD:=strTo講座CD,
                      str処理区分:="個別")
            Next

            gv_宛先.DataBind()
        End If
    End Sub
#End Region

End Class
