/*
 * Created by JFormDesigner on Fri Apr 04 18:19:47 CST 2025
 */

package gui.UI_LenheartUI;

import java.awt.event.*;
import gui.Start;
import gui.tw.AotuNpc;
import gui.tw.rw.XuanShangRenWu;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.table.*;

/**
 * @author dongj
 */
public class KillMonsterStatistics extends JPanel {
    public KillMonsterStatistics() {
        initComponents();
    }

    private void 载入杀怪统计(ActionEvent e) {
        // TODO add your code here
        DefaultTableModel tableModel = (DefaultTableModel)this.table1.getModel();
        tableModel.setRowCount(0);
        // 创建一个自定义的单元格渲染器，用于右对齐字符串
        DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
        rightRenderer.setHorizontalAlignment(JLabel.RIGHT);
        // 遍历表格的所有列，将字符串类型的列设置为右对齐
        for (int i = 0; i < table1.getColumnCount(); i++) {
            if (table1.getColumnClass(i) == String.class) {
                table1.getColumnModel().getColumn(i).setCellRenderer(rightRenderer);
            }
        }

        settable(new ArrayList<>(Start.xsRW.values()));
    }

    private void settable(List<XuanShangRenWu> tttt) {
        DefaultTableModel tableModel = (DefaultTableModel) this.table1.getModel();
        tableModel.setRowCount(0);
        for (XuanShangRenWu obj : tttt) {
            Object[] rowData = {obj.getAccount(), obj.getCid(), obj.getName(), obj.getMobId(), obj.getMobName(), obj.getMobNum()};
            tableModel.addRow(rowData);
        }
    }

    private void 搜索(ActionEvent e) {
        // TODO add your code here
        String BUF = textField1.getText();
        List<XuanShangRenWu> xuanShangRenWus = XuanShangRenWu.fuzzySearch(BUF);
        settable(xuanShangRenWus);
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        button1 = new JButton();
        textField1 = new JTextField();
        button2 = new JButton();

        //======== this ========
        setLayout(null);

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                    {null, null, "", null, null, null, null},
                    {null, null, null, null, null, null, null},
                },
                new String[] {
                    "\u8d26\u53f7\u4fe1\u606f", "\u89d2\u8272ID", "\u89d2\u8272\u540d", "\u602a\u7269ID", "\u602a\u7269\u540d", "\u51fb\u6740\u6570\u91cf", "\u6700\u5927\u6740\u602a\u6570\u91cf"
                }
            ) {
                boolean[] columnEditable = new boolean[] {
                    false, false, false, true, false, true, true
                };
                @Override
                public boolean isCellEditable(int rowIndex, int columnIndex) {
                    return columnEditable[columnIndex];
                }
            });
            scrollPane1.setViewportView(table1);
        }
        add(scrollPane1);
        scrollPane1.setBounds(10, 10, 1285, scrollPane1.getPreferredSize().height);

        //---- button1 ----
        button1.setText("\u8f7d\u5165\u6740\u602a\u7edf\u8ba1");
        button1.addActionListener(e -> 载入杀怪统计(e));
        add(button1);
        button1.setBounds(10, 455, 260, 50);
        add(textField1);
        textField1.setBounds(295, 455, 245, 50);

        //---- button2 ----
        button2.setText("\u641c\u7d22");
        button2.addActionListener(e -> 搜索(e));
        add(button2);
        button2.setBounds(565, 455, 260, 50);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < getComponentCount(); i++) {
                Rectangle bounds = getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            setMinimumSize(preferredSize);
            setPreferredSize(preferredSize);
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on

    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JScrollPane scrollPane1;
    private JTable table1;
    private JButton button1;
    private JTextField textField1;
    private JButton button2;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
