package com.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import map.baidu.com.BDMapActivity;

import org.kobjects.base64.Base64;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;

import com.chaoran.imp.InputInterface;
import com.activity.chaoran.DjActivity;
import com.activity.chaoran.ExitThread;
import com.activity.chaoran.MipcaActivityCapture;
import com.activity.chaoran.RunYmupThread;
import com.sys.SysData;

public class DjMenuFun {
	public void Msave_dj(final DjActivity djActivity) throws Exception {
//        if(djActivity.pd.isShowing()){
//        	return;
//        }
		new AlertDialog.Builder(djActivity).setTitle("确定取消框")
				.setMessage("确定保存单据")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						Map map = new HashMap();
						Object ob=DjUtil.saveOrganizationHzData(djActivity.hzTab,djActivity.pageMap.get("GZID"));
						Map hzData=null;
						if(ob instanceof Map){
							hzData=(Map)ob;
						}else{
							View view=(View)ob;
							if(view.isEnabled()){
							  ((View)ob).requestFocus();
							}
							DialogUtil.builder(djActivity, "错误信息", ((InputInterface)ob).getLabel().concat("不能为空"),0);
							djActivity.pd.dismiss();
							return;
						}
//						Map hzData = DjUtil.organizationHzData(djActivity.layout);
						((HashMap)hzData.get("hzData")).putAll(djActivity.pageMap);
						map.put("hzData", hzData);
//						if (djActivity.mxData.size() > 0) {
//							map.put("mxSql", djActivity.mxSql);
//							map.put("mxData", DjUtil.organizationMxData(djActivity.mxData,
//									djActivity.pageMap.get("GZID")));
//						} else {
//							map.put("mxSql", "");
//							map.put("mxData", null);
//						}
						map.put("gzid", djActivity.pageMap.get("GZID"));
						map.put("mxTempTable", djActivity.mxTempTable);
						map.put("djlx", djActivity.pageMap.get("DJLX"));
						map.put("djbs", djActivity.pageMap.get("DJBS"));
						byte[] b;
						try {
							b = IoUtil.getbyte(map);
							String paramString = Base64.encode(b);
							DialogUtil.setDialog(djActivity.pd, "消息提示", "单据保存……");
							new RunYmupThread(paramString, djActivity.runHandler, "saveDj",0).start();
						} catch (Exception e) {
							DialogUtil.setDialog(djActivity.pd, "消息提示", "参数转换成流失败……");
						}
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				}).show();
		
	}

	public void Mdj_over(final DjActivity djActivity) {
		new AlertDialog.Builder(djActivity).setTitle("确定取消框").setMessage("确定退出")
		.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				exitDj(djActivity);
			}})
			.setNegativeButton("取消", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			}).show();
	}
    public  void  exitDj(DjActivity djActivity){
    	DialogUtil.setDialog(djActivity.pd, "消息提示", "单据退出……");
		new ExitThread(djActivity.pageMap.get("GZID"), djActivity.mxTempTable,
				djActivity.runHandler,"exitDj").start();
    }
	public String getusername(DjActivity djActivity) {
		return SysData.lgnname;
	}

	public String getjigid(DjActivity djActivity) {
		return SysData.jigid;
	}
	public String getclientid(DjActivity djActivity) {
		return SysData.clientid.replaceAll(":", "");
	}
	public String getdate(DjActivity djActivity) throws ParseException {
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd");
		Date date1 = dateFormat.parse(djActivity.pageMap.get("RQ"));
//		java.text.DateFormat d3 = DateFormat.getDateInstance();
		return dateFormat.format(date1);
	}

	public String getdatetime(DjActivity djActivity) {
		return djActivity.pageMap.get("RQ");
	}

	/*
	 * -取得当前月份
	 */
	public int getmonth(DjActivity djActivity) throws ParseException {
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd kk:mm:ss");
		Date date1 = dateFormat.parse(djActivity.pageMap.get("RQ"));
		Calendar calendar = GregorianCalendar.getInstance();
		calendar.setTime(date1);
		return calendar.get(Calendar.MONTH) + 1;
	}

	/*
	 * -取得当前年份
	 */
	public int getyear(DjActivity djActivity) throws ParseException {
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd kk:mm:ss");
		Date date1 = dateFormat.parse(djActivity.pageMap.get("RQ"));
		Calendar calendar = GregorianCalendar.getInstance();
		calendar.setTime(date1);
		return calendar.get(Calendar.YEAR);
	}

	/*
	 * -取得当前天数
	 */
	public int getday(DjActivity djActivity) throws ParseException {
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd kk:mm:ss");
		Date date1 = dateFormat.parse(djActivity.pageMap.get("RQ"));
		Calendar calendar = GregorianCalendar.getInstance();
		calendar.setTime(date1);
		return calendar.get(Calendar.DATE);
	}
	/*
	 * 定位当前位置
	 */
	public void Mlocation(DjActivity djActivity){
		Intent intent = new Intent();
		intent.setClass(djActivity,BDMapActivity.class);
		djActivity.startActivityForResult(intent,3);
	}
	/*
	 * 调用摄像头扫描条码
	 */
	public void scan(DjActivity djActivity,String funName){
		String uiId=funName.substring(5, funName.length()-1);
		Intent intent = new Intent();
		intent.setClass(djActivity,MipcaActivityCapture.class);
		intent.putExtra("uiId", uiId);
		djActivity.startActivityForResult(intent,4);
	}
	public static void main(String[] args) {
		String funName="scan(ckname)";
		String uiId=funName.substring(5, funName.length()-6);
		System.out.println(funName+"----"+uiId);
	}
}
