package com.cwbuyer.code;

import java.util.LinkedList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
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.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cwbuyer.adapter.CategoryAdapter;
import com.cwbuyer.adapter.CountryAdapter;
import com.cwbuyer.adapter.ElementAdapter;
import com.cwbuyer.adapter.MaccAdapter;
import com.cwbuyer.adapter.PpriAdapter;
import com.cwbuyer.adapter.PricingAdapter;
import com.cwbuyer.adapter.PricingAdapter.PriceData;
import com.cwbuyer.adapter.SizeAdapter;
import com.cwbuyer.adapter.WashAdapter;
import com.cwbuyer.format.IDialog;
import com.cwbuyer.lib.DBCloud;
import com.cwbuyer.lib.DBUtil;
import com.cwbuyer.lib.DialogUtilis;
import com.cwbuyer.lib.Utilis;
import com.pwbuyer.main.R;

//代碼設定 內頁
public class CodeList extends Activity{
	
	private int mMode = 0;
	private int mEditMode = EditMode.EDIT;
	
	CountryAdapter mCountryAdapter = null;
	CategoryAdapter mCategoryAdapter = null;
	ColorAdapter mColorAdapter = null;
	SizeAdapter mSizeAdapter = null;
	PricingAdapter mPricingAdapter = null;
	ElementAdapter mElementAdapter = null;
	WashAdapter mWashAdapter = null;
	MaccAdapter mMaccAdapter = null;
	PpriAdapter mPpriAdapter = null;
	
	interface EditMode {
		int EDIT = 0;
		int ITEM = 1;
	}
	int PSWD=Utilis.toInt(Utilis.getIni(this,"SYS","DEPT",5));

	int nPart=0;
	//0單機 1On_line 2.Off_line
	int of_line=Utilis.toInt(Utilis.getIni(CodeList.this, "SYS", "IMPORT", 3));

