package com.activity.chaoran;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.kobjects.base64.Base64;

import com.chaoran.component.MyAdapter;
import com.chaoran.entiry.DataGrid;
import com.chaoran.entiry.Djselefa;
import com.activity.chaoran.R;
import com.util.DialogUtil;
import com.util.IoUtil;
import com.util.SqlUtil;
import com.util.SysUtil;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

public class DjtqActivity extends Activity {
	private ListView listview;
	private int cur_pos = -1;// 当前显示的一行
	// private ArrayList items_text = new ArrayList();
	private Djselefa djselefa;
	public ProgressDialog pd;
	public MyAdapter adapter;
	private ArrayList listdata;
	private String filedName;// 显示字段的名字
	private String gzid;
	private String mxTempTable;
	private Intent intent;
	private Handler runHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == -1) {
				DialogUtil.builder(DjtqActivity.this, "错误信息", "提取方案初始化失败:"
						+ msg.obj.toString(),0);
			} else if (msg.what == -4) {
				DialogUtil.builder(DjtqActivity.this, "错误信息", "提取方案运行失败:"
						+ msg.obj.toString(),0);
			} else {
				if (msg.arg1 == 4) {
					Object ob = msg.obj;
					if (ob instanceof ArrayList) {
						ArrayList list = (ArrayList) ob;
						if (list.size() > 0) {
							intent.putExtra("param", (HashMap) list.get(0));
							setResult(2, intent);
						}
						clear();
					} else {
						DialogUtil.builder(DjtqActivity.this, "提示信息",
								ob.toString(),0);
					}
				} else {
					HashMap map = (HashMap) msg.obj;
					DataGrid dg = (DataGrid) map.get("dg");
					listdata = dg.getTableData();
					if(listdata==null||listdata.size()<1){
					   filedName="";
					}else{
					   filedName=SysUtil.mapFirst(listdata);
					}
					if (adapter == null) {
						adapter = new MyAdapter(DjtqActivity.this, listdata,
								filedName);
						listview.setAdapter(adapter);
					}
				}
			}
			if (pd != null) {
				pd.dismiss();
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);
		listview = (ListView) findViewById(R.id.mxlist);
		intent = getIntent();
		HashMap paramMap = (HashMap) intent.getSerializableExtra("paramMap");
		djselefa = (Djselefa) paramMap.get("djselefa");
		pd = ProgressDialog.show(DjtqActivity.this, "正在执行提取方案", "正在下载……");
		try {
			new RunSearchThread(djselefa.getT_sql(), IoUtil.ob_base64(paramMap
					.get("t_sqlParam")), runHandler).start();
		} catch (IOException e) {
			// e.printStackTrace();
			DialogUtil.builder(this, "错误信息", "转换成Base64编码失败！",0);
		}
		gzid = paramMap.get("GZID").toString();
		mxTempTable = paramMap.get("mxTempTable").toString();
		intent.removeExtra("paramMap");
		TextView tv = (TextView) findViewById(R.id.mxtitle);
		tv.setText(djselefa.getFangamch());
		((Button) findViewById(R.id.mxbut)).setText("提取");
		listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);// 一定要设置这个属性，否则ListView不会刷新
		listview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long id) {
				cur_pos = position;// 更新当前行
				adapter.cur_pos = cur_pos;
			}
		});
	}

	public void queryMx(View v) {
		try {
			if (cur_pos < 0) {
				DialogUtil.builder(this, "提示信息", "请选择一行！",0);
			} else {
				if (mxTempTable == null || mxTempTable.length() < 0) {
					DialogUtil.builder(this, "提示信息", "明细表不存在！",0);
				}
				HashMap item = (HashMap) listdata.get(cur_pos);
				HashMap hzData = SqlUtil.regSql(djselefa.getHz_sql(), item);
				HashMap mxData = SqlUtil.regSql(djselefa.getMx_sql(), item);
				HashMap hm = new HashMap();
				hm.put("gzid", gzid);
				hm.put("mxTempTable", mxTempTable);
				hm.put("hzSql", hzData.get("sql"));
				hm.put("hzParam", hzData.get("param"));
				hm.put("mxSql", mxData.get("sql"));
				hm.put("mxParam", mxData.get("param"));
				byte[] b = IoUtil.getbyte(hm);
				DialogUtil.setDialog(pd, "提示信息", "正在运行提取方案");
				new RunYmupThread(Base64.encode(b), runHandler, "runTqfa",0)
						.start();
			}
		} catch (Exception e) {
			pd.dismiss();
			DialogUtil.builder(DjtqActivity.this, "错误信息", "提取修改方案组织数据失败!",0);
		}
	}

	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
				&& !event.isCanceled()) {
			clear();
			return true;
		}
		return super.onKeyUp(keyCode, event);
	}

	private void clear() {
		listview = null;
		if (pd != null) {
			pd.dismiss();
			pd = null;
		}
		djselefa = null;
		adapter = null;
		if (listdata != null) {
			listdata.clear();
			listdata = null;
		}
		filedName = null;
		gzid = null;
		mxTempTable = null;
		intent = null;
		runHandler = null;
		intent = null;
		finish();
	}

}
