package BWP.MobileSale.MobileAdjustGoodsBill;

import java.math.BigDecimal;
import java.util.Date;
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.LoginedActivity;
import BWP.MobileSale.R;
import BWP.MobileSale.Util.ActivityUtil;
import BWP.MobileSale.Util.CallableTask;
import BWP.MobileSale.Util.DateTimeUtil;
import BWP.MobileSale.Util.DialogUtil;
import BWP.MobileSale.Util.DialogUtil.SetDate;
import BWP.MobileSale.Util.StringUtil;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.Spinner;
import android.widget.TextView;
import android.app.Activity;

public class MobileAdjustGoodsBillListActivity extends LoginedActivity {
	ListView listView;
	private SimpleAdapter adapter;
	private Button buttonDate;
	private Button btnRefresh;

	private Spinner filter;
	private Spinner billStateFilter;
	
	
	String[] columnNames = new String[]{"ID","OutMoney","AdjustOutBuyer_Name","AdjustOutDate","BillState"};
	
	int[] columnIDs = new int[]{R.id.TextView1,R.id.TextView2,R.id.TextView3,R.id.TextView_Date,R.id.TextView_BillState};
	
	
	class SumInfo{
		public int RowCount;
		public BigDecimal Money;
		@Override
		public String toString() {
			return String.format("共%s条记录,%s元",RowCount,Money);
		}
		
	}
	
	SumInfo mSumInfo = new SumInfo();
	CallableTask searchTask = new CallableTask(this, "查询中...") {

		public Object call() throws Exception {
			String method = "/Sale/SaleXiWang/Rpcs/MobileAdjustGoodsBillRpc/Search";
			RpcObject argument = RpcObject.create("/Sale/Sale/Rpcs/RpcSearch");
			
			Resources resource = getResources();
			String searchkey = (String) filter.getSelectedItem();
			argument.setString("SearchKey", searchkey);
		
			argument.setString("SearchKey2", (String)billStateFilter.getSelectedItem());
			
			String dateText = buttonDate.getText().toString();
			if(!StringUtil.IsNullOrEmpty(dateText)){
				Date parseDate = DateTimeUtil.ParseDate(dateText);
				argument.setDate("Date1", parseDate);
			}
			
			DFDataTable table = RpcFacade.rpcCall(method, argument);
			List<RpcObject> rows = table.getRows();
			mSumInfo.RowCount = rows.size();
			mSumInfo.Money = new BigDecimal(0);
			for(RpcObject row:rows){
				mSumInfo.Money = mSumInfo.Money.add(row.getMoney("OutMoney"));
			}
			
			
			adapter = new SimpleAdapter(MobileAdjustGoodsBillListActivity.this, rows,
					R.layout.order_list_item, columnNames,columnIDs);
			adapter.setViewBinder(viewBinder);
			return null;
		}

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


	};
	ViewBinder viewBinder = new ViewBinder() {
		public boolean setViewValue(View view, Object data,
				String textRepresentation) {
			if (view.getId() == R.id.TextView_Date) {
				((TextView)view).setText(DateTimeUtil.FormatDate((Date) data));
				return true;
			}
			else{
				return false;
			}
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mobileadjustgoodsbill_list);
		initButtonRefresh();
		
		initListView();

		initFilter();
		
		initButtonDate();

		StartQuery();
	}
	
	private void initButtonRefresh()
	{
		btnRefresh=(Button)findViewById(R.id.Button_Refresh);
		btnRefresh.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				StartQuery();
			}
		});
		
	}
	
	
	private void StartQuery() {
		searchTask.execute();
	}
	
	private void initButtonDate() {
		buttonDate = ActivityUtil.FindButton(this, R.id.Button_Date);
		DialogUtil.AddSelectDateDialog(buttonDate, new SetDate() {

			public void setDate(Date date) {
				StartQuery();
			}

		});
	}
	
	private void initFilter() {
		filter = ActivityUtil.Find(this, R.id.Spinner_Filter);
		billStateFilter = (Spinner) findViewById(R.id.Spinner_BillStateFilter);
		
		OnItemSelectedListener listener = new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				StartQuery();
				
			}

			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				
			}
		};
		
		filter.setOnItemSelectedListener(listener);
		billStateFilter.setOnItemSelectedListener(listener);
		
	}
	
	private void initListView() {
		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("ID", (Long) map.get("ID"));
				intent.setClass(arg1.getContext(), MobileAdjustGoodsBillEditActivity.class);
				startActivityForResult(intent,MobileAdjustGoodsBillEditActivity.RequestCode_Open);
			}

		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, Menu.FIRST, 0, "刷新");
		menu.add(0,2,1,"统计");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case Menu.FIRST:
			StartQuery();
			break;
		case 2:
			DialogUtil.Alert(this, mSumInfo.toString());
			break;
		}
		return super.onOptionsItemSelected(item);
	}

}
