package com.example.tabbuy.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.tabbuy.data.local.entity.PelangganEntity
import com.example.tabbuy.databinding.ItemPelangganBinding

class PelangganAdapter(
    private val onDeleteClick: (PelangganEntity) -> Unit
) : RecyclerView.Adapter<PelangganAdapter.PelangganViewHolder>() {

    private var pelangganList = emptyList<PelangganEntity>()

    class PelangganViewHolder(val binding: ItemPelangganBinding) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PelangganViewHolder {
        val binding = ItemPelangganBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return PelangganViewHolder(binding)
    }

    override fun onBindViewHolder(holder: PelangganViewHolder, position: Int) {
        val currentPelanggan = pelangganList[position]
        holder.binding.tvNamaPelanggan.text = currentPelanggan.nama
        holder.binding.tvEmailPelanggan.text = currentPelanggan.email

        holder.binding.btnDeletePelanggan.setOnClickListener {
            onDeleteClick(currentPelanggan)
        }
    }

    override fun getItemCount() = pelangganList.size

    fun setData(pelanggan: List<PelangganEntity>) {
        this.pelangganList = pelanggan
        notifyDataSetChanged()
    }
}
