package nc.pub.filesystem.newui;

import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFileChooser;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import nc.bs.logging.Logger;
import nc.desktop.ui.WorkbenchEnvironment;
import nc.funcnode.ui.action.AbstractNCAction;
import nc.ui.ml.NCLangRes;
import nc.ui.pub.beans.MessageDialog;
import nc.ui.pub.bill.BillCardPanel;
import nc.ui.pub.filesystem.FileManageServletClient;
import nc.ui.uif2.editor.BillForm;
import nc.vo.pub.filesystem.NCFileNode;


public class TestUploadAction extends AbstractNCAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private TestFileContentPanel parentpanel ;
	public TestUploadAction(TestFileContentPanel para){
		parentpanel = para;
		this.putValue(AbstractAction.NAME,NCLangRes.getInstance().getStrByID("sftemp", "UploadAction-0000")/*上传附件*/);
		this.putValue(Action.SHORT_DESCRIPTION, NCLangRes.getInstance().getStrByID("sftemp", "UploadAction-0000")/*上传附件*/);
//		this.putValue(AbstractAction.SMALL_ICON, ClientToolKit.loadImageIcon("images/filesystem/newfolder.png"));
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (parentpanel.rootNode == null) {
			return;
		}
		TreePath path = parentpanel.getPatchTree().getSelectionPath();
		if (path == null) {
			path = new TreePath(parentpanel.rootNode.getPath());
		}
		DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
		
		JFileChooser chooser = new JFileChooser();
		//要求上传多个文件
		chooser.setMultiSelectionEnabled(true);
		if (chooser.showOpenDialog(parentpanel) == JFileChooser.APPROVE_OPTION) {
			String pPath = "";
			if (node instanceof NCFileNode) {
				pPath = ((NCFileNode) node).getFullPath();
			}
//			File file = chooser.getSelectedFile();
			//多文件上传
			File[] arrfiles = chooser.getSelectedFiles();
			if(arrfiles==null ||arrfiles.length==0){
				//没有选择文件退出
				return;
			}
			
			String dsName = WorkbenchEnvironment.getInstance().getLoginBusiCenter().getDataSourceName();
			
			String creator = WorkbenchEnvironment.getInstance().getLoginUser().getCuserid();
			FileInputStream input = null;
			
			
				
				
			try {
				for(File file :arrfiles){
					String fileName = file.getName();
					//传ocr
					InputStream inputStream = new FileInputStream(file);
				    byte[] byt = new byte[inputStream.available()];
				    inputStream.read(byt);
					//Encoder encode = Base64.getEncoder();
					//System.out.println(encode.encodeToString(byt));
				    BillCardPanel cpanel=parentpanel.editor_.getBillCardPanel();
					 cpanel.setHeadItem("vmemo", "OCR回写消息");
					//校验是否同名
					if(parentpanel.CheckTheSameFileName((NCFileNode)node, fileName)){
						continue ;
					}
					//检查 附件大小 ,文件超过  参数设置的大小不能上传
					long size =(long)FileBroseConst.getfilelimit()*1024L*1024L;
					if(size<file.length()){
						MessageDialog.showErrorDlg(parentpanel, NCLangRes.getInstance().getStrByID("sftemp", "UploadAction-0001")/*文件超过限制*/, NCLangRes.getInstance().getStrByID("sftemp", "UploadAction-0002", null, new String[]{fileName})/*{0}大小超过限制不能上传！*/);
						continue ;
					}
					input = new FileInputStream(file);
					NCFileNode newNode = FileManageServletClient.uploadFile(dsName, pPath, fileName, input, creator, file.length());
					if (newNode != null) {
//						NCFileVO  newfilevo = new NCFileVO();
//						newfilevo.set
//						newfilevo.setPath(newpath+newaddfilevo.getFullPath());
//						newfilevo.setCreator(newaddfilevo.getCreator());
//						newfilevo.setTime(newaddfilevo.getTime());
//						newfilevo.setFileLen(newaddfilevo.getFileLen());
						parentpanel.nodeToFileVO((NCFileNode)node, newNode);
					}
				}
			} catch (Exception ex) {
				Logger.error(ex.getMessage(), ex);
				MessageDialog.showErrorDlg(parentpanel, NCLangRes.getInstance().getStrByID("pc", "FileManageUI-000000")/*错误*/,NCLangRes.getInstance().getStrByID("sftemp", "UploadAction-0003")/*上传时发生错误！*/);
			} finally {
				try {
					if (input != null) {
						input.close();
					}
				} catch (Exception e2) {
				}
			}
		}
	
	}
}
