Imports System.Data
Imports AppCommon
Imports AppVariable

Partial Class uc_SearchCityCd
    Inherits System.Web.UI.UserControl

#Region "******************** メンバ変数 ********************"
    Private Const SESSION_LIST_PAGE_INDEX As String = "SearchCityCd_PageIndex"   'ページの位置
    Private Const S_SIGNUP_SEARCH_PREF_CD As String = "S_Signup_Search_PrefCd"   '検索条件
#End Region

    Public Class CitySelectedEventArgs
        Inherits EventArgs

        Public Property CityCd As String
        Public Property CityName As String
        Public Sub New(cityCd As String, cityName As String)
            Me.CityCd = cityCd
            Me.CityName = cityName
        End Sub

    End Class

    Public Event CitySelected As EventHandler(Of CitySelectedEventArgs)

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

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


        If Not IsPostBack() Then
            CreateControls()

        ElseIf Not IsNothing(Session(S_SIGNUP_SEARCH_PREF_CD)) Then

            searchValue = Session(S_SIGNUP_SEARCH_PREF_CD).ToString()
            '検索条件を復元
            If Not searchValue = "" Then
                dd_検索_都道府県.SelectedValue = searchValue
            End If

            Search市区町村()

            Session(S_SIGNUP_SEARCH_PREF_CD) = 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 cityCd As String = DataBinder.Eval(e.Row.DataItem, "市区町村CD").ToString()
            Dim cityName As String = DataBinder.Eval(e.Row.DataItem, "団体名").ToString()
            btnSelect.Attributes("onclick") = String.Format("selectCityCode('{0}','{1}'); return false;", cityCd, cityName)

            ' データが空の場合、ボタンを非表示にする
            If cityCd 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 cityCd As String = arguments(0)
            Dim cityName As String = arguments(1)

            '親ページにデータを渡す
            RaiseEvent CitySelected(Me, New CitySelectedEventArgs(cityCd, cityName))

            'モーダル閉じる
            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_SIGNUP_SEARCH_PREF_CD)) 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 CreateControls()
        Create検索_都道府県()
    End Sub

    Private Sub Create検索_都道府県()
        Using spm As New StoredProcedureManager("共通_県_DDL")
            spm.Fill()

            dd_検索_都道府県.DataSource = spm.ds.Tables(0)
        End Using

        dd_検索_都道府県.DataBind()
    End Sub


    Private Sub Search市区町村()

        Session(S_SIGNUP_SEARCH_PREF_CD) = dd_検索_都道府県.SelectedValue
        Create市区町村一覧()
    End Sub

    Private Sub Create市区町村一覧()

        Dim prefCd As String = Session(S_SIGNUP_SEARCH_PREF_CD).ToString

        Using spm As New StoredProcedureManager("共通_市区町村一覧_SELECT")
            spm.AddPrmIn("@県CD", SqlDbType.VarChar, 20, prefCd)
            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
