220 lines
8.1 KiB
Java
220 lines
8.1 KiB
Java
package com.example.pap_teste;
|
|
|
|
import android.os.Bundle;
|
|
import android.widget.ArrayAdapter;
|
|
import android.widget.Button;
|
|
import android.widget.ListView;
|
|
import android.widget.TextView;
|
|
import android.widget.Toast;
|
|
|
|
import androidx.activity.EdgeToEdge;
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
import androidx.core.graphics.Insets;
|
|
import androidx.core.view.ViewCompat;
|
|
import androidx.core.view.WindowInsetsCompat;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class DetalhesReservasActivity extends AppCompatActivity {
|
|
|
|
private final List<ReservaItem> reservas = new ArrayList<>();
|
|
private ArrayAdapter<String> adapter;
|
|
private ListView listReservas;
|
|
private TextView txtInfo;
|
|
private TextView txtNotas;
|
|
private TextView txtEstado;
|
|
private TextView txtMensagem;
|
|
private Button btnConfirmar, btnRecusar, btnApagar;
|
|
private int selectedIndex = -1;
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
EdgeToEdge.enable(this);
|
|
setContentView(R.layout.activity_detalhes_reservas);
|
|
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.detalhesReservasRoot), (v, insets) -> {
|
|
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
|
|
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
|
|
return insets;
|
|
});
|
|
|
|
bindViews();
|
|
seedReservasDemo();
|
|
setupList();
|
|
setupActions();
|
|
}
|
|
|
|
private void bindViews() {
|
|
listReservas = findViewById(R.id.listReservas);
|
|
txtInfo = findViewById(R.id.txtReservaInfo);
|
|
txtNotas = findViewById(R.id.txtReservaNotas);
|
|
txtEstado = findViewById(R.id.txtReservaEstado);
|
|
txtMensagem = findViewById(R.id.txtMensagemReserva);
|
|
|
|
btnConfirmar = findViewById(R.id.btnConfirmarReserva);
|
|
btnRecusar = findViewById(R.id.btnCancelarReserva);
|
|
btnApagar = findViewById(R.id.btnApagarReserva);
|
|
|
|
Button back = findViewById(R.id.btnVoltar);
|
|
if (back != null) {
|
|
back.setOnClickListener(v -> finish());
|
|
}
|
|
}
|
|
|
|
private void seedReservasDemo() {
|
|
reservas.add(new ReservaItem("Ana Ribeiro", "Mesa 12", "20h00", 4, "Aniversário", "Confirmada"));
|
|
reservas.add(new ReservaItem("Bruno Costa", "Mesa 03", "21h15", 2, "Preferência por janela", "Pendente"));
|
|
reservas.add(new ReservaItem("Carla Silva", "Mesa 07", "19h30", 3, "Levar bolo para a mesa", "Pendente"));
|
|
reservas.add(new ReservaItem("Duarte Neves", "Mesa 01", "22h00", 2, "", "Concluída"));
|
|
}
|
|
|
|
private void setupList() {
|
|
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_activated_1);
|
|
listReservas.setAdapter(adapter);
|
|
refreshList();
|
|
|
|
listReservas.setOnItemClickListener((parent, view, position, id) -> {
|
|
selectedIndex = position;
|
|
mostrarDetalhe(reservas.get(position));
|
|
});
|
|
}
|
|
|
|
private void setupActions() {
|
|
if (btnConfirmar != null) {
|
|
btnConfirmar.setOnClickListener(v -> {
|
|
ReservaItem item = reservas.get(selectedIndex);
|
|
if ("Pendente".equals(item.estado)) {
|
|
atualizarEstadoSelecionado("Confirmada");
|
|
} else if ("Confirmada".equals(item.estado)) {
|
|
atualizarEstadoSelecionado("Concluída");
|
|
}
|
|
});
|
|
}
|
|
|
|
if (btnRecusar != null) {
|
|
btnRecusar.setOnClickListener(v -> showRecusarDialog());
|
|
}
|
|
|
|
if (btnApagar != null) {
|
|
btnApagar.setOnClickListener(v -> apagarReserva());
|
|
}
|
|
}
|
|
|
|
private void showRecusarDialog() {
|
|
if (selectedIndex < 0)
|
|
return;
|
|
|
|
String[] motivos = { "Sem espaço no restaurante", "Fora de horas", "Reserva duplicada", "Outro" };
|
|
new androidx.appcompat.app.AlertDialog.Builder(this)
|
|
.setTitle("Motivo da Recusa")
|
|
.setItems(motivos, (dialog, which) -> {
|
|
atualizarEstadoSelecionado("Recusada (" + motivos[which] + ")");
|
|
})
|
|
.setNegativeButton("Voltar", null)
|
|
.show();
|
|
}
|
|
|
|
private void apagarReserva() {
|
|
if (selectedIndex < 0)
|
|
return;
|
|
|
|
new androidx.appcompat.app.AlertDialog.Builder(this)
|
|
.setTitle("Apagar Reserva")
|
|
.setMessage("Tem a certeza que deseja apagar esta reserva?")
|
|
.setPositiveButton("Apagar", (dialog, which) -> {
|
|
reservas.remove(selectedIndex);
|
|
selectedIndex = -1;
|
|
txtInfo.setText("Selecione uma reserva");
|
|
txtNotas.setText("");
|
|
txtEstado.setText("Estado:");
|
|
txtMensagem.setText("Reserva apagada com sucesso.");
|
|
toggleButtons(null);
|
|
refreshList();
|
|
})
|
|
.setNegativeButton("Voltar", null)
|
|
.show();
|
|
}
|
|
|
|
private void atualizarEstadoSelecionado(String novoEstado) {
|
|
if (selectedIndex < 0 || selectedIndex >= reservas.size()) {
|
|
Toast.makeText(this, "Selecione uma reserva para atualizar.", Toast.LENGTH_SHORT).show();
|
|
return;
|
|
}
|
|
|
|
ReservaItem item = reservas.get(selectedIndex);
|
|
item.estado = novoEstado;
|
|
txtMensagem.setText(String.format("Reserva de %s marcada como %s.", item.nomeCliente, novoEstado));
|
|
mostrarDetalhe(item);
|
|
refreshList();
|
|
}
|
|
|
|
private void mostrarDetalhe(ReservaItem item) {
|
|
txtInfo.setText(String.format("%s • %s • %s • %dp", item.nomeCliente, item.mesa, item.hora, item.pessoas));
|
|
txtNotas.setText(item.notas);
|
|
txtEstado.setText(String.format("Estado: %s", item.estado));
|
|
toggleButtons(item);
|
|
}
|
|
|
|
private void toggleButtons(ReservaItem item) {
|
|
if (item == null) {
|
|
btnConfirmar.setVisibility(android.view.View.GONE);
|
|
btnRecusar.setVisibility(android.view.View.GONE);
|
|
btnApagar.setVisibility(android.view.View.GONE);
|
|
return;
|
|
}
|
|
|
|
switch (item.estado) {
|
|
case "Pendente":
|
|
btnConfirmar.setText("Confirmar");
|
|
btnConfirmar.setVisibility(android.view.View.VISIBLE);
|
|
btnRecusar.setVisibility(android.view.View.VISIBLE);
|
|
btnApagar.setVisibility(android.view.View.GONE);
|
|
break;
|
|
case "Confirmada":
|
|
btnConfirmar.setText("Concluir");
|
|
btnConfirmar.setVisibility(android.view.View.VISIBLE);
|
|
btnRecusar.setVisibility(android.view.View.VISIBLE); // Still allow refusal
|
|
btnApagar.setVisibility(android.view.View.GONE);
|
|
break;
|
|
case "Concluída":
|
|
btnConfirmar.setVisibility(android.view.View.GONE);
|
|
btnRecusar.setVisibility(android.view.View.GONE);
|
|
btnApagar.setVisibility(android.view.View.VISIBLE);
|
|
break;
|
|
default: // Recusada or Cancelada
|
|
btnConfirmar.setVisibility(android.view.View.GONE);
|
|
btnRecusar.setVisibility(android.view.View.GONE);
|
|
btnApagar.setVisibility(android.view.View.VISIBLE); // Allow deleting refused ones as well
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void refreshList() {
|
|
adapter.clear();
|
|
for (ReservaItem item : reservas) {
|
|
String resumo = String.format("%s - %s (%s) • %s", item.hora, item.mesa, item.estado, item.nomeCliente);
|
|
adapter.add(resumo);
|
|
}
|
|
adapter.notifyDataSetChanged();
|
|
}
|
|
|
|
private static class ReservaItem {
|
|
String nomeCliente;
|
|
String mesa;
|
|
String hora;
|
|
int pessoas;
|
|
String notas;
|
|
String estado;
|
|
|
|
ReservaItem(String nomeCliente, String mesa, String hora, int pessoas, String notas, String estado) {
|
|
this.nomeCliente = nomeCliente;
|
|
this.mesa = mesa;
|
|
this.hora = hora;
|
|
this.pessoas = pessoas;
|
|
this.notas = notas;
|
|
this.estado = estado;
|
|
}
|
|
}
|
|
}
|