package net.kerul.fstmdirektori;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class UpdateRecord extends Activity {
	private EditText txtnama, txtjbt, txttelefon, txtemel;
	private Button btnsave;
	private DBHelper mHelper;
	private SQLiteDatabase dataBase;
	private String id,name,telno,email,dept;//to hold the data strings

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_update_record);
		
		mHelper=new DBHelper(this);
		dataBase = mHelper.getWritableDatabase();
		
		//capture sent parameter from previous screen
		id=getIntent().getExtras().getString("stafid");
		
		//initiate all textbox container to hold the data for the staff
		txtnama=(EditText)findViewById(R.id.txtnama);
		txtjbt=(EditText)findViewById(R.id.txtjbt);
		txttelefon=(EditText)findViewById(R.id.txttelefon);
		txtemel=(EditText)findViewById(R.id.txtemel);
		
		//display field record inside the textboxes
		displayData();
		
		btnsave=(Button)findViewById(R.id.btnsave);
		btnsave.setOnClickListener( new OnClickListener(){
			public void onClick(View v){
				//capture amendment
				name=txtnama.getText().toString();
				telno=txttelefon.getText().toString();
				email=txtemel.getText().toString();
				dept=txtjbt.getText().toString();
				
				saveData();
			}
		});//end btnsave setOnCLickCListener
	}
	
	//display single record of data from stafid
	private void displayData() {
		
		//the SQL command to fetched all records from the table
		String sql="SELECT * FROM "
				+ DBHelper.TABLE_NAME +" WHERE stafid='"+id+"';";
		Cursor mCursor = dataBase.rawQuery(sql, null);
		
		//fetch the record
		if (mCursor.moveToFirst()) {
			//fetch each field and transfer to textbox
			telno=mCursor.getString(mCursor.getColumnIndex(DBHelper.TELEFON));
			email=mCursor.getString(mCursor.getColumnIndex(DBHelper.EMEL));
			name=mCursor.getString(mCursor.getColumnIndex(DBHelper.NAMA));
			//get data from field and transfer to EditText
			txtnama.setText(name);
			txtjbt.setText(mCursor.getString(mCursor.getColumnIndex(DBHelper.JBT)));
			txttelefon.setText(telno);
			txtemel.setText(email);
				
		}
		else{
			//do something here if no record fetched from database
			txtnama.setText(sql);
		}
	}//end displayData
	
	
	//save updated data
	private void saveData(){
		//dataBase=mHelper.getWritableDatabase();
		ContentValues values=new ContentValues();
		
		values.put(DBHelper.NAMA,name);
		values.put(DBHelper.EMEL,email );
		values.put(DBHelper.TELEFON,telno );
		values.put(DBHelper.JBT,dept);
		
		System.out.println("");
		
		//update database with new data 
		dataBase.update(DBHelper.TABLE_NAME, values, DBHelper.STAFID+"="+id, null);

		//close database
		dataBase.close();
		finish();
		
		
	}

}
