package com.connor.erp.searchImpResult;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

import com.teamcenter.rac.aif.AbstractAIFDialog;
import com.teamcenter.rac.aif.AbstractAIFUIApplication;
import com.teamcenter.rac.common.actions.NewAddressListAction;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.util.MessageBox;
import com.teamcenter.rac.util.PropertyLayout;


public class SearchImpResultDialog extends AbstractAIFDialog implements ActionListener{

	private AbstractAIFUIApplication application;
	private TCSession session;
	private JTextField userField;
	private JTextField fromDate;
	private JTextField toDate;
	private JButton searchButton;
	private JPanel panel;
	private JPanel panel1;
	private JPanel panel2;
	private JScrollPane scrollPane;
	private DateChooser dateChooser1;
	private DateChooser dateChooser2;
	private JTable table;
	private JComboBox<String> taskTypeBox;
	private JComboBox<String> impResultBox;
	private SimpleDateFormat df = new SimpleDateFormat("yyyy-M-dd HH:mm");
	
	public SearchImpResultDialog() {
		
	}
	
	public SearchImpResultDialog(AbstractAIFUIApplication application,TCSession session) {
		this.application = application;
		this.session = session;
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
//		super.run();
		try {
			init();
		} catch (TCException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void init() throws TCException {
		panel = new JPanel(new BorderLayout());
		panel1 = new JPanel(new GridLayout(3,4,10,20));
		panel2 = new JPanel(new BorderLayout());
		scrollPane = new JScrollPane();
		userField = new JTextField(10);
		fromDate = new JTextField(10);
		toDate = new JTextField(10);
		searchButton = new JButton("查询");
		taskTypeBox = new JComboBox<String>();
		impResultBox = new JComboBox<String>();
		
		taskTypeBox.addItem("");
		taskTypeBox.addItem("基础物料");
		taskTypeBox.addItem("PBOM");
		taskTypeBox.addItem("工艺路线");
		impResultBox.addItem("");
		impResultBox.addItem("成功");
		impResultBox.addItem("警告");
		impResultBox.addItem("错误");
		impResultBox.addItem("正在执行");
		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.MONTH, -1);
		dateChooser1 = DateChooser.getInstance(calendar.getTime(),"yyyy-M-dd HH:mm");
		dateChooser2 = DateChooser.getInstance(new Date(),"yyyy-M-dd HH:mm");
		dateChooser1.register(fromDate);
		dateChooser2.register(toDate);
		fromDate.setText(df.format(calendar.getTime()));
		toDate.setText(df.format(new Date()));
		String[] property = new String[] { "序号", "导入对象", "任务类型 ", "任务号", "导入时间", "导入人", "导入结果","错误信息","对象版本"};
		String[][] data = new String[][] {};

		DefaultTableModel model = new DefaultTableModel(data, property);
		table =  new JTable(model) {
			public boolean isCellEditable(int row, int column) {
				return false;// 表格不允许被编辑
			}
		};
		table.setColumnModel(getColumn(table, new int[] { 70, 70, 70, 70,70,70,70,70}));
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
//		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		
//		panel1.add("1.1.left.top",new Label("导入人"));
//		panel1.add("1.2.left.top",userField);
//		panel1.add("1.3.left.top",new Label());
//		panel1.add("1.4.right.top",searchButton);
//		panel1.add("2.1.left.top",new Label("任务类型"));
//		panel1.add("2.2.left.top",taskTypeBox);
//		panel1.add("2.3.left.top",new Label("导入结果"));
//		panel1.add("2.4.left.top",impResultBox);
//		panel1.add("3.1.left.top",new Label("传递时间从"));
//		panel1.add("3.2.left.top",fromDate);
//		panel1.add("3.3.left.top",new Label("传递时间至"));
//		panel1.add("3.4.left.top",toDate);
		panel1.add(new Label("导入人"));
		panel1.add(userField);
		panel1.add(new Label());
		panel1.add(searchButton);
		panel1.add(new Label("任务类型"));
		panel1.add(taskTypeBox);
		panel1.add(new Label("导入结果"));
		panel1.add(impResultBox);
		panel1.add(new Label("传递时间从"));
		panel1.add(fromDate);
		panel1.add(new Label("传递时间至"));
		panel1.add(toDate);
		panel1.setBorder(new EmptyBorder(0,0,30,0));
		searchButton.addActionListener(this);
		userField.setText(session.getUser().getProperty("user_id"));
		
		panel.add(panel1,BorderLayout.NORTH);
		panel.add(panel2,BorderLayout.CENTER);
		panel.setBorder(new EmptyBorder(20,20,20,20));
		panel2.add(scrollPane,BorderLayout.CENTER);
		scrollPane.setViewportView(table);
		
		setLayout(new BorderLayout());
		add(panel,BorderLayout.CENTER);
		setTitle("ERP导入任务记录");
		pack();
		setLocationRelativeTo(null);
		setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		Object source = e.getSource();
		if(source.equals(searchButton)) {
			if(userField.getText().trim()!=null&&userField.getText().trim().length()>0) {
				String taskType = taskTypeBox.getSelectedItem().toString().trim().equals("")?null:taskTypeBox.getSelectedItem().toString().trim();
				String impResult = impResultBox.getSelectedItem().toString().trim().equals("")?null:impResultBox.getSelectedItem().toString().trim();
				SearchImpResultOperation operation = new SearchImpResultOperation(application,session,table,userField.getText().trim(),fromDate.getText().trim(),toDate.getText().trim(),taskType,impResult);
				session.queueOperation(operation);
			}else {
				MessageBox.post("请填写导入人","",MessageBox.INFORMATION);
			}
		}
	}
	
	/**
	 * JTable设置列宽
	 */
	public static TableColumnModel getColumn(JTable table, int[] width) {
		TableColumnModel columns = table.getColumnModel();
		for (int i = 0; i < width.length; i++) {
			TableColumn column = columns.getColumn(i);
			column.setPreferredWidth(width[i]);
		}
		return columns;
	}
}
