package co.dc.aptiv;

import co.dc.commons.utils.DateTimeUtil;
import co.dc.aptiv.pojo.BillPojo;
import co.dc.aptiv.pojo.MaterielPojo;
import co.dc.aptiv.pojo.UserPojo;
import co.dc.aptiv.utils.DateChooser;
import co.dc.aptiv.utils.ResetJButton;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.swing.*;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.Vector;

/**
 * 产品界面管理
 *
 * @author lyy
 */
public class BillFrame extends JFrame {

    private static final Log log = LogFactory.getLog(BillFrame.class);
    private DefaultComboBoxModel productlistmodel_1 = new DefaultComboBoxModel();
    private DefaultComboBoxModel productlistmodel_2 = new DefaultComboBoxModel();
    private static final int SINGLE_SELECTION = 0;
    /**
     * 三个按钮加载层
     */
    private JPanel contentPane;
    /**
     * 模式选择框
     */
    private JComboBox onlineBox;
    /**
     * 默认模式显示为自动
     */
    private boolean online = false;
    /**
     * 物料选择框
     */
    private JComboBox comboBox_1;
    /**
     * 开始时间
     */
    private JTextField beginDate;
    /**
     * 结束时间
     */
    private JTextField endDate;
    /**
     * 物料pojo
     */
    private List<MaterielPojo> materielPojos = null;
    private MaterielPojo nowMaterielPojo = null;
    private JTable jt;
    private JScrollPane jsp;
    private Vector CellsVector = new Vector();
    private Vector TitleVector = new Vector();
    private UserPojo userPojo;

    /**
     * 查询结果
     */
    List<BillPojo> billPojos = null;

    /**
     * Create the frame.
     */
    public BillFrame() {
        userPojo = AptivApp.session;
        setResizable(false);
        setUndecorated(true);
        setAlwaysOnTop(!isAlwaysOnTop());
        setSize(1024, 768);
        setLocationRelativeTo(null);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setBackground(Color.white);
        setContentPane(contentPane);
        contentPane.setLayout(null);
        AptivApp.billDetailService.setBillFrame(this);

        //模式下拉框
        onlineBox = new JComboBox();
        onlineBox.setBackground(Color.white);
        //模式下拉框初始化
        onlineBox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    int index = onlineBox.getSelectedIndex();
                    onlineBox.setBackground(Color.white);
                    if (index == 1) {
                        online = true;
                        comboBox_1.setEnabled(false);
                        contentPane.updateUI();
                    } else {
                        online = false;
                        comboBox_1.setEnabled(true);
                        contentPane.updateUI();
                    }
                }
            }
        });
        productlistmodel_2.addElement("单机模式");
        productlistmodel_2.addElement("联机模式");
        onlineBox.setModel(productlistmodel_2);
        onlineBox.setBounds(80, 110, 150, 45);
        contentPane.add(onlineBox);

        //物料下拉框
        comboBox_1 = new JComboBox();
        comboBox_1.setBackground(Color.white);
        //物料下拉框初始化
        comboBox_1.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    int index = comboBox_1.getSelectedIndex();
                    if (index > 0) {
                        comboBox_1.setBackground(Color.white);
                        index--;
                        nowMaterielPojo = materielPojos.get(index);
                    } else {
                        nowMaterielPojo = null;
                    }
                }
            }
        });
        comboBox_1.setModel(productlistmodel_1);
        comboBox_1.setBounds(240, 110, 150, 45);
        contentPane.add(comboBox_1);

        //开始日期框
        beginDate = new JTextField(DateTimeUtil.getDateString());
        //第一步
        DateChooser dateChooser = DateChooser.getInstance("yyyy-MM-dd");
        //第二步
        dateChooser.register(beginDate);
        beginDate.setEditable(false);
        beginDate.setBounds(420, 110, 110, 45);
        contentPane.add(beginDate);
        //线
        JLabel line = new JLabel("~");
        line.setBounds(537, 110, 25, 45);
        contentPane.add(line);
        //结束日期框
        endDate = new JTextField(DateTimeUtil.getDateString());
        DateChooser dateChooser2 = DateChooser.getInstance("yyyy-MM-dd");
        dateChooser2.register(endDate);
        endDate.setEditable(false);
        endDate.setBounds(555, 110, 110, 45);
        contentPane.add(endDate);

        //返回按钮组件
        JPanel psnel1 = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Image image = new ImageIcon(MainFrame.class.getResource("/co/dc/aptiv/img/head.png")).getImage();
                g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);
            }
        };
        psnel1.setBounds(0, 0, 1024, 78);
        contentPane.add(psnel1);
        psnel1.setLayout(null);

        //返回按钮
        JButton btnNewButton = new ResetJButton("/co/dc/aptiv/img/return-normal.png", "/co/dc/aptiv/img/return-hover.png", "/co/dc/aptiv/img/return-hover.png");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                new MainFrame().setVisible(true);
                BillFrame.this.dispose();
            }
        });
        btnNewButton.setBounds(894, 0, 130, 53);
        psnel1.add(btnNewButton);

        //查询按钮
        JButton btnNewButton_1 = new JButton("查询");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                loadjtable();
            }
        });
        btnNewButton_1.setBounds(700, 110, 110, 45);
        contentPane.add(btnNewButton_1);

        //导出按钮
        JButton btnNewButton_2 = new JButton("导出");
        btnNewButton_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                AptivApp.billDetailService.exportDetails(billPojos);
            }
        });
        btnNewButton_2.setBounds(820, 110, 110, 45);
        contentPane.add(btnNewButton_2);


        this.TitleVector.add("序号");
        this.TitleVector.add("物料名称");
        this.TitleVector.add("物料编号");
        this.TitleVector.add("总数");
        this.TitleVector.add("合格");
        this.TitleVector.add("不合格");
        this.TitleVector.add("开始时间");
        this.TitleVector.add("结束时间");
        jt = new JTable(CellsVector, TitleVector) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        jt.getColumnModel().getColumn(0).setPreferredWidth(3);
        jt.getColumnModel().getColumn(1).setPreferredWidth(40);
        jt.getColumnModel().getColumn(2).setPreferredWidth(45);
        jt.getColumnModel().getColumn(3).setPreferredWidth(5);
        jt.getColumnModel().getColumn(4).setPreferredWidth(5);
        jt.getColumnModel().getColumn(5).setPreferredWidth(3);
        jt.getColumnModel().getColumn(6).setPreferredWidth(110);
        jt.getColumnModel().getColumn(7).setPreferredWidth(110);

        jt.setSurrendersFocusOnKeystroke(true);
        //单选
        jt.setSelectionMode(SINGLE_SELECTION);
        //充满
