package com.connor.touliao;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import com.connor.erp.BomChgTransferToERP.DBUtils;
import com.teamcenter.rac.aif.AbstractAIFDialog;
import com.teamcenter.rac.kernel.TCComponentItem;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.util.MessageBox;



public class MessageDialog extends AbstractAIFDialog{

	private JFileChooser jFileChooser;
	private DefaultTableModel model;
	private List<String[]> difBomline=new ArrayList<String[]>();
	private TCSession session;
	private Map<TCComponentItem, String> quantityNull;
	private Map<TCComponentItem, String> quantityNullBom;
	
	public MessageDialog(TCSession session,List<String[]> difBomline,Map<TCComponentItem, String> quantityNull, Map<TCComponentItem, String> quantityNullBom) throws TCException {
		super();
		this.session=session;
		this.difBomline=difBomline;
		this.quantityNull=quantityNull;
		this.quantityNullBom=quantityNullBom;
		model = new DefaultTableModel(new Object[][] {},
				new String[] { "序号","BOM子件号","子件名称","BOM数量","工艺投料件号","投料名称","工艺用量"});
		
		int xh= difBomline.size();
		for(Map.Entry<TCComponentItem, String> key:quantityNull.entrySet()) {
			TCComponentItem key2 = key.getKey();
			String value = key.getValue();
			String item_id = key2.getStringProperty("item_id");
			String revision_id = key2.getStringProperty("object_name");
			String type = key2.getType();
			xh=xh+1;
			if(value.equals("process")) {
				difBomline.add(new String[] {String.valueOf(xh),"","","",item_id,revision_id,""});
			}
		}
		
		for(Map.Entry<TCComponentItem, String> key:quantityNullBom.entrySet()) {
			TCComponentItem key2 = key.getKey();
			String value = key.getValue();
			String item_id = key2.getStringProperty("item_id");
			String revision_id = key2.getStringProperty("object_name");
			String type = key2.getType();
			xh=xh+1;
			if(value.equals("bom")) {
				difBomline.add(new String[] {String.valueOf(xh),item_id,revision_id,"","","",""});
			}
		}
		
		if(quantityNull.size()>0 || quantityNullBom.size()>0) {
			MessageBox.post(MessageDialog.this,"设计BOM或工艺BOM中存在数量为空的零组件，请修正后再进行投料检查","提示",2);
		}
		
		initDialog();
	}
	private void initDialog() {
		// TODO Auto-generated method stub
		for(String[] row:difBomline) {
		System.out.println(row[1]);
		model.addRow(row);
	}
	
		
		this.setModal(true);
		this.setSize(1000, 500);
	//	this.getContentPane().setBackground(bgColor);
		setTitle("投料检查结果");
		setLayout(null);
		jFileChooser = new JFileChooser();
	//	this.jFileChooser.setCurrentDirectory(new File());
		this.jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(0, 0, 1000, 400);
		
		
		
		JTable table = new JTable();
		table.setEnabled(false);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setSurrendersFocusOnKeystroke(true);
		table.setColumnSelectionAllowed(true);
		table.setCellSelectionEnabled(true);	
		table.setRowHeight(30);
	//	table.setForeground(Color.DARK_GRAY);
		table.setModel(model);
	//	TableModel model2 = table.getModel();
	//	System.out.println(model2.getRowCount()+"model"+model2.getValueAt(0, 0));
		
	//	table.getTableHeader().setBackground(bgColor);
		table.getColumnModel().getColumn(0).setPreferredWidth(50);
		table.getColumnModel().getColumn(1).setPreferredWidth(100);
		table.getColumnModel().getColumn(2).setPreferredWidth(150);
		table.getColumnModel().getColumn(3).setPreferredWidth(50);
		table.getColumnModel().getColumn(4).setPreferredWidth(100);
		table.getColumnModel().getColumn(5).setPreferredWidth(150);
		table.getColumnModel().getColumn(6).setPreferredWidth(50);
		MyRenderer myRender = new MyRenderer();
		table.setDefaultRenderer(Object.class, myRender);
		
		JTableHeader header1 = new JTableHeader();
		header1.setTable(table);
		scrollPane.setViewportView(table);
	//	scrollPane.getViewport().setBackground(bgColor);
	
		this.add(scrollPane);
		
		JButton cancelBtn=new JButton("关闭");
		cancelBtn.setBounds(550, 410, 100, 30);
		
		cancelBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				dispose();
				
			}
		});
		
		
		this.add(cancelBtn);
		JButton outBtn = new JButton("导出");
		outBtn.setBounds(400, 410, 100, 30);
	//	outBtn.setBackground(bgColor);
		
		
		outBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				selectFileButtonEvent();
//				Date now = new Date();
//				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//				File saveExcelChooser = DBUtils.saveExcelChooser("使用于机型清单" + sdf.format(now));
//				
//				System.out.println(saveExcelChooser.getAbsolutePath());
			}
		});
		this.add(outBtn);
	//	JButton okBtn = new JButton("关闭");
	//	okBtn.setBounds(500, 410, 60, 30);
	////	okBtn.setBackground(bgColor);
	//	scrollPane.add(okBtn);
	//	
	//	okBtn.addActionListener(new ActionListener() {
	//		@Override
	//		public void actionPerformed(ActionEvent e) {
	//			// TODO Auto-generated method stub
	//			dispose();
	//		}
	//	});
		
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}
	public void selectFileButtonEvent(){
		int state = jFileChooser.showOpenDialog(null);
		if(state == 1) {
			return;
		}else{
			File f = jFileChooser.getSelectedFile();
			TouliaoOperation operation=new TouliaoOperation(f,difBomline);
			session.queueOperation(operation);
			System.out.println(f.getAbsolutePath());
		}
	}
	
}
