package com.cwbuyer.lib;

import java.util.HashMap;
import java.util.LinkedList;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.TextView;

import com.pwbuyer.main.R;

//銷售王的核心2層式ADAPTER,複製自 MANAGEITEM採購王
@SuppressLint({ "UseSparseArrays", "InflateParams" })
@SuppressWarnings("unused")
public class QRep_Stand extends Activity{
	
	
	//private LinkedList<RepData> mList = new LinkedList<RepData>();
	ReportAdapter mAdapter = null;
	ListView mListView = null;

	
	TextView mTextDuration = null;

	
	String mWho ="";
	String mWhoName ="";
	
	
	
	/** State :  true 正式 , false 樣品*/
	String mEndDay = null;
	String mStartDay = null;
	int Pswd=1;
	int nPart=0;
	int CountX=0;//還要製作 統計判斷 //03040 1.只要40 2.只要30
	int deptN=1;

	//發展為長按而啟動的功能
	String mKind=null; //單別 102030405060
	
	int nKind=0;
	
   	String btns=null;
   	String[] bttns;

	public class RepData{ 
		public String iLla; 
		public String iLlu; 
		public String iLld; 
		public String iRla; 
		public String iRlu; 
		public String iRld; 
		
	}

//	ListView mListView = null;
	ExpandableListView mExpandListView = null;
	
//For  Table
	int Wheight;
	int Wwidth;
	int tableHeight=30;
	int tabless=6;

	HashMap<Integer,Integer> mCountRow = new HashMap<Integer,Integer>(); // 顏色行資料
	HashMap<Integer,String> mCountCell = new HashMap<Integer,String>();  // 尺寸欄資料
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.qrepclock);
        
        //@@應增加營業時間的判斷而視為當日或前一天
        mStartDay =DateUtil.getCurrentDate();  //2012/10/15 將 batch 視為當日的關鍵"/"
        mEndDay=mStartDay;
        
        mWho = getIntent().getStringExtra("WHO");  //傳過來的
        mWhoName = getIntent().getStringExtra("WHONAME");  //傳過來的
        
        //本單元就 上月 下月 2個 紐
        //和 列印 離開
        ((Button)findViewById(R.id.btn_prev_date)).setOnClickListener(new MainClick());
        ((Button)findViewById(R.id.btn_next_date)).setOnClickListener(new MainClick());
        ((TextView)findViewById(R.id.text_duration)).setText(mWho+"/"+mWhoName);
        ((TextView)findViewById(R.id.text_momday)).setText(mStartDay.substring(0, 7));

        mListView = (ListView)findViewById(R.id.list_clockitem);
        mAdapter = new ReportAdapter(this);
        mListView.setAdapter(mAdapter);
       
        chkMode();
	}        
	
	public void onResume(){
		super.onResume();

		 chkMode();
		//表示非外來程式
/*		 
		getGroupData(QRep_05.this, mStartDay, mEndDay, null, null);
		
		int count =  mAdapter.getCount(); 
		for (int i = 0; i <count ; i++) 
		mAdapter.notifyDataSetChanged();
*/		
		}
	
	// 特別處理 日結單 GROUPS第一層 是以(日/週/月)為統計結算 childs第二層 才是單頭
	//如何分開 日結+單頭  及 單頭+單身 的製作就是關鍵
	public void getGroupData(Context context, String startDay, String endDay, String condition, String conditionValue) {
		
		if (mAdapter.mList != null) {
			mAdapter.mList.clear();
		}
		if(startDay != null && startDay.length() > 0)	startDay = startDay.replace("/", "-");
		if(endDay != null && endDay.length() > 0) endDay = endDay.replace("/", "-");
		
			SQLiteDatabase db = Utilis.getDB(context);
			try {
				Cursor cursorGroup = null;

				StringBuffer bufCategory = new StringBuffer();
				if(startDay != null && startDay.length() > 0)	startDay = startDay.replace("/", "-");
				if(endDay != null && endDay.length() > 0) endDay = endDay.replace("/", "-");

				bufCategory=bufCategory.delete(0, bufCategory.toString().length());
				bufCategory.append("select * from qemp where QEMPID='"+mWho+"' and "); //總件數,總實應收,總成本
				
				
				
				
 				cursorGroup = db.rawQuery(bufCategory.toString(), null);
				if (cursorGroup != null) {
					if (cursorGroup.getCount() > 0) {
						cursorGroup.moveToFirst();
						for (int i = 0; i < cursorGroup.getCount(); i++) {
							RepData data = new RepData();							
							cursorGroup.moveToNext();

						}	
 					}	
 				}
				cursorGroup.close();
			}	
		          catch (Exception e)
		          {
		            e.printStackTrace();
		          }
		          finally{}
		}
	
	
	class MainClick implements OnClickListener { //所有的觸發事件,在此集中管理
		@Override
		public void onClick(View v) {
			int id = v.getId();
			Utilis.runVibrate(QRep_Stand.this);
			switch (id){
			//上月
			
			//下月
			
			}
		}
	}
	
    
    private void chkMode(){        
		String day = Utilis.getMonthDate();
		String[] days = day.split("~");
		mStartDay = days[0];
		mEndDay = days[1];
        ((TextView)findViewById(R.id.text_momday)).setText(mStartDay.substring(0, 7));
	    		
    	getGroupData(QRep_Stand.this, mStartDay, mEndDay, null, null);
		    	
		int count =  mAdapter.getCount(); 
		for (int i = 0; i <count ; i++) 
        mAdapter.notifyDataSetChanged();        
    }

private class ReportAdapter extends BaseAdapter{
		
		private LayoutInflater mInflater;
		
		public LinkedList<RepData> mList = new LinkedList<RepData>();
		
		public ReportAdapter (Context context ){
		      mInflater = LayoutInflater.from(context);
		}

		public RepData getData(int Position){
			return mList.get(Position);
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mList.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}
		
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.list_clock_rep1, null);
				holder = new ViewHolder();
				holder.text_lla = (TextView) convertView.findViewById(R.id.text_lla);
				holder.text_llu = (TextView) convertView.findViewById(R.id.text_llu);
				holder.text_lld = (TextView) convertView.findViewById(R.id.text_lld);
				holder.text_rla = (TextView) convertView.findViewById(R.id.text_rla);
				holder.text_rlu = (TextView) convertView.findViewById(R.id.text_rlu);
				holder.text_rld = (TextView) convertView.findViewById(R.id.text_rld);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			java.text.DecimalFormat myformat=new java.text.DecimalFormat("0.00");
			RepData data = mList.get(position);
			
			holder.text_lla.setText(data.iLla);
			holder.text_llu.setText(data.iLld);
			holder.text_lld.setText(data.iLlu);
			holder.text_rla.setText(data.iRla);
			holder.text_rlu.setText(data.iRld);
			holder.text_rld.setText(data.iRlu);
			return convertView;
		}

		/* class ViewHolder */
		private class ViewHolder {
			TextView text_lla;
			TextView text_llu;
			TextView text_lld;
			TextView text_rla;
			TextView text_rlu;
			TextView text_rld;
			
		}
	}//end of Report_adapter


}
