Imports System.Data
Imports AppCommon
Imports AppInfo
Imports AppVariable
Imports AppVariable_teacher

Partial Class teacher_RequestForm
    Inherits System.Web.UI.Page

#Region "************************* 定数 *************************"
    Const CONST_SUMMITTED_MES As String = "事務連絡票は既に提出済みです。"
#End Region

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

#End Region

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

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
            BindAllListViews()
            If lv_謝金旅費.Items.Count > 0 Then
                Dim lbl_提出日時 As Label = CType(lv_謝金旅費.Items(0).FindControl("lbl_提出日時"), Label)

                If lbl_提出日時 IsNot Nothing AndAlso Not String.IsNullOrWhiteSpace(lbl_提出日時.Text) Then
                    DisabledButton(btn_提出)
                    AllListViewOff()
                    lbl_errMes.Text = CONST_SUMMITTED_MES
                    lbl_errMes.Visible = True
                End If
            End If
        End If

    End Sub

#End Region

#Region "***************** コントロールイベント *****************"
    Protected Sub frm_研修情報_DataBound(sender As Object, e As EventArgs) Handles frm_研修情報.DataBound
        Dim rowView As DataRowView = CType(frm_研修情報.DataItem, DataRowView)

        If rowView IsNot Nothing Then
            ' 各ボタンを取得して、対応するデータの有無で表示/非表示を切り替え
            For i As Integer = 1 To 9
                Dim btn As Button = CType(frm_研修情報.FindControl("btn_編集" & i), Button)

                If btn IsNot Nothing Then
                    Dim idx As String = i.ToString("00")
                    Dim 講義日 As Object = rowView("講義日" & idx)

                    ' NULLまたは空文字の場合は非表示
                    If 講義日 Is DBNull.Value OrElse String.IsNullOrEmpty(講義日.ToString()) Then
                        btn.Visible = False
                    Else
                        btn.Visible = True
                    End If
                End If
            Next
        End If

    End Sub
    Protected Sub frm_研修情報_ItemCommand(sender As Object, e As FormViewCommandEventArgs) Handles frm_研修情報.ItemCommand
        If e.CommandName = "Edit" Then '更新ボタン
            Try
                'ユーザコントロールを開く
                Com_UC1_Visible("slideopen")
                Com_Change_UC1("menu_ks_003")
                Session(S_Info_Selected_No) = Convert.ToString(e.CommandArgument)
                Response.Redirect(Request.RawUrl.ToString)

            Catch ex As Exception
                ' エラー処理
                Dim mes = ex.Message
            End Try
        End If
    End Sub
    Protected Sub btn_戻る_Click(sender As Object, e As EventArgs) Handles btn_戻る.Click
        '一覧画面に戻る
        Response.Redirect(Com_Get_Content("menu_ks_001"))
    End Sub
    Protected Sub btn_提出_Click(ByVal sender As Object, ByVal e As EventArgs)

        lbl_errMes.Visible = False

        ' 提出ボタンのクリックイベント内
        Dim frm As FormView = frm_研修情報

        For i As Integer = 1 To 9
            Dim suffix As String = i.ToString("00")
            ' 講義日ラベルを取得
            Dim lblKougibi As Label = CType(frm.FindControl("lbl_講義日" & suffix), Label)

            ' 講義日が空でなければ、昼食をチェック
            If lblKougibi IsNot Nothing AndAlso Not String.IsNullOrEmpty(lblKougibi.Text) Then
                Dim lblChushoku As Label = CType(frm.FindControl("lbl_昼食" & suffix), Label)

                If lblChushoku Is Nothing OrElse String.IsNullOrEmpty(lblChushoku.Text) Then
                    ' エラー表示してreturn
                    lbl_errMes.Text = "課目" & i & "の連絡票が未入力です。"
                    lbl_errMes.Visible = True
                    Return
                End If
            End If
        Next


        'データ登録と一時保存データの削除
        Try
            '提出日時の更新
            RegistTeacherRequest()

            '研修担当職員IDの取得
            Dim lbl As Label = CType(lv_謝金旅費.Items(0).FindControl("lbl_担当教職員ID01"), Label)
            Dim 教職員ID01 As String = lbl.Text
            'PKeyの取得
            Dim lbl_履修年度 As Label = CType(lv_謝金旅費.Items(0).FindControl("lbl_履修年度"), Label)
            Dim lbl_成績年度 As Label = CType(lv_謝金旅費.Items(0).FindControl("lbl_成績年度"), Label)
            Dim lbl_分野CD As Label = CType(lv_謝金旅費.Items(0).FindControl("lbl_分野CD"), Label)
            Dim lbl_科目CD As Label = CType(lv_謝金旅費.Items(0).FindControl("lbl_科目CD"), Label)
            Dim lbl_講座CD As Label = CType(lv_謝金旅費.Items(0).FindControl("lbl_講座CD"), Label)
            Dim userCd As String = Session(S_COM_UserCD)

            Dim 出講師PKey As String = lbl_履修年度.Text & "_" & lbl_成績年度.Text & "_" & lbl_分野CD.Text & "_" & lbl_科目CD.Text & "_" & lbl_講座CD.Text & "_" & userCd
            'メール送信
            If Not String.IsNullOrEmpty(教職員ID01) Then
                AppMail.SendUpdateTeacherRequestMail(教職員ID01, userCd, 出講師PKey)
            End If
            lbl = CType(lv_謝金旅費.Items(0).FindControl("lbl_担当教職員ID02"), Label)
            Dim 教職員ID02 As String = lbl.Text
            If Not String.IsNullOrEmpty(教職員ID02) Then
                AppMail.SendUpdateTeacherRequestMail(教職員ID02, userCd, 出講師PKey)
            End If

            If String.IsNullOrEmpty(教職員ID01) And String.IsNullOrEmpty(教職員ID02) Then
                Dim defaultStaff As String = Com_Get_変数("PT_QA_TO_DEFAULT_USERCD") '送信先デフォルトユーザ
                AppMail.SendUpdateTeacherRequestMail(defaultStaff, userCd, 出講師PKey)
            End If

            DisabledButton(btn_提出)
            '各編集ボタンも無効化
            AllListViewOff()

            'メッセージの表示
            Dim script As String = "alert('事務連絡票を提出しました。');"
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), Guid.NewGuid().ToString(), script, True)
            lbl_errMes.Text = CONST_SUMMITTED_MES
            lbl_errMes.Visible = True
        Catch ex As Exception
            lbl_errMes.Text = "提出に失敗しました。"
            lbl_errMes.Visible = True
        End Try

    End Sub
    Protected Sub lv_謝金旅費_ItemEditing(sender As Object, e As ListViewEditEventArgs) Handles lv_謝金旅費.ItemEditing
        lv_謝金旅費.EditIndex = e.NewEditIndex
        BindListView(lv_謝金旅費)
    End Sub
    Protected Sub lv_謝金旅費_ItemCanceling(ByVal sender As Object, ByVal e As ListViewCancelEventArgs) Handles lv_謝金旅費.ItemCanceling
        lv_謝金旅費.EditIndex = -1
        BindListView(lv_謝金旅費)
    End Sub
    Protected Sub lv_謝金旅費_ItemCommand(sender As Object, e As ListViewCommandEventArgs) Handles lv_謝金旅費.ItemCommand
        If e.CommandName = "cmd_Update" Then '更新ボタン
            Try
                Dim item As ListViewItem = CType(e.Item, ListViewItem)
                Dim rbl_謝金旅費 As RadioButtonList = CType(item.FindControl("rbl_謝金旅費"), RadioButtonList)

                Dim selVal As String = rbl_謝金旅費.SelectedValue
                Dim selArr() As String = {"×", "×", "×", "×"}
                Select Case selVal
                    Case 1
                        selArr = {"○", "×", "×", "×"}
                    Case 2
                        selArr = {"×", "×", "○", "×"}
                    Case 3
                        selArr = {"×", "○", "×", "×"}
                    Case 4
                        selArr = {"×", "×", "×", "○"}
                End Select

                Dim 謝金旅費受取可 As String = selArr(0)
                Dim 謝金のみ受取可 As String = selArr(1) '旅費は不要
                Dim 旅費のみ受取可 As String = selArr(2) '謝金は不要
                Dim 謝金旅費受取不可 As String = selArr(3)

                ' 一時保存
                SaveToTemporary(謝金旅費受取可:=謝金旅費受取可, 謝金のみ受取可:=謝金のみ受取可, 旅費のみ受取可:=旅費のみ受取可, 謝金旅費受取不可:=謝金旅費受取不可)

                ' 編集モード解除
                lv_謝金旅費.EditIndex = -1
                BindListView(lv_謝金旅費)

            Catch ex As Exception
                ' エラー処理
                lbl_errMes.Text = "更新に失敗しました。"
                lbl_errMes.Visible = True
            End Try
        End If
    End Sub
    Protected Sub lv_謝金旅費_ItemDataBound(sender As Object, e As ListViewItemEventArgs) Handles lv_謝金旅費.ItemDataBound
        If e.Item.ItemType = ListViewItemType.DataItem Then
            Dim dataItem As ListViewDataItem = CType(e.Item, ListViewDataItem)
            Dim rowView As DataRowView = CType(dataItem.DataItem, DataRowView)

            Dim 謝金旅費受取可 As String = If(rowView("謝金旅費受取可") IsNot DBNull.Value, rowView("謝金旅費受取可").ToString(), "")
            Dim 謝金のみ受取可 As String = If(rowView("謝金のみ受取可") IsNot DBNull.Value, rowView("謝金のみ受取可").ToString(), "")
            Dim 旅費のみ受取可 As String = If(rowView("旅費のみ受取可") IsNot DBNull.Value, rowView("旅費のみ受取可").ToString(), "")
            Dim 謝金旅費受取不可 As String = If(rowView("謝金旅費受取不可") IsNot DBNull.Value, rowView("謝金旅費受取不可").ToString(), "")
            ' 表示用ラベルだけ取得
            Dim lbl_謝金旅費 As Label = CType(e.Item.FindControl("lbl_謝金旅費"), Label)
            If lbl_謝金旅費 IsNot Nothing Then
                Dim displayText As String = ""
                If 謝金旅費受取可 = "○" Then
                    displayText = "謝金も旅費も支払"
                ElseIf 謝金のみ受取可 = "○" Then
                    displayText = "旅費は不要"
                ElseIf 旅費のみ受取可 = "○" Then
                    displayText = "謝金は不要"
                ElseIf 謝金旅費受取不可 = "○" Then
                    displayText = "謝金も旅費も不要"
                Else
                    displayText = "謝金も旅費も支払"
                End If

                lbl_謝金旅費.Text = displayText
            End If

            ' EditItemTemplateの場合 - ラジオボタン初期値設定
            Dim rbl_謝金旅費 As RadioButtonList = CType(e.Item.FindControl("rbl_謝金旅費"), RadioButtonList)
            If rbl_謝金旅費 IsNot Nothing Then

                If 謝金旅費受取可 = "○" Then
                    rbl_謝金旅費.SelectedValue = "1"
                ElseIf 謝金のみ受取可 = "○" Then
                    rbl_謝金旅費.SelectedValue = "3"
                ElseIf 旅費のみ受取可 = "○" Then
                    rbl_謝金旅費.SelectedValue = "2"
                ElseIf 謝金旅費受取不可 = "○" Then
                    rbl_謝金旅費.SelectedValue = "4"
                Else
                    rbl_謝金旅費.SelectedValue = "1"
                End If
            End If
        End If
    End Sub
    Protected Sub lv_その他_ItemEditing(sender As Object, e As ListViewEditEventArgs) Handles lv_その他.ItemEditing
        lv_その他.EditIndex = e.NewEditIndex
        BindListView(lv_その他)
    End Sub
    Protected Sub lv_その他_ItemCanceling(ByVal sender As Object, ByVal e As ListViewCancelEventArgs) Handles lv_その他.ItemCanceling
        lv_その他.EditIndex = -1
        BindListView(lv_その他)
    End Sub
    Protected Sub lv_その他_ItemCommand(sender As Object, e As ListViewCommandEventArgs) Handles lv_その他.ItemCommand
        If e.CommandName = "cmd_Update" Then '更新ボタン
            Try
                Dim item As ListViewItem = CType(e.Item, ListViewItem)
                Dim txt_諸事項 As TextBox = CType(item.FindControl("txt_諸事項"), TextBox)
                Dim 諸事項 As String = txt_諸事項.Text

                ' 一時保存
                SaveToTemporary(諸事項:=諸事項)

                ' 編集モード解除
                lv_その他.EditIndex = -1
                BindListView(lv_その他)

            Catch ex As Exception
                ' エラー処理
                Dim mes = ex.Message
            End Try
        End If
    End Sub
