package com.cwbuyer.main;

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 android.widget.Toast;

import com.cwbuyer.format.KeySet;
import com.cwbuyer.lib.GalleryViewO;
import com.cwbuyer.lib.QRep_01;
import com.cwbuyer.lib.QRep_06;
import com.cwbuyer.lib.QRep_07;
import com.cwbuyer.lib.QRep_12;
import com.cwbuyer.lib.QRep_03;
import com.cwbuyer.lib.QRep_14;
import com.cwbuyer.lib.Utilis;
import com.cwbuyer.lib.QRep_01.GroupMode;
import com.pwbuyer.main.R;

// 統計分析
public class AnslyManage  extends Activity {

    private final int RESULT_QCUSTFF = 9999;
	public static int PSWD=0; //0OK 1密碼不符
	private int nPart=0; //預設0總店  1分店
	private String[] mNames;
	Bundle bundle;	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.manage_layout);
		
		PSWD=Utilis.toInt(Utilis.getIni(this,"SYS","DEPT",5));
		mNames= new String[]{"新品冠軍排行","暢銷品排行","預購品排行","追加品排行","銷售分析","預購統計輸出","門市業績統計","預購留貨維護","總庫存分析","門市補貨配貨","櫃員業務業績統計","返回"};
		if (PSWD==1)mNames= new String[]{"預購統計輸出","預購留貨維護","銷售分析","門市補貨配貨","返回"}; 
		if (!Utilis.getIni(AnslyManage.this,"SYS","DEPT",1).equalsIgnoreCase(Utilis.getIni(AnslyManage.this,"SYS","DEPT",6))){
			nPart=1;  //1.門市版 0.總店
		}

		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(AnslyManage.this);
				Intent intent = new Intent();
				switch(pos){
				case 0: { //新品冠軍排行
					intent.setClass(AnslyManage.this, QRep_01.class);
					intent.putExtra("search_mode", KeySet.SearchMode.WEEK);		
					if (PSWD!=1){
						intent.putExtra("search_group", GroupMode.TOP1);
					}else{//沒PSWD 預購排行
						intent.putExtra("search_group", GroupMode.TOP5);	
					}
					startActivity(intent);					
				break;				
				}
				case 1: { //暢銷品排行
					if (PSWD!=1){
						intent.setClass(AnslyManage.this, QRep_01.class);
						intent.putExtra("search_mode", KeySet.SearchMode.WEEK);		
						intent.putExtra("search_group", GroupMode.TOP2);	
					}else{//沒PSWD 預購統計輸出
						intent.putExtra("GalleryUser","");
						intent.putExtra("GalleryGoods","");
						intent.putExtra("GalleryDept",Utilis.getIni(AnslyManage.this,"SYS","DEPT",1));
						intent.putExtra("GalleryASK","");
	    				intent.putExtra("FormNo","");
						if (nPart==0) intent.putExtra("GalleryQKIND","30"); else intent.putExtra("GalleryQKIND","31"); 
						intent.putExtra("GalleryMode",3);//0mUser 1mGoods 2KeepaToSale
						intent.setClass(AnslyManage.this, GalleryViewO.class);
					}
					startActivity(intent);
				break;				
				}
				case 2: { //預購品排行
					if (PSWD!=1){
						intent.setClass(AnslyManage.this, QRep_01.class);
						intent.putExtra("search_mode", KeySet.SearchMode.WEEK);		
						intent.putExtra("search_group", GroupMode.TOP3);
					}else{
						intent.setClass(AnslyManage.this, QRep_03.class);
						intent.putExtra("search_mode", KeySet.SearchMode.DAY);		
						intent.putExtra("search_group", GroupMode.TOP3);	
					}
					startActivity(intent);
				break;				
				}
				case 3: { //追加品排行
					if (PSWD!=1){
						intent.setClass(AnslyManage.this, QRep_01.class);
						intent.putExtra("search_mode", KeySet.SearchMode.WEEK);		
						intent.putExtra("search_group", GroupMode.TOP4);
					}else{//門市補貨配貨
						intent.putExtra("search_group",GroupMode.TOP2);// @@TR30為預設(暫時廠進貨單) 
						intent.setClass(AnslyManage.this,QRep_06.class);
						startActivity(intent);
					}
					startActivity(intent);
				break;				
				}
				case 4: { //銷售分析
					if (PSWD!=1){
						intent.setClass(AnslyManage.this, QRep_03.class);
						intent.putExtra("search_mode", KeySet.SearchMode.DAY);		
						intent.putExtra("search_group", GroupMode.TOP3);	
						startActivity(intent);
					}else finish(); 	
				break;
				}
				case 5: { //預購統計輸出
					intent.setClass(AnslyManage.this, QRep_01.class);
					intent.putExtra("search_mode", KeySet.SearchMode.WEEK);		
					intent.putExtra("search_group", GroupMode.TOP5);	
					startActivity(intent);					
				break;				
				}				
				case 6: { //門市業績分析
					intent.putExtra("QRep_02",30);// @@TR30為預設(暫時廠進貨單) 
					intent.putExtra("QRep_who","QPosff");// @@TR30為預設(暫時廠進貨單)
					intent.setClass(AnslyManage.this,QRep_12.class);
					startActivity(intent);					
				break;
				}				
				case 7: { //預購留貨維護
    				intent.putExtra("GalleryUser","");
    				intent.putExtra("GalleryGoods","");
					intent.putExtra("GalleryDept",Utilis.getIni(AnslyManage.this,"SYS","DEPT",1));
    				intent.putExtra("GalleryASK","");
					if (nPart==0) intent.putExtra("GalleryQKIND","30"); else intent.putExtra("GalleryQKIND","31"); 
    				intent.putExtra("GalleryMode",3);//0mUser 1mGoods 2KeepaToSale
					intent.setClass(AnslyManage.this, GalleryViewO.class);
					startActivity(intent);					
				break;				
				}				
				case 8: {//總庫存分析 
					intent.putExtra("search_group",GroupMode.TOP3);// @@TR30為預設(暫時廠進貨單) 
					intent.setClass(AnslyManage.this,QRep_07.class);
					startActivity(intent);
					break;
				}
				case 9: {//門市補貨配貨 
						intent.putExtra("search_group",GroupMode.TOP1);// @@TR30為預設(暫時廠進貨單) 
						intent.setClass(AnslyManage.this,QRep_06.class);
						startActivity(intent);
				break;
				}
				case 10: {//業務業績分析
					//intent.putExtra("QRep_02",30);// @@TR30為預設(暫時廠進貨單) 
					//intent.putExtra("QRep_who","QPosff");// @@TR30為預設(暫時廠進貨單)
					intent.setClass(AnslyManage.this,QRep_14.class);
					startActivity(intent);					
					break;
				}
				case 11: finish();
				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);
		String SS,SK;
		if(requestCode == RESULT_QCUSTFF){
			if (resultCode == RESULT_OK) {
//				String ECHOS = data.getDataString("acust_key");
				SS=data.getStringExtra("acust_key");
				SK=data.getStringExtra("acust_kind");
    			Toast.makeText(AnslyManage.this, "回傳訊息="+SS+"資料TR="+SK, Toast.LENGTH_SHORT).show();
			}
		}
	}
	
	// 顯示匯入新品資料的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(AnslyManage.this);
			int id = v.getId();
			switch (id) {
				case R.id.btn_exit:
				{
					finish();
					break;
				}
			}
		}
	}
}
