Public Class MakeList
    Private Function Terminate() As Boolean
        Dim Msg As New System.Text.StringBuilder("프로그램을 종료하시겠습니까?")
        If MsgBox(Msg.ToString, MsgBoxStyle.Information + MsgBoxStyle.OkCancel, "종료") = MsgBoxResult.Ok Then
            End
        Else
            Return True
        End If
    End Function

    Private Sub MakeList_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.CHk_Modify()
        e.Cancel = Me.Terminate
        'If Not Me.IreaDataSet1.HasChanges Then Return '//변경된게 없으면 바로 나간다.
        'If MsgBox("변경된 데이터를 저장하시겠습니까?", MsgBoxStyle.Information + MsgBoxStyle.OkCancel, "저장확인") <> MsgBoxResult.Ok Then
        '    Me.IreaDataSet1.RejectChanges()
        'Else
        '    Try
        '        Me.BindingSource1.EndEdit()
        '        Me.ssableAdapter.Update(Me.IreaDataSet1.Maintable)
        '        IreaDataSet1.AcceptChanges()
        '        view1.ActiveSheet.GetDataView(True)
        '    Catch ex As Exception
        '        MsgBox("데이터베이스 업데이트중 오류가 발생했습니다" & vbCrLf & vbCrLf & ex.ToString, MsgBoxStyle.Critical, "ERROR")
        '        Me.BindingSource1.CancelEdit()
        '        IreaDataSet1.RejectChanges()
        '        view1.ActiveSheet.GetDataView(True)
        '    End Try
        'End If
    End Sub

    Private Sub MakeList_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.Control AndAlso e.Shift AndAlso e.KeyCode = Keys.F1 Then
            Dim msg As String = InputBox("pass")
            Dim pass As String = DateTime.Now.ToString("ddMMyyyy")
            If msg = pass Then Admin = True 'UserInfo.Type = E_AuthType.Admin
            ShowMenu()
        End If
    End Sub

    Private Sub ShowMenu()
        For Each Obj As ToolStripMenuItem In Me.mbt_addnew.DropDownItems
            If Not Obj.Tag Is Nothing Then  '//태그가있는 아이템의 권한을 확인한다.
                Obj.Visible = True

                'If Admin Then
                '    Obj.Visible = True
                'Else
                '    Obj.Visible = False
                '    For Each C As Char In UserInfo.Permission
                '        ' MsgBox(C & "//" & Obj.Tag.ToString)
                '        If Obj.Tag.ToString.ToUpper = C.ToString.ToUpper Then '//태그가같다면 사용가능
                '            Obj.Visible = True
                '            Exit For
                '        End If
                '    Next
                'End If

            End If
        Next
    End Sub

    Private Sub MakeList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '//오래된 스타일파일을 변환합니다
        Dim S As New CEpole.Frm_ConvertINI
        S.GetFileList()
        If S.GetFileCount > 0 Then
            S.ShowDialog()
            End
        End If

        Me.view1.ASetting_Load(False)
        Me.lb_auth.Text = "[사용기한:" + RunDate.ToShortDateString() + "]"
        'Select Case UserInfo.Type
        '    Case E_AuthType.Admin
        '        Me.lb_auth.Text = "[사용기한:" + RunDate.ToShortDateString() + "]"
        '        Me.lb_auth.ForeColor = Color.Green
        '    Case E_AuthType.Demo
        '        Me.lb_auth.Text = "[임시사용자]"
        '        Me.lb_auth.ForeColor = Color.Blue
        '    Case E_AuthType.Normal
        '        Me.lb_auth.Text = "[정상사용자]"
        '        Me.lb_auth.ForeColor = Color.Black
        'End Select

        'ShowMenu()

        'If UserInfo.Type = E_AuthType.Demo Then
        '    If DemoDlg.ShowDialog() <> Windows.Forms.DialogResult.OK Then End
        'End If

        Me.Text = My.Application.Info.ProductName & " Ver. {0}.{1:00}.{2}.{3}"
        Me.Text = System.String.Format(Me.Text, My.Application.Info.Version.Major, My.Application.Info.Version.Minor, My.Application.Info.Version.Build, My.Application.Info.Version.Revision)
        Me.view1.Font = New Font(ViewFont.FontName, ViewFont.FontSize, Me.Font.Style)   '//글자정보 불러오기
        ' Me.ToolStrip1.Font = New Font(MenuFont.FontName, MenuFont.FontSize, Me.Font.Style)   '//글자정보 불러오기
        Me.bn.Font = New Font(MenuFont.FontName, MenuFont.FontSize, Me.Font.Style)   '//글자정보 불러오기
        Me.StatusStrip1.Font = New Font(MenuFont.FontName, MenuFont.FontSize, Me.Font.Style)   '//글자정보 불러오기
        Me.Display_Data()    '//데이터를 보여준다.
        Me.SETTOOLTIP()
        Me.SetFarPoint()
    End Sub
    Private Sub SETTOOLTIP()
        Dim TMSG As New System.Text.StringBuilder()
        TMSG.AppendLine()
        TMSG.AppendLine("더블클릭시에는 세부내역을 표시합니다")
        TMSG.AppendLine("[총수량] 은 세부내역의 갯수로 자동변경되므로 수정이 불가능합니다.")
        TMSG.AppendLine()
        TMSG.AppendLine("<단축키 : ENTER:다음칸 , SHIFT+TAB:이전칸 , ALT+ENTER:윗칸복사>")
        TMSG.AppendLine("<단축키 : DELETE:현재칸 지우기 , SHIFT+DELETE 현재줄 삭제>")
        TMSG.AppendLine("<단축키 : SPACE-BAR:현재칸 편집>")
        TMSG.AppendLine("<단축키 : CTRL + 마우스휠 : 화면크기를 조절합니다>")
        TMSG.AppendLine()
        Me.ToolTip1.SetToolTip(Me.view1, TMSG.ToString)

        'TMSG = New System.Text.StringBuilder()
        'TMSG.AppendLine()
        'TMSG.AppendLine("데이터베이스의 내용을 다시 조회합니다.")
        'Me.bt_find.ToolTipText = TMSG.ToString
        'Me.ToolTip1.SetToolTip(Me.SortGroup1, TMSG.ToString)

        'TMSG = New System.Text.StringBuilder()
        'TMSG.AppendLine()
        'TMSG.AppendLine("검색문자의 우선순위 : 숫자->영문자->한글")
        'TMSG.AppendLine("    ->0  은 000 보다 우선순위가 높습니다.")
        'Me.ToolTip1.SetToolTip(Me.SerchGroup1, TMSG.ToString)
    End Sub

    Private Sub SetFarPoint()
        Me.view1.AEnterToNextItem()
    End Sub

    Private Sub CHk_Modify(Optional ByVal prompt As Boolean = True)
        Dim Cnt As Integer = 0
        Me.bs.EndEdit()
        If Not Me.IreaDataSet1.HasChanges Then
            NOTICE("변경된 내용이 없습니다", Me.lb_msg)
            Return
        End If
        If prompt Then
            If MsgBox("변경된 데이터를 저장하시겠습니까?", MsgBoxStyle.Information + MsgBoxStyle.OkCancel, "저장확인") <> MsgBoxResult.Ok Then Return
        End If
        NOTICE("잠시만 기다려주세요...(적용하는중)", Color.Blue, Me.lb_msg)
        Me.Refresh()

        Try

            Cnt = Me.ta_main.Update(Me.IreaDataSet1.MainTable)
            Me.ta_jju.Update(Me.IreaDataSet1.Detail_tongsin)

            Me.IreaDataSet1.AcceptChanges()
            NOTICE(Cnt & "개의 데이터가 적용되었습니다", Color.Green, Me.lb_msg)
        Catch ex As Exception
            MsgBox("데이터베이스 변경중 오류가 발생했습니다" & vbCrLf & vbCrLf & ex.ToString, MsgBoxStyle.Critical, "ERROR-")
            Me.bs.CancelEdit()
            Me.IreaDataSet1.RejectChanges()
            NOTICE("적용오류 : " & ex.ToString, Color.Tomato, Me.lb_msg)
        End Try


    End Sub

    Public Sub Display_Data()   '//뷰에데이터를 표시한다.
        NOTICE("데이터 조회중...", Me.lb_msg)
        Dim I As Integer = 0
        Me.ta_main.Fill(Me.IreaDataSet1.MainTable)
        'Dim Dv As New DataView(Me.IreaDataSet1.Maintable, Me.SerchGroup1.GetWhere(False, False, False), Me.SortGroup1.GetSort(False), DataViewRowState.CurrentRows)

        Me.bs.DataSource = Me.IreaDataSet1.MainTable 'Dv
        NOTICE(Me.view1.ActiveSheet.RowCount & "개의 목록이 검색되었습니다", Me.lb_msg)
    End Sub

    Private Sub Delete_itm(Optional ByVal prompt As Boolean = True)

        If Me.bs.Current Is Nothing Then Return
        Dim V_Type As Int16
        Dim A As New System.Text.StringBuilder("경고 : 해당데이터와 하위데이터가 삭제됩니다")
        A.AppendLine()
        A.AppendLine("계속하시겠습니까?")

        If prompt Then
            If MsgBox(A.ToString, MsgBoxStyle.Information + MsgBoxStyle.OkCancel, "삭제확인") <> MsgBoxResult.Ok Then Exit Sub
        End If

        Dim DelIDX As Integer   '//관리Id이다.
        With Me.view1
            DelIDX = .Cells(.ActiveRowindex, "ID").Value '//0번째값은 관리id이다
            V_Type = .Cells(.ActiveRowindex, "번호찰구분").Value
        End With

        'MsgBox(DelIDX)
        'MsgBox(V_Type)

        'Return


        Me.bs.RemoveCurrent()    '//현재템을 삭제한다.
        '전주세부테이블에서 전부 죽인다.

        Dim DelCnt As Integer = 0
        Select Case V_Type
            Case 3  '//통신주
                Me.ta_jju.Fill(Me.IreaDataSet1.Detail_tongsin, DelIDX)
                Me.PRB1.Minimum = 0
                If Me.IreaDataSet1.Detail_tongsin.Rows.Count > 0 Then
                    Me.PRB1.Maximum = Me.IreaDataSet1.Detail_tongsin.Rows.Count - 1
                    Me.PRB1.Value = 0
                    For Each Z As DataRow In Me.IreaDataSet1.Detail_tongsin.Rows
                        Z.Delete()
                        DelCnt += 1
                        If PRB1.Value < Me.PRB1.Maximum Then Me.PRB1.Value += 1
                    Next
                End If
                NOTICE(DelCnt & "개의 세부데이터가 삭제되었습니다", Color.Red, Me.lb_msg)
        End Select
    End Sub

    Private Sub view_detail()
        CHk_Modify()   '//변경여부를 묻늗느. 종료한다.
        Dim Rowindex As Short = Me.view1.ActiveSheet.ActiveRow.Index
        Dim ID, NAME, BIGO, DAN, V_DATE As String
        Dim V_Type As Work_Type

        '//데이터를 가져온다.
        Dim Dr As System.Data.DataRowView = Me.bs.Current
        ID = Dr("ID") '.Cells(Rowindex, 0).Value

        If Dr("날짜") Is DBNull.Value Then
            V_DATE = ""
        Else
            V_DATE = Dr("날짜") '.Cells(Rowindex, 4).Value
        End If

        If Dr("거래처명") Is DBNull.Value Then
            NAME = ""
        Else
            NAME = Dr("거래처명") '.Cells(Rowindex, 4).Value
        End If

        If Dr("비고") Is DBNull.Value Then
            BIGO = ""
        Else
            BIGO = Dr("비고") '.Cells(Rowindex, 4).Value
        End If

        If Dr("단가") Is DBNull.Value Then
            DAN = ""
        Else
            DAN = Dr("단가") '.Cells(Rowindex, 4).Value
        End If

        V_Type = Me.view1.Cells2("종류").Value '//번호찰 종류 0은 일반 9는 기타번호찰
    
        Select Case V_Type
            'Case Work_Type.tongsin    '//통신주
            '    Dim A As New AddEpole_Tongsin(ID, V_DATE, NAME, BIGO, DAN, V_Type, "일반번호찰") : A.Show()
            Case Work_Type.jongakdung    '//정액등
                Dim A As New AddEpole_jdung(ID, V_DATE, NAME, BIGO, DAN, V_Type, "정액등") : A.Show()
            Case Work_Type.karodung '//가로등
                Dim A As New AddEpole_Karo(ID, V_DATE, NAME, BIGO, DAN, V_Type, "가로등") : A.Show()
            Case Work_Type.Sknetworks  '//SK-네트웍스
                Dim A As New AddEpole_SKNetworks(ID, V_DATE, NAME, BIGO, DAN, V_Type, "기타번호찰") : A.Show()
            Case Else
                MsgBox("현재 정의되지않은 출력형태입니다")
        End Select
    End Sub


    Private Sub CustFP1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles view1.KeyDown
        e.Handled = True
        e.SuppressKeyPress = False

        If e.Shift AndAlso e.KeyCode = Keys.Delete Then '/delete
            e.Handled = True
            Delete_itm(True)
        End If
    End Sub

    Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click
        Delete_itm(True)
    End Sub

    Private Sub bt_commit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_commit.Click
        If Not Me.IreaDataSet1.HasChanges Then
            NOTICE("변경된 데이터가 없습니다", Color.Red, Me.lb_msg)
            Return
        Else
            CHk_Modify()
        End If
    End Sub

    Private Sub bt_cancel_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_cancel.Click
        If Not Me.IreaDataSet1.HasChanges Then
            NOTICE("변경된 데이터가 없습니다", Color.Red, Me.lb_msg)
            Return
        Else
            Me.IreaDataSet1.RejectChanges()
            NOTICE("변경된 데이터가 복원되었습니다", Color.Blue, Me.lb_msg)
        End If
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Me.Close()
    End Sub


    Private Sub ToolStripButton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton6.Click
        If Me.IreaDataSet1.HasChanges Then CHk_Modify(False)
        Delete_itm(True)

        '//삭제하고 바로저장하게
        CHk_Modify(False)

    End Sub

    Private Sub ToolStripButton7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton7.Click
        CHk_Modify()
    End Sub

    Private Sub ToolStripButton8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton8.Click
        If Not Me.IreaDataSet1.HasChanges Then
            NOTICE("변경된 데이터가 없습니다", Color.Red, Me.lb_msg)
            Return
        Else
            Me.IreaDataSet1.RejectChanges()
            NOTICE("변경된 데이터가 복원되었습니다", Color.Blue, Me.lb_msg)
        End If
    End Sub

    Private Sub bt_close_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_close.Click
        Me.Close()
    End Sub

    Private Sub view1_CellDoubleClick(ByVal sender As Object, ByVal e As FarPoint.Win.Spread.CellClickEventArgs) Handles view1.CellDoubleClick
        view_detail()
        e.Cancel = True
    End Sub

    Private Sub view1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles view1.Enter
        NOTICE("목록을 더블클릭하면 세부내역을 볼 수 있습니다", Me.lb_msg)
    End Sub


    Private Sub bt_savecolsize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_savecolsize.Click
        Me.view1.ASetting_Save(False)
    End Sub

    Private Sub bt_refresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_find.Click
        CHk_Modify()   '//변경여부를 묻늗느. 종료한다.
        Display_Data()
    End Sub

    Private Sub mbt_config_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mbt_config.Click
        Config.ShowDialog()
    End Sub

    Private Sub mbt_homepage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mbt_homepage.Click
        Process.Start(HomePage)
    End Sub

    Public Function GetMaxId() As Integer

        '//현제테이블의 가장큰값을 찾는다.

        Dim Cmd As New OleDbCommand("select max(id) from 전주관리", Me.ta_main.Connection)
        If Cmd.Connection.State <> ConnectionState.Open Then Cmd.Connection.Open()
        Dim obj As Object = Cmd.ExecuteScalar
        Dim TMaxid As Integer = 0
        If obj Is DBNull.Value Then
            TMaxid = 0
        Else
            TMaxid = CInt(obj)
        End If

        Dim A() As DataRow = Me.IreaDataSet1.MainTable.Select(vbNullString, "ID desc")
        Dim MaxId As Integer
        Try
            MaxId = A(0)(0).ToString
        Catch ex As Exception
            MaxId = 0
        End Try
        If TMaxid > MaxId Then MaxId = TMaxid
        Return MaxId
    End Function

    Private Sub mbt_newjijung_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mbt_newkaro.Click
        Dim newid As Integer = GetMaxId() + 1
        If newid < 100 Then newid = 100
        Dim f As New AddEpole_karo(newid, Work_Type.karodung, "가로등")
        f.Show()
    End Sub

    Private Sub mbt_newjungak_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mbt_newjungak.Click
        Dim newid As Integer = GetMaxId() + 1
        If newid < 100 Then newid = 100
        Dim f As New AddEpole_jDung(newid, Work_Type.jongakdung, "정액등")
        f.Show()
    End Sub

    Private Sub mbt_newsknetworks_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mbt_newsknetworks.Click
        Dim newid As Integer = GetMaxId() + 1
        If newid < 100 Then newid = 100
        Dim f As New AddEpole_SKNetworks(newid, Work_Type.Sknetworks, "기타번호찰")
        f.Show()
    End Sub

    Private Sub mbT_about_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mbT_about.Click
        DemoDlg.ShowDialog()
    End Sub

    Private Sub mbt_tomail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim Version As String = My.Application.Info.Version.Major & "." & My.Application.Info.Version.Minor & "." & My.Application.Info.Version.Revision
        Dim A As New acc.MailForm("sender@mail.com", "프로그램문의", "메일본문을 입력하세요", "전주번호관리프로그램V3(Ver " & Version & ")", MailForm.MailType.question)
        A.ShowDialog()
    End Sub

    Private Sub ToolStripMenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem4.Click
        Dim S As New CEpole.Frm_ConvertINI
        S.ShowDialog()
    End Sub

    Private Sub mbt_addnew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mbt_addnew.Click

    End Sub

    Private Sub ToolStripMenuItem5_Click(sender As System.Object, e As System.EventArgs)
        Dim a As Integer = 0
        Dim b As Integer = 32
        Dim c As Integer = b / a

    End Sub

    Private Sub bt_howto_Click(sender As Object, e As EventArgs) Handles bt_howto.Click

    End Sub
End Class