package BWP.MobileSale.SaleOutStore;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

import org.forks.jsonrpc.ManyList;
import org.forks.jsonrpc.RpcFacade;
import org.forks.jsonrpc.RpcObject;

import BWP.MobileSale.LoginedActivity;
import BWP.MobileSale.R;
import BWP.MobileSale.SelectAccountingUnitActivity;
import BWP.MobileSale.SelectBuyerActivity;
import BWP.MobileSale.SelectGoodsActivity;
import BWP.MobileSale.SelectRemarkActivity;
import BWP.MobileSale.SelectStoreActivity;
import BWP.MobileSale.Order.OrderDetailEditActivity;
import BWP.MobileSale.Order.OrderEditActivity;
import BWP.MobileSale.Util.ActivityUtil;
import BWP.MobileSale.Util.CallableTask;
import BWP.MobileSale.Util.DateTimeUtil;
import BWP.MobileSale.Util.DialogUtil;
import BWP.widget.SelectSimpleAdapter;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class SaleOutStoreEditActivity extends LoginedActivity {

	public static RpcObject EditingDetail;

	static final int RequestCode_SelectBuyer = 1;
	static final int RequestCode_SelectGoods = 2;
	static final int RequestCode_Open = 3; // 打开单据
	static final int RequestCode_EditDetail = 4;
	static final int RequestCode_SelectRemark = 5;
	static final int RequestCode_SelectAccountingUnit = 6;
	static final int RequestCode_SelectStore = 7;
 
	private TextView textView_BuyerName;
	private TextView textView_BuyerCode;
  
	private long mBillID;
	private RpcObject mDmo;
	private int selectedPosition = -1;
 
	boolean mEnableCheck = false;
	boolean mEnableDelete = false;
	private TextView button_Date;
	private TextView textView_WrappageAllNum;
	private ListView listView;

	private TextView textViewDetailCount;
	String detailSearchKey;
	private int firstVisiblePosition;
	private Button btnReturn;
 
	//private boolean isOrNotShowSecond;
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.saleoutstore_edit);
		Intent intent = getIntent();
		mBillID = intent.getLongExtra("ID", 0);
		btnReturn = (Button) this.findViewById(R.id.Order_Button_Return);

		btnReturn.setOnClickListener(new OnClickListener() {

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

	CallableTask initBillTask = new CallableTask(this, "读取单据中...") {
		public Object call() throws Exception {
			if (mBillID > 0) {
				String method = "/Sale/Sale/Rpcs/SaleOutStoreRpc/Load";
				mDmo = RpcFacade.rpcCall(method, mBillID);

			} else {
				mDmo = RpcObject.create("/Sale/Sale/BO/SaleOutStore");
				mDmo.setDate("ArriveTime", new Date());
			}
			return null;
		}

		@Override
		protected void successUI() {

			initControls();
			applyToUI();
		}

	};

	private void initControls() {

		InitListView();
		InitDate();
 
		InitBuyer();
 
		InitWrappageAllNum();
	}

	private void InitDate() {
		button_Date = ActivityUtil.Find(this, R.id.Order_Button_Date);
 
	}

	private void InitBuyer() {
		textView_BuyerName = ActivityUtil.Find(this,
				R.id.Order_TextView_BuyerName);
		textView_BuyerCode = ActivityUtil.Find(this,
				R.id.Order_TextView_BuyerCode);

	}

	private void InitWrappageAllNum() {
 
		textView_WrappageAllNum = ActivityUtil.Find(this,
				R.id.Order_TextView_WrappageAllNum);
		 
	}
 
	private void InitListView() {
		textViewDetailCount = ActivityUtil.Find(this,
				R.id.Order_TextView_DetailCount);
		listView = ActivityUtil.Find(this, R.id.Order_ListView);
   

		listView.setOnScrollListener(new OnScrollListener() {

			public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub

			}

			public void onScrollStateChanged(AbsListView arg0, int arg1) {
				if (arg1 == OnScrollListener.SCROLL_STATE_IDLE) {
					firstVisiblePosition = listView.getFirstVisiblePosition();
				}

			}

		});

 
	}

	private void applyToUI() {
		if (mDmo == null) {
			DialogUtil.Alert(SaleOutStoreEditActivity.this, "当前可编辑的对象为空");
			finish();
			return;
		}

		if (mBillID > 0) {
			this.setTitle(String.format("%s-出库单No.%s(%s)",
					getString(R.string.app_name), mBillID,
					mDmo.getNamedValue("BillState")));
		}

		textView_BuyerName.setText(mDmo.getString("Buyer_Name"));
		textView_BuyerCode.setText(mDmo.getString("Buyer_Code"));
		if (mDmo.getMoney("WrappageAllNum") != null) {
			textView_WrappageAllNum.setText(mDmo.getMoney("WrappageAllNum" ).toString() );
		}
		button_Date.setText(DateTimeUtil.FormatDate(mDmo.getDate("ArriveTime")));
		ManyList details = mDmo.getManyList("Details");

		BigDecimal allMoney = new BigDecimal(0);
		BigDecimal allNum = new BigDecimal(0);		
		for (int i = 0; i < details.size(); i++) {
			RpcObject item = details.get(i);
			BigDecimal price = item.getMoney("Price");
			if (price == null) {
				price = new BigDecimal(0);
			}
			BigDecimal number = item.getMoney("UnitNum");
			if (number == null) {
				number = new BigDecimal(0);
			}
			allNum = allNum.add(number);
			allMoney = allMoney.add(price.multiply(number));
		}

		textViewDetailCount.setText(String.format("%s￥ %.2f,%.2f", details.size(),
				allMoney,allNum));

		SelectSimpleAdapter adapter = new SelectSimpleAdapter(
				SaleOutStoreEditActivity.this, details,
				R.layout.saleoutstore_edit_detail_list_item, new String[] {
						"Goods_Name", "UnitNum",  "Price",
						"Money" }, new int[] { R.id.Goods_Name,
						R.id.Number,  R.id.Price,
						R.id.Money });
		adapter.setSelect(selectedPosition);
		listView.setAdapter(adapter);
		if (firstVisiblePosition == -1) {
			firstVisiblePosition = details.size() - 1;
		} else if (firstVisiblePosition > details.size() - 1) {
			firstVisiblePosition = details.size() - 1;
		}
		listView.setSelection(firstVisiblePosition);

	}
}
