package BWP.MobileSale.Order;

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

	
	Spinner spAccountUnit,systemSign,takeGoods_Type,saleType,orderFarePayer,orderCarriageManner;
	private LoginUserConfig config = new LoginUserConfig(this);
	private RpcObject mDmo;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.order_setuserprofile);
		loadTask.execute();
	}
	
	CallableTask loadTask = new CallableTask(OrderSetUserProfileActivity.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");
			}
			//System.out.println(mDmo);
			return null;
		}

		@Override
		protected void successUI() {			
			try {
				initControls();				
			} catch (JsonRpcException e) {
				DialogUtil.Alert(OrderSetUserProfileActivity.this, e.getDetailMessage());
				e.printStackTrace();
			}
		}
		
	};
	
	private void initControls() throws JsonRpcException
	{
		initAccountUnit();
		initSystemSign();
		initTakeGoods_Type();
		initSaleType();
		initOrderFarePayer();
		initOrderCarriageManner();
		initButton();
	}
	
	private void initOrderFarePayer() throws JsonRpcException
	{
		orderFarePayer = (Spinner)findViewById(R.id.order_setuser_setOrderFarePayer);
		List<String> list=new  ArrayList<String>();
		list.add("");
		list.add("购方");
		list.add("供方");
		ArrayAdapter<String> adapter=new ArrayAdapter<String>(OrderSetUserProfileActivity.this, android.R.layout.simple_spinner_item, list);
		orderFarePayer.setAdapter(adapter);
		if(mDmo.getNamedValue("OrderFarePayer") != null){
			if(mDmo.getNamedValue("OrderFarePayer").getValue() == 0){
				orderFarePayer.setSelection(list.indexOf("购方")); 
			}
			if(mDmo.getNamedValue("OrderFarePayer").getValue() == 1){
				orderFarePayer.setSelection(list.indexOf("供方")); 
			}
		}
	}
	
	private void initOrderCarriageManner() throws JsonRpcException
	{
		orderCarriageManner = (Spinner)findViewById(R.id.order_setuser_setOrderCarriageManner);
		List<String> list=new  ArrayList<String>();
		list.add("");
		list.add("计重");
		list.add("包车");
		ArrayAdapter<String> adapter=new ArrayAdapter<String>(OrderSetUserProfileActivity.this, android.R.layout.simple_spinner_item, list);
		orderCarriageManner.setAdapter(adapter);
		if(mDmo.getNamedValue("OrderCarriageManner") != null){
			if(mDmo.getNamedValue("OrderCarriageManner").getValue() == 0){
				orderCarriageManner.setSelection(list.indexOf("计重")); 
			}
			if(mDmo.getNamedValue("OrderCarriageManner").getValue() == 1){
				orderCarriageManner.setSelection(list.indexOf("包车")); 
			}
		}
	}

	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(OrderSetUserProfileActivity.this, "保存中...") 
	{
		@Override
		public Object call() throws Exception {
			
			//UpdateUserProfile
			mDmo.setString("AccountingUnit_Name", (String)spAccountUnit.getSelectedItem());
			mDmo.setString("Order_SystemSign_Name", (String)systemSign.getSelectedItem());
			String takeGoods_TypeString = (String)takeGoods_Type.getSelectedItem();
			if(takeGoods_TypeString == "公司配送"){
				mDmo.setNamedValue("TakeGoods_Type", new Short("0"));	
			}else if(takeGoods_TypeString == "客户自运"){
				mDmo.setNamedValue("TakeGoods_Type", new Short("1"));	
			}else if(takeGoods_TypeString == "物流配送"){
				mDmo.setNamedValue("TakeGoods_Type", new Short("2"));	
			}
			
			//生成销售订单时用到【运费承担方】
			String orderFarePayerString = (String)orderFarePayer.getSelectedItem();
			if(orderFarePayerString == "购方"){
				mDmo.setNamedValue("OrderFarePayer", new Short("0"));	
			}else if(takeGoods_TypeString == "供方"){
				mDmo.setNamedValue("OrderFarePayer", new Short("1"));	
			}
			
			//生成销售订单时用到【运费结算方式】
			String orderCarriageMannerString = (String)orderCarriageManner.getSelectedItem();
			if(orderCarriageMannerString == "计重"){
				mDmo.setNamedValue("OrderCarriageManner", new Short("0"));	
			}else if(orderCarriageMannerString == "包车"){
				mDmo.setNamedValue("OrderCarriageManner", new Short("1"));	
			}
			
			mDmo.setString("OrderSaleType_Name", (String)saleType.getSelectedItem());
			String method = "/Sale/Sale/Rpcs/MobileOrderRpc/UpdateUserProfile";	
			
			RpcFacade.rpcCall(method,config.Username,mDmo);
			
			return null;
		}

		@Override
		protected void successUI() {
			DialogUtil.AutoHiddenAlert(OrderSetUserProfileActivity.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>(OrderSetUserProfileActivity.this, android.R.layout.simple_spinner_item, list);
		spAccountUnit.setAdapter(adapter);
		spAccountUnit.setSelection(list.indexOf(mDmo.getString("AccountingUnit_Name")));
	}
	
	private void initSystemSign() throws JsonRpcException
	{
		systemSign = (Spinner)findViewById(R.id.order_setuser_setSystemSign);
		List<String> list=new  ArrayList<String>();
		list.add("");
		String method = "/Sale/Sale/Rpcs/MobileLoadRpc/LoadSystemSign";		
		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>(OrderSetUserProfileActivity.this, android.R.layout.simple_spinner_item, list);
		systemSign.setAdapter(adapter);
		systemSign.setSelection(list.indexOf(mDmo.getString("Order_SystemSign_Name")));
	}
	
	private void initTakeGoods_Type() throws JsonRpcException
	{
		takeGoods_Type = (Spinner)findViewById(R.id.order_setuser_setTakeGoods_Type);
		List<String> list=new  ArrayList<String>();
		list.add("");
		list.add("公司配送");
		list.add("客户自运");
		list.add("物流配送");
		ArrayAdapter<String> adapter=new ArrayAdapter<String>(OrderSetUserProfileActivity.this, android.R.layout.simple_spinner_item, list);
		takeGoods_Type.setAdapter(adapter);
		if(mDmo.getNamedValue("TakeGoods_Type") != null){
			if(mDmo.getNamedValue("TakeGoods_Type").getValue() == 0){
				takeGoods_Type.setSelection(list.indexOf("公司配送")); 
			}
			if(mDmo.getNamedValue("TakeGoods_Type").getValue() == 1){
				takeGoods_Type.setSelection(list.indexOf("客户自运")); 
			}
			if(mDmo.getNamedValue("TakeGoods_Type").getValue() == 2){
				takeGoods_Type.setSelection(list.indexOf("物流配送")); 
			}
		}
	}
	
	private void initSaleType() throws JsonRpcException
	{
		saleType = (Spinner)findViewById(R.id.order_setuser_setSaleType);
		List<String> list=new  ArrayList<String>();
		list.add("");
		String method = "/Sale/Sale/Rpcs/MobileLoadRpc/LoadSaleType";		
		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>(OrderSetUserProfileActivity.this, android.R.layout.simple_spinner_item, list);
		saleType.setAdapter(adapter);
		saleType.setSelection(list.indexOf(mDmo.getString("OrderSaleType_Name")));
	}
	
}
