Imports System.Data
Imports AppCommon
Imports AppVariable
Imports AppVariable_teacher

Partial Class teacher_uc_RequestForm
    Inherits System.Web.UI.UserControl

    Private _dict質問 As Dictionary(Of String, String)

#Region "******************** ページイベント ********************"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session(S_Info_Selected_No) Is Nothing Then
            Session(S_Info_Selected_No) = 0
        End If
        If Session(S_Info_Selected_No) = 0 Then
            btn_更新.Visible = False
            Return
        End If

        Dim dt As DataTable = GetData()
        frm_研修科目.DataSource = dt
        frm_研修科目.DataBind()

        _dict質問 = Get質問マスタ()
        frm_課目別連絡票.DataSource = dt
        frm_課目別連絡票.DataBind()

    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
            Dim idx As String = Session(S_Info_Selected_No)

            If idx = "0" Then Return
            ' 各ラベルに動的に値を設定
            CType(frm_研修科目.FindControl("lbl_課目"), Label).Text =
            If(idx IsNot DBNull.Value, idx.ToString(), "")

            CType(frm_研修科目.FindControl("lbl_課目名"), Label).Text =
            If(rowView("課目名" & idx) IsNot DBNull.Value, rowView("課目名" & idx).ToString(), "")

            Dim lbl_講義日 As Label = CType(frm_研修科目.FindControl("lbl_講義日"), Label)
            If rowView("講義日" & idx) IsNot DBNull.Value Then
                lbl_講義日.Text = Convert.ToString(rowView("講義日" & idx))
            End If

            CType(frm_研修科目.FindControl("lbl_講義開始時間"), Label).Text =
            If(rowView("講義開始時間" & idx) IsNot DBNull.Value, rowView("講義開始時間" & idx).ToString(), "")

            CType(frm_研修科目.FindControl("lbl_講義終了時間"), Label).Text =
            If(rowView("講義終了時間" & idx) IsNot DBNull.Value, rowView("講義終了時間" & idx).ToString(), "")

            CType(frm_研修科目.FindControl("lbl_迎え時刻"), Label).Text =
            If(rowView("迎え時刻" & idx) IsNot DBNull.Value, rowView("迎え時刻" & idx).ToString(), "")

            CType(frm_研修科目.FindControl("lbl_送り時刻"), Label).Text =
            If(rowView("送り時刻" & idx) IsNot DBNull.Value, rowView("送り時刻" & idx).ToString(), "")
        End If

    End Sub

    Protected Sub frm_課目別連絡票_DataBound(sender As Object, e As EventArgs) Handles frm_課目別連絡票.DataBound
        Dim rowView As DataRowView = CType(frm_課目別連絡票.DataItem, DataRowView)
        Dim idx As String = Session(S_Info_Selected_No)
        If idx = "0" Then Return
        If rowView IsNot Nothing Then

            For Each 項目名 As String In _dict質問.Keys
                Dim lbl As Label = TryCast(frm_課目別連絡票.FindControl("lbl_質問_" & 項目名), Label)

                If lbl IsNot Nothing Then
                    lbl.Text = _dict質問(項目名)
                End If

                Dim txt As TextBox = CType(frm_課目別連絡票.FindControl("txt_回答_" & 項目名), TextBox)
                If txt IsNot Nothing Then
                    txt.Text = If(rowView(項目名 & idx) IsNot DBNull.Value, Convert.ToString(rowView(項目名 & idx)), "")
                    If rowView(項目名 & idx) IsNot DBNull.Value Then
                        If 項目名 = "宿泊到着" Or 項目名 = "送り出発" Or 項目名 = "到着" Then
                            Dim dt As DateTime = CDate(rowView(項目名 & idx))
                            txt.Text = dt.ToString("HH:mm")
                        End If
                    End If
                End If
                Dim ddl As DropDownList = CType(frm_課目別連絡票.FindControl("ddl_回答_" & 項目名), DropDownList)
                If ddl IsNot Nothing Then
                    Set回答DDL(ddl, 項目名)
                    If 項目名 = "別手段" Then
                        ddl.SelectedValue = If(rowView("迎え" & idx) IsNot DBNull.Value, rowView("迎え" & idx).ToString(), "")
                    Else
                        ddl.SelectedValue = If(rowView(項目名 & idx) IsNot DBNull.Value, rowView(項目名 & idx).ToString(), "")
                    End If
                End If
            Next
        End If

        Dim ddl回答 As DropDownList = CType(frm_課目別連絡票.FindControl("ddl_回答_別手段"), DropDownList)
        Dim txt回答 As TextBox = CType(frm_課目別連絡票.FindControl("txt_回答_別手段"), TextBox)
        Dim txt回答2 As TextBox = CType(frm_課目別連絡票.FindControl("txt_回答_到着"), TextBox)

        If ddl回答 IsNot Nothing AndAlso txt回答 IsNot Nothing Then
            ' 初期状態の設定
            txt回答.Enabled = ddl回答.SelectedValue = "その他"
            txt回答2.Enabled = ddl回答.SelectedValue <> "○"
        End If
        ddl回答 = CType(frm_課目別連絡票.FindControl("ddl_回答_機器"), DropDownList)
        txt回答 = CType(frm_課目別連絡票.FindControl("txt_回答_機器他"), TextBox)

        If ddl回答 IsNot Nothing AndAlso txt回答 IsNot Nothing Then
            txt回答.Enabled = ddl回答.SelectedValue = "その他"
        End If
        ddl回答 = CType(frm_課目別連絡票.FindControl("ddl_回答_宿泊"), DropDownList)
        txt回答 = CType(frm_課目別連絡票.FindControl("txt_回答_宿泊到着"), TextBox)
        If ddl回答 IsNot Nothing AndAlso txt回答 IsNot Nothing Then
            txt回答.Enabled = ddl回答.SelectedValue = "所内泊(前泊)"
        End If
    End Sub
    Protected Sub btn_更新_Click(ByVal sender As Object, ByVal e As EventArgs)

        '一時保存
        Try
            Dim 回数 As String = Session(S_Info_Selected_No)
            Dim frm As FormView = CType(frm_課目別連絡票, FormView)
            Dim frm_研修 As FormView = CType(frm_研修科目, FormView)
            Dim ddl_別手段 As DropDownList = CType(frm.FindControl("ddl_回答_別手段"), DropDownList)
            Dim txt_別手段 As TextBox = CType(frm.FindControl("txt_回答_別手段"), TextBox)
            Dim txt_到着 As TextBox = CType(frm.FindControl("txt_回答_到着"), TextBox)
            Dim txt_車種 As TextBox = CType(frm.FindControl("txt_回答_車種"), TextBox)
            Dim ddl_宿泊 As DropDownList = CType(frm.FindControl("ddl_回答_宿泊"), DropDownList)
            Dim txt_宿泊到着 As TextBox = CType(frm.FindControl("txt_回答_宿泊到着"), TextBox)
            Dim ddl_夕食 As DropDownList = CType(frm.FindControl("ddl_回答_夕食"), DropDownList)
            Dim ddl_朝食 As DropDownList = CType(frm.FindControl("ddl_回答_朝食"), DropDownList)
            Dim ddl_昼食 As DropDownList = CType(frm.FindControl("ddl_回答_昼食"), DropDownList)
            Dim ddl_機器 As DropDownList = CType(frm.FindControl("ddl_回答_機器"), DropDownList)
            Dim txt_機器他 As TextBox = CType(frm.FindControl("txt_回答_機器他"), TextBox)
            Dim ddl_送り As DropDownList = CType(frm.FindControl("ddl_回答_送り"), DropDownList)
            Dim txt_送り他 As TextBox = CType(frm.FindControl("txt_回答_送り他"), TextBox)
            Dim txt_送り出発 As TextBox = CType(frm.FindControl("txt_回答_送り出発"), TextBox)
            Dim lbl_err_別手段 As Label = CType(frm.FindControl("lbl_err_別手段"), Label)
            Dim lbl_err_別手段他 As Label = CType(frm.FindControl("lbl_err_別手段他"), Label)
            Dim lbl_err_到着 As Label = CType(frm.FindControl("lbl_err_到着"), Label)
            Dim lbl_err_車種 As Label = CType(frm.FindControl("lbl_err_車種"), Label)
            Dim lbl_err_宿泊 As Label = CType(frm.FindControl("lbl_err_宿泊"), Label)
            Dim lbl_err_宿泊到着 As Label = CType(frm.FindControl("lbl_err_宿泊到着"), Label)
            Dim lbl_err_夕食 As Label = CType(frm.FindControl("lbl_err_夕食"), Label)
            Dim lbl_err_朝食 As Label = CType(frm.FindControl("lbl_err_朝食"), Label)
            Dim lbl_err_昼食 As Label = CType(frm.FindControl("lbl_err_昼食"), Label)
            Dim lbl_err_機器 As Label = CType(frm.FindControl("lbl_err_機器"), Label)
            Dim lbl_err_機器他 As Label = CType(frm.FindControl("lbl_err_機器他"), Label)
            Dim lbl_err_送り As Label = CType(frm.FindControl("lbl_err_送り"), Label)
            Dim lbl_err_送り他 As Label = CType(frm.FindControl("lbl_err_送り他"), Label)
            Dim lbl_err_送り出発 As Label = CType(frm.FindControl("lbl_err_送り出発"), Label)

            Dim lbl_講義開始時間 As Label = CType(frm_研修.FindControl("lbl_講義開始時間"), Label)
            Dim lbl_講義終了時間 As Label = CType(frm_研修.FindControl("lbl_講義終了時間"), Label)

            Dim 迎え As String = ddl_別手段.SelectedValue
            Dim 別手段 As String = txt_別手段.Text
            Dim 到着 As String = txt_到着.Text
            Dim 車種 As String = txt_車種.Text
            Dim 宿泊 As String = ddl_宿泊.SelectedValue
            Dim 宿泊到着 As String = txt_宿泊到着.Text
            Dim 夕食 As String = ddl_夕食.SelectedValue
            Dim 朝食 As String = ddl_朝食.SelectedValue
            Dim 昼食 As String = ddl_昼食.SelectedValue
            Dim 機器 As String = ddl_機器.SelectedValue
            Dim 機器他 As String = txt_機器他.Text
            Dim 送り As String = ddl_送り.SelectedValue
            Dim 送り他 As String = txt_送り他.Text
            Dim 送り出発 As String = txt_送り出発.Text
            Dim 講義開始時間 As String = lbl_講義開始時間.Text
            Dim 講義終了時間 As String = lbl_講義終了時間.Text
            Dim arrivalTime As TimeSpan
            Dim startTime As TimeSpan
            Dim departTime As TimeSpan
            Dim endTime As TimeSpan

            ' バリデーション開始時に全エラーラベルをリセット
            lbl_err_別手段.Visible = False
            lbl_err_別手段.Text = ""
            lbl_err_別手段他.Visible = False
            lbl_err_別手段他.Text = ""
            lbl_err_到着.Visible = False
            lbl_err_到着.Text = ""
            lbl_err_車種.Visible = False
            lbl_err_車種.Text = ""
            lbl_err_宿泊.Visible = False
            lbl_err_宿泊.Text = ""
            lbl_err_宿泊到着.Visible = False
            lbl_err_宿泊到着.Text = ""
            lbl_err_夕食.Visible = False
            lbl_err_夕食.Text = ""
            lbl_err_朝食.Visible = False
            lbl_err_朝食.Text = ""
            lbl_err_昼食.Visible = False
            lbl_err_昼食.Text = ""
            lbl_err_機器.Visible = False
            lbl_err_機器.Text = ""
            lbl_err_機器他.Visible = False
            lbl_err_機器他.Text = ""
            lbl_err_送り.Visible = False
            lbl_err_送り.Text = ""
            lbl_err_送り他.Visible = False
            lbl_err_送り他.Text = ""
            lbl_err_送り出発.Visible = False
            lbl_err_送り出発.Text = ""

            Dim errMes As String = ""
            If String.IsNullOrEmpty(迎え) Then
                lbl_err_別手段.Text = "<br />選択してください。"
                lbl_err_別手段.Visible = True
            End If
            If 迎え = "その他" AndAlso String.IsNullOrEmpty(別手段) Then
                lbl_err_別手段他.Text = "<br />入力してください。"
                lbl_err_別手段他.Visible = True
            End If
            If 迎え <> "○" AndAlso String.IsNullOrEmpty(到着) Then
                lbl_err_到着.Text = "<br />入力してください。"
                lbl_err_到着.Visible = True
            End If
            'If 迎え <> "○" AndAlso TimeSpan.TryParse(到着, arrivalTime) AndAlso TimeSpan.TryParse(講義開始時間, startTime) AndAlso arrivalTime >= startTime Then
            '    lbl_err_到着.Text = "<br />講義開始時間より前の時間を入力してください。"
            '    lbl_err_到着.Visible = True
            'End If
            If (迎え = "公用車" Or 迎え = "自家用車") And String.IsNullOrEmpty(車種) Then
                lbl_err_車種.Text = "<br />入力してください。"
                lbl_err_車種.Visible = True
            End If
            If 宿泊 = "所内泊(前泊)" AndAlso String.IsNullOrEmpty(宿泊到着) Then
                lbl_err_宿泊到着.Text = "<br />入力してください。"
                lbl_err_宿泊到着.Visible = True
            End If
            If (宿泊 = "所内泊(前泊)" Or 宿泊 = "所内泊(後泊)") AndAlso String.IsNullOrEmpty(夕食) Then
                lbl_err_夕食.Text = "<br />選択してください。"
                lbl_err_夕食.Visible = True
            End If
            If String.IsNullOrEmpty(昼食) Then
                lbl_err_昼食.Text = "<br />選択してください。"
                lbl_err_昼食.Visible = True
            End If
            If String.IsNullOrEmpty(機器) Then
                lbl_err_機器.Text = "<br />選択してください。"
                lbl_err_機器.Visible = True
            End If
            If 機器 = "その他" AndAlso String.IsNullOrEmpty(機器他) Then
                lbl_err_機器他.Text = "<br />入力してください。"
                lbl_err_機器他.Visible = True
            End If
            If String.IsNullOrEmpty(送り) Then
                lbl_err_送り.Text = "<br />選択してください。"
                lbl_err_送り.Visible = True
            End If
            If 送り <> "○" AndAlso String.IsNullOrEmpty(送り出発) Then
                lbl_err_送り出発.Text = "<br />入力してください。"
                lbl_err_送り出発.Visible = True
            End If
            If 送り = "その他" AndAlso String.IsNullOrEmpty(送り他) Then
                lbl_err_送り他.Text &= "<br />入力してください。"
                lbl_err_送り他.Visible = True
            End If
            If 送り <> "○" AndAlso TimeSpan.TryParse(送り出発, departTime) AndAlso TimeSpan.TryParse(講義終了時間, endTime) AndAlso departTime <= endTime Then
                lbl_err_送り出発.Text = "<br />講義終了時間より後の時間を入力してください。"
                lbl_err_送り出発.Visible = True
            End If

            Dim hasError As Boolean = lbl_err_別手段.Visible OrElse
                          lbl_err_別手段他.Visible OrElse
                          lbl_err_到着.Visible OrElse
                          lbl_err_車種.Visible OrElse
                          lbl_err_宿泊.Visible OrElse
                          lbl_err_宿泊到着.Visible OrElse
                          lbl_err_夕食.Visible OrElse
                          lbl_err_昼食.Visible OrElse
                          lbl_err_機器.Visible OrElse
                          lbl_err_機器他.Visible OrElse
                          lbl_err_送り.Visible OrElse
                          lbl_err_送り他.Visible OrElse
                          lbl_err_送り出発.Visible

            If hasError Then Return

            ' 一時保存
            SaveToTemporary(回数:=回数, 迎え:=迎え, 別手段:=別手段, 到着:=到着, 車種:=車種, 宿泊:=宿泊, 宿泊到着:=宿泊到着, 夕食:=夕食,
                            朝食:=朝食, 昼食:=昼食, 機器:=機器, 機器他:=機器他, 送り:=送り, 送り他:=送り他, 送り出発:=送り出発)

            Com_UC1_Visible("close")

            Response.Redirect(Com_Get_Content("menu_ks_003"))
        Catch ex As Exception
            ' エラー処理
            Dim mes = "登録に失敗しました。"
            lbl_errMes.Text = mes
            lbl_errMes.Visible = True
        End Try

    End Sub
