package retail;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.*;

import database_comm.ItemDatabase;
import database_comm.UserDatabase;
import login.User;

public class RemoveUserFrame extends JFrame {
	
	private String username;
	private String username2;
	private String error;
	private JFrame User;
	
	public RemoveUserFrame() {
		User = new JFrame("Delete User");
		User.setResizable(false);
		User.setTitle("Delete User");
		User.setSize(400, 225);
		User.setLocationRelativeTo(null);
		Panel greeting = new Panel();
		JTextArea greet = new JTextArea("Please enter the following information");
		greet.setBackground(null);
		greet.setEditable(false);
		greeting.add(greet, BorderLayout.CENTER);
		
		Panel information = new Panel();
		information.setLayout(new GridLayout(6,2));
		
		
		JTextArea USERNAME = new JTextArea("  Username:");
		USERNAME.setEditable(false);
		USERNAME.setBackground(null);
		JTextField userID = new JTextField();
		userID.setColumns(7);
		information.add(USERNAME);
		information.add(userID);
		
		JTextArea USERNAME2 = new JTextArea("  Re-Enter Username:");
		USERNAME2.setEditable(false);
		USERNAME2.setBackground(null);
		JTextField userID2 = new JTextField();
		userID2.setColumns(7);
		information.add(USERNAME2);
		information.add(userID2);
		
		
		
		Panel buttons = new Panel();
		buttons.setLayout(new GridLayout(1,2));
		
		JButton exit = new JButton("Exit");
		buttons.add(exit);
		exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				User.dispose();
			}
		});
		
		JButton enter = new JButton("Delete User");
		buttons.add(enter);
		enter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				username = userID.getText();
				username2 = userID2.getText();
				
				deleteUser(username);
				
			}
		});
		
		Panel combo = new Panel();
		combo.setLayout(new BorderLayout());
		combo.add(greeting, BorderLayout.NORTH);
		combo.add(information, BorderLayout.CENTER);
		combo.add(buttons, BorderLayout.SOUTH);
		
		User.add(combo);
		User.setVisible(true);
		User.setAlwaysOnTop(true);
	}

	public void deleteUser(String userID) {
		// TODO Auto-generated method stub
		
		User.setAlwaysOnTop(false);
		if (validUser() == false) {
			JOptionPane.showMessageDialog(null, error);
		}
		else {
			Object[] options = { "Yes", "No" };
			 int n = JOptionPane.showOptionDialog(new JFrame(),
			            "Are you sure you want to permenatly delete Item?", "Delete Item?",
			            JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
			            options, options[1]);
			    if(n == JOptionPane.OK_OPTION){ // Afirmative
			        //.... 
			    	try {
						UserDatabase.removeUser(userID);
						JOptionPane.showMessageDialog(null, "User Successfully Removed");
						User.dispose();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			    }
			
		}
		User.setAlwaysOnTop(true);
		}

	
	public Boolean validUser() {
		
		if(username.length() >= 4) {
			
			if(username.equals(username2)) {
				return true;
			} else {
				error = "Usernames do not match";
				return false;
			}
			
		} else {
			error = "Username not Found";
			return false;
		}
		
	}
	
}

@@ -187,43 +187,8 @@
 *;%0A%0A
-import database_comm.ItemDatabase;%0A
 impo
@@ -2598,20 +2598,20 @@
  delete 
-Item
+User
 ?%22, %22Del
@@ -2618,12 +2618,12 @@
 ete 
-Item
+User
 ?%22,%0A
@@ -3147,55 +3147,18 @@
 er()
- %7B%0A%09%09%0A%09%09if(username.length() %3E= 4) %7B%0A%09%09%09
 %0A%09
+%7B%0A
 %09%09if(
+!
 user
@@ -3190,111 +3190,309 @@
 %0A%09%09%09
-%09return true;%0A%09%09%09%7D else %7B%0A%09%09%09%09error = %22Usernames do not match%22;%0A%09%09%09%09return false;%0A%09%09%09%7D
+error = %22Usernames do not match%22;%0A%09%09%09return false;%0A%09%09%7D%0A%09%09%0A%09%09try %7B%0A%09%09%09String%5B%5D allUsernames = UserDatabase.getAllUsernames();%0A%09%09%09for(int i = 0; i %3C allUsernames.length; i++)%0A%09%09%09%7B%0A%09%09%09%09if (allUsernames%5Bi%5D.equals(username))%0A%09%09%09%09%09return true;
 %0A%09%09%09
+%7D
 %0A%09%09%7D 
-else %7B%0A%09
+catch (SQLException e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%0A
 %09%09er
@@ -3517,25 +3517,24 @@
 t Found%22;%0A%09%09
-%09
 return false
@@ -3531,26 +3531,22 @@
 turn false;%0A
-%09%09%7D%0A
 %09%09%0A%09%7D%0A%09%0A%7D%0A

Fix remove user not checking properly for existing names