package com.cwbuyer.main;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.cwbuyer.format.KeySet;
import com.cwbuyer.lib.AppUtil;
import com.cwbuyer.lib.QpandnF;
import com.cwbuyer.lib.Utilis;
import com.cwbuyer.main.APos.PItemKind;
import com.cwbuyer.main.QCustff.QCustfMode;
import com.pwbuyer.main.R;

// 庫存管理
@SuppressLint("InflateParams")
public class StockManage  extends Activity {

    private final int RESULT_QCUSTFF_QPOS21 = 7777;
    private final int RESULT_QCUSTFF_QPOS30 = 8888;
    private final int RESULT_QCUSTFF_QPOS40 = 9999;
	private int nPart=0; //預設0總店  1分店
	private String[] mNames;
   	String btns=null;
   	String[] bttns;
	int isPass=0;
	int PSWD=Utilis.toInt(Utilis.getIni(this,"SYS","DEPT",5));

   	
	private String mDept="SOS";	
	Bundle bundle;	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.manage_layout);

		isPass=AppUtil.isRegSucceed(this,1);
		if (!Utilis.getIni(StockManage.this,"SYS","DEPT",1).equalsIgnoreCase(Utilis.getIni(StockManage.this,"SYS","DEPT",6))){
			nPart=1;  //1.門市版 0.總店
			btns=Utilis.getIni(this,"SYS","MAILFROM",9);
    		if (isPass<6 && isPass!=3 && isPass!=4){
    			mNames= Utilis.getIni(this,"SYS","MAILFROM",9).split("!&!");
    			mNames[4]="採購單(新品&追加)";mNames[5]="採購參數設定";mNames[6]="代碼設定";mNames[7]="廠商進貨";
    		}
		}else {
			 //mNames= new String[]{"採購參數設定","採購新品","廠商進貨","門市出貨","門市銷貨","員工自購","代碼設定","門市轉貨單","盤點系統"};
			mNames= Utilis.getIni(this,"SYS","STOCK",9).split("!&!");
		}

		DataAdapter adapter = new DataAdapter(this);
		ListView listview = (ListView)findViewById(R.id.listview);
		listview.setAdapter(adapter);
		listview.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> adapter, View view, int pos,
					long arg3) {
				Utilis.runVibrate(StockManage.this);
				Intent intent = new Intent();
				switch(pos){
				case 0: { //採購參數設定  //門市轉貨單
					if (nPart==0) {
						intent.setClass(StockManage.this, AParam.class);
						
					} else { //門市轉貨單
						intent.putExtra("search_mode",KeySet.SearchMode.ACTION);
						intent.putExtra("search_dept",mDept);
						intent.putExtra("QPosff",PItemKind.TR21);//門市轉貨單
						intent.setClass(StockManage.this, QPosff.class);
					}
					
					startActivity(intent);					
				break;				
				}
				
				case 1: { 
					
					if (nPart==0) {  //採購新品
				        	intent.setClass(StockManage.this, ManageItem.class);
							startActivity(intent);
					} else { //總倉出貨
						intent.putExtra("search_mode",KeySet.SearchMode.ACTION);
						intent.putExtra("search_dept",Utilis.getIni(StockManage.this,"SYS","DEPT",6));
						intent.putExtra("QPosff",PItemKind.TR20);
						intent.setClass(StockManage.this, QPosff.class);
						startActivity(intent);
					}
					break;					
				}
				case 2: { //廠商進貨  //
						intent.putExtra("search_mode",KeySet.SearchMode.ACTION);
						if (nPart==0) {
								intent.putExtra("search_dept",Utilis.getIni(StockManage.this,"SYS","DEPT",6));
					        	intent.putExtra("QPosff",PItemKind.TR10);//廠商進貨單
								intent.setClass(StockManage.this, QPosff.class);
								startActivity(intent);
						}else {
							//櫃員銷貨
							intent.putExtra("search_dept",mDept);
							intent.putExtra("QPosff",PItemKind.TR41);
							intent.setClass(StockManage.this, QPosff.class);
							startActivity(intent);
						}
				break;				
				}
				case 3: { 
					if (nPart==0) {  //門市出貨	
						intent.putExtra("search_mode",KeySet.SearchMode.ACTION);
						intent.putExtra("search_dept",mDept);
						intent.putExtra("QPosff",PItemKind.TR20);
						intent.setClass(StockManage.this, QPosff.class);
						startActivity(intent);
					}else { //門市盤點
						intent.setClass(StockManage.this, QpandnF.class);						
						startActivity(intent);
					}
				break;				
				}
				case 4: { //門市銷貨
					if (nPart==0) {
						intent.putExtra("qcustf_qkind", 21);
						intent.putExtra("qcustf_mode", QCustfMode.NORMAL); 
						intent.putExtra("qcustf_key", "");
						intent.putExtra("pos_dept", mDept);
						intent.setClass(StockManage.this, QCustff.class);
						startActivityForResult(intent, RESULT_QCUSTFF_QPOS30);
					} else {  //門市採購單
							intent.setClass(StockManage.this, ManageItem.class);
							startActivity(intent);
					}

				break;				
				}
				case 5: { //櫃員銷貨
					if (nPart==0){
						intent.putExtra("qcustf_qkind", PItemKind.TR20);
						intent.putExtra("qcustf_mode", QCustfMode.NORMAL); //普
						intent.putExtra("qcustf_key", "AA");
						intent.putExtra("pos_dept",Utilis.getIni(StockManage.this,"SYS","DEPT",6));//理論上為"SOS" 或 門市代號
						intent.setClass(StockManage.this, QCustff.class);
						startActivityForResult(intent, RESULT_QCUSTFF_QPOS40);
					}else{
						if (PSWD!=1) {
							intent.setClass(StockManage.this, AParam.class);
							startActivity(intent);
						}
					}
					
				break;				
				}
				case 6:{  
						intent.setClass(StockManage.this, ACode.class);
						startActivity(intent);
				break;				
				}
				
				case 7:{//
					if (nPart==0) {
						intent.putExtra("qcustf_qkind", 21);
						intent.putExtra("qcustf_mode", QCustfMode.NORMAL); 
						intent.putExtra("qcustf_key", "");
						intent.putExtra("pos_dept", mDept);
						intent.setClass(StockManage.this, QCustff.class);
						startActivityForResult(intent, RESULT_QCUSTFF_QPOS21);
					} else {  //(門市版)廠商進貨
						intent.putExtra("search_mode",KeySet.SearchMode.ACTION);
						intent.putExtra("search_dept",Utilis.getIni(StockManage.this,"SYS","DEPT",6));
						intent.putExtra("QPosff",PItemKind.TR10);//廠商進貨單
						intent.setClass(StockManage.this, QPosff.class);
						startActivity(intent);
					}
				break;	
				}
				
				case 8:
				{
					intent.setClass(StockManage.this, QpandnF.class);						
					startActivity(intent);
				break;	
				}
				default:
					break;				
				} //end_switch
			}
		});
		
		((Button)findViewById(R.id.btn_exit)).setOnClickListener(new MainClick());
	}

