package CHAPTER10;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;

public class C10_LAB02_ProductManagement extends JFrame {

	private List<String> productList;
	private DefaultMutableTreeNode rootNode;
	private JTree productTree;
	private JTextArea inputArea;

	public C10_LAB02_ProductManagement() {
		// 상품 리스트 초기화
		productList = new ArrayList<>();
		productList.add("Laptop");
		productList.add("Smartphone");
		productList.add("Tablet");

		// 메뉴바 생성
		JMenuBar menuBar = new JMenuBar();
		JMenu fileMenu = new JMenu("File");
		JMenuItem exitItem = new JMenuItem("Exit");
		fileMenu.add(exitItem);
		menuBar.add(fileMenu);
		setJMenuBar(menuBar);

		// 레이아웃 설정
		setLayout(new BorderLayout());

		// 입력 패널 생성
		JPanel inputPanel = new JPanel(new BorderLayout());
		inputArea = new JTextArea(2, 30);
		inputArea.setText("Pad");
		JButton addButton = new JButton("Add Product");
		inputPanel.add(new JScrollPane(inputArea), BorderLayout.CENTER);
		inputPanel.add(addButton, BorderLayout.SOUTH);

		// 상품 정보 패널 추가
		JPanel infoPanel = new JPanel(new GridLayout(3, 2));
		JLabel nameLabel = new JLabel("Product Name:");
		JTextField nameField = new JTextField();
		JLabel priceLabel = new JLabel("Price:");
		JTextField priceField = new JTextField();
		JLabel categoryLabel = new JLabel("Category:");
		JComboBox<String> categoryBox = new JComboBox<>(new String[] { "Electronics", "Furniture", "Groceries" });

		infoPanel.add(nameLabel);
		infoPanel.add(nameField);
		infoPanel.add(priceLabel);
		infoPanel.add(priceField);
		infoPanel.add(categoryLabel);
		infoPanel.add(categoryBox);
		inputPanel.add(infoPanel, BorderLayout.NORTH);

		// 상품 트리 생성
		rootNode = new DefaultMutableTreeNode("Products");
		DefaultMutableTreeNode electronicsNode = new DefaultMutableTreeNode("Electronics");
		rootNode.add(electronicsNode);
		for (String product : productList) {
			electronicsNode.add(new DefaultMutableTreeNode(product));
		}
		productTree = new JTree(rootNode);
		JScrollPane treeScrollPane = new JScrollPane(productTree);

		// 상품 테이블 생성
		JPanel tablePanel = new JPanel(new BorderLayout());
		String[] columnNames = { "Product Name", "Price", "Category" };
		Object[][] data = { 
				{ "Laptop", "$1000", "Electronics" }, 
				{ "Smartphone", "$800", "Electronics" },
				{ "Tablet", "$600", "Electronics" } 
		};
		JTable productTable = new JTable(data, columnNames);
		tablePanel.add(new JScrollPane(productTable), BorderLayout.CENTER);

		// 중앙 패널 구성
		JSplitPane centerSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treeScrollPane, tablePanel);

		// 메인 프레임에 컴포넌트 추가
		add(inputPanel, BorderLayout.NORTH);
		add(centerSplitPane, BorderLayout.CENTER);

		// JFrame 설정
		setTitle("Product Management App");
		setSize(600, 400);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}

	public static void main(String[] args) {
		new C10_LAB02_ProductManagement();
	}
}