package BWP.MobileSale.ChinaPay;

import java.util.ArrayList;
import java.util.List;

import org.forks.jsonrpc.DFDataTable;
import org.forks.jsonrpc.JsonRpcException;
import org.forks.jsonrpc.NamedValue;
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.DialogUtil;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class SetPayProfileActivity extends LoginedActivity {

	Spinner spAccountUnit, spHeadOffice, spCardNo;
	TextView UsrName, CertId, OpenBankId, CardType, CertType;
	List<String> listCardNo = new ArrayList<String>();
	private RpcObject mDmo;

	ArrayAdapter<String> adapter1;
	//ArrayAdapter<String> cardNoAdapter;
	DFDataTable tableHeadOffice;
	DFDataTable tableOpenBankId;	
	DFDataTable tableCertType;
	DFDataTable tablePayInfo;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chinapay_setuserprofile);
		loadTask.execute();
	}

	CallableTask loadTask = new CallableTask(SetPayProfileActivity.this,
			"读取中...") {

		@Override
		public Object call() throws Exception {

			String method = "/Sale/SaleExportBase/ChinaPayRpc/LoadUserProfile";
			mDmo = RpcFacade.rpcCall(method);
			if (mDmo == null) {
				mDmo = RpcObject
						.create("/Sale/SaleExportBase/BO/ChinaPayProfile");
			}
			return null;
		}

		@Override
		protected void successUI() {
			try {
				initControls();
			} catch (JsonRpcException e) {
				DialogUtil.Alert(SetPayProfileActivity.this,
						e.getDetailMessage());
				e.printStackTrace();
			}
		}

	};

	private void initControls() throws JsonRpcException {
		initAccountUnit();
		initOpenBankId();
		initcardNo();
		initUsrName();
		initCertId();
		initCardType();
		initCertType();
		initButton();
		initHeadOffice();
		
		String method = "/Sale/SaleExportBase/ChinaPayRpc/GetCertType";
		tableCertType = RpcFacade.rpcCall(method);
		
		method = "/Sale/SaleExportBase/ChinaPayRpc/GetBankType";
		tableOpenBankId = RpcFacade.rpcCall(method); 
		
		method = "/Sale/SaleExportBase/ChinaPayRpc/GetPayInfo";
		tablePayInfo = RpcFacade.rpcCall(method, mDmo.getLong("HeadOffice_ID"));
		
		listCardNo.clear();
		List<RpcObject> rows1 = tablePayInfo.getRows();
		for (RpcObject b : rows1) {
			listCardNo.add(b.getString("CardNo"));
		}
		String card = mDmo.getString("CardNo");
		if (!listCardNo.contains(card)) {
			listCardNo.add(0, card);
		}
		ArrayAdapter<String> cardNoAdapter = new ArrayAdapter<String>(SetPayProfileActivity.this,
				android.R.layout.simple_spinner_item, listCardNo);
		spCardNo.setAdapter(cardNoAdapter);
		spCardNo.setSelection(listCardNo.indexOf(card));
		spCardNo.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				List<RpcObject> rows = tablePayInfo.getRows();
				Boolean h = false;
				String cardNo = arg0.getItemAtPosition(arg2).toString();   //cardNoAdapter.getItem(arg2);
				for (RpcObject b : rows) {
					if ( cardNo== b.getString("CardNo")) {
						UsrName.setText(b.getString("UsrName"));
						CertId.setText(b.getString("CertId"));

						NamedValue bankId = b.getNamedValue("OpenBankId");
						if (bankId != null) {
							OpenBankId.setText(bankId.getName());
						} else {
							OpenBankId.setText("");
						}
						NamedValue ctype = b.getNamedValue("CertType");
						if (ctype != null) {
							CertType.setText(ctype.getName());
						} else {
							CertType.setText("");
						}
						if (b.getNamedValue("CardType") != null) {
							CardType.setText(b.getNamedValue("CardType")
									.getName());
						} else {
							CardType.setText("");
						}
						mDmo.setNamedValue("OpenBankId", b.getNamedValue("OpenBankId")); 
						mDmo.setNamedValue("CertType", b.getNamedValue("CertType"));
						mDmo.setNamedValue("CardType", b.getNamedValue("CardType"));
						h = true;
						break;
					}
				}
				if (!h) {
					UsrName.setText("");
					CertId.setText("");
					OpenBankId.setText("");
					CertType.setText("");
					CardType.setText("");
				}
			}

			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
	}

	private void initCardType() throws JsonRpcException {
		CardType = ActivityUtil.Find(this, R.id.order_setuser_setCardType);
		if (mDmo.getNamedValue("CardType") != null) {
			CardType.setText(mDmo.getNamedValue("CardType").getName());
		}
	}

	private void initCertType() throws JsonRpcException {
		CertType = ActivityUtil.Find(this, R.id.order_setuser_setCerType);

		NamedValue ctype = mDmo.getNamedValue("CertType");
		if (ctype != null) {
			CertType.setText(ctype.getName());
		}
	}

	private void initButton() {
		Button btnReturn = (Button) findViewById(R.id.Order_SetUser_Ruturn);
		btnReturn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		Button btnSave = (Button) findViewById(R.id.Order_SetUser_Save);
		btnSave.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				saveTask.execute();

			}
		});
	}

	CallableTask saveTask = new CallableTask(SetPayProfileActivity.this,
			"保存中...") {
		@Override
		public Object call() throws Exception {
 
			mDmo.setString("AccountingUnit_Name",
					(String) spAccountUnit.getSelectedItem());
			mDmo.setString("HeadOffice_Name",
					(String) spHeadOffice.getSelectedItem());
 
			mDmo.setString("CardNo", (String) spCardNo.getSelectedItem());
			mDmo.setString("UsrName", UsrName.getText().toString());
			mDmo.setString("CertId", CertId.getText().toString());
			String method = "/Sale/SaleExportBase/ChinaPayRpc/UpdateUserProfile";

			RpcFacade.rpcCall(method, mDmo);

			return null;
		}

		@Override
		protected void successUI() {
			DialogUtil.AutoHiddenAlert(SetPayProfileActivity.this, "保存成功");
			finish();
		}

	};

	private void initAccountUnit() throws JsonRpcException {
		spAccountUnit = (Spinner) findViewById(R.id.order_setuser_setaccountunit);
		List<String> list = new ArrayList<String>();
		list.add("");
		String method = "/Sale/Sale/Rpcs/MobileLoadRpc/LoadAccountingUnit";
		DFDataTable table = RpcFacade.rpcCall(method);
		List<RpcObject> rows = table.getRows();
		for (RpcObject rpcObject : rows) {
			list.add(rpcObject.getString("Name"));
		}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(
				SetPayProfileActivity.this,
				android.R.layout.simple_spinner_item, list);
		spAccountUnit.setAdapter(adapter);
		spAccountUnit.setSelection(list.indexOf(mDmo
				.getString("AccountingUnit_Name")));

	}

	private void initHeadOffice() throws JsonRpcException {
		spHeadOffice = (Spinner) findViewById(R.id.order_setuser_sethheadoffice);
		List<String> list = new ArrayList<String>();
		list.add("");
		String method = "/Sale/SaleExportBase/ChinaPayRpc/LoadHeadOffice";
		tableHeadOffice = RpcFacade.rpcCall(method);
		List<RpcObject> rows = tableHeadOffice.getRows();
		for (RpcObject rpcObject : rows) {
			list.add(rpcObject.getString("Name"));
		}
		adapter1 = new ArrayAdapter<String>(SetPayProfileActivity.this,
				android.R.layout.simple_spinner_item, list);
		spHeadOffice.setAdapter(adapter1);
		spHeadOffice.setSelection(list.indexOf(mDmo
				.getString("HeadOffice_Name")));

		spHeadOffice
				.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
					public void onItemSelected(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						List<RpcObject> rows = tableHeadOffice.getRows();
						Long id = 0L;
						for (RpcObject b : rows) {
							if (adapter1.getItem(arg2) == b.getString("Name")) {
								id = b.getLong("ID");
								break;
							}
						}
						String method = "/Sale/SaleExportBase/ChinaPayRpc/GetPayInfo";
						tablePayInfo = RpcFacade.rpcCall(method, id);
						List<RpcObject> rows1 = tablePayInfo.getRows();
						listCardNo.clear();
						listCardNo.add("");
						for (RpcObject b : rows1) {
							listCardNo.add(b.getString("CardNo"));
						}
						ArrayAdapter<String> cardNoAdapter = new ArrayAdapter<String>(SetPayProfileActivity.this,
								android.R.layout.simple_spinner_item, listCardNo);
						spCardNo.setAdapter(cardNoAdapter);
						String card = mDmo.getString("CardNo");
						if (listCardNo.contains(card)) {
							spCardNo.setSelection(listCardNo.indexOf(card)); 
						} 
					}

					public void onNothingSelected(AdapterView<?> arg0) {
					}
				});

	}

	private void initOpenBankId() throws JsonRpcException {
		OpenBankId = ActivityUtil.Find(this, R.id.order_setuser_setBankID);

		NamedValue bankId = mDmo.getNamedValue("OpenBankId");
		if (bankId != null) {
			OpenBankId.setText(bankId.getName());
		}
	}

	private void initcardNo() throws JsonRpcException {
		spCardNo = (Spinner) findViewById(R.id.order_setuser_setCarNo); 
	}

	private void initUsrName() throws JsonRpcException {
		UsrName = ActivityUtil.Find(this, R.id.order_setuser_setPayUserName);
		UsrName.setText(mDmo.getString("UsrName"));
	}

	private void initCertId() throws JsonRpcException {
		CertId = ActivityUtil.Find(this, R.id.order_setuser_setCerID);
		CertId.setText(mDmo.getString("CertId"));
	}
}
