import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JMenu;
import javax.swing.DefaultListSelectionModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Label;

import javax.swing.JTextArea;
import java.awt.Color;
import java.awt.Component;

import javax.swing.JProgressBar;
import javax.swing.JList;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import lib.SQLConnection;

import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.temporal.IsoFields;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

import javax.swing.event.CaretListener;
import javax.swing.event.CaretEvent;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import net.miginfocom.swing.MigLayout;
import javax.swing.JTextPane;

public class MainFrame extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTable table;
	private JTextField textField_2;
	private JTextField textField_3;
	private JPasswordField textField_4;
	private JTable table_2;
	private float Preis = 0.0F;
	DefaultTableModel model;
	JScrollPane scrollPane_1 = new JScrollPane();
	private JTextField txtProduktID;
	private JTextField txtProduktName;
	private JTextField txtPreis;
	JScrollPane scrollPane = new JScrollPane();

	JTextArea textArea_AX = new JTextArea();
	JTextArea textArea_BX = new JTextArea();
	JTextArea textArea_CX = new JTextArea();
	JTextArea textArea_AY = new JTextArea();
	JTextArea textArea_BY = new JTextArea();
	JTextArea textArea_CY = new JTextArea();
	JTextArea textArea_AZ = new JTextArea();
	JTextArea textArea_BZ = new JTextArea();
	JTextArea textArea_CZ = new JTextArea();
	
	JFrame _this = this;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame frame = new MainFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 882, 623);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		SQLConnection db = new SQLConnection();

		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				textArea_AX.setText("");
				textArea_BX.setText("");
				textArea_CX.setText("");
				textArea_AY.setText("");
				textArea_BY.setText("");
				textArea_CY.setText("");
				textArea_AZ.setText("");
				textArea_BZ.setText("");
				textArea_CZ.setText("");
				
				if (tabbedPane.getSelectedIndex() != 2)
					return;

				// ABC-Analyse ausführen
				SQLConnection db = new SQLConnection("102-012/sven");
				
				
				HashMap<Integer, Float> abcxyz = new HashMap<>();

				ArrayList<HashMap<String, String>> produkte = db.queryToMap("SELECT ID FROM t_artikel");
				for (HashMap<String, String> produkt : produkte) {
					int produktID = Integer.parseInt(produkt.get("ID"));

					Float verkauf = 0f;

					ArrayList<HashMap<String, String>> verkaufswerte = db.queryToMap(
							"SELECT Menge, Verkaufspreis, Woche FROM t_statistik WHERE f_Artikel_ID = " + produktID);

					for (HashMap<String, String> wert : verkaufswerte) {
						float summe = Integer.parseInt(wert.get("Menge")) * Float.parseFloat(wert.get("Verkaufspreis"));
						verkauf += summe;
					}

					abcxyz.put(produktID, verkauf);
				}

				//
				// Funtkion implementieren ids durch namern ersetzen
				//

				ArrayList<Integer> sortiert = new ArrayList<>(abcxyz.keySet());
				sortiert.sort((a, b) -> Float.compare(abcxyz.get(b), abcxyz.get(a)));

				// Gesamtumsatz errechnen
				float gesamtUmsatz = 0;

				for (int i = 0; i < abcxyz.keySet().size(); i++) {
					gesamtUmsatz += abcxyz.get(abcxyz.keySet().toArray()[i]);
				}

				System.out.println(gesamtUmsatz);
				
				// Prozentualen Anteil für jedes Produkt ausrechnen
				HashMap<Integer, Float> prozentualerAnteil = new HashMap<>();

				for (int i = 0; i < abcxyz.keySet().size(); i++) {
					Float ant = abcxyz.get(abcxyz.keySet().toArray()[i]).floatValue() / gesamtUmsatz;
					prozentualerAnteil.put((Integer) abcxyz.keySet().toArray()[i], ant);
				}
				
				/*int AlleProdukte = 50;
				int[] VerkaufteMenge = new int[AlleProdukte];
				ArrayList<HashMap<String, String>> Verkaufte_Artikel;
				
				/*for(int i = 0; i < Verkaufte_Artikel.size(); i++)
				{
					//VerkaufteMenge += Integer.parseInt(Verkaufte_Artikel.get(i).get("Menge"));
				}//
				
				
				int ProduktMenge = 0;
				float[] Verkauft_Durchschnitt = new float[AlleProdukte];
				
				for(int i = 1; i < AlleProdukte; i++)
				{
					ArrayList<HashMap<String, String>> Artikel_sortiert = db.queryToMap("SELECT * FROM `t_statistik` WHERE `f_Artikel_ID` =  " + i);
					if(Artikel_sortiert.size() > 0)
					{
						ProduktMenge++;
					}
				}
				
				VerkaufteMenge / ProduktMenge;
				
				*/

				int wochen = 48;
				
				HashMap<Integer, Double> Varianzkoeffizienten = new HashMap<>();
				
				for (HashMap<String, String> produkt : produkte) {
					int produktID = Integer.parseInt(produkt.get("ID"));
					
					ArrayList<HashMap<String, String>> verkauf = db.queryToMap("SELECT * FROM t_statistik WHERE f_Artikel_ID = " + produktID + " AND Woche <= 48");
					
					double summeVerkaufteMenge = 0;
					
					for (HashMap<String, String> elem : verkauf) {
						summeVerkaufteMenge += Double.parseDouble(elem.get("Menge"));
					}
					
					double Erwartungswert = summeVerkaufteMenge / wochen;
					
					double summeStichproben = 0;
					
					for (HashMap<String, String> elem : verkauf) {
						double menge = Double.parseDouble(elem.get("Menge"));
						summeStichproben += Math.pow(menge - Erwartungswert, 2);
					}
					
					double Standardabweichung = Math.sqrt(summeStichproben / wochen);
					
					Varianzkoeffizienten.put(produktID, Standardabweichung / Erwartungswert);
				}

				// Auswerten
				double valABC = 0;
				double valXYZ = 0;
				for (Integer produktID : prozentualerAnteil.keySet()) {
					
					valABC += prozentualerAnteil.get(produktID);
					valXYZ = Varianzkoeffizienten.get(produktID);
					
					String name = db.queryToMap("SELECT Name FROM t_artikel WHERE ID = " + produktID).get(0).get("Name");
					
					if (valABC <= 0.8f) {
						if (valXYZ <= 0.5) {
							textArea_AX.append(name + "\n");
						} else if (valXYZ <= 1.1) {
							textArea_AY.append(name + "\n");
						} else {
							textArea_AZ.append(name + "\n");
						}
					} else if (valABC > 0.8f && valABC <= 0.9) {
						if (valXYZ <= 0.5) {
							textArea_BX.append(name + "\n");
						} else if (valXYZ <= 1.1) {
							textArea_BY.append(name + "\n");
						} else {
							textArea_BZ.append(name + "\n");
						}
					} else {
						if (valXYZ <= 0.5) {
							textArea_CX.append(name + "\n");
						} else if (valXYZ <= 1.1) {
							textArea_CY.append(name + "\n");
						} else {
							textArea_CZ.append(name + "\n");
						}
					}
				}
				
			}
		});
		tabbedPane.setBounds(10, 11, 846, 563);
		contentPane.add(tabbedPane);

		String comboBoxWoche[] = { "Woche", "1", "2", "3", "4", };

		String a = "Januar";
		String b = "Februar";
		String c = "März";
		String d = "April";
		String e = "Mai";
		String f = "Juni";
		String g = "Juli";
		String o = "August";
		String i = "September";
		String j = "Oktober";
		String k = "November";
		String l = "Dezember";

		String comboBoxMonate[] = { "Monat", a, b, c, d, e, f, g, o, i, j, k, l, };
		Helper h = new Helper();
		h.connect("sven");

		JPanel panel_1 = new JPanel();
		tabbedPane.addTab("   Anmelden / Abmelden   ", null, panel_1, null);
		panel_1.setLayout(null);

		JLabel lblBenutzername = new JLabel("Benutzername:");
		lblBenutzername.setBounds(123, 134, 93, 14);
		panel_1.add(lblBenutzername);

		textField_1 = new JTextField();
		textField_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				Helper g = new Helper();
				g.connect("sven");

				String SQL = "SELECT Name FROM `t_benutzer` WHERE Name = '" + textField_1.getText() + "'";

				DefaultTableModel model = g.getTableModel(SQL);
				if (model.getRowCount() == 1) {
					textField_4.setEnabled(true);
				}
			}
		});
		textField_1.setBounds(226, 131, 110, 20);
		panel_1.add(textField_1);
		textField_1.setColumns(10);

		JLabel lblPasswort = new JLabel("Passwort:");
		lblPasswort.setBounds(149, 159, 67, 14);
		panel_1.add(lblPasswort);

		JButton btnBesttigen = new JButton("Best\u00E4tigen");
		btnBesttigen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				
				Helper g = new Helper();
				g.connect("sven");
				String SQL = "SELECT Passwort FROM `t_benutzer` WHERE Name = '" + textField_1.getText() + "'";
				
				DefaultTableModel model = g.getTableModel(SQL);
				if(model.getRowCount() == 1) {
				String hash = model.getValueAt(0, 0).toString();
				  
				// BCrypt Format Backport. Versionen Prefix Änderung
				if(hash.toCharArray()[2] == 'y') {
					char[] hashArray = hash.toCharArray();
					hashArray[2] = 'a';
					hash = String.valueOf(hashArray);
				}
				
				// wenn der gehashte Wert in der Datenbank mit dem textArea_BX.append(prozentualerAnteil.get(prozentualerAnteil.keySet().toArray()[i]).toString() + "\n");
				  
				if(org.mindrot.BCrypt.checkpw(String.valueOf(textField_4.getPassword()), hash)) {
					tabbedPane.setVisible(true);
					  
					tabbedPane.setEnabledAt(1, true);
					tabbedPane.setEnabledAt(2, true);
					tabbedPane.setSelectedIndex(1);
					}
				}
						 
			}
		});
		btnBesttigen.setBounds(123, 193, 213, 47);
		panel_1.add(btnBesttigen);

		textField_4 = new JPasswordField();
		textField_4.setEnabled(false);
		textField_4.setColumns(10);
		textField_4.setBounds(226, 162, 110, 20);
		panel_1.add(textField_4);
		model = new DefaultTableModel();
		model.setColumnIdentifiers(new String[] { "ID", "Menge", "Preis", "SVEN Bon" });

		JPanel panel = new JPanel();
		tabbedPane.addTab("   Kasse   ", null, panel, null);
		tabbedPane.setEnabledAt(1, false);

		JLabel label = new JLabel("0,00\u20AC");
		label.setBounds(571, 38, 142, 14);
		panel.add(label);

		JButton btnTotal = new JButton("Bezahlen");
		btnTotal.setBounds(525, 424, 188, 82);
		btnTotal.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int rowCount = model.getRowCount();

				String Woche = textField_3.getText();
				
				if (Woche.isEmpty() == false) {

					for (int r = 0; r < rowCount; r++) {
						String ProduktID = (String) model.getValueAt(r, 0);
						String Menge = (String) model.getValueAt(r, 1);
						String Einzelpreis = (String) model.getValueAt(r, 2);
						
						db.execute("INSERT INTO t_statistik (`ID`, `f_Artikel_ID`, `Woche`, `Menge`, `Verkaufspreis`) VALUES (NULL, '" + ProduktID + "', '" + Woche + "', '" + Menge + "', '" + Einzelpreis + "')");
					}
					
					textField_2.setText("");
					txtPreis.setText("");
					txtProduktID.setText("");
					txtProduktName.setText("");

					model = new DefaultTableModel();
					model.setColumnIdentifiers(new String[] { "ID", "Menge", "Preis", "SVEN Bon" });
					table_2.setModel(model);
					label.setText("0,00 €");
					Preis = 0;
				}
			}
		});
		panel.setLayout(null);
		btnTotal.setFont(new Font("Tahoma", Font.PLAIN, 12));
		panel.add(btnTotal);

		JButton button_1 = new JButton("3");
		button_1.setBounds(607, 372, 41, 41);
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textField_2.setText(textField_2.getText() + "3");
			}
		});

		JButton button_2 = new JButton("6");
		button_2.setBounds(607, 332, 41, 41);
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textField_2.setText(textField_2.getText() + "6");
			}
		});
		panel.add(button_2);

		JButton btnDel = new JButton("DEL");
		btnDel.setBounds(648, 291, 65, 41);
		btnDel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text = textField_2.getText();
				textField_2.setText(text.substring(0, text.length() - 1));
			}
		});
		btnDel.setFont(new Font("Tahoma", Font.PLAIN, 12));
		btnDel.setForeground(Color.RED);
		panel.add(btnDel);

		JButton button_3 = new JButton("9");
		button_3.setBounds(607, 292, 41, 41);
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textField_2.setText(textField_2.getText() + "9");
			}
		});
		panel.add(button_3);

		JButton button_5 = new JButton("2");
		button_5.setBounds(566, 372, 41, 41);
		button_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textField_2.setText(textField_2.getText() + "2");
			}
		});
		panel.add(button_5);

		JButton button_6 = new JButton("5");
		button_6.setBounds(566, 332, 41, 41);
		button_6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textField_2.setText(textField_2.getText() + "5");
			}
		});
		panel.add(button_6);

		JButton button_7 = new JButton("8");
		button_7.setBounds(566, 292, 41, 41);
		button_7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textField_2.setText(textField_2.getText() + "8");
			}
		});
		panel.add(button_7);

		JButton button_8 = new JButton("0");
		button_8.setBounds(648, 372, 65, 41);
		button_8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				textField_2.setText(textField_2.getText() + "0");
			}
		});
		panel.add(button_8);

		JButton button_9 = new JButton("1");
		button_9.setBounds(525, 372, 41, 41);
		button_9.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textField_2.setText(textField_2.getText() + "1");
			}
		});
		panel.add(button_9);

		JButton button_10 = new JButton("4");
		button_10.setBounds(525, 332, 41, 41);
		button_10.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textField_2.setText(textField_2.getText() + "4");
			}
		});
		panel.add(button_10);

		JButton button_11 = new JButton("7");
		button_11.setBounds(525, 292, 41, 41);
		button_11.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textField_2.setText(textField_2.getText() + "7");
			}
		});
		panel.add(button_11);

		JButton btnAbbruch = new JButton("Abbruch");
		btnAbbruch.setBounds(443, 291, 82, 41);
		btnAbbruch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textField_2.setText("");
				txtPreis.setText("");
				txtProduktID.setText("");
				txtProduktName.setText("");

				model = new DefaultTableModel();
				model.setColumnIdentifiers(new String[] { "ID", "Menge", "Preis", "SVEN Bon" });
				table_2.setModel(model);
				label.setText("0,00 €");
				Preis = 0;
			}
		});
		btnAbbruch.setFont(new Font("Tahoma", Font.PLAIN, 12));
		panel.add(btnAbbruch);

		textField = new JTextField();
		textField.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent arg0) {
				String Suchen = textField.getText();

				if (Suchen.equals("")) {
					table = new JTable();
					Helper h = new Helper();
					h.connect("sven");
					table.setModel(h.getTableModel("SELECT ID, Name, Marke, Verkaufspreis FROM t_artikel"));
					scrollPane.setViewportView(table);

				} else {
					table = new JTable();
					Helper g = new Helper();
					g.connect("sven");

					String SQL = "SELECT ID, Name, Marke, Verkaufspreis FROM `t_artikel` WHERE Name LIKE '" + Suchen
							+ "%' ";

					table.setModel(g.getTableModel(SQL));
					scrollPane.setViewportView(table);
				}

				table.getColumnModel().getColumn(0).setPreferredWidth(25);

				table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
					@Override
					public void valueChanged(ListSelectionEvent e) {

						if (e.getValueIsAdjusting() == false) {

							txtProduktID.setText((String) table.getModel().getValueAt(table.getSelectedRow(), 0));

							txtProduktName.setText((String) table.getModel().getValueAt(table.getSelectedRow(), 1));

							float preis = Float.parseFloat((String) table.getModel().getValueAt(table.getSelectedRow(), 3));
							
							txtPreis.setText(String.format("%.2f €", preis));
						}
					}
				});
			}
		});
		textField.setBounds(10, 11, 423, 20);
		panel.add(textField);
		textField.setColumns(10);

		JLabel lblBetrag = new JLabel("Betrag:");
		lblBetrag.setBounds(525, 38, 60, 14);
		panel.add(lblBetrag);

		JComboBox Woche = new JComboBox(comboBoxWoche);
		Woche.setBounds(505, 11, 80, 20);
		panel.add(Woche);

		JComboBox Monate = new JComboBox(comboBoxMonate);
		Monate.setBounds(583, 11, 133, 20);
		panel.add(Monate);

		JButton btnKw = new JButton("KW");
		btnKw.setBounds(716, 11, 55, 20);
		btnKw.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int p;
				String w = (String) Woche.getSelectedItem();
				p = Integer.valueOf(w);
				String m = (String) Monate.getSelectedItem();
				int n = 0;
				if (m.equals(a)) {
					n = 1;
				}
				if (m.equals(b)) {
					n = 2;
				}
				if (m.equals(c)) {
					n = 3;
				}
				if (m.equals(d)) {
					n = 4;
				}
				if (m.equals(e)) {
					n = 5;
				}
				if (m.equals(f)) {
					n = 6;
				}
				if (m.equals(g)) {
					n = 7;
				}
				if (m.equals(o)) {
					n = 8;
				}
				if (m.equals(i)) {
					n = 9;
				}
				if (m.equals(j)) {
					n = 10;
				}
				if (m.equals(k)) {
					n = 11;
				}
				if (m.equals(l)) {
					n = 12;
				}
				int k;
				k = (p + ((n - 1) * 4));
				String q;
				q = String.valueOf(k);
				textField_3.setText(q);
			}
		});
		panel.add(btnKw);

		scrollPane.setBounds(10, 36, 423, 375);
		panel.add(scrollPane);

		panel.add(button_1);

		JButton btnEnter = new JButton("Enter");
		btnEnter.setBounds(648, 331, 65, 41);
		btnEnter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				int Menge = Integer.parseInt(textField_2.getText());
				
				float Einzelpreis = Float.valueOf(txtPreis.getText().replace("€", "").replace(",", "."));

				float Summe = Menge * Einzelpreis;

				Preis += Summe;
				label.setText(String.format("%.2f €", Preis));

				model.addRow(new String[] { txtProduktID.getText(), Menge + "", Einzelpreis + "", Summe + " €" });

				textField_2.setText("");
				txtPreis.setText("");
				txtProduktID.setText("");
				txtProduktName.setText("");

				
				
			}
		});
		btnEnter.setFont(new Font("Tahoma", Font.PLAIN, 12));
		btnEnter.setForeground(Color.GREEN);
		panel.add(btnEnter);

		scrollPane_1.setBounds(505, 51, 208, 193);
		panel.add(scrollPane_1);

		table_2 = new JTable();
		table_2.setModel(model);
		table_2.getColumnModel().getColumn(0).setPreferredWidth(20);
		scrollPane_1.setViewportView(table_2);

		JButton btnQrcodeScannen = new JButton("QR-Code");
		btnQrcodeScannen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// JFileChooser erstellen
				JFileChooser fc = new JFileChooser();
				fc.setCurrentDirectory(new File("T:/INTABI19/Projekt/ELMO Interactive Toolbox/rec"));
				
				// Wenn im Dialog nicht OK gedrückt wurde, Methode verlassen
				if (fc.showOpenDialog(_this) != JFileChooser.APPROVE_OPTION)
					return;
				
				// Ausgewählte Datei auslesen
				File f = fc.getSelectedFile();
					
				// Ausgelesener Wert
				int value = -1;
				
				// Versuche, den QR-Code einzulesen
				// Wenn das nicht geht, wird eine Fehlermeldung ausgegeben
				try {
					value = SvenQR.ReadQR(f.getAbsolutePath());
				} catch (IOException e1) {
					JOptionPane.showMessageDialog(_this, "QR-Code konnte nicht eingelesen werden: " + e1.getMessage());
					return;
				}
				
				int Menge = 1;

				Helper g = new Helper();
				g.connect("sven");
				String SQL = "SELECT Verkaufspreis FROM `t_artikel` WHERE QR = " + value;	
				
				DefaultTableModel _model = g.getTableModel(SQL);
				
				if (_model.getRowCount() != 1) {
					JOptionPane.showMessageDialog(_this, "QR-Code konnte nicht zugeordnet werden.");
					return;
				}
				
				float Einzelpreis = Float.valueOf(_model.getValueAt(0, 0).toString());
				float Summe = Menge * Einzelpreis;
				
				Preis += Summe;
				label.setText(String.format("%.2f €", Preis));

				model.addRow(new String[] { GetProductIDFromQRCode(value), Menge + "", Einzelpreis + "", Summe + " €" });
			}
		});
		btnQrcodeScannen.setBounds(443, 331, 82, 82);
		btnQrcodeScannen.setFont(new Font("Trebuchet MS", Font.PLAIN, 12));
		panel.add(btnQrcodeScannen);

		textField_2 = new JTextField();
		textField_2.setBounds(505, 271, 103, 20);
		textField_2.setColumns(10);
		panel.add(textField_2);

		textField_3 = new JTextField();
		textField_3.setBounds(771, 11, 60, 20);
		panel.add(textField_3);
		textField_3.setColumns(10);

		txtProduktID = new JTextField();
		txtProduktID.setEditable(false);
		txtProduktID.setBounds(505, 248, 61, 20);
		panel.add(txtProduktID);
		txtProduktID.setColumns(10);

		txtProduktName = new JTextField();
		txtProduktName.setEditable(false);
		txtProduktName.setBounds(566, 248, 147, 20);
		panel.add(txtProduktName);
		txtProduktName.setColumns(10);

		txtPreis = new JTextField();
		txtPreis.setEditable(false);
		txtPreis.setBounds(607, 271, 106, 20);
		panel.add(txtPreis);
		txtPreis.setColumns(10);

		textField.setText("4");
		textField.setText("");

		JPanel panel_2 = new JPanel();
		tabbedPane.addTab("   ABC- / XYZ-Analyse   ", null, panel_2, null);
		tabbedPane.setEnabledAt(2, false);
		panel_2.setLayout(new MigLayout("", "[][grow][grow][grow]", "[][grow][grow][grow]"));

		JPanel panel_3 = new JPanel();
		panel_2.add(panel_3, "cell 1 0,grow");
		panel_3.setLayout(new BorderLayout(0, 0));

		JLabel lblA = new JLabel("A");
		lblA.setFont(new Font("Tahoma", Font.BOLD, 18));
		lblA.setHorizontalAlignment(SwingConstants.CENTER);
		panel_3.add(lblA, BorderLayout.CENTER);

		JPanel panel_4 = new JPanel();
		panel_2.add(panel_4, "cell 2 0,grow");
		panel_4.setLayout(new BorderLayout(0, 0));

		JLabel lblB = new JLabel("B");
		lblB.setHorizontalAlignment(SwingConstants.CENTER);
		lblB.setFont(new Font("Tahoma", Font.BOLD, 18));
		panel_4.add(lblB, BorderLayout.CENTER);

		JPanel panel_5 = new JPanel();
		panel_2.add(panel_5, "cell 3 0,grow");
		panel_5.setLayout(new BorderLayout(0, 0));

		JLabel lblNewLabel = new JLabel("C");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 18));
		panel_5.add(lblNewLabel, BorderLayout.CENTER);

		JPanel panel_6 = new JPanel();
		panel_2.add(panel_6, "cell 0 1,grow");
		panel_6.setLayout(new BorderLayout(0, 0));

		JLabel lblX = new JLabel("X");
		lblX.setHorizontalAlignment(SwingConstants.CENTER);
		lblX.setFont(new Font("Tahoma", Font.BOLD, 18));
		panel_6.add(lblX, BorderLayout.CENTER);

		JScrollPane scrollPane_2 = new JScrollPane();
		panel_2.add(scrollPane_2, "cell 1 1,grow");
		textArea_AX.setEditable(false);
		textArea_AX.setWrapStyleWord(true);
		scrollPane_2.setViewportView(textArea_AX);

		JScrollPane scrollPane_3 = new JScrollPane();
		panel_2.add(scrollPane_3, "cell 2 1,grow");

		
		textArea_BX.setWrapStyleWord(true);
		textArea_BX.setEditable(false);
		scrollPane_3.setViewportView(textArea_BX);

		JScrollPane scrollPane_4 = new JScrollPane();
		panel_2.add(scrollPane_4, "cell 3 1,grow");

		
		textArea_CX.setWrapStyleWord(true);
		textArea_CX.setEditable(false);
		scrollPane_4.setViewportView(textArea_CX);

		JPanel panel_7 = new JPanel();
		panel_2.add(panel_7, "cell 0 2,grow");
		panel_7.setLayout(new BorderLayout(0, 0));

		JLabel lblY = new JLabel("Y");
		lblY.setHorizontalAlignment(SwingConstants.CENTER);
		lblY.setFont(new Font("Tahoma", Font.BOLD, 18));
		panel_7.add(lblY, BorderLayout.CENTER);

		JScrollPane scrollPane_5 = new JScrollPane();
		panel_2.add(scrollPane_5, "cell 1 2,grow");

		textArea_AY.setWrapStyleWord(true);
		textArea_AY.setEditable(false);
		scrollPane_5.setViewportView(textArea_AY);

		JScrollPane scrollPane_6 = new JScrollPane();
		panel_2.add(scrollPane_6, "cell 2 2,grow");

		textArea_BY.setWrapStyleWord(true);
		textArea_BY.setEditable(false);
		scrollPane_6.setViewportView(textArea_BY);

		JScrollPane scrollPane_7 = new JScrollPane();
		panel_2.add(scrollPane_7, "cell 3 2,grow");

		textArea_CY.setWrapStyleWord(true);
		textArea_CY.setEditable(false);
		scrollPane_7.setViewportView(textArea_CY);

		JPanel panel_8 = new JPanel();
		panel_2.add(panel_8, "cell 0 3,grow");
		panel_8.setLayout(new BorderLayout(0, 0));

		JLabel lblZ = new JLabel("Z");
		lblZ.setHorizontalAlignment(SwingConstants.CENTER);
		lblZ.setFont(new Font("Tahoma", Font.BOLD, 18));
		panel_8.add(lblZ, BorderLayout.CENTER);

		JScrollPane scrollPane_8 = new JScrollPane();
		panel_2.add(scrollPane_8, "cell 1 3,grow");

		textArea_AZ.setWrapStyleWord(true);
		textArea_AZ.setEditable(false);
		scrollPane_8.setViewportView(textArea_AZ);

		JScrollPane scrollPane_9 = new JScrollPane();
		panel_2.add(scrollPane_9, "cell 2 3,grow");

		textArea_BZ.setWrapStyleWord(true);
		textArea_BZ.setEditable(false);
		scrollPane_9.setViewportView(textArea_BZ);

		JScrollPane scrollPane_10 = new JScrollPane();
		panel_2.add(scrollPane_10, "cell 3 3,grow");

		textArea_CZ.setWrapStyleWord(true);
		textArea_CZ.setEditable(false);
		scrollPane_10.setViewportView(textArea_CZ);

	}

	private String GetProductIDFromQRCode(int value) {
		Helper g = new Helper();
		g.connect("sven");
		String SQL = "SELECT ID FROM `t_artikel` WHERE QR = " + value;	
		
		DefaultTableModel model = g.getTableModel(SQL);
		
		if (model.getRowCount() != 1) {
			JOptionPane.showMessageDialog(_this, "QR-Code konnte nicht zugeordnet werden.");
			return "";
		}

		return String.valueOf(model.getValueAt(0, 0));
	}
}