//範例:帶回QCUSTFF中找到的客戶資料(CUSTNO,TR)
//需搭配臨時的[密碼設定]及本段的回應OK時,取得	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == RESULT_QCUSTFF_QPOS30){
			if (resultCode == RESULT_OK) {
	        	String Deptno=data.getStringExtra("qcust_key");
            	Intent intent = new Intent();
            	intent.putExtra("search_mode",KeySet.SearchMode.ACTION);					            	
				intent.putExtra("search_dept",Deptno);
				intent.putExtra("QPosff",PItemKind.TR31);
				intent.setClass(StockManage.this, QPosff.class);
				startActivity(intent);
			}
		}else if(requestCode == RESULT_QCUSTFF_QPOS40){
			if (resultCode == RESULT_OK) {
	        	Intent intent = new Intent();
	        	intent.putExtra("search_mode",KeySet.SearchMode.ACTION);
	        	String Deptno=data.getStringExtra("qcust_key");
				
				if (!Deptno.equalsIgnoreCase(Utilis.getIni(StockManage.this,"SYS","DEPT",6))){
					intent.putExtra("search_dept",Deptno);
					intent.putExtra("QPosff",PItemKind.TR41);
				}else{
					intent.putExtra("search_dept","SOS");
					intent.putExtra("QPosff",PItemKind.TR40);
				}
				intent.setClass(StockManage.this, QPosff.class);
				startActivity(intent);
			}
		}else if(requestCode == RESULT_QCUSTFF_QPOS21){
			if (resultCode == RESULT_OK) {
	        	Intent intent = new Intent();
	        	intent.putExtra("search_mode",KeySet.SearchMode.ACTION);
	        	String Deptno=data.getStringExtra("qcust_key");
				intent.putExtra("search_dept",Deptno);
				intent.putExtra("QPosff",PItemKind.TR21);
				intent.setClass(StockManage.this, QPosff.class);
				startActivity(intent);
			}
		}
	}
	
	// 顯示匯入新品資料的Dialog//path 就是實際的檔案全名,只等開啟
	
	
	class DataAdapter extends BaseAdapter{
		
		private LayoutInflater mInflater;
		
		public DataAdapter(Context context) {
		      /* 參數初始化 */
		      mInflater = LayoutInflater.from(context);
		}
		
		public int getCount() {
			return mNames.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.list_param, null);
				holder = new ViewHolder();
				holder.textName = (TextView) convertView
						.findViewById(R.id.text_title);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.textName.setText(mNames[position]);
			return convertView;
		}

		/* class ViewHolder */
		private class ViewHolder {
			TextView textName;
		}
	}
	
	class MainClick implements OnClickListener {
		@Override
		public void onClick(View v) {
			Utilis.runVibrate(StockManage.this);
			int id = v.getId();
			switch (id) {
				case R.id.btn_exit:
				{
					finish();
					break;
				}
			}
		}
	}
}
