package com.example.listviewcustom05;


import android.app.Activity;
import android.app.AlertDialog;

// https://www.simplifiedcoding.net/android-sqlite-database-tutorial-insert-into-sqlite-db/

//https://www.simplifiedcoding.net/sqlite-database-in-android-using-sqliteopenhelper-class/ 

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
 
public class Insertion extends Activity 
{
    private EditText editText_nom;
    private EditText editText_prenom;
    private EditText editText_adresse;
    private EditText editText_email;
    private EditText editText_portable;
    private EditText editText_fixe;
    
    private Button btnInsertion;
    private Button btnView;
 
    private SQLiteDatabase db;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.insertion);
 
       // createDatabase();
        openDatabase();
 
        editText_nom = (EditText) findViewById(R.id.nom);
        editText_prenom = (EditText) findViewById(R.id.prenom);
        
        editText_adresse = (EditText) findViewById(R.id.adresse);
        editText_email = (EditText) findViewById(R.id.email);
        
        editText_portable = (EditText) findViewById(R.id.portable);
        editText_fixe = (EditText) findViewById(R.id.fixe);
 
        btnInsertion = (Button) findViewById(R.id.btnAdd);
        btnView = (Button) findViewById(R.id.btnView);
 
      //  btnAdd.setOnClickListener(this);
      //  btnView.setOnClickListener(this);
        
        btnInsertion.setOnClickListener(new OnClickListener()
        {

			@Override
			public void onClick(View arg0)
			{
				insertIntoDB();
			}

		});
        
    }
 
 
    protected void openDatabase()
	 {
	        db = openOrCreateDatabase("pat06", Context.MODE_PRIVATE, null);
	 }
    
    protected void createDatabase()
    {
        db=openOrCreateDatabase("PersonDB", Context.MODE_PRIVATE, null);
        //db.execSQL("CREATE TABLE IF NOT EXISTS persons(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR,address VARCHAR);");
    }
 
    protected void insertIntoDB()
    {
        String nom = editText_nom.getText().toString().trim();
        String prenom = editText_prenom.getText().toString().trim();
        String adresse = editText_adresse.getText().toString().trim();
        String email = editText_email.getText().toString().trim();
        String portable = editText_portable.getText().toString().trim();
        String fixe = editText_fixe.getText().toString().trim();

        /*if(name.equals("") || add.equals("")){
            Toast.makeText(getApplicationContext(),"Please fill all fields", Toast.LENGTH_LONG).show();
            return;
        }*/
 
        String query = "INSERT INTO contact (nom,prenom,adresse,email,portable,fixe) VALUES('"+nom+"', '"+prenom+"'  ,  '"+adresse+"', '"+email+"' ,   '"+portable+"', '"+fixe+"' );";
        
        //String query = "INSERT INTO persons (name,address) VALUES('"+name+"', '"+add+"');";
        db.execSQL(query);
        Toast.makeText(getApplicationContext(),"Saved Successfully", Toast.LENGTH_LONG).show();
        
        
       // db.execSQL("INSERT INTO student VALUES('"+editRollno.getText()+"','"+
       // 		editName.getText()+"','"+editMarks.getText()+"');");

    }
 
    
    protected void saveRecord() 
    {
        String name = editText_nom.getText().toString().trim();
        String add = editText_prenom.getText().toString().trim();
        String id = "5";

        String sql = "UPDATE persons SET name='" + name + "', address='" + add + "' WHERE id=" + id + ";";

        if (name.equals("") || add.equals("")) {
            Toast.makeText(getApplicationContext(), "You cannot save blank values", Toast.LENGTH_LONG).show();
            return;
        }

        db.execSQL(sql);
        Toast.makeText(getApplicationContext(), "Records Saved Successfully", Toast.LENGTH_LONG).show();
     
        //db.execSQL("UPDATE student SET name='"+editName.getText()+"',marks='"+
        //		editMarks.getText()+"' WHERE rollno='"+editRollno.getText()+"'");


        
        
    }
    
    private void deleteRecord() 
    {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setMessage("Are you sure you want delete this person?");

        alertDialogBuilder.setPositiveButton("Yes",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1)
                    {
                        String id = "5";
                        String sql = "DELETE FROM persons WHERE id=" + id + ";";
                        db.execSQL(sql);
                        Toast.makeText(getApplicationContext(), "Record Deleted", Toast.LENGTH_LONG).show();
                        
                     //   db.execSQL("DELETE FROM student WHERE rollno='"+editRollno.getText()+"'");


                       
                    }
                });

        alertDialogBuilder.setNegativeButton("No",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1)
                    {

                    }
                });


        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();

    }
 
    
    
    
   // Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+editRollno.getText()+"'", null);
   // if(c.moveToFirst())
   // {
   //     editName.setText(c.getString(1));
   //     editMarks.setText(c.getString(2));
   // }
    
}