	String mUid=Utilis.getIni(CodeList.this, "SYS", "IMPORT", 2);  //ex:3g001
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.codelist_layout);
		
		mMode = getIntent().getIntExtra("code_mode", CodeMode.COUNTRY);
		mEditMode = getIntent().getIntExtra("edit_mode", EditMode.EDIT);
		
		ListView listview = (ListView)findViewById(R.id.list_param);
		
		if (!Utilis.getIni(this,"SYS","DEPT",1).equalsIgnoreCase(Utilis.getIni(this,"SYS","DEPT",6))) nPart=1;  //門市版
		
		if(mMode == CodeMode.COUNTRY){
			mCountryAdapter = new CountryAdapter(this);
			listview.setVisibility(View.VISIBLE);
			listview.setAdapter(mCountryAdapter);
			setTitle("產地國別清單");
			listview.setOnItemClickListener(new OnItemClickListener(){
				@Override
				public void onItemClick(AdapterView<?> arg0, View view,
						int pos, long arg3) {
					Utilis.runVibrate(CodeList.this);
					if(mEditMode == EditMode.EDIT){
						//
						openIntent(mMode, pos);
					}else{
						Intent intent = new Intent();
//						intent.putExtra("country_pic", mCountryAdapter.getCountryPID(pos));
						intent.putExtra("country_id", mCountryAdapter.getCountryId(pos));
						intent.putExtra("country_pic", mCountryAdapter.getCountryPic(pos));
						intent.putExtra("country_name", mCountryAdapter.getCountryName(pos));
						intent.putExtra("country_rate", mCountryAdapter.getCountryRate(pos));
						setResult(Activity.RESULT_OK, intent);
						finish();
					}
				}
			});
			listview.setOnItemLongClickListener(mLongClickListener);
		}
		else if(mMode == CodeMode.CATEGORY){
			if(mEditMode == EditMode.EDIT){
				mCategoryAdapter = new CategoryAdapter(this);
			}else{
				int country = getIntent().getIntExtra("edit_country", 0);
				int season = getIntent().getIntExtra("edit_season", 1);
				mCategoryAdapter = new CategoryAdapter(this, country, season);
			}
			listview.setVisibility(View.VISIBLE);
			listview.setAdapter(mCategoryAdapter);
			setTitle("商品類別清單");
			listview.setOnItemClickListener(new OnItemClickListener(){
				@Override
				public void onItemClick(AdapterView<?> arg0, View view,
						int pos, long arg3) {
					// TODO Auto-generated method stub
					Utilis.runVibrate(CodeList.this);
					if(mEditMode == EditMode.EDIT){
						openIntent(mMode, pos);
					}else{
						Intent intent = new Intent();
						intent.putExtra("category_name", mCategoryAdapter.getCategoryName(pos));
						intent.putExtra("type_id", mCategoryAdapter.getID(pos));
						intent.putExtra("type_addcost", mCategoryAdapter.getAddCost(pos));
						setResult(CodeList.RESULT_OK, intent);
						finish();
					}
				}
			});
			listview.setOnItemLongClickListener(mLongClickListener);
		}
		else if(mMode == CodeMode.COLOR){
			setTitle("商品顏色盤設定");
			((LinearLayout)findViewById(R.id.layout_color)).setVisibility(View.VISIBLE);
        	GridView grid = (GridView)findViewById(R.id.gridview);
        	mColorAdapter = new ColorAdapter(CodeList.this);
        	grid.setAdapter(mColorAdapter);
        	grid.setOnItemClickListener(new OnItemClickListener(){
				@Override
				public void onItemClick(AdapterView<?> arg0, View view,
						int pos, long arg3) {
					openIntent(mMode, pos);
				}
        	});
        	grid.setOnItemLongClickListener(mLongClickListener);
        	
		}else if(mMode == CodeMode.SIZE){
			mSizeAdapter = new SizeAdapter(this);
			listview.setVisibility(View.VISIBLE);
			listview.setAdapter(mSizeAdapter);
			setTitle("商品尺寸組合");
			listview.setOnItemClickListener(new OnItemClickListener(){
				@Override
				public void onItemClick(AdapterView<?> arg0, View view,
						int pos, long arg3) {
					openIntent(mMode, pos);
				}
			});
			listview.setOnItemLongClickListener(mLongClickListener);
		}else if(mMode == CodeMode.PRICING){
         if (PSWD!=1){  //密碼OK 或不用密碼
			if(mEditMode == EditMode.EDIT){
				mPricingAdapter = new PricingAdapter(this);
			}else{
				// 帶入 CountryId
				int country = getIntent().getIntExtra("edit_country", 0);
				mPricingAdapter = new PricingAdapter(this, country);
			}
			
			listview.setVisibility(View.VISIBLE);
			listview.setAdapter(mPricingAdapter);
			setTitle("訂價策略");
			listview.setOnItemClickListener(new OnItemClickListener(){
				@Override
				public void onItemClick(AdapterView<?> arg0, View view,
						int pos, long arg3) {
					if(mEditMode == EditMode.EDIT){
						openIntent(mMode, pos);
					}else{
						Intent intent = new Intent();
						PriceData data = mPricingAdapter.getData(pos);
						if(data != null){
							intent.putExtra("p0", data.p0);
							intent.putExtra("p1", data.p1);
							intent.putExtra("p2", data.p2);
							intent.putExtra("p3", data.p3);
							intent.putExtra("p4", data.p4);
							intent.putExtra("p5", data.p5);
						}
						setResult(Activity.RESULT_OK, intent);
						finish();
					}
				}
			});
			listview.setOnItemLongClickListener(mLongClickListener);
         }
		}else if(mMode == CodeMode.ELEMENTS){
			mElementAdapter = new ElementAdapter(this);
			listview.setVisibility(View.VISIBLE);
			listview.setAdapter(mElementAdapter);
			setTitle("成份組合");
			listview.setOnItemClickListener(new OnItemClickListener(){
				@Override
				public void onItemClick(AdapterView<?> arg0, View view,
						int pos, long arg3) {
					if(mEditMode == EditMode.EDIT){
						openIntent(mMode, pos);
					}else{
						Intent intent = new Intent();

						intent.putExtra("id", mElementAdapter.getId(pos));
						setResult(Activity.RESULT_OK, intent);
						finish();
					}
				}
			});
			listview.setOnItemLongClickListener(mLongClickListener);
		}else if(mMode == CodeMode.WASHING){
			mWashAdapter = new WashAdapter(this);
			listview.setVisibility(View.VISIBLE);
			listview.setAdapter(mWashAdapter);
			setTitle("洗滌組合");
			listview.setOnItemClickListener(new OnItemClickListener(){
				@Override
				public void onItemClick(AdapterView<?> arg0, View view,
						int pos, long arg3) {
					if(mEditMode == EditMode.EDIT){
						openIntent(mMode, pos);
					}else{
						Intent intent = new Intent();
						intent.putExtra("id", mWashAdapter.getId(pos));
						setResult(Activity.RESULT_OK, intent);
						finish();
					}
				}
			});
			listview.setOnItemLongClickListener(mLongClickListener);
		//@@@會計科目	
		}else if(mMode == CodeMode.MACC){
			mMaccAdapter = new MaccAdapter(this);
			listview.setVisibility(View.VISIBLE);
			listview.setAdapter(mMaccAdapter);
			setTitle("收支代碼");
			listview.setOnItemClickListener(new OnItemClickListener(){
				@Override
				public void onItemClick(AdapterView<?> arg0, View view,
						int pos, long arg3) {
					if(mEditMode == EditMode.EDIT){
						openIntent(mMode, pos);
					}else{
						Intent intent = new Intent();
						intent.putExtra("QNOM", mMaccAdapter.getAccNo(pos));
						intent.putExtra("QNOM_TXT", mMaccAdapter.getAccName(pos));
						setResult(Activity.RESULT_OK, intent);
						finish();
					}
				}
			});
			listview.setOnItemLongClickListener(mLongClickListener);
			
			//@@@特價對照表	
			}else if(mMode == CodeMode.PPRI){
				mPpriAdapter = new PpriAdapter(this);
				listview.setVisibility(View.VISIBLE);
				listview.setAdapter(mPpriAdapter);
				setTitle("專櫃特價對照表");
				listview.setOnItemClickListener(new OnItemClickListener(){
					@Override
					public void onItemClick(AdapterView<?> arg0, View view,
							int pos, long arg3) {
						if(mEditMode == EditMode.EDIT){
							openIntent(mMode, pos);
						}else{
							Intent intent = new Intent();
							intent.putExtra("BPRI", mPpriAdapter.getBpri(pos));
							intent.putExtra("APRI", mPpriAdapter.getApri(pos));
							intent.putExtra("YEAR", mPpriAdapter.getYear(pos));
							intent.putExtra("SEASON", mPpriAdapter.getSeason(pos));
							setResult(Activity.RESULT_OK, intent);
							finish();
						}
					}
				});
				listview.setOnItemLongClickListener(mLongClickListener);
			
		}
		((Button)findViewById(R.id.btn_add)).setOnClickListener(new MenuClick());
		((Button)findViewById(R.id.btn_exit)).setOnClickListener(new MenuClick());
		
	}
	
	//3g001,Q1-Q30,Local_(SYS,ALBUN,n)LAST_DAY
	  private 	void Httpdel_one_qc(String gUid,String Kind,String Id){
		  
			if (Utilis.haveInternet(this) && of_line!=0){  //有網路的狀態且非單機
					StringBuffer bfstr = new StringBuffer();
					StringBuffer bfstrF = new StringBuffer();
						bfstr.append(gUid).append(";");
						bfstrF.append("UID").append(";");
						bfstr.append(Id).append(";");
						bfstrF.append("_ID").append(";");
						bfstr.append(Kind);
						bfstrF.append("QKIND");
						
						Toast.makeText(CodeList.this, "正前往雲端刪除一筆資料中,請稍候..."+gUid+Id+Kind, Toast.LENGTH_SHORT).show();
					Thread t = new Thread(new DBCloud.sendPostRunnableQC(CodeList.this,bfstr.toString(),bfstrF.toString(),0));
					t.start();
			}
	  }
	
	public void onResume(){
		super.onResume();
		if(mMode == CodeMode.COUNTRY){
			if(mCountryAdapter != null){
				mCountryAdapter.getData(this);
				mCountryAdapter.notifyDataSetChanged();
			}
		}else if(mMode == CodeMode.COLOR){
			if(mColorAdapter != null){
				mColorAdapter.getData(this);
				mColorAdapter.notifyDataSetChanged();
			}
		}else if(mMode == CodeMode.CATEGORY){
			if(mCategoryAdapter != null){
				mCategoryAdapter.getData(this);
				mCategoryAdapter.notifyDataSetChanged();
			}
		}else if(mMode == CodeMode.ELEMENTS){
			if(mElementAdapter != null){
				mElementAdapter.getData(this, 0);
				mElementAdapter.notifyDataSetChanged();
			}
		}else if(mMode == CodeMode.PRICING){
			if(mPricingAdapter != null){
				mPricingAdapter.getData(this);
				mPricingAdapter.notifyDataSetChanged();
			}
		}else if(mMode == CodeMode.SIZE){
			if(mSizeAdapter != null){
				mSizeAdapter.getData(this);
				mSizeAdapter.notifyDataSetChanged();
			}
		}else if(mMode == CodeMode.WASHING){
			if(mWashAdapter != null){
				mWashAdapter.getData(this, -1);
				mWashAdapter.notifyDataSetChanged();
			}
		}else if(mMode == CodeMode.MACC){
			if(mMaccAdapter != null){
				mMaccAdapter.getData(this);
				mMaccAdapter.notifyDataSetChanged();
			}
		}else if(mMode == CodeMode.PPRI){
			if(mPpriAdapter != null){
				mPpriAdapter.getData(this);
				mPpriAdapter.notifyDataSetChanged();
			}
		}
	}
	
	private LongClick mLongClickListener = new LongClick();
	
	class LongClick implements OnItemLongClickListener{
		@Override
		public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
				final int position, long arg3) {
			Utilis.runVibrate(CodeList.this);
			if ((of_line!=0 && Utilis.haveInternet(CodeList.this)) || (of_line==0 ) ){
					DialogUtilis.showDialog(CodeList.this, "請選擇以下功能", -1, new String[]{"編輯","刪除"}, new IDialog(){
						@Override
						public void onDialogFinish(int pos, String choice) {
							switch(pos){
								case 0: // 編輯資料
									openIntent(mMode, position);
									break;
								case 1: // 刪除資料
									if (PSWD!=1 && nPart==0){ //權限+總倉OK									
										deleteData(mMode, position);
									}	
									break;
							}
						}
					});
			}else { //
				Toast.makeText(CodeList.this, "無法連線至雲端,請檢查網路是否暢通!!", Toast.LENGTH_SHORT).show();
			}
			return false;
		}

	}

	class MenuClick implements OnClickListener{
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			int id = v.getId();
			Utilis.runVibrate(CodeList.this);
			switch(id){
			case R.id.btn_add:
			{
			if ((of_line!=0 && Utilis.haveInternet(CodeList.this)) || (of_line==0 ) ){	
				switch(mMode){
					case CodeMode.COUNTRY:
					{
						Intent intent = new Intent();
						intent.setClass(CodeList.this, AddCountry.class);
						intent.putExtra("mode", 0);
						startActivity(intent);
						break;
					}
					case CodeMode.CATEGORY:
					{
						Intent intent = new Intent();
						intent.setClass(CodeList.this, AddCategory.class);
						intent.putExtra("mode", 0);
						startActivity(intent);
						break;
					}
					case CodeMode.COLOR:
					{
						Intent intent = new Intent();
						intent.setClass(CodeList.this, AddColor.class);
						intent.putExtra("mode", 0);
						startActivity(intent);
						break;
					}
					case CodeMode.SIZE:
					{
						Intent intent = new Intent();
						intent.setClass(CodeList.this, AddSize.class);
						intent.putExtra("mode", 0);
						startActivity(intent);
						break;
					}
					case CodeMode.PRICING:
					{
					 if (Utilis.getIni(CodeList.this,"SYS","DEPT",5).equalsIgnoreCase("0")){  //密碼OK 或不用密碼						
						Intent intent = new Intent();
						intent.setClass(CodeList.this, AddPricing.class);
						intent.putExtra("mode", 0);
						startActivity(intent);
					 }	
						break;
					}
					case CodeMode.ELEMENTS:
					{
						Intent intent = new Intent();
						intent.setClass(CodeList.this, AddElements.class);
						intent.putExtra("mode", 0);
						startActivity(intent);
						break;
					}
					case CodeMode.WASHING:
					{
						Intent intent = new Intent();
						intent.setClass(CodeList.this, AddWash.class);
						intent.putExtra("mode", 0);
						startActivity(intent);
						break;
					}
					case CodeMode.MACC:
					{
						Intent intent = new Intent();
						intent.setClass(CodeList.this, AddMacc.class);
						intent.putExtra("mode", 0);
						startActivity(intent);
						break;
					}
					case CodeMode.PPRI:
					{
						Intent intent = new Intent();
						intent.setClass(CodeList.this, AddPpri.class);
						intent.putExtra("mode", 0);
						startActivity(intent);
						break;
					}
					
					default:
						Toast.makeText(CodeList.this, "尚無此功能", Toast.LENGTH_SHORT).show();
						break;
				}
			}
			} //end of haveinternet
				break;
				
			case R.id.btn_exit:
				finish();
				break;
			}
				
		}
	}
	
	
	@SuppressWarnings("unused")
	private void openIntent(int mode, int pos){
		Utilis.runVibrate(this);
		if ((of_line!=0 && Utilis.haveInternet(this)) || (of_line==0 ) ){ 
			switch(mode){
			case CodeMode.COUNTRY:
				{
					Intent intent = new Intent();
					intent.setClass(CodeList.this, AddCountry.class);
					int id = mCountryAdapter.getID(pos);
					intent.putExtra("id", id);
					intent.putExtra("mode", 1);
					startActivity(intent);
					break;
				}
				case CodeMode.CATEGORY:
				{
					Intent intent = new Intent();
					intent.setClass(CodeList.this, AddCategory.class);
					int id = mCategoryAdapter.getID(pos);
					intent.putExtra("id", id);
					intent.putExtra("mode", 1);
					startActivity(intent);
					break;
				}
				case CodeMode.COLOR:
				{
					Intent intent = new Intent();
					intent.setClass(CodeList.this, AddColor.class);
					ColorData data = mColorAdapter.getColorData(pos);
					intent.putExtra("id", data.id);
					intent.putExtra("color", data.color);
					intent.putExtra("color_number", data.number);
					intent.putExtra("color_name", data.name);
					intent.putExtra("mode", 1);
					startActivity(intent);
					break;
				}
				case CodeMode.SIZE:
				{
					Intent intent = new Intent();
					intent.setClass(CodeList.this, AddSize.class);
					String size = mSizeAdapter.getSize(pos);
					int id = mSizeAdapter.getId(pos);
					String[] sizes = size.split(",");
					intent.putExtra("size", size);
					intent.putExtra("id", id);
					intent.putExtra("mode", 1);
					startActivity(intent);
					break;
				}
				case CodeMode.PRICING:
				{
					Intent intent = new Intent();
					int id = mPricingAdapter.getId(pos);
					intent.putExtra("id", id);
					intent.putExtra("mode", 1);
					intent.setClass(CodeList.this, AddPricing.class);
					startActivity(intent);
					break;
				}
				case CodeMode.ELEMENTS:
				{
					Intent intent = new Intent();
					int id = mElementAdapter.getId(pos);
					intent.putExtra("id", id);
					intent.putExtra("mode", 1);
					intent.setClass(CodeList.this, AddElements.class);
					startActivity(intent);
					break;
				}
				case CodeMode.WASHING:
				{
					Intent intent = new Intent();
					int id = mWashAdapter.getId(pos);
					intent.putExtra("id", id);
					intent.putExtra("mode", 1);
					intent.setClass(CodeList.this, AddWash.class);
					startActivity(intent);
					break;
				}
				case CodeMode.MACC:
				{
					Intent intent = new Intent();
					intent.setClass(CodeList.this, AddMacc.class);
					int id = mMaccAdapter.getID(pos);
					intent.putExtra("id", id);
					intent.putExtra("mode", 1);
					startActivity(intent);
					break;
				}
				case CodeMode.PPRI:
				{
					Intent intent = new Intent();
					intent.setClass(CodeList.this, AddPpri.class);
					int id = mPpriAdapter.getID(pos);
					intent.putExtra("id", id);
					intent.putExtra("mode", 1);
					startActivity(intent);
					break;
				}
			}
		} else {//
			Toast.makeText(CodeList.this, "無法連線至雲端,請檢查網路是否暢通!!", Toast.LENGTH_SHORT).show();
		}
	}
	
	private void deleteData(int mode, int pos){
		switch(mode){
			//	Toast.makeText(this, "錯誤!目前此類別尚有資料使用,無法刪除", Toast.LENGTH_SHORT).show();
			case CodeMode.COUNTRY:
			{
							Toast.makeText(CodeList.this, "不可刪除國家,請用修改或新增!!", Toast.LENGTH_SHORT).show();
				break;
			}
			case CodeMode.CATEGORY:
			{
				try{
						SQLiteDatabase db = Utilis.getDB(this);
						int id = mCategoryAdapter.getID(pos);
						// 需先判斷 qdetail 裡面是否有使用到這筆資料
						if(DBUtil.isDataExistQDetail(db, "GOODSTYPE", String.valueOf(id), false) == true){
							Toast.makeText(this, "錯誤!目前此類別尚有資料使用,無法刪除", Toast.LENGTH_SHORT).show();
						}else{
							db.execSQL("delete from qc_type where _ID="+id);
							Toast.makeText(CodeList.this, "刪除資料成功", Toast.LENGTH_SHORT).show();
							if (of_line!=0) Httpdel_one_qc(mUid,"2",""+id);
							// 更新資料
							mCategoryAdapter.getData(CodeList.this);
							mCategoryAdapter.notifyDataSetChanged();
						}
					db.close();
				}catch(Exception e){
					Toast.makeText(CodeList.this, "刪除資料失敗", Toast.LENGTH_SHORT).show();
					e.printStackTrace();
				}
				break;
			}
			case CodeMode.COLOR:
			{
				try{
						SQLiteDatabase db = Utilis.getDB(this);
						int id = mColorAdapter.getId(pos);
						db.execSQL("delete from qc_color where _ID="+id);
						//
						Toast.makeText(CodeList.this, "刪除資料成功", Toast.LENGTH_SHORT).show();
						if (of_line!=0) Httpdel_one_qc(mUid,"3",""+id);
						// 更新資料
						mColorAdapter.getData(CodeList.this);
						mColorAdapter.notifyDataSetChanged();
						db.close();
				}catch(Exception e){
					Toast.makeText(CodeList.this, "刪除資料失敗", Toast.LENGTH_SHORT).show();
					e.printStackTrace();
				}
				break;
			}
			case CodeMode.SIZE:
			{
				try{
						SQLiteDatabase db = Utilis.getDB(this);
						int id = mSizeAdapter.getId(pos);
						db.execSQL("delete from qc_size where _ID="+id);
						Toast.makeText(CodeList.this, "刪除資料成功", Toast.LENGTH_SHORT).show();
						if (of_line!=0) Httpdel_one_qc(mUid,"4",""+id);
						// 更新資料
						mSizeAdapter.getData(CodeList.this);
						mSizeAdapter.notifyDataSetChanged();
						db.close();
				}catch(Exception e){
					Toast.makeText(CodeList.this, "刪除資料失敗", Toast.LENGTH_SHORT).show();
					e.printStackTrace();
				}
				break;
			}
			case CodeMode.PRICING:
			{
				try{
						SQLiteDatabase db = Utilis.getDB(this);
						int id = mPricingAdapter.getId(pos);
						db.execSQL("delete from qc_defpri where _ID="+id);
						Toast.makeText(CodeList.this, "刪除資料成功", Toast.LENGTH_SHORT).show();
						if (of_line!=0) Httpdel_one_qc(mUid,"5",""+id);
						// 更新資料
						mPricingAdapter.getData(CodeList.this);
						mPricingAdapter.notifyDataSetChanged();
						db.close();
				}catch(Exception e){
					Toast.makeText(CodeList.this, "刪除資料失敗", Toast.LENGTH_SHORT).show();
					e.printStackTrace();
				}
				break;
			}
			case CodeMode.ELEMENTS:
			{
				try{
						SQLiteDatabase db = Utilis.getDB(this);
						int id = mElementAdapter.getId(pos);
						if(DBUtil.isDataExistQDetail(db, "QUALITY_ID", String.valueOf(id), false) == true){
							Toast.makeText(this, "錯誤!目前此類別尚有資料使用,無法刪除", Toast.LENGTH_SHORT).show();
						}else{
							db.execSQL("delete from qc_quality where _ID="+id);
							Toast.makeText(CodeList.this, "刪除資料成功", Toast.LENGTH_SHORT).show();
							if (of_line!=0) Httpdel_one_qc(mUid,"6",""+id);
							// 更新資料
							mElementAdapter.getData(CodeList.this, 0);
							mElementAdapter.notifyDataSetChanged();
						}
						db.close();
				}catch(Exception e){
					Toast.makeText(CodeList.this, "刪除資料失敗", Toast.LENGTH_SHORT).show();
					e.printStackTrace();
				}
				break;
			}
			case CodeMode.WASHING:
			{
				try{
						SQLiteDatabase db = Utilis.getDB(this);
						int id = mWashAdapter.getId(pos);
						if(DBUtil.isDataExistQDetail(db, "WASH_ID", String.valueOf(id), false) == true){
							Toast.makeText(this, "錯誤!目前此類別尚有資料使用,無法刪除", Toast.LENGTH_SHORT).show();
						}else{
							db.execSQL("delete from qc_wash where _ID="+id);
							Toast.makeText(CodeList.this, "刪除資料成功", Toast.LENGTH_SHORT).show();
							if (of_line!=0) Httpdel_one_qc(mUid,"7",""+id);
							// 更新資料
							mWashAdapter.getData(CodeList.this, -1);
							mWashAdapter.notifyDataSetChanged();
						}
					db.close();
				}catch(Exception e){
					Toast.makeText(CodeList.this, "刪除資料失敗", Toast.LENGTH_SHORT).show();
					e.printStackTrace();
				}
				break;
			}
			case CodeMode.MACC:
			{
				try{
						SQLiteDatabase db = Utilis.getDB(this);
						int id = mMaccAdapter.getID(pos);
						db.execSQL("delete from qc_macc where _ID="+id);
						Toast.makeText(CodeList.this, "刪除資料成功", Toast.LENGTH_SHORT).show();
						if (of_line!=0) Httpdel_one_qc(mUid,"8",""+id);
							// 更新資料
						mMaccAdapter.getData(CodeList.this);
						mMaccAdapter.notifyDataSetChanged();
					db.close();
				}catch(Exception e){
					Toast.makeText(CodeList.this, "刪除資料失敗", Toast.LENGTH_SHORT).show();
					e.printStackTrace();
				}
				break;
			}
			case CodeMode.PPRI:
			{
				try{
						SQLiteDatabase db = Utilis.getDB(this);
						int id = mPpriAdapter.getID(pos);
						db.execSQL("delete from qc_ppri where _ID="+id);
						Toast.makeText(CodeList.this, "刪除資料成功", Toast.LENGTH_SHORT).show();
						if (of_line!=0) Httpdel_one_qc(mUid,"9",""+id);
							// 更新資料
						mPpriAdapter.getData(CodeList.this);
						mPpriAdapter.notifyDataSetChanged();
					db.close();
				}catch(Exception e){
					Toast.makeText(CodeList.this, "刪除資料失敗", Toast.LENGTH_SHORT).show();
					e.printStackTrace();
				}
				break;
			}
		}
	}
	
	class Codedapter extends BaseAdapter {
		private LayoutInflater mInflater;
		private String[] mNames = new String[]{"產地國別", "商品類別"
				, "商品顏色", "尺寸規格", "訂價策略", "成份組合","洗滌說明","收支代碼","價格對照表"};
		public Codedapter(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;
		}
	}
	
	
	public class ColorData{
		int id;
		String name;
		String number;
		int color;
	}
	public class ColorAdapter extends BaseAdapter{
		private LinkedList<ColorData> mList = null;
		private LayoutInflater mInflater;
    	/* MyAdapter的建構子，傳入三個參數 */
        public ColorAdapter(Context context) {
          /* 參數初始化 */
          mInflater = LayoutInflater.from(context);
          mList = new LinkedList<ColorData>();
        }
        
        public ColorData getColorData(int pos){
        	return mList.get(pos);
        }
        
        public int getId(int pos){
        	return mList.get(pos).id;
        }
        
        public void getData(Context context){
    		if(mList != null){
    			mList.clear();
    		}
    		SQLiteDatabase db = Utilis.getDB(context);
    		Cursor cursor = db.rawQuery("select * from qc_color order by NO", null);
    		if(cursor != null){
    			try{
    				if(cursor.getCount() > 0){
    					cursor.moveToFirst();
    					for(int i = 0; i < cursor.getCount(); i++){
    						ColorData data = new ColorData();
    						data.id = cursor.getInt(cursor.getColumnIndex("_ID"));
    						data.color = cursor.getInt(cursor.getColumnIndex("PIC"));
    						data.name = cursor.getString(cursor.getColumnIndex("TXT"));
    						data.number = cursor.getString(cursor.getColumnIndex("NO"));
    						mList.add(data);
    						cursor.moveToNext();
    					}
    				}
    			}catch(Exception e){
    				e.printStackTrace();
    			}finally{
    				cursor.close();
    				cursor = null;
    			}
    		}
    		db.close();
    		db = null;
    	}
    	
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mList.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			 ViewHolder holder;
		      if (convertView == null) {
		    	convertView = mInflater.inflate(R.layout.list_color, null);  
		        holder = new ViewHolder();
		        holder.textName = (TextView)convertView.findViewById(R.id.text_name);
		        holder.textColor = (ImageView)convertView.findViewById(R.id.text_color);
		        holder.check = (CheckBox)convertView.findViewById(R.id.checkbox);
		        holder.check.setVisibility(View.INVISIBLE);
		        convertView.setTag(holder);
		      }else{
		    	  holder = (ViewHolder) convertView.getTag();
		      }
		      ColorData data = mList.get(position);
		      holder.textColor.setBackgroundColor(data.color);
		      holder.textName.setText(data.number + " " + data.name);
		      return convertView;
		}
		
		private class ViewHolder {
		  ImageView textColor;
	      TextView textName;
	      CheckBox check;
		}
	}
	
}
