package com.example.depanntout;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import bdd.ClientDAO;

public class GestionClient extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gestion_client);
        initialisation();
        afficherLesClients();
    }

    private void initialisation() {
        // gestion du boutton Quitter
        Button btQuitterClient = (Button) findViewById(R.id.btQuitterInter);
        // Association d'un écouteur d'événement au clic de btQuitter
        btQuitterClient.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                finish();
            }
        });

        // gestion du boutton Gestion des clients
        Button btCreerClient = (Button) findViewById(R.id.btCreerInter);
        btCreerClient.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                demarrerCreerClient();
            }
        });
    }

    private void demarrerCreerClient(){
        Intent intent = new Intent(GestionClient.this, CreateClient.class);
        startActivity(intent);
    }

    public void afficherLesClients(){
        Log.d("bdd", "Début de afficherLesClients");
        ClientDAO clientDAO = new ClientDAO(this);
        Cursor c = clientDAO.readLesClients();
        Toast.makeText(getApplicationContext(), "Il y a +" +
                c.getCount() + "articles", Toast.LENGTH_SHORT).show();
        String[] from = new String[]{"_id", "nom", "prenom", "numTel", "adrMail", "adrPostale"};
        int[] to = new int[] {R.id.tvIdClient, R.id.tvNomClient, R.id.tvDateInter, R.id.tvHeureInter, R.id.tvObervInter, R.id.tvIdClientInter};
        SimpleCursorAdapter dataAdapter = new SimpleCursorAdapter(this, R.layout.ligne_client3, c, from, to, 0);
        ListView lvClients = (ListView) findViewById(R.id.lvClients);
        lvClients.setAdapter(dataAdapter);

        // Set an item click listener for the ListView
        lvClients.setOnItemClickListener((parent, view, position, id) -> {
            // Move the Cursor to the clicked item's position
            c.moveToPosition(position);

            // Extract details from the cursor
            String date = c.getString(c.getColumnIndexOrThrow("date"));
            String heure = c.getString(c.getColumnIndexOrThrow("heure"));
            String observation = c.getString(c.getColumnIndexOrThrow("observation"));
            int idCli = c.getInt(c.getColumnIndexOrThrow("idCli"));

            // Pass the details to the new activity using Intent
            Intent intent = new Intent(this, ModifInter.class);
            intent.putExtra("idCli", idCli);
            intent.putExtra("date", date);
            intent.putExtra("heure", heure);
            intent.putExtra("observation", observation);
            startActivity(intent);
        });
    }
}