<reponame>WangHuilongSDUT/PSSSystem
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pss.view;

import com.pss.dao.BuyImpl;
import com.pss.dao.ProductDao;
import com.pss.dao.ProductImpl;
import com.pss.dao.supplierImpl;
import com.pss.dao.supplierdao;
import com.pss.po.Buy;
import com.pss.util.LocationUtil;
import java.util.List;
import com.pss.po.Product;
import com.pss.po.Supplier;
import com.pss.util.ExportExcel;
import java.io.File;
import java.util.ArrayList;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author Administrator
 */
public class PurchasingCheck extends javax.swing.JInternalFrame {

    /**
     * Creates new form NewJInternalFrame4
     */
    public PurchasingCheck() {
        initComponents();
        LocationUtil.setFullScreen(this);  //设置全屏显示
        ProductDao pd=new ProductImpl();
        List<Product> plist=new ArrayList<Product>();
        plist=pd.QueryProduct();
        for(Product p:plist)
        {
            this.cmName.addItem(p.getpName());
        }
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cmName = new javax.swing.JComboBox<>();
        tipName = new javax.swing.JLabel();
        cmSupplier = new javax.swing.JComboBox<>();
        tipSupplier = new javax.swing.JLabel();
        btnSearch = new javax.swing.JButton();
        btnChart = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblProduct = new javax.swing.JTable();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("采购查询");

        cmName.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmNameItemStateChanged(evt);
            }
        });

        tipName.setText("商品名称");

        tipSupplier.setText("供应商");

        btnSearch.setText("采购查询");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        btnChart.setText("导出报表");
        btnChart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChartActionPerformed(evt);
            }
        });

        tblProduct.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "采购单号", "采购商品名", "采购数量", "采购价格", "供应商", "采购时间"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblProduct.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblProductMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblProduct);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(tipName)
                .addGap(18, 18, 18)
                .addComponent(cmName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(51, 51, 51)
                .addComponent(tipSupplier, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cmSupplier, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(91, 91, 91)
                .addComponent(btnSearch)
                .addGap(82, 82, 82)
                .addComponent(btnChart)
                .addContainerGap(208, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tipName)
                    .addComponent(cmSupplier, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tipSupplier)
                    .addComponent(btnSearch)
                    .addComponent(btnChart))
                .addGap(28, 28, 28)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(264, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tblProductMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblProductMouseClicked
        //商品表
    }//GEN-LAST:event_tblProductMouseClicked

    private void cmNameItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmNameItemStateChanged
        // TODO add your handling code here:
        cmSupplier.removeAllItems();
        String proName = cmName.getItemAt(cmName.getSelectedIndex());
        supplierdao sd=new supplierImpl();
        List<Supplier> list=sd.QuerySupplierByGoods(proName);
        for(Supplier s:list)
        {
            cmSupplier.addItem(s.getsName());
        }
    }//GEN-LAST:event_cmNameItemStateChanged

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        // TODO add your handling code here:
        List<String[]> list=new ArrayList<String[]>();
        String pName= cmName.getItemAt(cmName.getSelectedIndex());
        String supName= cmSupplier.getItemAt(cmSupplier.getSelectedIndex());
        Product p=new ProductImpl().QueryProductByName(pName).get(0);
        Supplier s=new supplierImpl().QuerySupplierByName(supName).get(0);
        List<Buy> blist=new BuyImpl().QueryBuyByPnoAndSid(p.getpNo(), s.getsId());
        DefaultTableModel tm = (DefaultTableModel) tblProduct.getModel();
        while (tm.getRowCount() > 0) {
            tm.removeRow(tm.getRowCount() - 1);//逐行清除表中内容
        }
        for(Buy b:blist)
        {
                String []str={
                b.getBuyNo(),
                pName,
                String.format("%d",b.getBuyCount()),
                String.format("%f",b.getPrice()),
                supName,
                b.getBuyTime().toString()};
                tm.addRow(str);
                list.add(str);
        }
        this.listChart=list;
    }//GEN-LAST:event_btnSearchActionPerformed

    private void btnChartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChartActionPerformed
        // TODO add your handling code here:
        JFileChooser savefile = new JFileChooser();//文件选择对话框
        FileFilter filter = new FileNameExtensionFilter("Excel文件(*.xls)", "xls");
        savefile.addChoosableFileFilter(filter);//添加过滤器
        savefile.setFileFilter(filter);
        //打开文件选择对话框，showSaveDialog是保存，showOpenDialog是打开
        int flag = savefile.showSaveDialog(this); 
        File file = null;
        //如果点击了保存按钮
        if (flag == JFileChooser.APPROVE_OPTION) {
        file = savefile.getSelectedFile();//所选择的文件名（手写或选择）
        System.out.println("文件名：" + file.getAbsolutePath());
        String filename = file.getAbsolutePath();
        //截取文件扩展名（文件名长度后4位）
        String ftype = filename.substring(filename.length()-4);
        if(!ftype.equals(".xls")){
            //如果用户没有填写扩展名，自动添加扩展名.xls
            file = new File(filename+".xls");
        }
        //集合获取数据，输出到文件：ExportExcel类的printSale方法
        ExportExcel.printSale(this.listChart, file); //psalelist是要导出到excel的数据集合，来自有数据库查询
        }
    }//GEN-LAST:event_btnChartActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnChart;
    private javax.swing.JButton btnSearch;
    private javax.swing.JComboBox<String> cmName;
    private javax.swing.JComboBox<String> cmSupplier;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblProduct;
    private javax.swing.JLabel tipName;
    private javax.swing.JLabel tipSupplier;
    // End of variables declaration//GEN-END:variables
    private List<String[]> listChart=null;
}