#End Region

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

    Private 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()

            Return spm.ds.Tables(0)
        End Using
    End Function

    Private Sub Set回答DDL(ddl As DropDownList, itemName As String)

        If ddl IsNot Nothing Then
            ' 選択肢リストを取得してバインド
            If itemName = "機器" Then itemName = "使用機材"
            Dim dt選択肢 As DataTable = GetDDLData(itemName)
            If dt選択肢.Rows.Count > 0 Then
                ddl.DataSource = dt選択肢
                ddl.DataTextField = "内容"
                ddl.DataValueField = "内容"
                ddl.DataBind()

                '先頭に空白行
                ddl.Items.Insert(0, New ListItem("", ""))

                If itemName = "別手段" Then
                    ddl.Items.Insert(1, New ListItem("迎えバス(幕張本郷駅発)", "○"))
                End If
                If itemName = "送り" Then
                    ddl.Items.Insert(1, New ListItem("送りバス(幕張本郷駅行き)", "○"))
                End If
            End If
        End If
    End Sub

    Private Function GetDDLData(itemName As String) As DataTable
        Dim dt As DataTable
        Using spm As New StoredProcedureManager("連絡票_入力_DDL")
            spm.AddPrmIn("@項目名", Data.SqlDbType.VarChar, 130, itemName)
            spm.Fill()

            dt = spm.ds.Tables(0)
            Return dt
        End Using
    End Function

    Private Function Get質問マスタ() As Dictionary(Of String, String)
        Dim dict As New Dictionary(Of String, String)
        Dim dt As DataTable
        Using spm As New StoredProcedureManager("連絡票_質問_SELECT")
            spm.Fill()

            dt = spm.ds.Tables(0)

            For Each row As DataRow In dt.Rows
                dict(Convert.ToString(row("項目名"))) = Convert.ToString(row("質問内容")).Replace(vbCrLf, "<br>").Replace(vbLf, "<br>")
            Next
        End Using

        Return dict
    End Function

    Private Sub SaveToTemporary(
        回数 As String,
        Optional 迎え As String = Nothing,
        Optional 別手段 As String = Nothing,
        Optional 到着 As String = Nothing,
        Optional 車種 As String = Nothing,
        Optional 宿泊 As String = Nothing,
        Optional 宿泊到着 As String = Nothing,
        Optional 夕食 As String = Nothing,
        Optional 朝食 As String = Nothing,
        Optional 昼食 As String = Nothing,
        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, 回数)
            spm.AddPrmIn("@迎え", Data.SqlDbType.VarChar, 10, ToDbValue(迎え))
            spm.AddPrmIn("@別手段", Data.SqlDbType.VarChar, 100, ToDbValue(別手段))
            spm.AddPrmIn("@到着", Data.SqlDbType.DateTime, ToDbDate(到着))
            spm.AddPrmIn("@車種", Data.SqlDbType.NVarChar, 100, ToDbValue(車種))
            spm.AddPrmIn("@宿泊", Data.SqlDbType.VarChar, 100, ToDbValue(宿泊))
            spm.AddPrmIn("@宿泊到着", Data.SqlDbType.DateTime, 4000, ToDbDate(宿泊到着))
            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, 100, ToDbValue(機器))
            spm.AddPrmIn("@機器他", Data.SqlDbType.NVarChar, 100, ToDbValue(機器他))
            spm.AddPrmIn("@送り", Data.SqlDbType.VarChar, 100, ToDbValue(送り)) '10→100にカラム長UP MOD 20260402 S.Iguchi
            spm.AddPrmIn("@送り他", Data.SqlDbType.NVarChar, 100, ToDbValue(送り他))
            spm.AddPrmIn("@送り出発", Data.SqlDbType.DateTime, ToDbDate(送り出発))

            spm.Exec()

        End Using
    End Sub
#End Region

End Class
