Option Explicit On
Imports System.IO
Public Class 系統組織結構與權限設定
    Dim HH As String
    Dim RI As Integer = -1
    Dim SK As String = "要查找的 KEY"
    Dim Str As String = Application.StartupPath
    Dim 對話框(18) As String
    Private Sub Set_使用者清單()
        Dim ds, ds1 As New DataSet : 清單_dgv.DataSource = Nothing : 清單_1_dgv.DataSource = Nothing : ds.Clear() : ds1.Clear()
        清單_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        清單_dgv.ColumnHeadersHeight = 35 : 清單_dgv.AllowUserToAddRows = False : 清單_dgv.RowTemplate.Height = 35
        清單_1_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        清單_1_dgv.ColumnHeadersHeight = 35 : 清單_1_dgv.AllowUserToAddRows = False : 清單_1_dgv.RowTemplate.Height = 35

        If 小間距_ch.Checked = False And 中間距_ch.Checked = False And 大間距_ch.Checked = True Then : 清單_1_dgv.RowTemplate.Height = 75
        ElseIf 小間距_ch.Checked = False And 中間距_ch.Checked = True And 大間距_ch.Checked = False Then : 清單_1_dgv.RowTemplate.Height = 45
        ElseIf 小間距_ch.Checked = True And 中間距_ch.Checked = False And 大間距_ch.Checked = False Then : 清單_1_dgv.RowTemplate.Height = 25 : End If

        SQL_系統組織結構圖_人員()
        da.Fill(ds) : 清單_dgv.DataSource = ds.Tables(0) : da.Fill(ds1) : 清單_1_dgv.DataSource = ds1.Tables(0) : conn.Close()

        清單_1_dgv.Columns(0).FillWeight = 90 : 清單_1_dgv.Columns(1).FillWeight = 140 : 清單_1_dgv.Columns(2).FillWeight = 45 : 清單_1_dgv.Columns(3).Visible = False
        清單_1_dgv.Columns(4).Visible = False : 清單_1_dgv.Columns(5).Visible = False : 清單_1_dgv.Columns(6).Visible = False : 清單_1_dgv.Columns(7).Visible = False

        For i As Integer = 0 To 清單_1_dgv.Rows.Count - 1
            Dim 驗證 As String = "" : Dim 取變數 As String
            取變數 = 清單_1_dgv.Rows(i).Cells(1).Value.ToString : NN1 = 0
            For ii As Integer = 1 To 20
                If ii = 1 Then : 驗證 = Strings.Left(取變數, ii) : NN1 += 1 : Else
                    If Strings.Mid(取變數, ii, 1) = "/" Then : Exit For : Else : 驗證 &= Strings.Mid(取變數, ii, 1) : NN1 += 1 : End If
                End If
            Next : 清單_1_dgv.Rows(i).Cells(1).Value = 驗證

            取變數 = 清單_1_dgv.Rows(i).Cells(3).Value.ToString : NN1 = 0
            For ii As Integer = 1 To 20
                If ii = 1 Then : 驗證 = Strings.Left(取變數, ii) : NN1 += 1 : Else
                    If Strings.Mid(取變數, ii, 1) = " " Then : Exit For : Else : 驗證 &= Strings.Mid(取變數, ii, 1) : NN1 += 1 : End If
                End If
            Next : 清單_1_dgv.Rows(i).Cells(3).Value = 驗證

            取變數 = 清單_1_dgv.Rows(i).Cells(4).Value.ToString : NN1 = 0
            For ii As Integer = 1 To 20
                If ii = 1 Then : 驗證 = Strings.Left(取變數, ii) : NN1 += 1 : Else
                    If Strings.Mid(取變數, ii, 1) = " " Then : Exit For : Else : 驗證 &= Strings.Mid(取變數, ii, 1) : NN1 += 1 : End If
                End If
            Next : 清單_1_dgv.Rows(i).Cells(4).Value = 驗證

            取變數 = 清單_1_dgv.Rows(i).Cells(5).Value.ToString : NN1 = 0
            For ii As Integer = 1 To 20
                If ii = 1 Then : 驗證 = Strings.Left(取變數, ii) : NN1 += 1 : Else
                    If Strings.Mid(取變數, ii, 1) = " " Then : Exit For : Else : 驗證 &= Strings.Mid(取變數, ii, 1) : NN1 += 1 : End If
                End If
            Next : 清單_1_dgv.Rows(i).Cells(5).Value = 驗證
        Next
    End Sub
    Private Sub Set_設定清單()
        Dim ds, ds1 As New DataSet
        設定_dgv.DataSource = Nothing : ds1.Clear() : 設定1_dgv.DataSource = Nothing : ds1.Clear()
        設定_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        設定_dgv.ColumnHeadersHeight = 25 : 設定_dgv.AllowUserToAddRows = False : 設定_dgv.RowTemplate.Height = 35
        設定1_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        設定1_dgv.ColumnHeadersHeight = 25 : 設定1_dgv.AllowUserToAddRows = False : 設定1_dgv.RowTemplate.Height = 35

        If 小間距1_ch.Checked = False And 中間距1_ch.Checked = False And 大間距1_ch.Checked = True Then : 設定1_dgv.RowTemplate.Height = 75
        ElseIf 小間距1_ch.Checked = False And 中間距1_ch.Checked = True And 大間距1_ch.Checked = False Then : 設定1_dgv.RowTemplate.Height = 45
        ElseIf 小間距1_ch.Checked = True And 中間距1_ch.Checked = False And 大間距1_ch.Checked = False Then : 設定1_dgv.RowTemplate.Height = 25
        End If

        PA1 = 姓名_tb.Text : PA2 = 帳號_tb.Text
        SQL_跨部門主管權限設定_確認有無資料()
        If dr.Read() Then : conn.Close() : PA3 = 級別_tb.Text : SQL_跨部門主管權限設定_已設定過() : Label2.Text = "Have Files"
        Else : conn.Close() : PA3 = 級別_tb.Text : SQL_跨部門主管權限設定_未設定過() : Label2.Text = "No Files" : End If

        da.Fill(ds1) : 設定_dgv.DataSource = ds1.Tables(0) : da.Fill(ds) : 設定1_dgv.DataSource = ds.Tables(0) : conn.Close()

        設定1_dgv.Columns(0).FillWeight = 90 : 設定1_dgv.Columns(1).FillWeight = 90 : 設定1_dgv.Columns(2).FillWeight = 90 : 設定1_dgv.Columns(3).FillWeight = 90
        設定1_dgv.Columns(4).FillWeight = 160 : 設定1_dgv.Columns(5).FillWeight = 60 : 設定1_dgv.Columns(6).FillWeight = 60 : 設定1_dgv.Columns(7).FillWeight = 40

        For i As Integer = 0 To 設定1_dgv.Rows.Count - 1
            If 設定1_dgv.Rows(i).Cells("級別").Value.ToString = "01" Then : 設定1_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightGoldenrodYellow
            ElseIf 設定1_dgv.Rows(i).Cells("級別").Value.ToString = "02" Then : 設定1_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightGreen
            ElseIf 設定1_dgv.Rows(i).Cells("級別").Value.ToString = "03" Then : 設定1_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightPink
            ElseIf 設定1_dgv.Rows(i).Cells("級別").Value.ToString = "04" Then : 設定1_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightBlue
            ElseIf 設定1_dgv.Rows(i).Cells("級別").Value.ToString = "05" Then : 設定1_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightBlue : End If

            If IsDBNull(設定1_dgv.Rows(i).Cells("授權").Value) Then : 設定1_dgv.Rows(i).Cells("授權").Value = False : End If
            If IsDBNull(設定_dgv.Rows(i).Cells("授權").Value) Then : 設定_dgv.Rows(i).Cells("授權").Value = False : End If

            Dim 驗證 As String = "" : Dim 取變數 As String
            取變數 = 設定1_dgv.Rows(i).Cells(0).Value.ToString : NN1 = 0
            For ii As Integer = 1 To 20
                If ii = 1 Then : 驗證 = Strings.Left(取變數, ii) : NN1 += 1 : Else
                    If Strings.Mid(取變數, ii, 1) = " " Then : Exit For : Else : 驗證 &= Strings.Mid(取變數, ii, 1) : NN1 += 1 : End If
                End If
            Next : 設定1_dgv.Rows(i).Cells(0).Value = 驗證

            取變數 = 設定1_dgv.Rows(i).Cells(1).Value.ToString : NN1 = 0
            For ii As Integer = 1 To 20
                If ii = 1 Then : 驗證 = Strings.Left(取變數, ii) : NN1 += 1 : Else
                    If Strings.Mid(取變數, ii, 1) = " " Then : Exit For : Else : 驗證 &= Strings.Mid(取變數, ii, 1) : NN1 += 1 : End If
                End If
            Next : 設定1_dgv.Rows(i).Cells(1).Value = 驗證

            取變數 = 設定1_dgv.Rows(i).Cells(2).Value.ToString : NN1 = 0
            For ii As Integer = 1 To 20
                If ii = 1 Then : 驗證 = Strings.Left(取變數, ii) : NN1 += 1 : Else
                    If Strings.Mid(取變數, ii, 1) = " " Then : Exit For : Else : 驗證 &= Strings.Mid(取變數, ii, 1) : NN1 += 1 : End If
                End If
            Next : 設定1_dgv.Rows(i).Cells(2).Value = 驗證

            取變數 = 設定1_dgv.Rows(i).Cells(4).Value.ToString : NN1 = 0
            For ii As Integer = 1 To 20
                If ii = 1 Then : 驗證 = Strings.Left(取變數, ii) : NN1 += 1 : Else
                    If Strings.Mid(取變數, ii, 1) = "/" Then : Exit For : Else : 驗證 &= Strings.Mid(取變數, ii, 1) : NN1 += 1 : End If
                End If
            Next : 設定1_dgv.Rows(i).Cells(4).Value = 驗證
        Next
    End Sub
    Private Sub 系統組織結構圖_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.MdiParent = YS_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True
        小間距1_ch.Checked = True : 中間距1_ch.Checked = False : 大間距1_ch.Checked = False
        小間距_ch.Checked = True : 中間距_ch.Checked = False : 大間距_ch.Checked = False
        Panel1.SendToBack()

        清單_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", 7.6)
        清單_1_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", 7.6)
        設定_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", 7.6)
        分流計算.Enabled = True
    End Sub
    Private Sub 介面關閉_Closed(sender As Object, e As EventArgs) Handles MyBase.Closed
        MyModule1.虛擬桌面開啟()
    End Sub
    Private Sub 登入閒置控制_MouseMove(sender As Object, e As EventArgs) Handles MyBase.MouseMove
        timeNow = 0 : YS_ERP_SYS.Timer1.Enabled = False : YS_ERP_SYS.Timer1.Enabled = True
    End Sub
    Private Sub 登入閒置控制_KeyPress(sender As Object, e As EventArgs) Handles MyBase.KeyPress
        timeNow = 0 : YS_ERP_SYS.Timer1.Enabled = False : YS_ERP_SYS.Timer1.Enabled = True
    End Sub
    Private Sub 分流計算_Tick(sender As Object, e As EventArgs) Handles 分流計算.Tick
        分流計算.Enabled = False : 資料數 = 50
        組織圖_tv.Nodes.Clear() : 組織圖_1_tv.Nodes.Clear()
        組織圖_tv.Nodes.Add("HX / PGS LTD. C.") : 組織圖_1_tv.Nodes.Add("HX / PGS LTD. C.")
        MyMod.進度條() : SQL_系統組織結構圖_部門()
        While (dr.Read())
            組織圖_tv.Nodes(0).Nodes.Add(dr("部門")) : MyMod.進度條()
            Dim 驗證 As String = "" : Dim 取變數 As String
            取變數 = dr("部門").ToString : NN1 = 0
            For ii As Integer = 1 To 20
                If ii = 1 Then : 驗證 = Strings.Left(取變數, ii) : NN1 += 1 : Else
                    If Strings.Mid(取變數, ii, 1) = " " Then : Exit For : Else : 驗證 &= Strings.Mid(取變數, ii, 1) : NN1 += 1 : End If
                End If
            Next : 組織圖_1_tv.Nodes(0).Nodes.Add(驗證)
        End While : conn.Close()
        For I As Integer = 0 To 組織圖_tv.Nodes(0).Nodes.Count - 1
            PA = 組織圖_tv.Nodes(0).Nodes(I).Text
            SQL_系統組織結構圖_處別()
            While (dr.Read())
                組織圖_tv.Nodes(0).Nodes(I).Nodes.Add(dr("處")) : MyMod.進度條()
                Dim 驗證 As String = "" : Dim 取變數 As String
                取變數 = dr("處").ToString : NN1 = 0
                For ii As Integer = 1 To 20
                    If ii = 1 Then : 驗證 = Strings.Left(取變數, ii) : NN1 += 1 : Else
                        If Strings.Mid(取變數, ii, 1) = " " Then : Exit For : Else : 驗證 &= Strings.Mid(取變數, ii, 1) : NN1 += 1 : End If
                    End If
                Next : 組織圖_1_tv.Nodes(0).Nodes(I).Nodes.Add(驗證)
            End While : conn.Close()
            For J As Integer = 0 To 組織圖_tv.Nodes(0).Nodes(I).Nodes.Count - 1
                PA1 = 組織圖_tv.Nodes(0).Nodes(I).Nodes(J).Text
                SQL_系統組織結構圖_課別()
                While (dr.Read())
                    組織圖_tv.Nodes(0).Nodes(I).Nodes(J).Nodes.Add(dr("課")) : MyMod.進度條()
                    Dim 驗證 As String = "" : Dim 取變數 As String
                    取變數 = dr("課").ToString : NN1 = 0
                    For ii As Integer = 1 To 20
                        If ii = 1 Then : 驗證 = Strings.Left(取變數, ii) : NN1 += 1 : Else
                            If Strings.Mid(取變數, ii, 1) = " " Then : Exit For : Else : 驗證 &= Strings.Mid(取變數, ii, 1) : NN1 += 1 : End If
                        End If
                    Next : 組織圖_1_tv.Nodes(0).Nodes(I).Nodes(J).Nodes.Add(驗證)
                End While : conn.Close()
                For K As Integer = 0 To 組織圖_tv.Nodes(0).Nodes(I).Nodes(J).Nodes.Count - 1
                    PA2 = 組織圖_tv.Nodes(0).Nodes(I).Nodes(J).Nodes(K).Text
                    SQL_系統組織結構圖_組別()
                    While (dr.Read())
                        組織圖_tv.Nodes(0).Nodes(I).Nodes(J).Nodes(K).Nodes.Add(dr("組")) : MyMod.進度條()
                        組織圖_1_tv.Nodes(0).Nodes(I).Nodes(J).Nodes(K).Nodes.Add(dr("組"))
                    End While : conn.Close()
                Next
            Next
        Next
        組織圖_tv.Nodes(0).Expand() : 組織圖_1_tv.Nodes(0).Expand()
        YS_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0
    End Sub
    Private Sub 清單_1_dgv_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles 清單_1_dgv.RowPostPaint
        Dim linePen As New Pen(Color.Blue, 2)
        If e.RowIndex = 清單_1_dgv.Rows.Count - 1 Then
            Exit Sub
        Else
            If 清單_1_dgv("級別", e.RowIndex).Value.ToString = "03" Then
                Dim startX As Integer = IIf(清單_1_dgv.RowHeadersVisible, 清單_1_dgv.RowHeadersWidth, 0)
                Dim startY As Integer = e.RowBounds.Top + e.RowBounds.Height - 1
                Dim endX As Integer = startX + 清單_1_dgv.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - 清單_1_dgv.HorizontalScrollingOffset
                e.Graphics.DrawLine(linePen, startX, startY, endX, startY)
                Exit Sub
            End If
        End If
        For i As Integer = 0 To 清單_1_dgv.Rows.Count - 1
            If 清單_1_dgv.Rows(i).Cells("級別").Value.ToString = "01" Then : 清單_1_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightGoldenrodYellow
            ElseIf 清單_1_dgv.Rows(i).Cells("級別").Value.ToString = "02" Then : 清單_1_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightGreen
            ElseIf 清單_1_dgv.Rows(i).Cells("級別").Value.ToString = "03" Then : 清單_1_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightPink
            ElseIf 清單_1_dgv.Rows(i).Cells("級別").Value.ToString = "04" Then : 清單_1_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightBlue
            ElseIf 清單_1_dgv.Rows(i).Cells("級別").Value.ToString = "05" Then : 清單_1_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightBlue
            End If
        Next
    End Sub
    Private Function GetNodePath(node As TreeNode) As String
        ' Get the path of the node by traversing its parents and appending their names
        Dim path As New List(Of String)()
        Dim currentNode As TreeNode = node

        While currentNode IsNot Nothing
            path.Insert(0, currentNode.Index.ToString())
            currentNode = currentNode.Parent
        End While

        Return String.Join("/", path)
    End Function
    Private Function FindTreeNodeByText(nodes As TreeNodeCollection, text As String) As TreeNode
        For Each node As TreeNode In nodes
            If node.Text = text Then
                Return node
            Else
                Dim foundNode As TreeNode = FindTreeNodeByText(node.Nodes, text)
                If foundNode IsNot Nothing Then
                    Return foundNode
                End If
            End If
        Next
        Return Nothing
    End Function
    Private Function FindNodeByText(nodes As TreeNodeCollection, text As String) As TreeNode
        For Each node As TreeNode In nodes
            If node.Text = text Then
                Return node
            End If
            Dim foundNode As TreeNode = FindNodeByText(node.Nodes, text)
            If foundNode IsNot Nothing Then
                Return foundNode
            End If
        Next
        Return Nothing
    End Function
    Private Sub 組織圖_1_tv_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles 組織圖_1_tv.AfterSelect
        部門2_1_tb.Text = 組織圖_1_tv.SelectedNode.Text
        Dim selectedNode As TreeNode = e.Node : Dim parentNode As TreeNode = selectedNode.Parent
        ' 取得所在層級與節點位置
        Dim level As Integer = selectedNode.Level : Dim positions(level) As Integer : positions(level) = selectedNode.Index
        ' 往上遍歷每一層的節點，取得節點位置
        For i As Integer = level - 1 To 0 Step -1 : positions(i) = parentNode.Index : parentNode = parentNode.Parent : Next
        ' 如果不是第一層則觸發 TreeView2 的 AfterSelect
        If level > 0 Then
            Dim targetNode As TreeNode = 組織圖_tv.Nodes(positions(0)) : For i As Integer = 1 To level : targetNode = targetNode.Nodes(positions(i)) : Next
            組織圖_tv.SelectedNode = targetNode
        End If
    End Sub
    Private Sub 組織圖_tv_Click(sender As Object, e As EventArgs) Handles 組織圖_tv.AfterSelect
        Dim ds1 As New DataSet : 部門_1_tb.Text = 組織圖_tv.SelectedNode.Text
        PA = 組織圖_tv.SelectedNode.Text : Set_使用者清單() : 設定_dgv.DataSource = Nothing : 設定1_dgv.DataSource = Nothing : ds1.Clear()
        級別_tb.Text = "" : 姓名_tb.Text = "" : 部門_tb.Text = "" : 處別_tb.Text = "" : 課別_tb.Text = "" : 組別_tb.Text = "" : 帳號_tb.Text = ""
    End Sub
    Private Sub 清單_1_dgv_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles 清單_1_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            姓名_tb.Text = 清單_dgv.Rows(e.RowIndex).Cells("姓名").Value.ToString
            級別_tb.Text = 清單_dgv.Rows(e.RowIndex).Cells("級別").Value.ToString : 級別2_tb.Text = 清單_1_dgv.Rows(e.RowIndex).Cells("級別").Value.ToString
            部門_tb.Text = 清單_dgv.Rows(e.RowIndex).Cells("部門").Value.ToString : 部門2_tb.Text = 清單_1_dgv.Rows(e.RowIndex).Cells("部門").Value.ToString
            處別_tb.Text = 清單_dgv.Rows(e.RowIndex).Cells("處別").Value.ToString : 處別2_tb.Text = 清單_1_dgv.Rows(e.RowIndex).Cells("處別").Value.ToString
            課別_tb.Text = 清單_dgv.Rows(e.RowIndex).Cells("課別").Value.ToString : 課別2_tb.Text = 清單_1_dgv.Rows(e.RowIndex).Cells("課別").Value.ToString
            組別_tb.Text = 清單_dgv.Rows(e.RowIndex).Cells("組別").Value.ToString : 組別2_tb.Text = 清單_1_dgv.Rows(e.RowIndex).Cells("組別").Value.ToString
            帳號_tb.Text = 清單_dgv.Rows(e.RowIndex).Cells("帳號").Value.ToString
            Set_設定清單()
            If Label2.Text = "No Files" Then
                For i As Integer = 0 To 設定_dgv.Rows.Count - 1
                    If 處別_tb.Text = "NA" And 課別_tb.Text = "NA" And 組別_tb.Text = "NA" Then
                        If 設定_dgv.Rows(i).Cells("部").Value.ToString = 部門_tb.Text Then
                            設定_dgv.Rows(i).Cells("授權").Value = True
                        End If
                    ElseIf 課別_tb.Text = "NA" And 組別_tb.Text = "NA" Then
                        If 設定_dgv.Rows(i).Cells("部").Value.ToString = 部門_tb.Text And 設定_dgv.Rows(i).Cells("處").Value.ToString = 處別_tb.Text Then
                            設定_dgv.Rows(i).Cells("授權").Value = True
                        End If
                    ElseIf 組別_tb.Text = "NA" Then
                        If 設定_dgv.Rows(i).Cells("部").Value.ToString = 部門_tb.Text And 設定_dgv.Rows(i).Cells("處").Value.ToString = 處別_tb.Text And
                            設定_dgv.Rows(i).Cells("課").Value.ToString = 課別_tb.Text Then
                            設定_dgv.Rows(i).Cells("授權").Value = True
                        End If
                    Else
                        If 設定_dgv.Rows(i).Cells("部").Value.ToString = 部門_tb.Text And 設定_dgv.Rows(i).Cells("處").Value.ToString = 處別_tb.Text And
                            設定_dgv.Rows(i).Cells("課").Value.ToString = 課別_tb.Text And 設定_dgv.Rows(i).Cells("組").Value.ToString = 組別_tb.Text Then
                            設定_dgv.Rows(i).Cells("授權").Value = True
                        End If
                    End If
                Next
            End If
            If Val(登入人級別) <= 2 Then
                部門權限指定_bt.Enabled = True : 選定權限取消_bt.Enabled = True : 存檔_bt.Enabled = True
            ElseIf Val(登入人級別) = 3 Then
                If Val(級別_tb.Text) <= 2 Then
                    部門權限指定_bt.Enabled = False : 選定權限取消_bt.Enabled = False : 存檔_bt.Enabled = False
                Else
                    部門權限指定_bt.Enabled = True : 選定權限取消_bt.Enabled = True : 存檔_bt.Enabled = True
                End If
            ElseIf Val(登入人級別) = 4 Then
                If Val(級別_tb.Text) <= 3 Then
                    部門權限指定_bt.Enabled = False : 選定權限取消_bt.Enabled = False : 存檔_bt.Enabled = False
                Else
                    部門權限指定_bt.Enabled = True : 選定權限取消_bt.Enabled = True : 存檔_bt.Enabled = True
                End If
            ElseIf Val(登入人級別) >= 5 Then
                If Val(級別_tb.Text) <= 5 Then
                    部門權限指定_bt.Enabled = False : 選定權限取消_bt.Enabled = False : 存檔_bt.Enabled = False
                Else
                    部門權限指定_bt.Enabled = True : 選定權限取消_bt.Enabled = True : 存檔_bt.Enabled = True
                End If
            End If

        End If
    End Sub
    Private Sub 設定1_dgv_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles 設定1_dgv.RowPostPaint
        Dim linePen As New Pen(Color.Blue, 2)
        If e.RowIndex = 設定1_dgv.Rows.Count - 1 Then
            Exit Sub
        Else
            If 設定1_dgv(1, e.RowIndex).Value.ToString <> 設定1_dgv(1, e.RowIndex + 1).Value.ToString And 設定1_dgv(1, e.RowIndex).Value.ToString <> "NA" Then
                Dim startX As Integer = IIf(設定1_dgv.RowHeadersVisible, 設定1_dgv.RowHeadersWidth, 0)
                Dim startY As Integer = e.RowBounds.Top + e.RowBounds.Height - 1
                Dim endX As Integer = startX + 設定1_dgv.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - 設定1_dgv.HorizontalScrollingOffset
                e.Graphics.DrawLine(linePen, startX, startY, endX, startY)
                Exit Sub
            End If
        End If
    End Sub
    Private Sub 設定_dgv_CellEndEdit(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles 設定1_dgv.CellEndEdit
        Dim EU As String
        EU = 設定_dgv("時效", e.RowIndex).Value.ToString
        If EU = "" Then
            設定_dgv.Rows(e.RowIndex).Cells("時效").Value = ""
        Else
            If IsDate(EU) Then
                DateTimePicker2.Text = EU
                設定_dgv.Rows(e.RowIndex).Cells("時效").Value = Format(DateTimePicker2.Value, "yyyy/MM/dd")
            Else
                設定_dgv.Rows(e.RowIndex).Cells("時效").Value = ""
            End If
        End If

        EU = 設定1_dgv("時效", e.RowIndex).Value.ToString
        If EU = "" Then
            設定1_dgv.Rows(e.RowIndex).Cells("時效").Value = ""
        Else
            If IsDate(EU) Then
                DateTimePicker2.Text = EU
                設定1_dgv.Rows(e.RowIndex).Cells("時效").Value = Format(DateTimePicker2.Value, "yyyy/MM/dd")
            Else
                設定1_dgv.Rows(e.RowIndex).Cells("時效").Value = ""
            End If
        End If
    End Sub
    Private Sub 設定1_dgv_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles 設定1_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            級別1_tb.Text = 設定_dgv.Rows(e.RowIndex).Cells("級別").Value.ToString
            部門1_tb.Text = 設定_dgv.Rows(e.RowIndex).Cells("部").Value.ToString : 處別1_tb.Text = 設定_dgv.Rows(e.RowIndex).Cells("處").Value.ToString
            課別1_tb.Text = 設定_dgv.Rows(e.RowIndex).Cells("課").Value.ToString : 組別1_tb.Text = 設定_dgv.Rows(e.RowIndex).Cells("組").Value.ToString

            級別3_tb.Text = 設定1_dgv.Rows(e.RowIndex).Cells("級別").Value.ToString
            部門3_tb.Text = 設定1_dgv.Rows(e.RowIndex).Cells("部").Value.ToString : 處別3_tb.Text = 設定1_dgv.Rows(e.RowIndex).Cells("處").Value.ToString
            課別3_tb.Text = 設定1_dgv.Rows(e.RowIndex).Cells("課").Value.ToString : 組別3_tb.Text = 設定1_dgv.Rows(e.RowIndex).Cells("組").Value.ToString
        End If
    End Sub
    Private Sub 跨部門權限指定_bt_Click(sender As Object, e As EventArgs) Handles 部門權限指定_bt.Click
        If 部門1_tb.Text = "" Then : MsgBox("沒有選擇部門!!") : Else
            For i As Integer = 0 To 設定_dgv.Rows.Count - 1
                If 處別1_tb.Text = "NA" And 課別1_tb.Text = "NA" And 組別1_tb.Text = "NA" Then
                    If 設定_dgv.Rows(i).Cells("部").Value.ToString = 部門1_tb.Text Then
                        設定_dgv.Rows(i).Cells("授權").Value = True
                    End If
                ElseIf 課別1_tb.Text = "NA" And 組別1_tb.Text = "NA" Then
                    If 設定_dgv.Rows(i).Cells("部").Value.ToString = 部門1_tb.Text And 設定_dgv.Rows(i).Cells("處").Value.ToString = 處別1_tb.Text Then
                        設定_dgv.Rows(i).Cells("授權").Value = True
                    End If
                ElseIf 組別1_tb.Text = "NA" Then
                    If 設定_dgv.Rows(i).Cells("部").Value.ToString = 部門1_tb.Text And 設定_dgv.Rows(i).Cells("處").Value.ToString = 處別1_tb.Text And
                        設定_dgv.Rows(i).Cells("課").Value.ToString = 課別1_tb.Text Then
                        設定_dgv.Rows(i).Cells("授權").Value = True
                    End If
                Else
                    If 設定_dgv.Rows(i).Cells("部").Value.ToString = 部門1_tb.Text And 設定_dgv.Rows(i).Cells("處").Value.ToString = 處別1_tb.Text And
                        設定_dgv.Rows(i).Cells("課").Value.ToString = 課別1_tb.Text And 設定_dgv.Rows(i).Cells("組").Value.ToString = 組別1_tb.Text Then
                        設定_dgv.Rows(i).Cells("授權").Value = True
                    End If
                End If
            Next
        End If
    End Sub
    Private Sub 選定權限取消_bt_Click(sender As Object, e As EventArgs) Handles 選定權限取消_bt.Click
        If 部門1_tb.Text = "" Then : MsgBox("沒有選擇部門!!") : Else
            For i As Integer = 0 To 設定_dgv.Rows.Count - 1
                If 處別1_tb.Text = "NA" And 課別1_tb.Text = "NA" And 組別1_tb.Text = "NA" Then
                    If 設定_dgv.Rows(i).Cells("部").Value.ToString = 部門1_tb.Text Then
                        設定_dgv.Rows(i).Cells("授權").Value = False
                    End If
                ElseIf 課別1_tb.Text = "NA" And 組別1_tb.Text = "NA" Then
                    If 設定_dgv.Rows(i).Cells("部").Value.ToString = 部門1_tb.Text And 設定_dgv.Rows(i).Cells("處").Value.ToString = 處別1_tb.Text Then
                        設定_dgv.Rows(i).Cells("授權").Value = False
                    End If
                ElseIf 組別1_tb.Text = "NA" Then
                    If 設定_dgv.Rows(i).Cells("部").Value.ToString = 部門1_tb.Text And 設定_dgv.Rows(i).Cells("處").Value.ToString = 處別1_tb.Text And
                        設定_dgv.Rows(i).Cells("課").Value.ToString = 課別1_tb.Text Then
                        設定_dgv.Rows(i).Cells("授權").Value = False
                    End If
                Else
                    If 設定_dgv.Rows(i).Cells("部").Value.ToString = 部門1_tb.Text And 設定_dgv.Rows(i).Cells("處").Value.ToString = 處別1_tb.Text And
                        設定_dgv.Rows(i).Cells("課").Value.ToString = 課別1_tb.Text And 設定_dgv.Rows(i).Cells("組").Value.ToString = 組別1_tb.Text Then
                        設定_dgv.Rows(i).Cells("授權").Value = False
                    End If
                End If
            Next
        End If
    End Sub
    Private Sub 存檔_bt_Click(sender As Object, e As EventArgs) Handles 存檔_bt.Click
        If 姓名_tb.Text = "" Then : MsgBox("沒有選擇人員!!") : Else
            For i As Integer = 0 To 設定_dgv.Rows.Count - 1
                資料數 = 設定_dgv.Rows.Count : MyMod.進度條()
                PA4 = 設定_dgv.Rows(i).Cells("職稱").Value.ToString : PA1 = 姓名_tb.Text : PA2 = 帳號_tb.Text
                PA5 = 設定_dgv.Rows(i).Cells("時效").Value.ToString : BL1 = 設定_dgv.Rows(i).Cells("授權").Value
                SQL_跨部門主管權限設定_存檔前確認()
                If dr.Read() Then : SQL_跨部門主管權限設定_修改() : conn.Close() : Else : SQL_跨部門主管權限設定_新增() : conn.Close() : End If
            Next : YS_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0 : MsgBox("存檔完成!!")
        End If
    End Sub
    Private Sub 小間距1_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 小間距1_ch.Click
        小間距1_ch.Checked = True : 中間距1_ch.Checked = False : 大間距1_ch.Checked = False : Set_設定清單()
    End Sub
    Private Sub 中間距1_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 中間距1_ch.Click
        小間距1_ch.Checked = False : 中間距1_ch.Checked = True : 大間距1_ch.Checked = False : Set_設定清單()
    End Sub
    Private Sub 大間距1_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 大間距1_ch.Click
        小間距1_ch.Checked = False : 中間距1_ch.Checked = False : 大間距1_ch.Checked = True : Set_設定清單()
    End Sub
    Private Sub 小間距_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 小間距_ch.Click
        小間距_ch.Checked = True : 中間距_ch.Checked = False : 大間距_ch.Checked = False : Set_使用者清單()
    End Sub
    Private Sub 中間距_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 中間距_ch.Click
        小間距_ch.Checked = False : 中間距_ch.Checked = True : 大間距_ch.Checked = False : Set_使用者清單()
    End Sub
    Private Sub 大間距_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 大間距_ch.Click
        小間距_ch.Checked = False : 中間距_ch.Checked = False : 大間距_ch.Checked = True : Set_使用者清單()
    End Sub
End Class