Imports System.Data

Partial Class uc_SearchBranchCd
    Inherits System.Web.UI.UserControl
#Region "******************** メンバ変数 ********************"
    Private Const SESSION_LIST_PAGE_INDEX As String = "SearchBranchCd_PageIndex"   'ページの位置
    Private Const S_TEACHER_PROF_SEARCH_BRANCH_NAME As String = "S_Teacher_Prof_Search_BranchName"   '検索条件
#End Region

    Public Property BankCode As String
        Get
            Return txt_検索銀行CD.Text
        End Get
        Set(value As String)
            txt_検索銀行CD.Text = value
            '銀行CDが設定されたら自動で支店一覧を読み込む
            LoadBranches()
        End Set
    End Property

    Private Sub LoadBranches()
        If String.IsNullOrEmpty(txt_検索銀行CD.Text) Then
            Return
        End If
        Dim dt As DataTable = GetBranchList(txt_検索銀行CD.Text, txt_検索_支店.Text)
        gv_支店一覧.DataSource = dt
        gv_支店一覧.DataBind()
    End Sub
    Public Sub Initialize(bankCode As String)
        txt_検索銀行CD.Text = bankCode
        LoadBranches()
    End Sub
#Region "******************** ページイベント ********************"
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        '
    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 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 branchCd As String = DataBinder.Eval(e.Row.DataItem, "支店CD").ToString()
            Dim branchName As String = DataBinder.Eval(e.Row.DataItem, "支店名").ToString()
            btnSelect.OnClientClick = String.Format("selectBranchCode('{0}','{1}'); return false;", branchCd, branchName)

            ' データが空の場合、ボタンを非表示にする
            If branchCd Is "" Then
                e.Row.CssClass = "emptyRow"
                If btnSelect IsNot Nothing Then
                    btnSelect.Visible = False
                End If
            End If

        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_支店一覧.PageIndex = e.NewPageIndex
        LoadBranches()
    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_BRANCH_NAME) = txt_検索_支店.Text
        Create支店一覧()
    End Sub

    Private Sub Create支店一覧()
        Dim bankCd As String = hf_BankCd.Value
        Dim branchName As String = txt_検索_支店.Text

        If String.IsNullOrEmpty(bankCd) Then
            Return
        End If

        gv_支店一覧.DataSource = GetBranchList(bankCd, branchName)
        gv_支店一覧.DataBind()
    End Sub

    Private Function GetBranchList(bankCd As String, branchName As String) As DataTable
        Using spm As New StoredProcedureManager("共通_銀行支店一覧_SELECT")
            spm.AddPrmIn("@銀行CD", SqlDbType.VarChar, 20, bankCd)
            spm.AddPrmIn("@支店名", SqlDbType.VarChar, 100, branchName)
            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

            Return dt
        End Using
    End Function
#End Region

End Class
