package com.lukouguoji.gjj.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.gjj.R
import com.lukouguoji.gjj.model.GjjChuKuListModel
import com.lukouguoji.gjj.viewModel.GjjChuKuListViewModel

class GjjChuKuListAdapter(
    val activity: Activity,
    private val viewModel: GjjChuKuListViewModel,
    private val collectList: MutableList<GjjChuKuListModel>
) :
    RecyclerView.Adapter<GjjChuKuListAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val leftIcon: ImageView = view.findViewById(R.id.leftIcon)
        val wbNo: TextView = view.findViewById(R.id.wbNo)
        val pickNo: TextView = view.findViewById(R.id.pickNo)
        val fno: TextView = view.findViewById(R.id.fno)
        val pc: TextView = view.findViewById(R.id.pc)
        val weight: TextView = view.findViewById(R.id.weight)

        val goods: TextView = view.findViewById(R.id.goods)
        val agentCode: TextView = view.findViewById(R.id.agentCode)
        val spCode: TextView = view.findViewById(R.id.spCode)
        val pickDate: TextView = view.findViewById(R.id.pickDate)

    }

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

        return viewHolder
    }

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

        val leftIcon = holder.leftIcon
        //选中
        if (collect.isCheck) {
            leftIcon.setImageResource(R.mipmap.gjj_chu_ku_left_icon_check)
        } else {
            leftIcon.setImageResource(R.mipmap.gnc_ware_house_left_icon)
        }

        holder.wbNo.text = collect.wbNo
        holder.pickNo.text = collect.pickNo.toString()
        holder.fno.text = collect.fno
        holder.pc.text = "${collect.pc}"
        holder.weight.text = "${collect.weight} KG"

        holder.goods.text = collect.goods
        holder.agentCode.text = collect.agentCode
        holder.spCode.text = collect.spCode
        holder.pickDate.text = collect.pickDate

        leftIcon.setOnClickListener {
            collect.isCheck = !collect.isCheck
            if (collect.isCheck) {
                viewModel.plusOne()
            } else {
                viewModel.subOne()
            }
            notifyDataSetChanged() // 更新数据
        }

    }

    override fun getItemCount() = collectList.size

    fun append(gjjChuKuListModel: GjjChuKuListModel) {
        collectList.add(gjjChuKuListModel)
        notifyDataSetChanged()
    }

}