private void PatientScreen_Load(object sender, EventArgs e)
        {
            viewControlPanel.Width     = 879;
            viewControlPanel.Height    = 365;
            viewControlPanel.Location  = new Point(5, 195);
            addRecordPanel.Width       = 879;
            addRecordPanel.Height      = 365;
            addRecordPanel.Location    = new Point(5, 195);
            updateRecordPanel.Width    = 879;
            updateRecordPanel.Height   = 365;
            updateRecordPanel.Location = new Point(5, 195);
            deleteRecordPanel.Width    = 879;
            deleteRecordPanel.Height   = 365;
            deleteRecordPanel.Location = new Point(5, 195);
            DBConnector dBconn = new DBConnector();

            dBconn.connect();

            PatientHandler patientHandler = new PatientHandler();

            patientGridView.DataSource = patientHandler.getAllPatient(dBconn.getConn());

            viewControlPanel.Show();
            addRecordPanel.Hide();
            updateRecordPanel.Hide();
            deleteRecordPanel.Hide();
        }
        //-----------------------------------------------------------Delete Record----------------------------------------------------------------------------//
        private void deleteRecordBtn_Click(object sender, EventArgs e)
        {
            PatientHandler patientHandler = new PatientHandler();

            viewControlPanel.Hide();
            addRecordPanel.Hide();
            updateRecordPanel.Hide();
            deleteRecordPanel.Show();
            closeAllTextBox(fullNameDeleteTextBox, nricDeleteTextBox, emailDeleteTextBox, phoneDeleteTextBox, addressDeleteRichTextBox, ageDeleteTextBox, bedsideIdDeleteTextBox, choosePatientIDDeleteComboBox, genderDeleteComboBox);
            patientHandler.FetchId(choosePatientIDDeleteComboBox);
        }
        private void updatePatientButton_Click(object sender, EventArgs e)
        {
            DBConnector dbC = new DBConnector();

            dbC.connect();
            PatientHandler patientHandler = new PatientHandler();
            Patient        oldPatientData = new Patient();

            if (choosePatientIDUpdateComboBox.SelectedIndex == 0)
            {
                MessageBox.Show("Please select valid ID");
                return;
            }
            if (nricUpdateTextBox.Enabled == false)
            {
                MessageBox.Show("Please press 'Enter' to generate data first");
                return;
            }
            else
            {
                oldPatientData = patientHandler.getSpecificPatient(dbC.getConn(), int.Parse(choosePatientIDUpdateComboBox.SelectedItem.ToString()));
            }

            if ((oldPatientData.IdentityCard.ToString() != nricUpdateTextBox.Text) || (oldPatientData.FullName != fullNameUpdateTextBox.Text) || (oldPatientData.Email != emailUpdateTextBox.Text) || (oldPatientData.Gender != genderUpdatecomboBox.SelectedItem.ToString()) || (oldPatientData.Phone.ToString() != phoneUpdateTextBox.Text) || (oldPatientData.Address != addressUpdateTextBox.Text) || (oldPatientData.Age.ToString() != ageUpdateTextBox.Text) || (oldPatientData.BedsideId.ToString() != bedsideIdUpdateTextBox.Text))
            {
                Patient newPatientData = new Patient();
                newPatientData.Id           = int.Parse(choosePatientIDUpdateComboBox.SelectedItem.ToString());
                newPatientData.IdentityCard = long.Parse(nricUpdateTextBox.Text);
                newPatientData.FullName     = fullNameUpdateTextBox.Text;
                newPatientData.Email        = emailUpdateTextBox.Text;
                newPatientData.Gender       = genderUpdatecomboBox.SelectedItem.ToString();
                newPatientData.Phone        = int.Parse(phoneUpdateTextBox.Text);
                newPatientData.Address      = addressUpdateTextBox.Text;
                newPatientData.Age          = int.Parse(ageUpdateTextBox.Text);
                newPatientData.BedsideId    = int.Parse(bedsideIdUpdateTextBox.Text);

                int updateResult = patientHandler.updatePatientData(dbC.getConn(), newPatientData);

                if (updateResult == 1)
                {
                    MessageBox.Show("Update Successfully");
                    closeAllTextBox(fullNameUpdateTextBox, nricUpdateTextBox, emailUpdateTextBox, phoneTextBox, addressUpdateTextBox, ageUpdateTextBox, bedsideIdUpdateTextBox, choosePatientIDUpdateComboBox, genderUpdatecomboBox);
                    patientHandler.FetchId(choosePatientIDUpdateComboBox);
                }
                else
                {
                    MessageBox.Show("Update Failed");
                }
            }
            else
            {
                MessageBox.Show("Nothing is changes");
            }
        }
        public PatientScreen()
        {
            InitializeComponent();
            genderComboBox.SelectedIndex = 0;
            DBConnector dBConn = new DBConnector();

            dBConn.connect();
            PatientHandler patientHandler = new PatientHandler();

            patientGridView.DataSource = patientHandler.getAllPatient(dBConn.getConn());
        }
        private void searchtextBox_TextChanged(object sender, EventArgs e)
        {
            DBConnector dBconn = new DBConnector();

            dBconn.connect();
            PatientHandler patientHandler = new PatientHandler();

            if (searchtextBox.Text != "")
            {
                patientGridView.DataSource = patientHandler.searchPatientData(dBconn.getConn(), searchtextBox.Text);
            }
        }
        //-----------------------------------------------------------Add Record----------------------------------------------------------------------------//
        private void viewRecordBtn_Click(object sender, EventArgs e)
        {
            DBConnector dBconn = new DBConnector();

            dBconn.connect();

            PatientHandler patientHandler = new PatientHandler();

            patientGridView.DataSource = patientHandler.getAllPatient(dBconn.getConn());
            viewControlPanel.Show();
            addRecordPanel.Hide();
            updateRecordPanel.Hide();
            deleteRecordPanel.Hide();
        }
        private void choosePatientComboBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                if (choosePatientIDUpdateComboBox.SelectedIndex == 0)
                {
                    MessageBox.Show("Please select valid ID");
                }
                else
                {
                    nricUpdateTextBox.Enabled      = true;
                    fullNameUpdateTextBox.Enabled  = true;
                    emailUpdateTextBox.Enabled     = true;
                    genderUpdatecomboBox.Enabled   = true;
                    phoneUpdateTextBox.Enabled     = true;
                    addressUpdateTextBox.Enabled   = true;
                    ageUpdateTextBox.Enabled       = true;
                    bedsideIdUpdateTextBox.Enabled = true;
                    genderUpdatecomboBox.Items.Add("Male");
                    genderUpdatecomboBox.Items.Add("Female");
                    DBConnector dbC = new DBConnector();
                    dbC.connect();
                    PatientHandler patientHandler = new PatientHandler();
                    List <Patient> patientList    = new List <Patient>();
                    patientList = patientHandler.getAllPatient(dbC.getConn());
                    for (int i = 0; i < patientList.Count; i++)
                    {
                        if (choosePatientIDUpdateComboBox.SelectedItem.ToString() == patientList[i].Id.ToString())
                        {
                            nricUpdateTextBox.Text     = patientList[i].IdentityCard.ToString();
                            fullNameUpdateTextBox.Text = patientList[i].FullName;
                            emailUpdateTextBox.Text    = patientList[i].Email;

                            for (int n = 0; n < genderUpdatecomboBox.Items.Count; n++)
                            {
                                if (patientList[i].Gender == genderUpdatecomboBox.Items[n].ToString())
                                {
                                    genderUpdatecomboBox.SelectedIndex = n;
                                }
                            }

                            phoneUpdateTextBox.Text     = patientList[i].Phone.ToString();
                            addressUpdateTextBox.Text   = patientList[i].Address;
                            ageUpdateTextBox.Text       = patientList[i].Age.ToString();
                            bedsideIdUpdateTextBox.Text = patientList[i].BedsideId.ToString();
                        }
                    }
                }
            }
        }
        //-----------------------------------------------------------Add Record----------------------------------------------------------------------------//
        private void addRecordBtn_Click(object sender, EventArgs e)
        {
            DBConnector dBconn = new DBConnector();

            dBconn.connect();
            PatientHandler patientHandler = new PatientHandler();

            patientIdTextBox.Text        = patientHandler.getLastRecordID(dBconn.getConn());
            genderComboBox.SelectedIndex = 0;

            viewControlPanel.Hide();
            addRecordPanel.Show();
            updateRecordPanel.Hide();
            deleteRecordPanel.Hide();
        }
        //public int alterTable(MySqlConnection conn, int id)
        //{
        //    string sql = "ALTER TABLE patient AUTO INCREMENT= '" + id + "'";
        //    MySqlCommand alterCommand = new MySqlCommand(sql, conn);
        //    return alterCommand.ExecuteNonQuery();
        //}

        public void FetchId(ComboBox selectedcomboBox)
        {
            selectedcomboBox.Items.Add("--Select ID--");

            selectedcomboBox.SelectedIndex = 0;
            DBConnector dbC = new DBConnector();

            dbC.connect();
            PatientHandler patientHandler = new PatientHandler();
            List <Patient> patientList    = new List <Patient>();

            patientList = patientHandler.getAllPatient(dbC.getConn());
            for (int i = 0; i < patientList.Count; i++)
            {
                selectedcomboBox.Items.Add(patientList[i].Id);
            }
        }
        private void deletePatientRecordBtn_Click(object sender, EventArgs e)
        {
            DBConnector dbC = new DBConnector();

            dbC.connect();
            PatientHandler patientHandler = new PatientHandler();
            Patient        oldPatientData = new Patient();

            if (choosePatientIDDeleteComboBox.SelectedIndex == 0)
            {
                MessageBox.Show("Please select valid ID");
                return;
            }
            if (string.IsNullOrEmpty(nricDeleteTextBox.Text))
            {
                MessageBox.Show("Please press 'Enter' to generate data first");
                return;
            }
            else
            {
                oldPatientData = patientHandler.getSpecificPatient(dbC.getConn(), int.Parse(choosePatientIDDeleteComboBox.SelectedItem.ToString()));
            }

            int deleteResult = patientHandler.deletePatientData(dbC.getConn(), oldPatientData.Id);

            if (deleteResult == 1)
            {
                MessageBox.Show("Delete Record Successfully");
                closeAllTextBox(fullNameDeleteTextBox, nricDeleteTextBox, emailDeleteTextBox, phoneDeleteTextBox, addressDeleteRichTextBox, ageDeleteTextBox, bedsideIdDeleteTextBox, choosePatientIDDeleteComboBox, genderDeleteComboBox);
                patientHandler.FetchId(choosePatientIDDeleteComboBox);
            }
            else
            {
                MessageBox.Show("Delete Failed");
            }
        }
        private void savePatientBtn_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(fullNameTextBox.Text))
            {
                MessageBox.Show("Please enter full name");
                return;
            }

            if (string.IsNullOrEmpty(emailTextBox.Text))
            {
                MessageBox.Show("Please enter email");
                return;
            }

            if (genderComboBox.SelectedIndex == 0)
            {
                MessageBox.Show("Please select gender");
                return;
            }

            if (string.IsNullOrEmpty(nricTextBox.Text))
            {
                MessageBox.Show("Please enter nric");
                return;
            }

            if (string.IsNullOrEmpty(addressTextBox.Text))
            {
                MessageBox.Show("Please enter address");
                return;
            }

            if (string.IsNullOrEmpty(phoneTextBox.Text))
            {
                MessageBox.Show("Please enter phone");
                return;
            }

            if (string.IsNullOrEmpty(ageTextBox.Text))
            {
                MessageBox.Show("Please enter age");
                return;
            }

            Patient newPatient = new Patient();

            newPatient.FullName     = fullNameTextBox.Text;
            newPatient.IdentityCard = long.Parse(nricTextBox.Text);
            newPatient.Address      = addressTextBox.Text;
            newPatient.Email        = emailTextBox.Text;
            newPatient.Gender       = genderComboBox.SelectedItem.ToString();
            newPatient.Phone        = int.Parse(phoneTextBox.Text);
            newPatient.Age          = int.Parse(ageTextBox.Text);
            newPatient.BedsideId    = 0;

            DBConnector dBconn = new DBConnector();

            dBconn.connect();
            PatientHandler patientHandler = new PatientHandler();
            int            addResult      = patientHandler.addNewPatient(dBconn.getConn(), newPatient);

            if (addResult == 1)
            {
                MessageBox.Show("New Patient Record is inserted");
                fullNameTextBox.Text         = "";
                nricTextBox.Text             = "";
                addressTextBox.Text          = "";
                emailTextBox.Text            = "";
                genderComboBox.SelectedIndex = 0;
                phoneTextBox.Text            = "";
                ageTextBox.Text       = "";
                patientIdTextBox.Text = patientHandler.getLastRecordID(dBconn.getConn());
            }
        }