package BWP.MobileSale;

import java.util.HashMap;
import java.util.List;

import org.forks.jsonrpc.DFDataTable;
import org.forks.jsonrpc.RpcFacade;
import org.forks.jsonrpc.RpcObject;

import BWP.MobileSale.Util.ActivityUtil;
import BWP.MobileSale.Util.StringUtil;
import BWP.MobileSale.Util.ViewOnClickTask;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class SelectAccountingUnitActivity extends LoginedActivity {
	private SimpleAdapter adapter;
	private ListView listView;
	private EditText search;

	private void BeginSearch() {
		searchTask.execute();
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.selectbuyer);
		listView = ActivityUtil.Find(this, R.id.ListView);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@SuppressWarnings("unchecked")			
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				HashMap<String, Object> map = (HashMap<String, Object>) arg0
						.getItemAtPosition(arg2);
				Intent intent = new Intent();
				intent.putExtra("Result", map);
				ActivityUtil.Finish(SelectAccountingUnitActivity.this, intent);

			}	

		});
		
		
	
		
		search = ActivityUtil.Find(this, R.id.EditText_Search);

		Button button = ActivityUtil.Find(this, R.id.Button_Search);

		button.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				BeginSearch();
			}

		});
		searchTask.execute();
		}
	
	ViewOnClickTask searchTask = new ViewOnClickTask(this, "查询中...") {
		
		public Object call() throws Exception {
			String method = "/Sale/Sale/Rpcs/MobileOrderRpc/AccountingUnitSearch";

			RpcObject argument = RpcObject
					.create("/Sale/Sale/Rpcs/RpcSearch");
			argument.setString("SearchKey", search.getText()
					.toString());

			DFDataTable table = RpcFacade.rpcCall(method,argument);
			List<RpcObject> rows = table.getRows();
			
			adapter = new SimpleAdapter(SelectAccountingUnitActivity.this,
					rows, R.layout.batchno_select_item,
					new String[] { "Name" },
					new int[] { R.id.TextView_Name
						});
			
			return null;
		}

		@Override
		protected void successUI() {
			listView.setAdapter(adapter);
		}
		
	};	
	
}
