package com.quansu.heifengwuliu.activity

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import com.google.gson.Gson
import com.quansu.heifengwuliu.R
import com.quansu.heifengwuliu.adapter.CollectionAdapter
import com.quansu.heifengwuliu.base.MBRActivity
import com.quansu.heifengwuliu.databinding.ActivityCollectionBinding
import com.quansu.heifengwuliu.model.DriverBean
import com.quansu.heifengwuliu.model.SaveDriverBean
import com.quansu.heifengwuliu.utils.net.NetEngine
import com.quansu.heifengwuliu.vmodel.CollectionVModel
import com.ysnows.base.net.IResponse
import io.reactivex.Observable

class AllCarActivity : MBRActivity<CollectionVModel, CollectionAdapter, ActivityCollectionBinding>() {

    var k:String=""

    override fun initView(view: View?) {
        super.initView(view)
        binding.butSure.text="确定"
    }


    override fun initCreate(savedInstanceState: Bundle?) {
        super.initCreate(savedInstanceState)
        search()
    }

    override fun listeners() {
        super.listeners()
        binding.butSure.setOnClickListener {
            var list=adapter().data as ArrayList<DriverBean>
            if(null!=list&&list.size>0){
                var choseList=ArrayList<SaveDriverBean>()
                for(item in list){
                    if(item.checked==1){
                      var ben=  SaveDriverBean(item.uid,item.car_id)
                        choseList.add(ben)
                    }
                }
               if(choseList.size==0){
                   toast("请选择车辆")
                  return@setOnClickListener
               }

                var arr=Gson().toJson(choseList)
                vm.setSaveDriver(arr)

            }

        }
    }

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

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

        binding.search.getTvSearch()!!.setOnClickListener { t ->
            k = binding.search.getEtName()!!.text.toString().trim()
            onRefresh()
        }
    }

    override fun binding(): ActivityCollectionBinding {
        return ActivityCollectionBinding.inflate(layoutInflater)
    }


    override fun initAdapter(): CollectionAdapter {
        return CollectionAdapter("1")
    }

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

    override fun title(): String? {
        return getString(R.string.add_vehicle)
    }
    override fun vmClass(): Class<CollectionVModel> {
        return CollectionVModel::class.java
    }
}
