package view;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.Vector;

import backend.service.impl.QuestionsService;

public class QuestionsPanel extends JPanel{
    private QuestionsService questionsService = new QuestionsService();
    private static Vector<String> columNames = new Vector<>();
    private DefaultTableModel model;
    private JTable table;
    private JScrollPane scrollPane;
    private Vector<Vector<String>> tableData;
    //按钮界面
    private JButton addButton;
    private JButton deleteButton;
    private JButton editButton;
    //搜索界面
    private JComboBox frequencyComboBox;
    private JTextField keyTextField;
    private JButton queryButton;
    static {
        columNames.add("ID");
        columNames.add("题目");
        columNames.add("描述");
        columNames.add("答案");
        columNames.add("频率");
    }
    public void initListener(){
        queryButton.addActionListener(e->{
            int frequency = frequencyComboBox.getSelectedIndex()==0?null:frequencyComboBox.getSelectedIndex();
            String key = keyTextField.getText();
            //清楚已有数据
            model.setRowCount(0);
            //条件查询
            tableData = questionsService.getByCondition(frequency,key);
            //刷新表格
            refreshData();
        });
        deleteButton.addActionListener(e->{
            int row = table.getSelectedRow();
            if(row!=-1){
                int id = Integer.valueOf(tableData.get(row).get(0));
                String word = tableData.get(row).get(1);
                int option = JOptionPane.showConfirmDialog(this,"确认删除"+word+"吗？","提示",JOptionPane.YES_NO_CANCEL_OPTION);
                if(option == JOptionPane.YES_OPTION){
                    questionsService.delete(id);
                    model.removeRow(row);
                }
            }
        });
    }
    public QuestionsPanel(){
        initView();
        initListener();
    }
    private void initView(){
        //设置主面板的布局为盒子布局，垂直方向
        setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
        //1.按钮界面
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(1,3,5,5));
        addButton = new JButton("添加");
        deleteButton = new JButton("删除");
        editButton = new JButton("修改");

        buttonPanel.add(addButton);
        buttonPanel.add(deleteButton);
        buttonPanel.add(editButton);
        buttonPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE,50));
        add(buttonPanel);
        //添加垂直间隔
        add(Box.createVerticalStrut(10));
        //2.搜索界面
        JPanel queryPanel = new JPanel();
        queryPanel.setLayout(new GridLayout(1,3,5,5));
        frequencyComboBox = new JComboBox<String>(new String[]{"全部","低频","中频","高频"});
        keyTextField = new JTextField(15);
        queryButton = new JButton("查询");
        queryPanel.add(frequencyComboBox);
        queryPanel.add(keyTextField);
        queryPanel.add(queryButton);
        queryPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE,30));
        add(queryPanel);
        //添加垂直间隔
        add(Box.createVerticalStrut(10));
        //3.表格界面
        model = new DefaultTableModel(){
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        table = new JTable(model);
        scrollPane = new JScrollPane(table);
        add(scrollPane);

        tableData = questionsService.getAll();
        //刷新数据
        refreshData();
    }
    private void refreshData(){
        model.setDataVector(tableData,columNames);
        model.fireTableDataChanged();
    }
}
