package BWP.MobileSale.Return;

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 ReturnSetUserProfileActivity extends LoginedActivity {

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

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

		@Override
		protected void successUI() {			
			try {
				initControls();				
			} catch (JsonRpcException e) {
				DialogUtil.Alert(ReturnSetUserProfileActivity.this, e.getDetailMessage());
				e.printStackTrace();
			}
		}
		
	};
	
	private void initControls() throws JsonRpcException
	{
		initDept();
		initStore();
		initReturnType();
		initAccountUnit();
		initHandleType();
		initButton();
	}
	private void initButton()
	{
		Button btnReturn=(Button)findViewById(R.id.Return_SetUser_Ruturn);
		btnReturn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		Button btnSave=(Button)findViewById(R.id.Return_SetUser_Save);
		btnSave.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				saveTask.execute();
				
			}
		});
	}
	
	CallableTask saveTask = new CallableTask(ReturnSetUserProfileActivity.this, "保存中...") 
	{

		@Override
		public Object call() throws Exception {
			
			//UpdateUserProfile
			mDmo.setString("ReturnsRecord_Dept_Name", (String)spDept.getSelectedItem());
			mDmo.setString("ReturnsRecord_AccountingUnit_Name", (String)spAccountUnit.getSelectedItem());
			mDmo.setString("ReturnsRecord_Store_Name", (String)spStore.getSelectedItem());
			mDmo.setString("ReturnsRecord_ReturnsType_Name", (String)spReturnType.getSelectedItem());
			
			
			String method = "/Sale/Returns/Rpcs/MobileReturnRpc/UpdateUserProfile";	
			
			RpcFacade.rpcCall(method,config.Username,mDmo,(String)spHandleType.getSelectedItem());
			
			return null;
		}

		@Override
		protected void successUI() {
			DialogUtil.AutoHiddenAlert(ReturnSetUserProfileActivity.this, "保存成功");
			finish();
		}
		
	};
	
	private void initDept() throws JsonRpcException
	{
		spDept=(Spinner)findViewById(R.id.return_setuser_setdept);		

		List<String> list=new  ArrayList<String>();
		list.add("");
		String method = "/Sale/Sale/Rpcs/MobileLoadRpc/LoadDept";		
		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>(ReturnSetUserProfileActivity.this, android.R.layout.simple_spinner_item, list);
		spDept.setAdapter(adapter);		
		
		spDept.setSelection(list.indexOf(mDmo.getString("ReturnsRecord_Dept_Name")));
		
	}
	private void initStore()throws JsonRpcException
	{
		spStore=(Spinner)findViewById(R.id.return_setuser_setstore);
		List<String> list=new  ArrayList<String>();
		list.add("");
		String method = "/Sale/Sale/Rpcs/MobileLoadRpc/LoadStore";		
		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>(ReturnSetUserProfileActivity.this, android.R.layout.simple_spinner_item, list);
		spStore.setAdapter(adapter);
		spStore.setSelection(list.indexOf(mDmo.getString("ReturnsRecord_Store_Name")));
	}
	private void initReturnType() throws JsonRpcException
	{
		spReturnType=(Spinner)findViewById(R.id.return_setuser_setreturntype);
		List<String> list=new  ArrayList<String>();
		list.add("");
		String method = "/Sale/Returns/Rpcs/MobileReturnRpc/LoadReturnsType";		
		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>(ReturnSetUserProfileActivity.this, android.R.layout.simple_spinner_item, list);
		spReturnType.setAdapter(adapter);
		spReturnType.setSelection(list.indexOf(mDmo.getString("ReturnsRecord_ReturnsType_Name")));
	}
	private void initAccountUnit() throws JsonRpcException
	{
		spAccountUnit=(Spinner)findViewById(R.id.return_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>(ReturnSetUserProfileActivity.this, android.R.layout.simple_spinner_item, list);
		spAccountUnit.setAdapter(adapter);
		spAccountUnit.setSelection(list.indexOf(mDmo.getString("ReturnsRecord_AccountingUnit_Name")));
	}
	private void initHandleType()
	{
		spHandleType=(Spinner)findViewById(R.id.return_setuser_sethandletype);
		
		List<String> list=new  ArrayList<String>();
		list.add("");
		list.add("退货拉回");
		list.add("丢弃或销毁");
		ArrayAdapter<String> adapter=new ArrayAdapter<String>(ReturnSetUserProfileActivity.this, android.R.layout.simple_spinner_item, list);
		spHandleType.setAdapter(adapter);
		if(mDmo.getNamedValue("ReturnsRecord_ReturnsManner")!=null)
		{
			if(mDmo.getNamedValue("ReturnsRecord_ReturnsManner").getValue()==0)
			{
				spHandleType.setSelection(1);
			}
			else if(mDmo.getNamedValue("ReturnsRecord_ReturnsManner").getValue()==1)
			{
				spHandleType.setSelection(2);
			}
		}
		
	}
}
