package BWP.MobileSale.MobileAdjustGoodsBill;

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

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

import BWP.MobileSale.LoginedActivity;
import BWP.MobileSale.R;
import BWP.MobileSale.Config.LoginUserConfig;
import BWP.MobileSale.Util.CallableTask;
import BWP.MobileSale.Util.ConfigUtil;
import BWP.MobileSale.Util.DialogUtil;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class MobileAdjustGoodsBillSetUserProfileActivity extends LoginedActivity {

	
	Spinner spAccountUnit;
	private LoginUserConfig config = new LoginUserConfig(this);
	private RpcObject mDmo;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mobileadjustgoodsbill_setuserprofile);
		loadTask.execute();
	}
	
	CallableTask loadTask = new CallableTask(MobileAdjustGoodsBillSetUserProfileActivity.this, "读取中...") 
	{

		@Override
		public Object call() throws Exception {
			ConfigUtil.Fill(config);
			
			String method = "/Sale/Sale/Rpcs/MobileOrderRpc/LoadUserProfile";
			mDmo = RpcFacade.rpcCall(method, config.Username);
			if(mDmo==null)
			{
				mDmo=RpcObject.create("/Sale/Sale/BO/UserProfile");
			}
			return null;
		}

		@Override
		protected void successUI() {			
			try {
				initControls();				
			} catch (JsonRpcException e) {
				DialogUtil.Alert(MobileAdjustGoodsBillSetUserProfileActivity.this, e.getDetailMessage());
				e.printStackTrace();
			}
		}
		
	};
	
	private void initControls() throws JsonRpcException
	{
		initAccountUnit();
		initButton();
	}
	
	private void initButton()
	{
		Button btnReturn=(Button)findViewById(R.id.mobileadjustgoodsbill_SetUser_Ruturn);
		btnReturn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		Button btnSave=(Button)findViewById(R.id.mobileadjustgoodsbill_SetUser_Save);
		btnSave.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				saveTask.execute();
				
			}
		});
	}
	
	CallableTask saveTask = new CallableTask(MobileAdjustGoodsBillSetUserProfileActivity.this, "保存中...") 
	{
		@Override
		public Object call() throws Exception {
			
			//UpdateUserProfile
			mDmo.setString("MobileAdjustGoodsBill_AccountingUnit_Name", (String)spAccountUnit.getSelectedItem());
			String method = "/Sale/Sale/Rpcs/MobileOrderRpc/UpdateUserProfile";	
			
			RpcFacade.rpcCall(method,config.Username,mDmo);
			
			return null;
		}

		@Override
		protected void successUI() {
			DialogUtil.AutoHiddenAlert(MobileAdjustGoodsBillSetUserProfileActivity.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>(MobileAdjustGoodsBillSetUserProfileActivity.this, android.R.layout.simple_spinner_item, list);
		spAccountUnit.setAdapter(adapter);
		spAccountUnit.setSelection(list.indexOf(mDmo.getString("MobileAdjustGoodsBill_AccountingUnit_Name")));
	}
}
