package com.dahe.gldriver.ui.mine.fragment

import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.LayoutManager
import com.dahe.gldriver.R
import com.dahe.gldriver.adapter.CarsAdapter
import com.dahe.gldriver.adapter.DriversAdapter
import com.dahe.gldriver.adapter.WaybillAdapter
import com.dahe.gldriver.base.AppConfig
import com.dahe.gldriver.bean.CarBean
import com.dahe.gldriver.databinding.ActivityCarsManBinding
import com.dahe.gldriver.databinding.FragmentWaybillListBinding
import com.dahe.gldriver.net.BaseObserver
import com.dahe.gldriver.net.DataManager
import com.dahe.gldriver.net.RxHttpCallBack
import com.dahe.gldriver.ui.mine.InviteDriverActivity
import com.dahe.gldriver.utils.SPUtils
import com.dahe.glex.bean.WayBillBean
import com.dahe.mylibrary.base.BaseActivity
import com.dahe.mylibrary.base.BaseFragment
import com.dahe.mylibrary.callback.RefreshCallBack
import com.dahe.mylibrary.net.CommonResponseBean
import com.dahe.mylibrary.recycleviewswipe.RecycleViewDivider
import com.dahe.mylibrary.utils.ActivityUtils
import com.dahe.mylibrary.utils.ConvertUtils
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers

/**
 * @ClassName CarsManActivity
 * @Author john
 * @Date 2024/2/1 08:59
 * @Description 车辆管理
 */
class CarsManFragment : BaseFragment<FragmentWaybillListBinding>(), RefreshCallBack {
    private var captainId: String = ""
    lateinit var adapter: CarsAdapter
    override fun onFragmentVisibleChange(isVisible: Boolean) {
    }

    override fun onFragmentFirstVisible() {
        arguments?.let {
            captainId = it.getString(AppConfig.ID, "")
        }
        binding.run {
            setRefresh(refresh, this@CarsManFragment)
            adapter = binding.recyclerView.run {
                layoutManager = LinearLayoutManager(mContext, RecyclerView.VERTICAL, false)
                addItemDecoration(
                    RecycleViewDivider(
                        LinearLayout.VERTICAL,
                        ConvertUtils.dp2px(16.0f),
                        Color.TRANSPARENT
                    )
                )
                adapter = CarsAdapter()
                adapter as CarsAdapter
            }.apply {
                isStateViewEnable = true
            }
            llOk.visibility=View.GONE

        }


        initData()
    }

    override fun getRefreshDate(stat: Int, page: Int, count: Int) {
        initData()
    }

    private fun initData() {
        DataManager.getInstance()
            .selectDriverIdsForCarPage(captainId)
            .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(BaseObserver(mContext, object : RxHttpCallBack<MutableList<CarBean>>() {
                override fun onSuccess(t: CommonResponseBean<MutableList<CarBean>>) {
                    super.onSuccess(t)
                    if (mRefreshPage === 1) {
                        if (t.data.isNullOrEmpty() || t.data.size == 0) {
                            adapter.submitList(null)
                            adapter.setStateViewLayout(mContext, R.layout.empty_view)
                        } else {
                            adapter?.submitList(t.data)
                        }
                    } else {
                        adapter?.addAll(t.data)
                    }
                    setFinishRefresh(
                        binding.refresh,
                        t.total > mRefreshPage * mRefreshCount
                    )
                }
            }))
    }
}