package com.lukouguoji.mit.adapt

import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.lukouguoji.mit.R
import com.lukouguoji.mit.model.MitInPackListModel
import com.lukouguoji.mit.viewModel.MitInPackListViewModel
import com.lukouguoji.module_base.util.Common

class MitInPackListAdapter(
    val activity: Activity,
    private val viewModel: MitInPackListViewModel,
    private val collectList: MutableList<MitInPackListModel>
) :
    RecyclerView.Adapter<MitInPackListAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val leftIcon: ImageView = view.findViewById(R.id.leftIcon)
        val carId: TextView = view.findViewById(R.id.carId)
        val flight: TextView = view.findViewById(R.id.flight)
        val countryType: TextView = view.findViewById(R.id.countryType)
        val opDate: TextView = view.findViewById(R.id.opDate)
        val opId: TextView = view.findViewById(R.id.opId)

        val delete: TextView = view.findViewById(R.id.delete)
    }

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

        viewHolder.itemView.isClickable = true

        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val collect = collectList[position]

        holder.carId.text = collect.carId
        holder.flight.text = collect.flight
        holder.countryType.text = collect.countryType
        holder.opDate.text = collect.opDate
        holder.opId.text = collect.opId

        holder.delete.setOnClickListener {
            Common.secondConfirmDialog(activity, "确认删除?") { dialog ->
                dialog.dismiss()
                viewModel.delete(collect.id, collect.carId)
            }
        }
    }

    override fun getItemCount() = collectList.size

    fun append(mitInPackListModel: MitInPackListModel) {
        collectList.add(mitInPackListModel)
        notifyDataSetChanged()
    }

}