//        jt.setFillsViewportHeight(true);
        //行的高度
        jt.setRowHeight(40);
        //得到表头
        JTableHeader header = jt.getTableHeader();
        //设置表头的高度
        header.setPreferredSize(new Dimension(header.getWidth(), 30));
        DefaultTableCellRenderer hr = (DefaultTableCellRenderer) header.getDefaultRenderer();
        //表头内容居左
        hr.setHorizontalAlignment(DefaultTableCellRenderer.LEFT);
        jsp = new JScrollPane(jt, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        jsp.setViewportBorder(new CompoundBorder());
        jsp.setBounds(77, 205, 880, 510);
        contentPane.add(jsp);
        viewChange();
        /*
         * 加载表格和数据
         */
        loadjtable();
    }

    /**
     * 初始化界面
     */
    public void viewChange() {

        //加载物料
        materielPojos = AptivApp.materielService.listAll();
        if (materielPojos != null && materielPojos.size() > 0) {
            this.productlistmodel_1.removeAllElements();
            this.productlistmodel_1.addElement("点击选择物料。");
            for (int i = 0; i < materielPojos.size(); i++) {
                productlistmodel_1.addElement(materielPojos.get(i).getDpn());
            }
        } else {
            productlistmodel_1.addElement("请到【物料管理】界面创建物料。");
            comboBox_1.setBackground(Color.red);
        }
    }

    /**
     * 加载表格和数据的方法
     */
    public void loadjtable() {
        this.CellsVector.clear();
        billPojos = AptivApp.billService.findByParam(online, nowMaterielPojo == null ? 0 : nowMaterielPojo.getId(), beginDate.getText(), endDate.getText());
        if (billPojos != null) {
            for (int i = 0; i < billPojos.size(); i++) {
                BillPojo bill = billPojos.get(i);
                Vector v = new Vector();
                MaterielPojo mp = AptivApp.materielService.findById(bill.getMaterielId());
                v.add(i + 1);
                v.add(mp != null ? mp.getDpn() : "");
                v.add(mp != null ? mp.getCpn() : "");
                v.add(bill.getNumber());
                v.add(bill.getQualifiedNum());
                v.add(bill.getUnqualifiedNum());
                v.add(bill.getCreateTime());
                v.add(bill.getModifyTime());
                CellsVector.add(v);
            }
        }
        this.jt.updateUI();
        this.jt.clearSelection();
    }

    /**
     * 弹出提示信息
     *
     * @param type 0 提示 1 警告
     * @param msg 信息
     */
    public void alertMsg(int type, String msg) {
        JOptionPane.showMessageDialog(BillFrame.this, msg, type == 0 ? "提示" : "警告", JOptionPane.WARNING_MESSAGE);
    }
}
