package com.b2lmobitech.fieldcloudhrms

import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.b2lmobitech.fieldcloudhrms.databinding.ItemTimesheetBinding

class TimesheetAdapter(
    private val list: List<TimesheetModel>
) : RecyclerView.Adapter<TimesheetAdapter.TimesheetVH>() {

    inner class TimesheetVH(val binding: ItemTimesheetBinding)
        : RecyclerView.ViewHolder(binding.root)

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

    override fun onBindViewHolder(holder: TimesheetVH, position: Int) {
        val item = list[position]

        holder.binding.txtTitle.text = item.title
        holder.binding.txtTimeRange.text = "${item.startTime} to ${item.endTime}"
        holder.binding.txtHours.text = item.totalHours
        holder.binding.txtDescription.text = item.description

        holder.itemView.setOnClickListener {
            // optional: open detailed timesheet screen
            val ctx = holder.itemView.context
            val intent = Intent(ctx, TimesheetDetailsActivity::class.java)
            intent.putExtra("timesheet_id", item.id)
            ctx.startActivity(intent)
        }
    }

    override fun getItemCount() = list.size
}
