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.EMode;
import com.cwbuyer.lib.Utilis;
import com.cwbuyer.main.APos.PItemKind;
import com.pwbuyer.main.R;

// 資料管理
@SuppressLint("InflateParams")
public class DataManage  extends Activity {

	private int nPart=0; //預設0總店  1分店
	private String[] mItem;
	private final String[] deptStr =Utilis.getIni(this,"SYS","DEPT",9).split("!&!");
	private static int PSWD=0;
	
	Bundle bundle;	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.manage_layout);
		
		PSWD=Utilis.toInt(deptStr[4]);

		mItem=new String[]{"會員通訊錄","櫃員通訊錄","門市通訊錄","廠商通訊錄"};		
		if (!deptStr[0].equalsIgnoreCase(deptStr[5]) ){
			nPart=1;  //1.門市版 0.總店
			if (PSWD==1)
				mItem=new String[]{"會員通訊錄","櫃員通訊錄","門市通訊錄"};
		}
		
		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(DataManage.this);
				Intent intent = new Intent();
				switch(pos){
				case 0: { //會員通訊錄
					if (nPart==1) intent.putExtra("qcustf_qkind", PItemKind.TR31); else intent.putExtra("qcustf_qkind", PItemKind.TR30);
					intent.putExtra("qcustf_mode", 0); //普
					intent.putExtra("qcustf_key", "888");					
					intent.putExtra("pos_dept",Utilis.getIni(DataManage.this,"SYS","DEPT",1));//理論上為"SOS" 或 門市代號
					intent.setClass(DataManage.this, QCustff.class);
					startActivity(intent);
	            	
				break;				
				}
				
				case 1: { //櫃員通訊錄 @在總倉或門市 一致
					intent.setClass(DataManage.this, QCustff.class);
					intent.putExtra("qcustf_mode", 0); //普
					intent.putExtra("qcustf_key", "888");					
					intent.putExtra("qcustf_qkind", PItemKind.TR40);
					intent.putExtra("pos_dept",Utilis.getIni(DataManage.this,"SYS","DEPT",6));//理論上為"SOS" 或 門市代號
					startActivity(intent);
					
					break;					
				}
				case 2: { //門市通訊錄
					
					intent.putExtra("qcustf_qkind", PItemKind.TR20);
					intent.putExtra("qcustf_mode", 0); //普
					//intent.putExtra("qcustf_key", ""); 2019-04-07 改為AA
					//使資料管理進入 門市通訊錄時,可以顯示 總倉的清單 可以供編改
					intent.putExtra("qcustf_key", "AA");
					intent.putExtra("pos_dept",Utilis.getIni(DataManage.this,"SYS","DEPT",6));//理論上為"SOS" 或 門市代號
					intent.setClass(DataManage.this, QCustff.class);
					startActivity(intent);
				break;				
				}
				case 3: { 
					if (nPart==0 || PSWD!=1){ //廠商通訊錄
						intent.putExtra("mode", EMode.EDIT);
						intent.putExtra("countryid", 0);
						intent.setClass(DataManage.this, Qfactff.class);
						startActivity(intent);
						
//						intent.putExtra("qcustf_qkind", PItemKind.TR10);
//						intent.putExtra("qcustf_mode", 0); //普
//						intent.putExtra("qcustf_key", "888");					
//						intent.setClass(DataManage.this, QCustff.class);
//						startActivity(intent);
					}
				break;				
				}
				default:
				break;				
				} //end_switch
			}
		});
		
		((Button)findViewById(R.id.btn_exit)).setOnClickListener(new MainClick());
	}
	
	class DataAdapter extends BaseAdapter{
		
		private LayoutInflater mInflater;
		
		public DataAdapter(Context context) {
		      /* 參數初始化 */
		      mInflater = LayoutInflater.from(context);
		}
		
		public int getCount() {
			return mItem.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(mItem[position]);
			return convertView;
		}

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