Option Strict Off
Public Class 部門權限設定
    Dim RI As Integer = -1
    Dim SK As String = "要查找的 KEY"
    Dim Str As String = Application.StartupPath
    Dim 對話框(10) As String
    Private Sub Set_使用者清單()
        Dim ds As New DataSet : 清單_dgv.DataSource = Nothing : ds.Clear()
        清單_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        清單_dgv.ColumnHeadersHeight = 25 : 清單_dgv.AllowUserToAddRows = False : 清單_dgv.RowTemplate.Height = 35
        SQL_跨部門主管權限設定_清單()
        da.Fill(ds) : 清單_dgv.DataSource = ds.Tables(0) : conn.Close()

        清單_dgv.Columns(0).FillWeight = 100 : 清單_dgv.Columns(1).FillWeight = 150 : 清單_dgv.Columns(2).FillWeight = 40 : 清單_dgv.Columns(3).Visible = False
        清單_dgv.Columns(4).Visible = False : 清單_dgv.Columns(5).Visible = False : 清單_dgv.Columns(6).Visible = False : 清單_dgv.Columns(7).Visible = False
    End Sub
    Private Sub Set_設定清單()
        Dim ds1 As New DataSet : 設定_dgv.DataSource = Nothing : ds1.Clear()
        設定_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        設定_dgv.ColumnHeadersHeight = 25 : 設定_dgv.AllowUserToAddRows = False : 設定_dgv.RowTemplate.Height = 35

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

        PA1 = 姓名_rtb.Text : PA2 = 帳號_tb.Text
        SQL_跨部門主管權限設定_確認有無資料()
        If dr.Read() Then : conn.Close() : PA3 = 級別_rtb.Text : SQL_跨部門主管權限設定_已設定過() : 是否建檔_lb.Text = 對話框(9)
        Else : conn.Close() : PA3 = 級別_rtb.Text : SQL_跨部門主管權限設定_未設定過() : 是否建檔_lb.Text = 對話框(10) : End If

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

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

        For i As Integer = 0 To 設定_dgv.Rows.Count - 1
            If 設定_dgv.Rows(i).Cells("級別").Value.ToString = "01" Then : 設定_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightGoldenrodYellow
            ElseIf 設定_dgv.Rows(i).Cells("級別").Value.ToString = "02" Then : 設定_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightGreen
            ElseIf 設定_dgv.Rows(i).Cells("級別").Value.ToString = "03" Then : 設定_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightPink
            ElseIf 設定_dgv.Rows(i).Cells("級別").Value.ToString = "04" Then : 設定_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightBlue
            ElseIf 設定_dgv.Rows(i).Cells("級別").Value.ToString = "05" Then : 設定_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightBlue
            End If
            If IsDBNull(設定_dgv.Rows(i).Cells("授權").Value) Then : 設定_dgv.Rows(i).Cells("授權").Value = False : End If
        Next
    End Sub
    Private Sub 存檔_rbt_MouseEnter(sender As Object, e As EventArgs) Handles 存檔_rbt.MouseEnter
        ToolTip1.SetToolTip(Me.存檔_rbt, 對話框(6))
    End Sub
    Private Sub 刪除_rbt_MouseEnter(sender As Object, e As EventArgs) Handles 刪除_rbt.MouseEnter
        ToolTip1.SetToolTip(Me.刪除_rbt, 對話框(7))
    End Sub
    Private Sub 批量刷新_rbt_MouseEnter(sender As Object, e As EventArgs) Handles 批量刷新_rbt.MouseEnter
        ToolTip1.SetToolTip(Me.批量刷新_rbt, 對話框(8))
    End Sub
    Private Sub 門主管權限設定_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.MdiParent = FH_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True
        Panel1.SendToBack()
        清單_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", FH_ERP_SYS.字體_NUD.Value)
        設定_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", FH_ERP_SYS.字體_NUD.Value)
    End Sub
    Private Sub 門主管權限設定_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
        Set_使用者清單()
    End Sub
    Private Sub 登入閒置控制_MouseMove(sender As Object, e As EventArgs) Handles MyBase.MouseMove
        timeNow = 0 : FH_ERP_SYS.Timer1.Enabled = False : FH_ERP_SYS.Timer1.Enabled = True
    End Sub
    Private Sub 登入閒置控制_KeyPress(sender As Object, e As EventArgs) Handles MyBase.KeyPress
        timeNow = 0 : FH_ERP_SYS.Timer1.Enabled = False : FH_ERP_SYS.Timer1.Enabled = True
    End Sub
    Private Sub 清單_dgv_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles 清單_dgv.RowPostPaint
        Dim linePen As New Pen(Color.Blue, 2)
        If e.RowIndex = 清單_dgv.Rows.Count - 1 Then
            Exit Sub
        Else
            If 清單_dgv("級別", e.RowIndex).Value.ToString = "03" Then
                Dim startX As Integer = IIf(清單_dgv.RowHeadersVisible, 清單_dgv.RowHeadersWidth, 0)
                Dim startY As Integer = e.RowBounds.Top + e.RowBounds.Height - 1
                Dim endX As Integer = startX + 清單_dgv.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - 清單_dgv.HorizontalScrollingOffset
                e.Graphics.DrawLine(linePen, startX, startY, endX, startY)
                Exit Sub
            End If
        End If
        For i As Integer = 0 To 清單_dgv.Rows.Count - 1
            If 清單_dgv.Rows(i).Cells("級別").Value.ToString = "01" Then : 清單_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightGoldenrodYellow
            ElseIf 清單_dgv.Rows(i).Cells("級別").Value.ToString = "02" Then : 清單_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightGreen
            ElseIf 清單_dgv.Rows(i).Cells("級別").Value.ToString = "03" Then : 清單_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightPink
            ElseIf 清單_dgv.Rows(i).Cells("級別").Value.ToString = "04" Then : 清單_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightBlue
            ElseIf 清單_dgv.Rows(i).Cells("級別").Value.ToString = "05" Then : 清單_dgv.Rows(i).DefaultCellStyle.BackColor = Color.LightBlue
            End If
        Next
    End Sub
    Private Sub 清單_dgv_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles 清單_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            級別_rtb.Text = 清單_dgv.Rows(e.RowIndex).Cells("級別").Value.ToString : 姓名_rtb.Text = 清單_dgv.Rows(e.RowIndex).Cells("姓名").Value.ToString
            部門_rtb.Text = 清單_dgv.Rows(e.RowIndex).Cells("部門").Value.ToString : 處別_rtb.Text = 清單_dgv.Rows(e.RowIndex).Cells("處別").Value.ToString
            課別_rtb.Text = 清單_dgv.Rows(e.RowIndex).Cells("課別").Value.ToString : 組別_rtb.Text = 清單_dgv.Rows(e.RowIndex).Cells("組別").Value.ToString
            帳號_tb.Text = 清單_dgv.Rows(e.RowIndex).Cells("帳號").Value.ToString
            Set_設定清單()
            If 是否建檔_lb.Text = "No Files" Then
                For i As Integer = 0 To 設定_dgv.Rows.Count - 1
                    If 處別_rtb.Text = "NA" And 課別_rtb.Text = "NA" And 組別_rtb.Text = "NA" Then
                        If 設定_dgv.Rows(i).Cells("部").Value.ToString = 部門_rtb.Text Then
                            設定_dgv.Rows(i).Cells("授權").Value = True
                        End If
                    ElseIf 課別_rtb.Text = "NA" And 組別_rtb.Text = "NA" Then
                        If 設定_dgv.Rows(i).Cells("部").Value.ToString = 部門_rtb.Text And 設定_dgv.Rows(i).Cells("處").Value.ToString = 處別_rtb.Text Then
                            設定_dgv.Rows(i).Cells("授權").Value = True
                        End If
                    ElseIf 組別_rtb.Text = "NA" Then
                        If 設定_dgv.Rows(i).Cells("部").Value.ToString = 部門_rtb.Text And 設定_dgv.Rows(i).Cells("處").Value.ToString = 處別_rtb.Text And
                            設定_dgv.Rows(i).Cells("課").Value.ToString = 課別_rtb.Text Then
                            設定_dgv.Rows(i).Cells("授權").Value = True
                        End If
                    Else
                        If 設定_dgv.Rows(i).Cells("部").Value.ToString = 部門_rtb.Text And 設定_dgv.Rows(i).Cells("處").Value.ToString = 處別_rtb.Text And
                            設定_dgv.Rows(i).Cells("課").Value.ToString = 課別_rtb.Text And 設定_dgv.Rows(i).Cells("組").Value.ToString = 組別_rtb.Text Then
                            設定_dgv.Rows(i).Cells("授權").Value = True
                        End If
                    End If
                Next
            End If
        End If
    End Sub
    Private Sub 設定_dgv_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles 設定_dgv.RowPostPaint
        Dim linePen As New Pen(Color.Blue, 2)
        If e.RowIndex = 設定_dgv.Rows.Count - 1 Then
            Exit Sub
        Else
            If 設定_dgv(1, e.RowIndex).Value.ToString <> 設定_dgv(1, e.RowIndex + 1).Value.ToString And 設定_dgv(1, e.RowIndex).Value.ToString <> "NA" Then
                Dim startX As Integer = IIf(設定_dgv.RowHeadersVisible, 設定_dgv.RowHeadersWidth, 0)
                Dim startY As Integer = e.RowBounds.Top + e.RowBounds.Height - 1
                Dim endX As Integer = startX + 設定_dgv.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - 設定_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 設定_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
    End Sub
    Private Sub 設定_dgv_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles 設定_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
        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 Strings.Right(處別1_tb.Text, 2) = "NA" And Strings.Right(課別1_tb.Text, 2) = "NA" And Strings.Right(組別1_tb.Text, 2) = "NA" Then
                    If 設定_dgv.Rows(i).Cells("部").Value.ToString = 部門1_tb.Text Then
                        設定_dgv.Rows(i).Cells("授權").Value = True
                    End If
                ElseIf Strings.Right(課別1_tb.Text, 2) = "NA" And Strings.Right(組別1_tb.Text, 2) = "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 Strings.Right(組別1_tb.Text, 2) = "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 存檔_rbt_Click(sender As Object, e As EventArgs) Handles 存檔_rbt.Click
        If 姓名_rtb.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 = 姓名_rtb.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 : FH_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0 : MsgBox("存檔完成!!")
        End If
    End Sub
    Private Sub 刪除_rbt_Click(sender As Object, e As EventArgs) Handles 刪除_rbt.Click
        If 姓名_rtb.Text = "" Then : MsgBox("沒有選擇人員!!") : Else
            Dim result As DialogResult = MessageBox.Show("是否刪除選擇的資料?", "請選擇", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If result = DialogResult.Yes Then : PA = 姓名_rtb.Text : SQL_跨部門主管權限設定_刪除() : conn.Close() : MsgBox("刪除完成!!") : Set_設定清單() : End If
        End If
    End Sub
    Private Sub 批量刷新_rbt_Click(sender As Object, e As EventArgs) Handles 批量刷新_rbt.Click
        For J As Integer = 0 To 清單_dgv.Rows.Count - 1
            級別_rtb.Text = 清單_dgv.Rows(J).Cells("級別").Value.ToString : 姓名_rtb.Text = 清單_dgv.Rows(J).Cells("姓名").Value.ToString
            部門_rtb.Text = 清單_dgv.Rows(J).Cells("部門").Value.ToString : 處別_rtb.Text = 清單_dgv.Rows(J).Cells("處別").Value.ToString
            課別_rtb.Text = 清單_dgv.Rows(J).Cells("課別").Value.ToString : 組別_rtb.Text = 清單_dgv.Rows(J).Cells("組別").Value.ToString
            帳號_tb.Text = 清單_dgv.Rows(J).Cells("帳號").Value.ToString
            Set_設定清單()
            If 是否建檔_lb.Text = "No Files" Then
                For i As Integer = 0 To 設定_dgv.Rows.Count - 1
                    If 處別_rtb.Text = "NA" And 課別_rtb.Text = "NA" And 組別_rtb.Text = "NA" Then
                        If 設定_dgv.Rows(i).Cells("部").Value.ToString = 部門_rtb.Text Then
                            設定_dgv.Rows(i).Cells("授權").Value = True
                        End If
                    ElseIf 課別_rtb.Text = "NA" And 組別_rtb.Text = "NA" Then
                        If 設定_dgv.Rows(i).Cells("部").Value.ToString = 部門_rtb.Text And 設定_dgv.Rows(i).Cells("處").Value.ToString = 處別_rtb.Text Then
                            設定_dgv.Rows(i).Cells("授權").Value = True
                        End If
                    ElseIf 組別_rtb.Text = "NA" Then
                        If 設定_dgv.Rows(i).Cells("部").Value.ToString = 部門_rtb.Text And 設定_dgv.Rows(i).Cells("處").Value.ToString = 處別_rtb.Text And
                            設定_dgv.Rows(i).Cells("課").Value.ToString = 課別_rtb.Text Then
                            設定_dgv.Rows(i).Cells("授權").Value = True
                        End If
                    Else
                        If 設定_dgv.Rows(i).Cells("部").Value.ToString = 部門_rtb.Text And 設定_dgv.Rows(i).Cells("處").Value.ToString = 處別_rtb.Text And
                            設定_dgv.Rows(i).Cells("課").Value.ToString = 課別_rtb.Text And 設定_dgv.Rows(i).Cells("組").Value.ToString = 組別_rtb.Text Then
                            設定_dgv.Rows(i).Cells("授權").Value = True
                        End If
                    End If
                Next
            End If
            For i As Integer = 0 To 設定_dgv.Rows.Count - 1
                資料數 = 設定_dgv.Rows.Count : MyMod.進度條()
                PA4 = 設定_dgv.Rows(i).Cells("職稱").Value.ToString : PA1 = 姓名_rtb.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 : FH_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0
        Next : MsgBox("存檔完成!!")
    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
End Class