package com.quansu.heifengwuliu.fragment

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.TextView.OnEditorActionListener
import com.quansu.heifengwuliu.adapter.AddressAdapter
import com.quansu.heifengwuliu.base.MBRFragment
import com.quansu.heifengwuliu.databinding.FragmentAddressBinding
import com.quansu.heifengwuliu.utils.net.NetEngine
import com.ysnows.base.base.BRRepository
import com.ysnows.base.base.BRViewModel
import com.ysnows.base.net.IResponse
import io.reactivex.Observable

//发货 -卸货地址
class AddressFragment : MBRFragment<BRViewModel<BRRepository>, AddressAdapter, FragmentAddressBinding>() {

    var type:String=""
    var k:String=""
    override fun __before(savedInstanceState: Bundle?) {
        super.__before(savedInstanceState)
        val arguments = arguments
        if(null!=arguments) type=arguments.getString("type")

    }

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        binding.sidebar.attachTipView(binding.tvTip)


    }


    override fun listeners() {
        super.listeners()

//        binding.sidebar.setOnSelectedListener({ position, text ->
//            if (adapter()!= null && adapter().size() > 0) {
//                for (i in 0 until adapter()) {
//                    val client: CustomerBean = adapter.getData().get(i) as CustomerBean
//                    if (text.equals(client.py)) {
//                        (binding.recyclerView.getLayoutManager() as LinearLayoutManager).scrollToPositionWithOffset(i, 0)
//                    } else {
//                    }
//                }
//            }
//        })


        binding.ivDelete.setOnClickListener {
            if (!TextUtils.isEmpty(binding.etName.text.toString().trim { it <= ' ' })) {
                binding.etName.text = null
                onRefresh()
            } else {
                toast( "已经删除搜索条件！")
            }
        }

        binding.etName.setOnEditorActionListener(OnEditorActionListener { textView, i, keyEvent ->
            if (i == EditorInfo.IME_ACTION_SEARCH) {
                k = binding.etName.text.toString().trim()
                onRefresh()
                return@OnEditorActionListener true
            }
            false
        })

        binding.tvSearch.setOnClickListener { t ->
            k = binding.etName.text.toString().trim()
            onRefresh()

        }

    }

    override fun initAdapter(): AddressAdapter {
        return AddressAdapter()
    }

    override fun api(): Observable<out IResponse<*>> {
        return NetEngine.service.addressList(type,k, vm?.repository()?.autoPage()!!)
    }

    override fun binding(inflater: LayoutInflater, container: ViewGroup?): FragmentAddressBinding {
        return FragmentAddressBinding.inflate(layoutInflater, container, false)
    }

    override fun title(): String? {
        return null
    }
}