#End Region

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

    Function GetData() As DataTable
        Using spm As New StoredProcedureManager("連絡票_TMP講師貼付_事務連絡票_SELECT")
            spm.AddPrmIn("@USERCD", Data.SqlDbType.VarChar, 20, Session(S_COM_UserCD))
            spm.AddPrmIn("@CNT", Data.SqlDbType.Int, Session(S_TeacherProf_Selected_ID))
            spm.Fill()

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

            Return dt

        End Using
    End Function

    Private Sub SaveToTemporary(
        Optional 謝金旅費受取可 As String = Nothing,
        Optional 謝金のみ受取可 As String = Nothing,
        Optional 旅費のみ受取可 As String = Nothing,
        Optional 謝金旅費受取不可 As String = Nothing,
        Optional 諸事項 As String = Nothing
        )
        Using spm As New StoredProcedureManager("連絡票_TMP講師貼付_UPDATE")
            spm.AddPrmIn("@USERCD", Data.SqlDbType.VarChar, 20, Session(S_COM_UserCD))
            spm.AddPrmIn("@CNT", Data.SqlDbType.Int, Session(S_TeacherProf_Selected_ID))

            '謝金・旅費
            spm.AddPrmIn("@謝金旅費受取可", Data.SqlDbType.VarChar, 10, ToDbValue(謝金旅費受取可))
            spm.AddPrmIn("@謝金のみ受取可", Data.SqlDbType.VarChar, 10, ToDbValue(謝金のみ受取可))
            spm.AddPrmIn("@旅費のみ受取可", Data.SqlDbType.VarChar, 10, ToDbValue(旅費のみ受取可))
            spm.AddPrmIn("@謝金旅費受取不可", Data.SqlDbType.VarChar, 10, ToDbValue(謝金旅費受取不可))

            'その他
            spm.AddPrmIn("@諸事項", Data.SqlDbType.NVarChar, 2000, ToDbValue(諸事項))
            spm.Exec()

        End Using
    End Sub
    Private Sub RegistTeacherRequest()
        Using spm As New StoredProcedureManager("連絡票_事務連絡票_提出_UPDATE")
            spm.AddPrmIn("@USERCD", Data.SqlDbType.VarChar, 20, Session(S_COM_UserCD))
            spm.AddPrmIn("@CNT", Data.SqlDbType.Int, Session(S_TeacherProf_Selected_ID))
            spm.Exec()
        End Using
    End Sub
    Private Sub AllListViewOff()
        '各編集ボタンも無効化
        frm_研修情報.Enabled = False
        lv_謝金旅費.Enabled = False
        lv_その他.Enabled = False
    End Sub
    Private Sub BindListView(lv As ListView)
        lv.DataSource = GetData()
        lv.DataBind()
    End Sub
    Private Sub BindAllListViews()
        Dim dt As DataTable = GetData()
        frm_研修科目.DataSource = dt
        frm_研修科目.DataBind()

        frm_研修情報.DataSource = dt
        frm_研修情報.DataBind()

        lv_謝金旅費.DataSource = dt
        lv_謝金旅費.DataBind()

        lv_その他.DataSource = dt
        lv_その他.DataBind()
    End Sub
    Private Function GetErrMes() As String
        Dim mes As String = ""
        Using spm As New StoredProcedureManager("連絡票_TMP講師貼付_SELECT")
            spm.AddPrmIn("@USERCD", Data.SqlDbType.VarChar, 20, Session(S_COM_UserCD))
            spm.AddPrmIn("@CNT", Data.SqlDbType.Int, Session(S_TeacherProf_Selected_ID))
            spm.Fill()

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

        End Using
        Return mes
    End Function

    Private Function ToDbValue(value As String) As Object
        Return If(String.IsNullOrEmpty(value), DBNull.Value, CObj(value))
    End Function

    Protected Function GetEmptyStyle(fieldValue As Object) As String
        Dim strValue As String = If(fieldValue, "").ToString()
        If String.IsNullOrWhiteSpace(strValue) Then
            Return "background-color:#cccccc;"
        Else
            Return ""
        End If
    End Function

    Protected Function GetEmptyStyleForDateTime(fieldValue As Object) As String
        If fieldValue Is Nothing OrElse fieldValue Is DBNull.Value Then
            Return "background-color:#cccccc;"
        Else
            Return ""
        End If
    End Function
    Private Sub DisabledButton(ByRef btn As Button)
        btn.Enabled = False
        btn.CssClass = "training-button training-button-disabled"
    End Sub
#End Region

End Class
