package com.lukouguoji.mit.adapt

import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.lukouguoji.mit.R
import com.lukouguoji.mit.model.AccidentVisaModel
import com.lukouguoji.mit.viewModel.AccidentVisaViewModel
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.router.ARouterConstants
import com.lukouguoji.module_base.util.Common

class AccidentVisaAdapter(
    val activity: Activity,
    private val viewModel: AccidentVisaViewModel,
    private val collectList: MutableList<AccidentVisaModel>
) :
    RecyclerView.Adapter<AccidentVisaAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val wbNo: TextView = view.findViewById(R.id.wbNo)
        val areaFlag: TextView = view.findViewById(R.id.areaFlag)
        val ieFlag: TextView = view.findViewById(R.id.ieFlag)
        val dpc: TextView = view.findViewById(R.id.dpc)
        val opId: TextView = view.findViewById(R.id.opId)
        val opDate: TextView = view.findViewById(R.id.opDate)

        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.accident_visa_list_item, parent, false)
        val viewHolder = ViewHolder(view)

        viewHolder.itemView.setOnClickListener {

            var adapterPosition = viewHolder.adapterPosition
            if (adapterPosition < 0 || collectList.isEmpty()) {
                return@setOnClickListener
            }
            var item = collectList[adapterPosition]

            ARouter.getInstance().build(ARouterConstants.ACTIVITY_URL_MONITOR_ACCIDENT_VISA_INFO)
                .withInt("id", item.id)
                .navigation(activity,Constant.RequestCode.accident_visa_list_query_result)

        }
        return viewHolder
    }

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

        holder.wbNo.text = collect.wbNo
        holder.areaFlag.text = collect.areaFlag
        holder.ieFlag.text = collect.ieFlag
        holder.dpc.text = collect.dpc
        holder.opId.text = collect.opId
        holder.opDate.text = collect.opDate

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

    override fun getItemCount() = collectList.size

    fun append(accidentVisaModel: AccidentVisaModel) {
        collectList.add(accidentVisaModel)
        notifyDataSetChanged()
    }

}