package nc.pub.filesystem.newui;

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

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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Logger;
import nc.desktop.ui.WorkbenchEnvironment;
import nc.funcnode.ui.action.AbstractNCAction;
import nc.itf.pu.service.IInvoiceOCRMaintain;
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.vo.pub.filesystem.NCFileNode;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDouble;


public class ThUploadAction extends AbstractNCAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ThFileContentPanel parentpanel ;
	public ThUploadAction(ThFileContentPanel 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();
					//校验是否同名
					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);
					
					IInvoiceOCRMaintain iInvoiceOCRMaintain = (IInvoiceOCRMaintain) NCLocator.getInstance().lookup(IInvoiceOCRMaintain.class.getName());
					//JSONObject scanjb = iInvoiceOCRMaintain.startInvoiceOCR(file);
					JSONArray scanjb = JSONArray.parseArray("[{\"info\":{\"id\":\"863671503327305730\",\"fid\":\"863671503327305730\",\"code\":\"1500183130\",\"number\":\"03097962\",\"ext\":\"{\\\"ttax\\\":\\\"244.35\\\",\\\"trate\\\":\\\"13.00\\\",\\\"products\\\":[{\\\"name\\\":\\\"*软饮料*1*12*2501谷拉多国谷牛奶饮品\\\",\\\"amount\\\":\\\"1879.65\\\",\\\"trate\\\":\\\"13.00\\\",\\\"tprice\\\":\\\"244.35\\\",\\\"smodel\\\":\\\"1*12*250l\\\",\\\"muint\\\":\\\"件\\\",\\\"qty\\\":\\\"100\\\",\\\"price\\\":\\\"18.7964601769\\\",\\\"total\\\":null,\\\"hsprice\\\":null,\\\"txrqq\\\":null,\\\"txrqz\\\":null,\\\"txfCph\\\":null,\\\"txfLx\\\":null,\\\"no\\\":null,\\\"row\\\":null,\\\"taxClassifyCode\\\":null,\\\"taxClassifyName\\\":null,\\\"specialPolicySign\\\":null,\\\"realTaxRate\\\":null,\\\"realTax\\\":null,\\\"lslbs\\\":null,\\\"zzstsgl\\\":null,\\\"kce\\\":null,\\\"qdbz\\\":null,\\\"cd\\\":null,\\\"nb\\\":null,\\\"enExInfo\\\":null,\\\"standard\\\":null,\\\"remark\\\":null,\\\"sprice\\\":null}],\\\"totalBig\\\":\\\"贰仟壹佰贰拾肆圆整\\\",\\\"pcontact\\\":\\\"锡林郭勒盟锡林浩特市经济开发区0479-6915081\\\",\\\"pbank\\\":\\\"中国银行锡林大街支行152406103228\\\",\\\"bcontact\\\":\\\"内蒙古自治区呼和浩特市金川开发区金山大街1号0471-3350168\\\",\\\"bbank\\\":\\\"中国农业银行呼和浩特市青城支行490101040009022\\\",\\\"hdlist\\\":false,\\\"content\\\":\\\"*软饮料*1*12*2501谷拉多国谷牛奶饮品\\\",\\\"comment\\\":\\\"\\\",\\\"city\\\":\\\"\\\",\\\"province\\\":\\\"内蒙古自治区\\\",\\\"reviewer\\\":\\\"王志学\\\",\\\"receiptor\\\":\\\"陈志颖\\\",\\\"issuer\\\":\\\"李朝红\\\",\\\"transit\\\":\\\"\\\",\\\"corp_seal\\\":\\\"0\\\",\\\"company_seal_mark\\\":\\\"0\\\",\\\"form_type\\\":\\\"一\\\",\\\"agent_mark\\\":\\\"\\\",\\\"block_chain\\\":\\\"增值税专用发票区块链\\\",\\\"service_name\\\":\\\"\\\",\\\"oil_mark\\\":\\\"\\\",\\\"machine_code\\\":\\\"499903631360\\\",\\\"travel_tax\\\":\\\"\\\",\\\"code_confirm\\\":\\\"1500183130\\\"}\",\"user\":{\"cid\":\"773676128725336064\",\"name\":\"\",\"uid\":\"2108\",\"appid\":\"773676128773832704\"},\"oa\":{\"nid\":0,\"doc_id\":\"\",\"img_id\":\"\"},\"rid\":\"816245486684110851\",\"holiday\":0,\"ic\":0,\"suser\":{\"cid\":\"773676128725336064\",\"name\":\"\",\"uid\":\"2108\",\"appid\":\"773676128773832704\"},\"rcid\":\"773676128725336064\",\"fylx\":\"0\",\"total\":0,\"flist\":[{\"id\":\"863671224163885057\",\"name\":\"发票.pdf\",\"type\":\"pdf\",\"flag\":0,\"travel_fid\":null,\"del\":0,\"ctm\":0,\"size\":0}],\"origin\":{\"id\":\"\",\"fid\":\"\"},\"modify_info\":{\"furl\":\"863682369512931336\",\"print\":0,\"attr\":1,\"ctm\":1684289192,\"utm\":1684289192,\"cstm\":1554048000,\"del\":0,\"source\":13,\"uo\":0,\"fname\":\"11111.jpg\",\"jzzt\":0,\"orf\":2,\"verr_msg\":\"\",\"is_share\":0},\"comm_info\":{\"pro\":{\"kind\":\"服务\",\"cname\":\"增值税专用发票\",\"type\":1,\"date\":\"2019-04-01\",\"chcode\":\"\",\"valid\":0,\"sreim\":0,\"status\":0,\"iele\":2},\"price\":{\"amount\":\"1879.65\",\"total\":\"2124.00\",\"treim\":\"\",\"zreim\":\"\",\"dtax\":\"244.35\",\"hasTalliedMoney\":\"0\"},\"buyer\":{\"tcode\":\"91150000114124263Y\",\"company\":\"内蒙古伊利实业集团股份有限公司\"},\"payer\":{\"tcode\":\"91152502667347301P\",\"company\":\"锡林浩特伊利乳品有限责任公司\"}},\"lian_id\":\"0\",\"origin_fid\":\"0\",\"custom_field_1\":\"2\",\"batch_no\":\"2023051716220100046\",\"bill_type\":0},\"ret\":1705,\"message\":\"发票重复，已被您录入\",\"is_valid\":1},{\"info\":{\"id\":\"861896633940221954\",\"fid\":\"861896633940221954\",\"code\":\"3200222130\",\"number\":\"41034762\",\"ext\":\"{\\\"ttax\\\":\\\"48.11\\\",\\\"trate\\\":\\\"0.00\\\",\\\"products\\\":[{\\\"name\\\":\\\"*住宿服务*住宿费\\\",\\\"amount\\\":\\\"801.89\\\",\\\"trate\\\":\\\"0.00\\\",\\\"tprice\\\":\\\"48.11\\\",\\\"smodel\\\":null,\\\"muint\\\":null,\\\"qty\\\":\\\"5\\\",\\\"price\\\":\\\"160.377999999\\\",\\\"total\\\":null,\\\"hsprice\\\":null,\\\"txrqq\\\":null,\\\"txrqz\\\":null,\\\"txfCph\\\":null,\\\"txfLx\\\":null,\\\"no\\\":null,\\\"row\\\":null,\\\"taxClassifyCode\\\":null,\\\"taxClassifyName\\\":null,\\\"specialPolicySign\\\":null,\\\"realTaxRate\\\":null,\\\"realTax\\\":null,\\\"lslbs\\\":null,\\\"zzstsgl\\\":null,\\\"kce\\\":null,\\\"qdbz\\\":null,\\\"cd\\\":null,\\\"nb\\\":null,\\\"enExInfo\\\":null,\\\"standard\\\":null,\\\"remark\\\":null,\\\"sprice\\\":null}],\\\"totalBig\\\":\\\"捌佰伍拾圆整\\\",\\\"pcontact\\\":\\\"无锡市北塘区中山路601-1号20楼,0510-82626258\\\",\\\"pbank\\\":\\\"工商银行北塘支行1103020609200034705\\\",\\\"bcontact\\\":\\\"上海市徐汇区钦州北路1088号74号楼1楼西侧021-54263566\\\",\\\"bbank\\\":\\\"上海银行横浜桥支行31674803002029692\\\",\\\"hdlist\\\":false,\\\"content\\\":\\\"*住宿服务*住宿费\\\",\\\"comment\\\":\\\"\\\",\\\"city\\\":\\\"\\\",\\\"province\\\":\\\"江苏省\\\",\\\"reviewer\\\":\\\"殷婷\\\",\\\"receiptor\\\":\\\"冯军\\\",\\\"issuer\\\":\\\"张午\\\",\\\"transit\\\":\\\"\\\",\\\"corp_seal\\\":\\\"1\\\",\\\"company_seal_mark\\\":\\\"0\\\",\\\"form_type\\\":\\\"二\\\",\\\"agent_mark\\\":\\\"\\\",\\\"block_chain\\\":\\\"增值税专用发票区块链\\\",\\\"service_name\\\":\\\"\\\",\\\"oil_mark\\\":\\\"\\\",\\\"machine_code\\\":\\\"\\\",\\\"travel_tax\\\":\\\"\\\",\\\"code_confirm\\\":\\\"3200222130\\\"}\",\"user\":{\"cid\":\"773676128725336064\",\"name\":\"\",\"uid\":\"2108\",\"appid\":\"773676128773832704\"},\"oa\":{\"nid\":0,\"doc_id\":\"\",\"img_id\":\"\"},\"rid\":\"861896633944449032\",\"holiday\":4,\"ic\":0,\"suser\":{\"cid\":\"773676128725336064\",\"name\":\"\",\"uid\":\"2108\",\"appid\":\"773676128773832704\"},\"rcid\":\"773676128725336064\",\"fylx\":\"0\",\"total\":0,\"flist\":[{\"id\":\"861816360934080514\",\"name\":\"195fbbba7c8a8672460aa6d6fef656c.jpg\",\"type\":\"jpg\",\"flag\":0,\"travel_fid\":null,\"del\":0,\"ctm\":0,\"size\":0}],\"origin\":{\"id\":\"\",\"fid\":\"\"},\"modify_info\":{\"furl\":\"863734115413295106\",\"print\":0,\"attr\":1,\"ctm\":1683875948,\"utm\":1683875948,\"cstm\":1683875948,\"del\":0,\"source\":13,\"uo\":0,\"fname\":\"\\\"发票名111称.jpg\",\"jzzt\":0,\"orf\":2,\"verr_msg\":\"\",\"is_share\":0},\"comm_info\":{\"pro\":{\"kind\":\"服务\",\"cname\":\"增值税专用发票\",\"type\":1,\"chcode\":\"\",\"valid\":0,\"sreim\":0,\"status\":0,\"iele\":2},\"price\":{\"amount\":\"801.89\",\"total\":\"9850.00\",\"treim\":\"\",\"zreim\":\"\",\"dtax\":\"48.11\",\"hasTalliedMoney\":\"0\"},\"buyer\":{\"tcode\":\"91310116062529620H\",\"company\":\"上海萃顺信息科技有限公司\"},\"payer\":{\"tcode\":\"913202047610317173\",\"company\":\"无锡如家快捷酒店有限公司\"}},\"lian_id\":\"0\",\"origin_fid\":\"0\",\"custom_field_1\":\"0\",\"batch_no\":\"2023051716220100046\",\"bill_type\":0},\"ret\":1705,\"message\":\"发票重复，已被您录入\",\"is_valid\":1}]");
					
					
					
					//发票号
					String vbillcode = "";
					//发票日期
					UFDate dbilldate = null;
					//开票方-----需要查询转PK
					String drawer = "";
					//发票分类-----需要查询转PK
					String invoiceType = "";
					//税码需要查询转PK
					String ctaxcodeid = "";
					//税率
					Double ntaxrate = 0.00; 
					//无税单价
					Double nastorigprice = 0.00; 
					//无税金额
					Double norigmny = 0.00; 
					//税额
					Double ntax = 0.00; 
					//价税合计
					Double norigtaxmny = 0.00; 
					for (int i = 0; i < scanjb.size(); i++) {
						JSONObject price = scanjb.getJSONObject(i).getJSONObject("info").getJSONObject("comm_info").getJSONObject("price");
						norigtaxmny += price.getDouble("total");
						ntax += price.getDouble("dtax");
						norigmny += price.getDouble("amount");
					}
					
					
					
					BillCardPanel cpanel=parentpanel.editor_.getBillCardPanel();
					cpanel.setHeadItem("vbillcode", "11211");
					//税率
					cpanel.setBodyValueAt("13", 0, "ntaxrate ");
					
					
					//税额
					cpanel.setBodyValueAt(ntax.toString(), 0, "ntax");
					//价税合计
					cpanel.setBodyValueAt(norigtaxmny.toString(), 0, "norigtaxmny");
					//本币价税合计
					cpanel.setBodyValueAt(norigtaxmny.toString(), 0, "ntaxmny");
					//本币无税金额
					cpanel.setBodyValueAt(norigmny.toString(), 0, "nmny");
					//无税金额
					cpanel.setBodyValueAt(norigmny.toString(), 0, "norigmny");
					
					
					String str = null;
					System.out.println(str.toString());
					
					
					
					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")/*错误*/,ex.getMessage()/*上传时发生错误！*/);
				//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) {
				}
			}
		}
	
	}
}
