package com.b2lmobitech.fieldcloudhrms

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class TravelAdapter(private val list: List<TravelModel>) :
    RecyclerView.Adapter<TravelAdapter.ViewHolder>() {

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val travelId: TextView = view.findViewById(R.id.txtTravelId)
        val txtPurpose: TextView = view.findViewById(R.id.txtPurpose)
        val txtLocation: TextView = view.findViewById(R.id.txtLocation)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_travel, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = list[position]
        holder.travelId.text = item.travel_id
        holder.txtPurpose.text = item.purpose
        holder.txtLocation.text = item.location

        holder.itemView.setOnClickListener {
            val ctx = holder.itemView.context
            val intent = Intent(ctx, TravelDetailsActivity::class.java)
            intent.putExtra(TravelDetailsActivity.EXTRA_TRAVEL, item)
            ctx.startActivity(intent)
        }
    }

    override fun getItemCount() = list.size
}
