package com.cwbuyer.code;

import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.cwbuyer.format.EMode;
import com.cwbuyer.lib.DBCloud;
import com.cwbuyer.lib.TbName;
import com.cwbuyer.lib.Utilis;
import com.pwbuyer.main.R;

public class AddSize  extends Activity{
	
	private int mMode = 0;
	private int mID = 0;
	@SuppressWarnings("unused")
	private String mSize = null;
	
		//	0單機 1On_line 2.Off_line
	int of_line=Utilis.toInt(Utilis.getIni(this, "SYS", "IMPORT", 3));
	String mUid=Utilis.getIni(this, "SYS", "IMPORT", 2);  //ex:3g001
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_size);
		setTitle("商品尺寸組合編輯");
		String size = getIntent().getStringExtra("size");
		int id = getIntent().getIntExtra("id", 0);
		mMode = getIntent().getIntExtra("mode", EMode.ADD);
		if(mMode == EMode.EDIT){
			if(size != null && size.length() > 0){
				mSize = size;
				mID = id;
				((EditText)findViewById(R.id.edit_size)).setText(size);
			}
		}
		((Button)findViewById(R.id.btn_save)).setOnClickListener(new MenuClick());
		((Button)findViewById(R.id.btn_exit)).setOnClickListener(new MenuClick());
	}
	class MenuClick implements OnClickListener{
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			int id = v.getId();
			Utilis.runVibrate(AddSize.this);
			switch(id){
			case R.id.btn_save:
				if(saveData() == true){
					finish();
				}
				break;
			case R.id.btn_exit:
				finish();
				break;
			}
		}
	}
	
	private boolean saveData(){
		String size = ((EditText)findViewById(R.id.edit_size)).getText().toString();
		boolean succed=true;
		
		if(size == null || size.length() <= 0 ){
			Toast.makeText(this, "請輸入尺寸", Toast.LENGTH_SHORT).show();
			return false;
		}
		
		String chkSize = DBCloud.chkStr(size.replace("，", ","));

		ContentValues value = new ContentValues();
    	value.put("SIZES", chkSize);
    	
		if (Utilis.haveInternet(this) && of_line!=0){  //有網路的狀態且非單機,優先雲端運算,再行Hander維護Client端
			StringBuffer bfstr = new StringBuffer();
			StringBuffer bfstrF = new StringBuffer();
					bfstr.append(mUid).append(";");
					bfstrF.append("UID").append(";");
					bfstr.append(mID).append(";");
					bfstrF.append("_ID").append(";");
					bfstr.append(chkSize);
					bfstrF.append("SIZES");
				Toast.makeText(this, "正前往雲端編輯_商品類別中,請稍候....", Toast.LENGTH_SHORT).show();
				Thread t = new Thread(new DBCloud.sendPostRunnableQC(this,bfstr.toString(),bfstrF.toString(),4));
				t.start();
    	}else if(mMode == EMode.ADD){
    		SQLiteDatabase db = Utilis.getDB(this);
	    	long result = db.insert(TbName.QC_SIZE, null, value);
	    	if(result > 0){
	    		Toast.makeText(this, "存取資料成功", Toast.LENGTH_SHORT).show();
	    	}else{
	    		Toast.makeText(this, "存取資料失敗", Toast.LENGTH_SHORT).show();
	    		succed =false;
	    	}
	    	db.close();
    	}else{  // EDIT
    		SQLiteDatabase db = Utilis.getDB(this);
    		String[] args = {String.valueOf(mID)};
			int success = db.update(com.cwbuyer.lib.TbName.QC_SIZE, value,
					"_ID=?", args);
			if(success > 0){
				Toast.makeText(this, "修改資料成功", Toast.LENGTH_SHORT).show();
			}else{
				Toast.makeText(this, "修改資料失敗", Toast.LENGTH_SHORT).show();
	    		succed =false;
			}
			db.close();
    	}
		value.clear();
		return succed;

	}
}	
