package com.cwbuyer.lib;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.cwbuyer.format.DefaultData;
import com.pwbuyer.main.R;

@SuppressLint("InflateParams")
@SuppressWarnings("unused")
public class SysSetup extends PreferenceActivity {

	private String[] beCode = new String[]{"不編","批發","零售","中盤","原幣","供應商","日期","網購","成本"};	
	private String[] beCodeLR = new String[]{"0","1","2","3","4","5","6","7","8"};
	private String[] beCodeNo = new String[]{"0","1","2","3","4","5","6","7","8"};

//	final String myAlbunList =android.os.Environment.getExternalStorageDirectory().getAbsolutePath()+Utilis.getIni(this, "SYS", "YEAR",8)+File.separator+"Goods_albun.lst";	
//	final String myImportPath =android.os.Environment.getExternalStorageDirectory().getAbsolutePath()+Utilis.getIni(this, "SYS", "YEAR",7);	


	private List<HashMap<String, Object>> albun_list = null;
	private DefaultData mDefaultData = null; // 採購參數
	
	private int pointed=0;
	private int totRec=0;
//	String batch=null;
	
	Bundle bundle;	
	  @Override
	  public void onConfigurationChanged(Configuration config) {
		super.onConfigurationChanged(config);
		
//		setAutoRotationMode();
//		int i = getRequestedOrientation();
		
		switch (getRequestedOrientation()) {
			case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
				if (config.orientation == Configuration.ORIENTATION_PORTRAIT) {
//					((RelativeLayout)findViewById(R.id.body)).setBackgroundResource(R.drawable.background);
				}
				break;
			case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
				if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//					((RelativeLayout)findViewById(R.id.body)).setBackgroundResource(R.drawable.background_land);
				}
				break;
			case ActivityInfo.SCREEN_ORIENTATION_SENSOR:
				if (config.orientation == Configuration.ORIENTATION_LANDSCAPE){
//					((RelativeLayout)findViewById(R.id.body)).setBackgroundResource(R.drawable.background_land);
				}else{
//					((RelativeLayout)findViewById(R.id.body)).setBackgroundResource(R.drawable.background);
				}
			default:
				break;
		}
	  }
	  
	  /** Called when the activity is first created. */
	  
	  @SuppressWarnings("deprecation")
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);

	    Utilis.setAutoRotationMode(this);	    
	    
	    addPreferencesFromResource(R.xml.user_preferences);
	    getDefaultData();
		
		int leave=0;
	  }
		
	  
	  protected void onDestroy() {
	    super.onDestroy();
	  }

	  public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
		  
		  if (preference.getKey().equalsIgnoreCase("key_company")) {  
			  showSysSetup(this,1300);
		  } else if(preference.getKey().equalsIgnoreCase("key_backup")){
			  showSysSetup(this,1250);
		  } else if(preference.getKey().equalsIgnoreCase("key_transdept")){
			  showSysSetup(this,1620);
		  } else if(preference.getKey().equalsIgnoreCase("key_export")){
			  showSysSetup(this,1600);
		  } else if(preference.getKey().equalsIgnoreCase("key_exp5263")){//
			  showSysSetup(this,1500);
		  } else if(preference.getKey().equalsIgnoreCase("key_exp5200")){//
			  showSysSetup(this,1400);
		  } else if(preference.getKey().equalsIgnoreCase("key_expdept")){
			  showSysSetup(this,1650);
		  } else if(preference.getKey().equalsIgnoreCase("key_setdept")){//
			  showSysSetup(this,1200);
		  } else if(preference.getKey().equalsIgnoreCase("key_setyear")){//
			  gotoMT();//cloudSetup(this,1700);
		  } else if(preference.getKey().equalsIgnoreCase("key_setsdb")){//
			  showSysSetup(this,1680);
		  } else if(preference.getKey().equalsIgnoreCase("key_printersetup")){//
			  showSysSetup(this,1201);
		  } else if(preference.getKey().equalsIgnoreCase("key_barcodesetup")){	
			  showBeCode();
		  } else if(preference.getKey().equalsIgnoreCase("key_socketsetup")){
			  showSysSetup(this,1550);
		  }
	    return true;
	  }
	

	  private void getDefaultData(){
    	SQLiteDatabase sdb = Utilis.getDB(this);
    	if(sdb != null){
			Cursor cursor = sdb.rawQuery("select * from qc_default", null);
			if(cursor != null){
				try{
					if(cursor.getCount() > 0){
						cursor.moveToFirst();
						mDefaultData = new DefaultData();
						mDefaultData.nYear = cursor.getInt(cursor.getColumnIndex("YEAR"));
						mDefaultData.employ = cursor.getString(cursor.getColumnIndex("EMPLOY"));
						mDefaultData.nBarOut = cursor.getInt(cursor.getColumnIndex("BAROUT")); //保留
						mDefaultData.nFileOut = cursor.getInt(cursor.getColumnIndex("FILEOUT")); //保留
						mDefaultData.nPrintOut = cursor.getInt(cursor.getColumnIndex("PRINTOUT")); //保留
						mDefaultData.nCa1 = cursor.getInt(cursor.getColumnIndex("CA1"));
						mDefaultData.nCa2 = cursor.getInt(cursor.getColumnIndex("CA2"));
						mDefaultData.nCa3 = cursor.getInt(cursor.getColumnIndex("CA3"));
						mDefaultData.nCb1 = cursor.getInt(cursor.getColumnIndex("CB1"));
						mDefaultData.nCb2 = cursor.getInt(cursor.getColumnIndex("CB2"));
						mDefaultData.nCb3 = cursor.getInt(cursor.getColumnIndex("CB3"));
						mDefaultData.nCc1 = cursor.getInt(cursor.getColumnIndex("CC1"));
						mDefaultData.nCc2 = cursor.getInt(cursor.getColumnIndex("CC2"));
						mDefaultData.nCc3 = cursor.getInt(cursor.getColumnIndex("CC3"));
						mDefaultData.nCd1 = cursor.getInt(cursor.getColumnIndex("CD1"));
						mDefaultData.nCd2 = cursor.getInt(cursor.getColumnIndex("CD2"));
						mDefaultData.nCd3 = cursor.getInt(cursor.getColumnIndex("CD3"));
					}
				}catch(Exception e){
					e.printStackTrace();
				}finally{
					cursor.close();
				}
			}
			sdb.close();
    	}
    }

	private void cloudSetup(Context context,int Points){
		//應具備 工程師 密碼
		
		AlertDialog.Builder dialogCode = new AlertDialog.Builder(this);
		final View root = this.getLayoutInflater().inflate(R.layout.syssetup, null);
  	  	final EditText ED01 =(EditText)root.findViewById(R.id.edt01);
	  	final EditText ED02 =(EditText)root.findViewById(R.id.edt02);
	  	final EditText ED03 =(EditText)root.findViewById(R.id.edt03);
	  	final EditText ED04 =(EditText)root.findViewById(R.id.edt04);
	  	final EditText ED05 =(EditText)root.findViewById(R.id.edt05);
	  	final EditText ED06 =(EditText)root.findViewById(R.id.edt06);
	  	final EditText ED07 =(EditText)root.findViewById(R.id.edt07);
	  	final EditText ED08 =(EditText)root.findViewById(R.id.edt08);
	  	final EditText ED09 =(EditText)root.findViewById(R.id.edt09);
	  	((TextView)root.findViewById(R.id.TextView00)).setText("雲端版資料庫相關設定");
	  	((TextView)root.findViewById(R.id.TextView01)).setText("資料庫IP位址");
	  	((TextView)root.findViewById(R.id.TextView02)).setText("雲端資料庫名稱");
	  	((TextView)root.findViewById(R.id.TextView03)).setText("0:不啟動雲端服務 1:啟動雲端服務");
	  	((TextView)root.findViewById(R.id.TextView04)).setText("FTP_00_帳號");
	  	((TextView)root.findViewById(R.id.TextView05)).setText("FTP_00_密碼");
	  	((TextView)root.findViewById(R.id.TextView06)).setText("FTP_IP位址");
	  	((TextView)root.findViewById(R.id.TextView07)).setText("FTP_01_帳號");
	  	((TextView)root.findViewById(R.id.TextView08)).setText("FTP_01_密碼");
	  	((TextView)root.findViewById(R.id.TextView09)).setText("預設FTP_PORT(21)");

	    try {
	      File file = new File(android.os.Environment.getExternalStorageDirectory().getAbsolutePath()+ "/cwbuyer/para/ini.sdb");
	      SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(file, null);
	      
	      StringBuffer mBuf = new StringBuffer();
	      mBuf.append("select * from system where POINT="+Points+" ORDER BY POINT,_ID");
	      Cursor cs = db.rawQuery(mBuf.toString(), null);
	      if (cs.getCount()>0){
	    	cs.moveToFirst();
		 	pointed=1700;
		 	ED01.setText(cs.getString(4));
		 	ED02.setText(cs.getString(5));
		 	ED03.setText(cs.getString(6));
		 	ED04.setText(cs.getString(7));
		 	ED05.setText(cs.getString(8));
		 	ED06.setText(cs.getString(9));
		 	ED07.setText(cs.getString(10));
		 	ED08.setText(cs.getString(11));
		 	ED09.setText(cs.getString(12));
	      }
	      db.close();
	      cs.close();
			dialogCode.setView(root);
			dialogCode.setTitle("系統參數設定");
			dialogCode.setPositiveButton("OK", new DialogInterface.OnClickListener() {
		          public void onClick(DialogInterface dialog, int which) {
		        	  //將所有欄位寫入 pointed
		          try{ 
				      File file = new File(android.os.Environment.getExternalStorageDirectory().getAbsolutePath()+ "/cwbuyer/para/ini.sdb");
				      SQLiteDatabase dbc = SQLiteDatabase.openOrCreateDatabase(file, null);
				      
				      StringBuffer mySqlBuf = new StringBuffer();
						mySqlBuf.append("update  system ");
						
						mySqlBuf.append(" set T1='"+ED01.getText().toString()+"'");
						mySqlBuf.append(" ,T2='"+ED02.getText().toString()+"'");
						mySqlBuf.append(" ,T3='"+ED03.getText().toString()+"'");
						mySqlBuf.append(" ,T4='"+ED04.getText().toString()+"'");
						mySqlBuf.append(" ,T5='"+ED05.getText().toString()+"'");
						mySqlBuf.append(" ,T6='"+ED06.getText().toString()+"'");
						mySqlBuf.append(" ,T7='"+ED07.getText().toString()+"'");
						mySqlBuf.append(" ,T8='"+ED08.getText().toString()+"'");
						mySqlBuf.append(" ,T9='"+ED09.getText().toString()+"'");
						
						mySqlBuf.append(" where _ID=" +pointed );
						dbc.execSQL(mySqlBuf.toString());			      
						
					dbc.close();
				}catch(Exception e){
						e.printStackTrace();
				}
		        }
			});
			
			dialogCode.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
		          public void onClick(DialogInterface dialog, int which) {
		          }
				});
			dialogCode.show();
		      
	    } catch (SQLException e) {
	    	
	    }

}
	
	private void gotoMT(){
		LayoutInflater factory = LayoutInflater.from(this);
	    final View v1 = factory.inflate(R.layout.exinput, null);
	    final TextView txtReg = (TextView) v1.findViewById(R.id.TextView01);
	    final EditText edtReg = (EditText) v1.findViewById(R.id.edtReg);
	    edtReg.setText("");

		  new AlertDialog.Builder(this).setTitle("工程師系統維護").setMessage("請輸入密碼").setView(v1).setPositiveButton("OK", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
            	  if (edtReg.getText().toString().equalsIgnoreCase(DateUtil.getSystemTime().substring(2,11)+"999")){
        			  cloudSetup(SysSetup.this,1700);
            	  }              
              }
		  }).setCancelable(false).show();
	}
	
	private void showSysSetup(Context context,int Points){  //帶入POINT ORDER BY_ID,可以決定幾個欄位,及順序,也有抬頭說明等
		AlertDialog.Builder dialogCode = new AlertDialog.Builder(this);
		final View root = this.getLayoutInflater().inflate(R.layout.syssetup, null);
  	  	final EditText ED01 =(EditText)root.findViewById(R.id.edt01);
	  	final EditText ED02 =(EditText)root.findViewById(R.id.edt02);
	  	final EditText ED03 =(EditText)root.findViewById(R.id.edt03);
	  	final EditText ED04 =(EditText)root.findViewById(R.id.edt04);
	  	final EditText ED05 =(EditText)root.findViewById(R.id.edt05);
	  	final EditText ED06 =(EditText)root.findViewById(R.id.edt06);
	  	final EditText ED07 =(EditText)root.findViewById(R.id.edt07);
	  	final EditText ED08 =(EditText)root.findViewById(R.id.edt08);
	  	final EditText ED09 =(EditText)root.findViewById(R.id.edt09);

		
	    try {
		      File file = new File(android.os.Environment.getExternalStorageDirectory().getAbsolutePath()+ "/cwbuyer/para/ini.sdb");
		      SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(file, null);
		      
		      StringBuffer mBuf = new StringBuffer();
		      mBuf.append("select * from system where POINT>="+Points+" ORDER BY POINT,_ID LIMIT 2");

		      
		      Cursor cs = db.rawQuery(mBuf.toString(), null);
		      if (cs.getCount()>0){
		    	 cs.moveToFirst();
		    	  ((TextView)root.findViewById(R.id.TextView00)).setText(cs.getString(13));
		    	  ((TextView)root.findViewById(R.id.TextView01)).setText(cs.getString(4));
		    	  ((TextView)root.findViewById(R.id.TextView02)).setText(cs.getString(5));
		    	  ((TextView)root.findViewById(R.id.TextView03)).setText(cs.getString(6));
		    	  ((TextView)root.findViewById(R.id.TextView04)).setText(cs.getString(7));
		    	  ((TextView)root.findViewById(R.id.TextView05)).setText(cs.getString(8));
		    	  ((TextView)root.findViewById(R.id.TextView06)).setText(cs.getString(9));
		    	  ((TextView)root.findViewById(R.id.TextView07)).setText(cs.getString(10));
		    	  ((TextView)root.findViewById(R.id.TextView08)).setText(cs.getString(11));
		    	  ((TextView)root.findViewById(R.id.TextView09)).setText(cs.getString(12));
	    		 cs.moveToNext();
		    	 	pointed=Utilis.toInt(cs.getString(0));
		    	 	ED01.setText(cs.getString(4));
		    	 	ED02.setText(cs.getString(5));
		    	 	ED03.setText(cs.getString(6));
		    	 	ED04.setText(cs.getString(7));
		    	 	ED05.setText(cs.getString(8));
		    	 	ED06.setText(cs.getString(9));
		    	 	ED07.setText(cs.getString(10));
		    	 	ED08.setText(cs.getString(11));
		    	 	ED09.setText(cs.getString(12));
		    	 	totRec=9;
			    	  ((TextView)root.findViewById(R.id.TextView03)).setVisibility(View.VISIBLE);
			    	  ED03.setVisibility(View.VISIBLE);
			    	  ((TextView)root.findViewById(R.id.TextView04)).setVisibility(View.VISIBLE);
			    	  ED04.setVisibility(View.VISIBLE);
			    	  ((TextView)root.findViewById(R.id.TextView05)).setVisibility(View.VISIBLE);
			    	  ED05.setVisibility(View.VISIBLE);
			    	  ((TextView)root.findViewById(R.id.TextView06)).setVisibility(View.VISIBLE);
			    	  ED06.setVisibility(View.VISIBLE);
			    	  ((TextView)root.findViewById(R.id.TextView07)).setVisibility(View.VISIBLE);
			    	  ED07.setVisibility(View.VISIBLE);
			    	  ((TextView)root.findViewById(R.id.TextView08)).setVisibility(View.VISIBLE);
			    	  ED08.setVisibility(View.VISIBLE);
			    	  ((TextView)root.findViewById(R.id.TextView09)).setVisibility(View.VISIBLE);
			    	  ED09.setVisibility(View.VISIBLE);
		    	 }

		      db.close();
		      cs.close();
		      
	    } catch (SQLException e) {
	    	
	    }
		dialogCode.setView(root);
		dialogCode.setTitle("系統參數設定");
		dialogCode.setPositiveButton("OK", new DialogInterface.OnClickListener() {
	          public void onClick(DialogInterface dialog, int which) {
	        	  //將所有欄位寫入 pointed
	          try{ 
			      File file = new File(android.os.Environment.getExternalStorageDirectory().getAbsolutePath()+ "/cwbuyer/para/ini.sdb");
			      SQLiteDatabase dbc = SQLiteDatabase.openOrCreateDatabase(file, null);
			      
			      StringBuffer mySqlBuf = new StringBuffer();
					mySqlBuf.append("update  system ");
					
					mySqlBuf.append(" set T1='"+ED01.getText().toString()+"'");
					mySqlBuf.append(" ,T2='"+ED02.getText().toString()+"'");
					mySqlBuf.append(" ,T3='"+ED03.getText().toString()+"'");
					mySqlBuf.append(" ,T4='"+ED04.getText().toString()+"'");
					mySqlBuf.append(" ,T5='"+ED05.getText().toString()+"'");
					mySqlBuf.append(" ,T6='"+ED06.getText().toString()+"'");
					mySqlBuf.append(" ,T7='"+ED07.getText().toString()+"'");
					mySqlBuf.append(" ,T8='"+ED08.getText().toString()+"'");
					mySqlBuf.append(" ,T9='"+ED09.getText().toString()+"'");
					
					mySqlBuf.append(" where _ID=" +pointed );
					dbc.execSQL(mySqlBuf.toString());			      
					
				dbc.close();
			}catch(Exception e){
					e.printStackTrace();
			}
	        }
		});
		
		dialogCode.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
	          public void onClick(DialogInterface dialog, int which) {
	          }
			});
		dialogCode.show();
		
	}
	
	
	 private void showBeCode(){
			AlertDialog.Builder dialogCode = new AlertDialog.Builder(this);
			final View root = this.getLayoutInflater().inflate(R.layout.dlg_becode, null);
			ArrayAdapter<CharSequence> adapter10 = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, beCode);
			adapter10.setDropDownViewResource(R.layout.sst_simple_dropdown_item);
			Spinner spinner10 = (Spinner)root.findViewById(R.id.trade_10);
			spinner10.setAdapter(adapter10);
			spinner10.setSelection(mDefaultData.nCa1);
	
			ArrayAdapter<CharSequence> adapter11 = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, beCodeLR);
			adapter11.setDropDownViewResource(R.layout.sst_simple_dropdown_item);
			Spinner spinner11 = (Spinner)root.findViewById(R.id.trade_11);
			spinner11.setAdapter(adapter11);
			spinner11.setSelection(mDefaultData.nCa2);

			ArrayAdapter<CharSequence> adapter12 = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, beCodeNo);
			adapter12.setDropDownViewResource(R.layout.sst_simple_dropdown_item);
			Spinner spinner12 = (Spinner)root.findViewById(R.id.trade_12);
			spinner12.setAdapter(adapter12);
			spinner12.setSelection(mDefaultData.nCa3);

			ArrayAdapter<CharSequence> adapter20 = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, beCode);
			adapter20.setDropDownViewResource(R.layout.sst_simple_dropdown_item);
			Spinner spinner20 = (Spinner)root.findViewById(R.id.trade_20);
			spinner20.setAdapter(adapter20);
			spinner20.setSelection(mDefaultData.nCb1);
	
			ArrayAdapter<CharSequence> adapter21 = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, beCodeLR);
			adapter21.setDropDownViewResource(R.layout.sst_simple_dropdown_item);
			Spinner spinner21 = (Spinner)root.findViewById(R.id.trade_21);
			spinner21.setAdapter(adapter21);
			spinner21.setSelection(mDefaultData.nCb2);

			ArrayAdapter<CharSequence> adapter22 = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, beCodeNo);
			adapter22.setDropDownViewResource(R.layout.sst_simple_dropdown_item);
			Spinner spinner22 = (Spinner)root.findViewById(R.id.trade_22);
			spinner22.setAdapter(adapter22);
			spinner22.setSelection(mDefaultData.nCb3);

			ArrayAdapter<CharSequence> adapter30 = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, beCode);
			adapter30.setDropDownViewResource(R.layout.sst_simple_dropdown_item);
			Spinner spinner30 = (Spinner)root.findViewById(R.id.trade_30);
			spinner30.setAdapter(adapter30);
			spinner30.setSelection(mDefaultData.nCc1);
	
			ArrayAdapter<CharSequence> adapter31 = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, beCodeLR);
			adapter31.setDropDownViewResource(R.layout.sst_simple_dropdown_item);
			Spinner spinner31 = (Spinner)root.findViewById(R.id.trade_31);
			spinner31.setAdapter(adapter31);
			spinner31.setSelection(mDefaultData.nCc2);

			ArrayAdapter<CharSequence> adapter32 = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, beCodeNo);
			adapter32.setDropDownViewResource(R.layout.sst_simple_dropdown_item);
			Spinner spinner32 = (Spinner)root.findViewById(R.id.trade_32);
			spinner32.setAdapter(adapter32);
			spinner32.setSelection(mDefaultData.nCc3);

			ArrayAdapter<CharSequence> adapter40 = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, beCode);
			adapter40.setDropDownViewResource(R.layout.sst_simple_dropdown_item);
			Spinner spinner40 = (Spinner)root.findViewById(R.id.trade_40);
			spinner40.setAdapter(adapter40);
			spinner40.setSelection(mDefaultData.nCd1);
	
			ArrayAdapter<CharSequence> adapter41 = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, beCodeLR);
			adapter41.setDropDownViewResource(R.layout.sst_simple_dropdown_item);
			Spinner spinner41 = (Spinner)root.findViewById(R.id.trade_41);
			spinner41.setAdapter(adapter41);
			spinner41.setSelection(mDefaultData.nCd2);

			ArrayAdapter<CharSequence> adapter42 = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, beCodeNo);
			adapter42.setDropDownViewResource(R.layout.sst_simple_dropdown_item);
			Spinner spinner42 = (Spinner)root.findViewById(R.id.trade_42);
			spinner42.setAdapter(adapter42);
			spinner42.setSelection(mDefaultData.nCd3);
			
			dialogCode.setView(root);
			dialogCode.setTitle("暗碼設定組合表(1-4)");
			dialogCode.setPositiveButton("OK", new DialogInterface.OnClickListener() {
		          public void onClick(DialogInterface dialog, int which) {
		        	  Spinner spinner10 = (Spinner)root.findViewById(R.id.trade_10);
		        	  Spinner spinner11 = (Spinner)root.findViewById(R.id.trade_11);
		        	  Spinner spinner12 = (Spinner)root.findViewById(R.id.trade_12);
		        	  Spinner spinner20 = (Spinner)root.findViewById(R.id.trade_20);
		        	  Spinner spinner21 = (Spinner)root.findViewById(R.id.trade_21);
		        	  Spinner spinner22 = (Spinner)root.findViewById(R.id.trade_22);		        	  
		        	  Spinner spinner30 = (Spinner)root.findViewById(R.id.trade_30);
		        	  Spinner spinner31 = (Spinner)root.findViewById(R.id.trade_31);
		        	  Spinner spinner32 = (Spinner)root.findViewById(R.id.trade_32);
		        	  Spinner spinner40 = (Spinner)root.findViewById(R.id.trade_40);
		        	  Spinner spinner41 = (Spinner)root.findViewById(R.id.trade_41);
		        	  Spinner spinner42 = (Spinner)root.findViewById(R.id.trade_42);		        	  
		        	  mDefaultData.nCa1 = spinner10.getSelectedItemPosition();
		        	  mDefaultData.nCa2 = spinner11.getSelectedItemPosition();
		        	  mDefaultData.nCa3 = spinner12.getSelectedItemPosition();
		        	  mDefaultData.nCb1 = spinner20.getSelectedItemPosition();
		        	  mDefaultData.nCb2 = spinner21.getSelectedItemPosition();
		        	  mDefaultData.nCb3 = spinner22.getSelectedItemPosition();
		        	  mDefaultData.nCc1 = spinner30.getSelectedItemPosition();
		        	  mDefaultData.nCc2 = spinner31.getSelectedItemPosition();
		        	  mDefaultData.nCc3 = spinner32.getSelectedItemPosition();
		        	  mDefaultData.nCd1 = spinner40.getSelectedItemPosition();
		        	  mDefaultData.nCd2 = spinner41.getSelectedItemPosition();
		        	  mDefaultData.nCd3 = spinner42.getSelectedItemPosition();
		        	  SQLiteDatabase dbc = Utilis.getDB(SysSetup.this);		        	  
		  			Cursor csc = dbc.rawQuery("select * from qc_default", null);
					csc.moveToFirst();
						ContentValues value = new ContentValues();
						value.put("CA1",mDefaultData.nCa1);				
						value.put("CA2",mDefaultData.nCa2);
						value.put("CA3",mDefaultData.nCa3);
						value.put("CB1",mDefaultData.nCb1);				
						value.put("CB2",mDefaultData.nCb2);
						value.put("CB3",mDefaultData.nCb3);
						value.put("CC1",mDefaultData.nCc1);				
						value.put("CC2",mDefaultData.nCc2);
						value.put("CC3",mDefaultData.nCc3);				
						value.put("CD1",mDefaultData.nCd1);				
						value.put("CD2",mDefaultData.nCd2);
						value.put("CD3",mDefaultData.nCd3);				
						long result = dbc.update(TbName.QC_DEFAULT, value, "", null);
					csc.close();
					dbc.close();
		          }
			});
			
			dialogCode.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
		          public void onClick(DialogInterface dialog, int which) {
		          }
				});
			dialogCode.show();
		 
	 }
}