package com.example;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

public class InventoryProductManagement extends JFrame implements ActionListener {
    JMenuBar menuBar;
    JMenu menu;
    JMenuItem menuItem1,menuItem2,menuItem3;
    private JTable table;
    private DefaultTableModel tableModel;
    InventoryProductManagement() {
        super("进货信息管理");
        menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        menu = new JMenu("菜单");
        menuBar.add(menu);
        menuItem1 = new JMenuItem("添加商品");
        menuItem2 = new JMenuItem("删除商品");
        menu.add(menuItem1);
        menu.add(menuItem2);
//        menu.add(menuItem3);
        menuItem1.addActionListener(this);
        menuItem2.addActionListener(this);
//        menuItem3.addActionListener(this);
        setSize(500,500);
        setLocationRelativeTo(null);
        // 表头
        String[] columnNames = {"商品名称", "商品数量", "进货单价"};
        tableModel = new DefaultTableModel(columnNames, 0);
        table = new JTable(tableModel);

        // 滚动面板
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane, BorderLayout.CENTER);

        // 初始化加载数据库数据
        loadData();
        setVisible(true);
    }

    private void loadData() {
        Jdbc jdbc = new Jdbc();
        try {
            ResultSet rs = jdbc.query("SELECT name,count,purchase_price,selling_price,profit,quantity_sold FROM commodity");
            // 清空旧数据
            tableModel.setRowCount(0);
            // 填充新数据
            while (rs.next()) {
                Object[] row = {
                        rs.getString("name"),
                        rs.getInt("count"),
                        rs.getDouble("purchase_price"),
                };
                tableModel.addRow(row);
            }
            rs.close();
            jdbc.close();
        } catch (SQLException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, "加载数据失败: " + e.getMessage());
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == menuItem1) {
            JFrame frame = new JFrame();
            frame.setTitle("添加商品");
            frame.setLayout(new GridLayout(4, 2));
            JLabel nameLabel = new JLabel("商品名称:");
            JTextField nameField = new JTextField();
            JLabel countLabel = new JLabel("商品数量:");
            JTextField countField = new JTextField();
            JLabel purchasePriceLabel = new JLabel("进货单价:");
            JTextField purchasePriceField = new JTextField();
            JButton addButton = new JButton("添加");
            JButton cancelButton = new JButton("取消");
            frame.add(nameLabel);
            frame.add(nameField);
            frame.add(countLabel);
            frame.add(countField);
            frame.add(purchasePriceLabel);
            frame.add(purchasePriceField);
            frame.add(addButton);
            frame.add(cancelButton);
            addButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String name = nameField.getText();
                    int count = Integer.parseInt(countField.getText());
                    double purchasePrice = Double.parseDouble(purchasePriceField.getText());
                    // 调用数据库添加方法
                    Jdbc jdbc = new Jdbc();
                    try {
                        jdbc.update("INSERT INTO commodity (name, count, purchase_price,  quantity_sold) VALUES ('" + name + "', " + count + ", " + purchasePrice + ", " + 0 + ");");
                        jdbc.close();
                        // 刷新数据
                        loadData();
                        // 关闭弹窗
                        frame.dispose();
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                        JOptionPane.showMessageDialog(frame, "添加商品失败: " + ex.getMessage());
                    }
                }
            });
            cancelButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    frame.dispose();
                }
            });
            frame.setSize(300, 200);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setVisible(true);
        }
        else if (e.getSource() == menuItem2) {
            JFrame frame = new JFrame();
            frame.setTitle("删除商品");
            frame.setLayout(new GridLayout(2, 2));
            JLabel nameLabel = new JLabel("商品名称:");
            JTextField nameField = new JTextField();
            JButton deleteButton = new JButton("删除");
            JButton cancelButton = new JButton("取消");
            frame.add(nameLabel);
            frame.add(nameField);
            frame.add(deleteButton);
            frame.add(cancelButton);
            deleteButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String name = nameField.getText();
                    // 调用数据库删除方法
                    Jdbc jdbc = new Jdbc();
                    try {
                        jdbc.update("DELETE FROM commodity WHERE name='" + name + "'");
                        jdbc.close();
                        // 刷新数据
                        loadData();
                        // 关闭弹窗
                        frame.dispose();
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                        JOptionPane.showMessageDialog(frame, "删除商品失败: " + ex.getMessage());
                    }
                }
            });
            cancelButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    frame.dispose();
                }
            });
            frame.setSize(300, 200);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setVisible(true);
        }
    }
    public static void main(String[] args) {
        new InventoryProductManagement();
    }
}
