package com.example.chemical.ui.adapter

import android.content.Context
import android.view.View
import android.widget.AdapterView
import android.widget.Spinner
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.chemical.R
import com.example.chemical.utils.ConversionUtils
import com.rc.httpcore.bean.ChemistryBean

//领用列表
class UsageLabelDialogAdapter(private val ct: Context) :
    BaseQuickAdapter<ChemistryBean, BaseViewHolder>(R.layout.item_usage_label_dialog) {

    init {
        addChildClickViewIds(R.id.imgDelete)
    }
    private val spinnerArray = listOf("否", "是")
    override fun convert(holder: BaseViewHolder, item: ChemistryBean) {
        holder.setText(R.id.chemicalName, "${item.chemicalName}")
        holder.setText(R.id.code, "${item.code}")
        if (item.weigh != null) {
            when {
                item.specUnit.equals("g") -> {
                    holder.setText(
                        R.id.surplus,
                        "${item.weigh!!.subtract(item.packNum)}${item.specUnit}"
                    )
                }
                item.specUnit.equals("ml") -> {
                    val subtract = item.weigh!!.subtract(item.packNum)
                    val gramsToMilliliters =
                        ConversionUtils.gramsToMilliliters(subtract, item.chemicalDensity)
                    holder.setText(R.id.surplus, "$gramsToMilliliters${item.specUnit}")
                }
                else -> {
                    holder.setText(R.id.surplus, "${item.surplus}${item.specUnit}")
                }
            }
        } else {
            holder.setText(R.id.surplus, "${item.surplus}${item.specUnit}")
        }

        holder.setText(R.id.spec, "${item.spec}")
//        holder.setText(R.id.surplus, "${item.weigh}${item.specUnit}")
        holder.setText(R.id.duration, "${item.duration}")
        val sPinner = holder.getView<Spinner>(R.id.sPinner)
        val adapter = CustomSpinnerFoutAdapter(ct,spinnerArray)
        sPinner.adapter = adapter
        sPinner.setSelection(0)
        sPinner.onItemSelectedListener =
            object : AdapterView.OnItemSelectedListener {
                override fun onItemSelected(
                    parent: AdapterView<*>?,
                    view: View?,
                    position: Int,
                    id: Long
                ) {
                    // 处理选项选择事件
                    val selectedItem = parent?.getItemAtPosition(position).toString()
                    item.allBottle = selectedItem != "否"
                }

                override fun onNothingSelected(parent: AdapterView<*>?) {
                    // 处理未选择任何选项情况
                }
            }
    }
}