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 SalesProductManagement extends JFrame implements ActionListener {
    private JTable table;
    private DefaultTableModel tableModel;
    private JTextField idField, nameField, phoneField, addressField;
    private JButton addBtn;

    public SalesProductManagement() {

        setTitle("商品信息管理");
        setSize(800, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        // 创建主面板
        JPanel mainPanel = new JPanel(new BorderLayout());

        // 创建表格面板
        JPanel tablePanel = new JPanel(new BorderLayout());
        tablePanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        // 创建表格模型
        String[] columnNames = {"名称", "单价", "购买数量"};
        tableModel = new DefaultTableModel(columnNames, 0);
        table = new JTable(tableModel);
        table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 14));
        table.setRowHeight(25);

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

        // 创建输入面板
        JPanel inputPanel = new JPanel(new GridLayout(1, 2, 10, 10));
        inputPanel.setBorder(BorderFactory.createTitledBorder("结算信息"));
        inputPanel.setFont(new Font("微软雅黑", Font.PLAIN, 14));

        addBtn = new JButton("结算");
        addBtn.addActionListener(this);


        inputPanel.add(addBtn);

        inputPanel.add(new JLabel("共计:"));
        addressField = new JTextField();
        inputPanel.add(addressField);




        // 组装面板
        mainPanel.add(tablePanel, BorderLayout.CENTER);
        mainPanel.add(inputPanel, BorderLayout.SOUTH);

        add(mainPanel);

        loadData();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

    }

    //主类，调试
    public static void main(String[] args) {
        new  SalesProductManagement();
    }



    private void loadData() {
        Jdbc jdbc = new Jdbc();
        try {
            ResultSet rs = jdbc.query("SELECT name,selling_price,quantity_sold FROM commodity");
            // 清空旧数据
            tableModel.setRowCount(0);
            // 填充新数据
            while (rs.next()) {
                Object[] row = {
                        rs.getString("name"),
                        rs.getDouble("selling_price"),
                        rs.getInt("quantity_sold"),
                };
                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() == addBtn) {
            double total = 0.0;  // 总金额

            // 获取表格的行数
            int rowCount = tableModel.getRowCount();

            // 遍历每一行数据
            for(int i = 0; i < rowCount; i++) {
                // 获取单价（第1列，索引从0开始）
                double price = (double) tableModel.getValueAt(i, 1);
                // 获取数量（第2列）
                int quantity = (int) tableModel.getValueAt(i, 2);

                // 计算该行的金额并累加到总金额
                total += price * quantity;
            }

            // 在文本框中显示总金额
            addressField.setText(String.format("%.2f", total) + " 元");
        }
    }
}