Imports System.Data

Partial Class uc_SearchBankCd
    Inherits System.Web.UI.UserControl

#Region "******************** メンバ変数 ********************"
    Private Const SESSION_LIST_PAGE_INDEX As String = "SearchBankCd_PageIndex"   'ページの位置
    Private Const S_TEACHER_PROF_SEARCH_BANK_NAME As String = "S_Teacher_Prof_Search_BankName"   '検索条件
#End Region
    Public Class BankSelectedEventArgs
        Inherits EventArgs

        Public Property BankCd As String
        Public Property BankName As String
        Public Sub New(bankCd As String, bankName As String)
            Me.BankCd = bankCd
            Me.BankName = bankName
        End Sub

    End Class

    Public Event BankSelected As EventHandler(Of BankSelectedEventArgs)
#Region "******************** ページイベント ********************"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        img_title_bankCd.ImageUrl = "~/img/title_point.png"
        img_title_search.ImageUrl = "~/img/search.png"
        Dim searchValue As String = ""


        If Not IsPostBack() Then
            '
        ElseIf Not IsNothing(Session(S_TEACHER_PROF_SEARCH_BANK_NAME)) Then

            searchValue = Session(S_TEACHER_PROF_SEARCH_BANK_NAME).ToString()
            '検索条件を復元
            If Not searchValue = "" Then
                txt_検索_銀行.Text = searchValue
            End If

            Search銀行()

            Session(S_TEACHER_PROF_SEARCH_BANK_NAME) = searchValue
        End If


    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銀行()
    End Sub

    Protected Sub sql_銀行_Init(sender As Object, e As EventArgs) Handles sql_銀行.Init
        Search銀行()
    End Sub

    Protected Sub gv_銀行一覧_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gv_銀行一覧.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then

            If e.Row.RowIndex Mod 2 = 0 Then
                e.Row.CssClass = "gridViewRow"
            Else
                e.Row.CssClass = "gridViewRowAlternate"
            End If

            Dim btnSelect As Button = CType(e.Row.FindControl("btn_選択"), Button)
            Dim bankCd As String = DataBinder.Eval(e.Row.DataItem, "銀行CD").ToString()
            Dim bankName As String = DataBinder.Eval(e.Row.DataItem, "銀行名").ToString()
            btnSelect.OnClientClick = String.Format("selectBankCode('{0}','{1}'); return false;", bankCd, bankName)
            'データが空の場合、ボタンを非表示
            If bankCd Is "" Then
                e.Row.CssClass = "emptyRow"
                If btnSelect IsNot Nothing Then
                    btnSelect.Visible = False
                End If
            End If

        End If
    End Sub
    Protected Sub gv_銀行一覧_RowCommand(sender As Object, e As GridViewCommandEventArgs)
        If e.CommandName = "Select" Then
            Dim arguments As String() = e.CommandArgument.ToString().Split(","c)
            Dim bankCd As String = arguments(0)
            Dim bankName As String = arguments(1)

            '親ページにデータを渡す
            RaiseEvent BankSelected(Me, New BankSelectedEventArgs(bankCd, bankName))

            'モーダル閉じる
            ScriptManager.RegisterStartupScript(Me, Me.GetType(), "closeModal", "closeModal();", True)
        End If
    End Sub

    Private Sub BtnClose_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_close.Click
        ScriptManager.RegisterStartupScript(Me, Me.GetType(), "closeModal", "closeModal();", True)
    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(Session(S_TEACHER_PROF_SEARCH_BANK_NAME)) 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

#End Region

#Region "******************* Sub及びFunction ********************"
    Private Sub Search銀行()

        Session(S_TEACHER_PROF_SEARCH_BANK_NAME) = txt_検索_銀行.Text
        Create銀行一覧()
    End Sub

    Private Sub Create銀行一覧()

        Dim bankName As String = Session(S_TEACHER_PROF_SEARCH_BANK_NAME).ToString

        Using spm As New StoredProcedureManager("共通_銀行一覧_SELECT")
            spm.AddPrmIn("@銀行名", SqlDbType.VarChar, 100, bankName)
            spm.Fill()

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

            '現ページのデータ
            Dim pagedData As New PagedDataSource()
            pagedData.DataSource = dt.DefaultView
            pagedData.AllowPaging = True
            pagedData.PageSize = gv_銀行一覧.PageSize
            pagedData.CurrentPageIndex = gv_銀行一覧.PageIndex

            '空行追加してレイアウト崩れ防止
            Dim pageSize As Integer = gv_銀行一覧.PageSize
            Dim currentPageRowCount As Integer = pagedData.Count
            If currentPageRowCount < pageSize Then
                For i As Integer = currentPageRowCount To pageSize - 1
                    dt.Rows.Add(dt.NewRow())
                Next
            End If

            gv_銀行一覧.DataSource = dt
        End Using

        gv_銀行一覧.DataBind()
    End Sub

#End Region

End Class
