Imports System.Data
Imports AppVariable

Partial Class uc_AttendClassEdit
    Inherits System.Web.UI.UserControl

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

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If 編集許可判定() = 1 Then
            btn_更新.Enabled = False
            btn_更新.ForeColor = Drawing.Color.DarkGray

            btn_削除.Enabled = False
            btn_削除.ForeColor = Drawing.Color.DarkGray
        Else
            btn_更新.Enabled = True
            btn_更新.ForeColor = Drawing.Color.Red

            btn_削除.Enabled = True
            btn_削除.ForeColor = Drawing.Color.Red
        End If

        '---------------------------------------------------------------
        'ダイアログセット
        '---------------------------------------------------------------
        btn_削除.Attributes("onclick") = "return Dialog_YN(""データを削除します。この操作は元に戻すことができません。よろしいですか？"")"
    End Sub

#End Region

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

    Protected Sub gv_出欠状況_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gv_出欠状況.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            If CType(e.Row.FindControl("lb_色CD"), Label).Text <> "" Then
                e.Row.BackColor = Drawing.Color.FromName(CType(e.Row.FindControl("lb_色CD"), Label).Text)
            ElseIf CType(e.Row.FindControl("lb_出欠CD"), Label).Text = "" Then
                e.Row.BackColor = Drawing.Color.FromName("#F0F0F0")
            End If
        End If
    End Sub

    Protected Sub Calendar_SelectionChanged(sender As Object, e As EventArgs) Handles Calendar.SelectionChanged
        CType(frm_実施授業.FindControl("txt_実施日"), TextBox).Text = Format(Me.Calendar.SelectedDate, "yyy/MM/dd")
        lb_err.Text = ""
    End Sub

    Protected Sub btn_更新_Click(sender As Object, e As EventArgs) Handles btn_更新.Click
        lb_err.Text = ""

        '入力チェック
        If Nothing Is (CType(frm_実施授業.FindControl("txt_実施日"), TextBox).Text) _
                Or CType(frm_実施授業.FindControl("txt_実施日"), TextBox).Text = "" Then
            lb_err.Text = "エラー：実施日未入力"
            Exit Sub
        End If

        'コンテンツページのgv_出欠コントロールを取得
        Dim gv As GridView = CType(Page.Master.FindControl("msp_asp_cph1").FindControl("gv_出欠"), GridView)

        '授業出欠の更新
        For Each r As GridViewRow In gv.Rows
            IsRowModified(r)
        Next

        gv.DataBind()
    End Sub

    Protected Sub btn_削除_Click(sender As Object, e As EventArgs) Handles btn_削除.Click
        出欠削除()
        Response.Redirect(Request.RawUrl.ToString)
    End Sub

    Protected Sub sql_実施授業_Init(sender As Object, e As EventArgs) Handles sql_実施授業.Init
        sql_実施授業_SET()
    End Sub

    Protected Sub sql_出欠種_Init(sender As Object, e As EventArgs) Handles sql_出欠種.Init
        sql_出欠種_SET()
    End Sub

    Protected Sub sql_出欠状況_Init(sender As Object, e As EventArgs) Handles sql_出欠状況.Init
        sql_出欠状況_SET()
    End Sub

