Imports Store_Managment.Messages
Public Class frmClient
#Region "FrmSettings"
    Private MouseDown As Boolean = False
    Private LastLoc As Point
    Private Sub pnlMain_MouseUp(sender As Object, e As MouseEventArgs) Handles pnlMain.MouseUp
        MouseDown = False
        pnlMain.Cursor = Cursors.Default
    End Sub

    Private Sub pnlMain_MouseDown(sender As Object, e As MouseEventArgs) Handles pnlMain.MouseDown
        MouseDown = True
        pnlMain.Cursor = Cursors.NoMove2D
        LastLoc = e.Location
    End Sub

    Private Sub pnlMain_MouseMove(sender As Object, e As MouseEventArgs) Handles pnlMain.MouseMove
        If (MouseDown) Then
            Dim NewX As Integer = (Me.Location.X - LastLoc.X) + e.X
            Dim NewY As Integer = (Me.Location.Y - LastLoc.Y) + e.Y
            Me.Location = New Point(NewX, NewY)
        End If
    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.Hide()
    End Sub

    Private Sub btnMinimuz_Click(sender As Object, e As EventArgs) Handles btnMinimuz.Click
        WindowState = FormWindowState.Minimized
    End Sub
#End Region


    Private Sub frmClient_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Icon = frmMain.Icon
        dgvClient.DataSource = DB.GetData("Select * from Client")
        dgvClient.ClearSelection()
        ClearClient()
    End Sub

    Private Sub txtClientTelephone_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtClientTelephone.KeyPress, txtClientPhone2.KeyPress, txtClientPhone1.KeyPress, txtClientFax.KeyPress
        If (Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar)) Then e.Handled = True
    End Sub

    Private Sub ClearClient()
        txtClientCode.Text = DB.GetData("select max(Client_Code)+1 from Client").Rows(0)(0).ToString()
        If (txtClientCode.Text.Trim() = "") Then txtClientCode.Text = "1"
        txtClientName.Clear()
        txtClientAddress.Clear()
        RdoClientPerson.Checked = True
        txtClientTelephone.Text = ""
        txtClientFax.Text = ""
        txtClientPhone1.Text = ""
        txtClientPhone2.Text = ""
        txtClientName.Select()
        tsAdd.Enabled = True
        tsDelete.Enabled = False
        tsEdit.Enabled = False
    End Sub

    Private Sub dgvClient_SelectionChanged(sender As Object, e As EventArgs) Handles dgvClient.SelectionChanged
        tsAdd.Enabled = False
        tsEdit.Enabled = True
        tsDelete.Enabled = True
        If (dgvClient.SelectedRows.Count > 0) Then
            txtClientCode.Text = dgvClient.SelectedRows(0).Cells(0).Value.ToString()
            txtClientName.Text = dgvClient.SelectedRows(0).Cells(1).Value.ToString()
            txtClientAddress.Text = dgvClient.SelectedRows(0).Cells(2).Value.ToString()
            If (dgvClient.SelectedRows(0).Cells(3).Value.ToString() = "فرد") Then
                RdoClientPerson.Checked = True
            Else
                RdoClientCompany.Checked = True
            End If
            txtClientTelephone.Text = dgvClient.SelectedRows(0).Cells(4).Value.ToString()
            txtClientFax.Text = dgvClient.SelectedRows(0).Cells(5).Value.ToString()
            txtClientPhone1.Text = dgvClient.SelectedRows(0).Cells(6).Value.ToString()
            txtClientPhone2.Text = dgvClient.SelectedRows(0).Cells(7).Value.ToString()

        End If
    End Sub

    Private Sub tsNew_Click(sender As Object, e As EventArgs) Handles tsNew.Click
        dgvClient.ClearSelection()
        ClearClient()
    End Sub

    Private Sub tsAdd_Click(sender As Object, e As EventArgs) Handles tsAdd.Click
        If (txtClientCode.Text = "") Then
            MsgError("حدث خطأ ما سيتم إصلاحة وإذا استمر العطل من فضلك تواصل مع المبرمج")
            tsNew.PerformClick()
        ElseIf (txtClientName.Text = "") Then
            MsgInformation("من فضلك قم بإدخال اسم العميل")
            txtClientName.Select()
        Else
            Dim KT As String = ""
            If (RdoClientPerson.Checked) Then
                KT = "فرد"
            Else
                KT = "شركة"
            End If
            DB.Run("Insert Into Client Values(" + txtClientCode.Text + ",'" + txtClientName.Text + "','" + txtClientAddress.Text + "','" + KT + "','" + txtClientTelephone.Text + "','" + txtClientFax.Text + "','" + txtClientPhone1.Text + "','" + txtClientPhone2.Text + "')")
            MsgInformation("تم إضافة العميل بنجاح")
            dgvClient.DataSource = DB.GetData("Select * from Client")
            dgvClient.ClearSelection()
            ClearClient()
        End If
    End Sub

    Private Sub tsEdit_Click(sender As Object, e As EventArgs) Handles tsEdit.Click
        If (txtClientCode.Text = "") Then
            MsgError("حدث خطأ ما سيتم إصلاحة وإذا استمر العطل من فضلك تواصل مع المبرمج")
            tsNew.PerformClick()
        ElseIf (txtClientName.Text = "") Then
            MsgInformation("من فضلك قم بإدخال اسم العميل")
            txtClientName.Select()
        Else
            Dim KT As String = ""
            If (RdoClientPerson.Checked) Then
                KT = "فرد"
            Else
                KT = "شركة"
            End If
            DB.Run("Update Client Set Client_Name = '" + txtClientName.Text + "' , Client_Address = '" + txtClientAddress.Text + "' , Client_Type = '" + KT + "' , Client_Telephone = '" + txtClientTelephone.Text + "' , Client_Fax = '" + txtClientFax.Text + "' , Client_Phone1 = '" + txtClientPhone1.Text + "' , Client_Phone2 = '" + txtClientPhone2.Text + "' where Client_Code = " + txtClientCode.Text)
            MsgInformation("تم عملية التعديل بنجاح")
            dgvClient.DataSource = DB.GetData("Select * from Client")
            dgvClient.ClearSelection()
            ClearClient()
        End If
    End Sub

    Private Sub tsDelete_Click(sender As Object, e As EventArgs) Handles tsDelete.Click
        Try
            Dim Delete As DialogResult
            Delete = MessageBox.Show("هل متأكد من استمرار عملية الحذف؟", "حذف العميل", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, MessageBoxOptions.RtlReading)
            If (Delete = DialogResult.Yes) Then
                DB.Run("Delete from Client Where Client_Code=" + txtClientCode.Text)
                MsgInformation("تم حذف العميل بنجاح")
                dgvClient.DataSource = DB.GetData("Select * from Client")
                ClearClient()
                dgvClient.ClearSelection()
                tsAdd.Enabled = True
                tsDelete.Enabled = False
                tsEdit.Enabled = False
            Else
                Return
            End If



        Catch ex As Exception
            MsgError(ex.Message)
        End Try
    End Sub
End Class