#End Region

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

    Private Function 編集許可判定() As Integer
        Dim ret As Integer

        Using spm As New StoredProcedureManager("出欠_出欠編集判定_OUTPUT")
            spm.AddPrmIn("@USERCD", Data.SqlDbType.VarChar, Session(S_COM_UserCD))
            spm.AddPrmIn("@履修年度", Data.SqlDbType.SmallInt, Session(S_COM_Selected_Risyu_Nendo))
            spm.AddPrmIn("@成績年度", Data.SqlDbType.SmallInt, Session(S_COM_Selected_Seiseki_Nendo))
            spm.AddPrmIn("@分野CD", Data.SqlDbType.VarChar, Session(S_COM_Selected_Bunya_CD))
            spm.AddPrmIn("@科目CD", Data.SqlDbType.VarChar, Session(S_COM_Selected_Kamoku_CD))
            spm.AddPrmIn("@講座CD", Data.SqlDbType.VarChar, Session(S_COM_Selected_Kouza_CD))
            spm.AddPrmOut("@編集判定", Data.SqlDbType.Int)
            spm.Exec()

            ret = spm.GetValue("@編集判定")
        End Using

        Return ret
    End Function

    Private Sub IsRowModified(ByVal r As GridViewRow)
        Dim 実施日 As Date
        Dim 学籍NO As String
        Dim Check As Integer
        Dim 出欠CD As String
        Dim 備考 As String

        実施日 = CType(frm_実施授業.FindControl("txt_実施日"), TextBox).Text
        学籍NO = CType(r.FindControl("lb_学籍NO"), Label).Text
        Check = 1
        出欠CD = CType(r.FindControl("rbl_出欠種"), RadioButtonList).SelectedValue
        備考 = CType(r.FindControl("txt_備考"), TextBox).Text

        出欠更新(実施日, 学籍NO, Check, 出欠CD, 備考)
    End Sub

    Private Sub 出欠更新(実施日 As Date, _
                            学籍NO As String, _
                            Check As Integer, _
                            出欠CD As String, _
                            備考 As String)
        Using spm As New StoredProcedureManager("出欠_授業出欠_UPDATE")
            spm.AddPrmIn("@実施日", Data.SqlDbType.DateTime, 実施日)
            spm.AddPrmIn("@履修年度", Data.SqlDbType.SmallInt, Session(S_COM_Selected_Risyu_Nendo))
            spm.AddPrmIn("@成績年度", Data.SqlDbType.SmallInt, Session(S_COM_Selected_Seiseki_Nendo))
            spm.AddPrmIn("@分野CD", Data.SqlDbType.VarChar, Session(S_COM_Selected_Bunya_CD))
            spm.AddPrmIn("@科目CD", Data.SqlDbType.VarChar, Session(S_COM_Selected_Kamoku_CD))
            spm.AddPrmIn("@講座CD", Data.SqlDbType.VarChar, Session(S_COM_Selected_Kouza_CD))
            spm.AddPrmIn("@回数", Data.SqlDbType.Int, Session(S_COM_Selected_Jyugyo_Kai))
            spm.AddPrmIn("@学籍NO", Data.SqlDbType.VarChar, 学籍NO)
            spm.AddPrmIn("@CK", Data.SqlDbType.Int, Check)
            spm.AddPrmIn("@出欠CD", Data.SqlDbType.VarChar, 出欠CD)
            spm.AddPrmIn("@備考", Data.SqlDbType.VarChar, 備考)
            spm.Exec()
        End Using
    End Sub

    Private Sub 出欠削除()
        Using spm As New StoredProcedureManager("出欠_授業出欠_DELETE")
            spm.AddPrmIn("@履修年度", Data.SqlDbType.SmallInt, Session(S_COM_Selected_Risyu_Nendo))
            spm.AddPrmIn("@成績年度", Data.SqlDbType.SmallInt, Session(S_COM_Selected_Seiseki_Nendo))
            spm.AddPrmIn("@分野CD", Data.SqlDbType.VarChar, Session(S_COM_Selected_Bunya_CD))
            spm.AddPrmIn("@科目CD", Data.SqlDbType.VarChar, Session(S_COM_Selected_Kamoku_CD))
            spm.AddPrmIn("@講座CD", Data.SqlDbType.VarChar, Session(S_COM_Selected_Kouza_CD))
            spm.AddPrmIn("@回数", Data.SqlDbType.Int, Session(S_COM_Selected_Jyugyo_Kai))
            spm.Exec()
        End Using
    End Sub

    Private Sub sql_実施授業_SET()
        Dim sds As SqlDataSource = sql_実施授業
        WebControlsUtil.DeclareSP(sds, "出欠_実施授業_個別_SELECT")
        With sds
            .SelectParameters.Add("履修年度", Session(S_COM_Selected_Risyu_Nendo))
            .SelectParameters.Add("成績年度", Session(S_COM_Selected_Seiseki_Nendo))
            .SelectParameters.Add("分野CD", Session(S_COM_Selected_Bunya_CD))
            .SelectParameters.Add("科目CD", Session(S_COM_Selected_Kamoku_CD))
            .SelectParameters.Add("講座CD", Session(S_COM_Selected_Kouza_CD))
            .SelectParameters.Add("回数", DbType.Int32, Session(S_COM_Selected_Jyugyo_Kai))
        End With
    End Sub

    Private Sub sql_出欠種_SET()
        Dim sds As SqlDataSource = sql_出欠種
        WebControlsUtil.DeclareSP(sds, "出欠_出欠種_SELECT")
    End Sub

    Private Sub sql_出欠状況_SET()
        Dim sds As SqlDataSource = sql_出欠状況
        WebControlsUtil.DeclareSP(sds, "出欠_実施授業_個別状況_SELECT")
        With sds
            .SelectParameters.Add("履修年度", Session(S_COM_Selected_Risyu_Nendo))
            .SelectParameters.Add("成績年度", Session(S_COM_Selected_Seiseki_Nendo))
            .SelectParameters.Add("分野CD", Session(S_COM_Selected_Bunya_CD))
            .SelectParameters.Add("科目CD", Session(S_COM_Selected_Kamoku_CD))
            .SelectParameters.Add("講座CD", Session(S_COM_Selected_Kouza_CD))
            .SelectParameters.Add("回数", DbType.Int32, Session(S_COM_Selected_Jyugyo_Kai))
        End With
    End Sub

#End Region

End Class
