package com.dahe.gldriver.ui.waybill.fragment

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.dahe.gldriver.R
import com.dahe.gldriver.adapter.WaybillListAdapter
import com.dahe.gldriver.base.AppConfig
import com.dahe.gldriver.callback.OnSearchListener
import com.dahe.gldriver.databinding.FragmentWaybillBinding
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.waybill.activity.WaybillRateActivity
import com.dahe.gldriver.utils.OrderUtils
import com.dahe.glex.bean.OrderBean
import com.dahe.glex.bean.WayBillBean
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 AllWaybillFragment
 * @Author 用户
 * @Date 2024/1/24 14:06
 * @Description 待评价-修改未待结算
 */
class WaitAppraiseFragment : BaseFragment<FragmentWaybillListBinding>(), RefreshCallBack,
    OnSearchListener {


    lateinit var adapter: WaybillListAdapter

    override fun onFragmentVisibleChange(isVisible: Boolean) {

    }

    override fun onFragmentFirstVisible() {
        binding.run {
            setRefresh(refresh, this@WaitAppraiseFragment)
            adapter = recyclerView.run {
                layoutManager = LinearLayoutManager(mContext, RecyclerView.VERTICAL, false)
                addItemDecoration(
                    RecycleViewDivider(
                        LinearLayout.VERTICAL,
                        ConvertUtils.dp2px(8.0f),
                        Color.TRANSPARENT
                    )
                )
                adapter = WaybillListAdapter()
                // 打开空布局功能
                (adapter as WaybillListAdapter).isStateViewEnable = true
                adapter as WaybillListAdapter
            }.apply {
                addOnItemChildClickListener(R.id.btnOk) { _, _, position ->
                    val orderBean = items[position]
                    ActivityUtils.startActivity(mContext,WaybillRateActivity::class.java, Bundle().apply {
                        putString(AppConfig.ORDER_ID,orderBean.orderId)
                    })
                }
            }
        }
        initData()
    }

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

    override fun onSearchClick(search: String) {
        initData(search)
    }

    private fun initData(search: String = "") {
        DataManager.getInstance().selectMyWaybillList(mRefreshPage, mRefreshCount, "102070", search)
            .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(BaseObserver(mContext, object : RxHttpCallBack<MutableList<OrderBean>>() {
                override fun onSuccess(t: CommonResponseBean<MutableList<OrderBean>>) {
                    super.onSuccess(t)
                    if (mRefreshPage === 1) {
                        if (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
                    )
                }

                override fun onCodeError(
                    mContext: Context?,
                    t: CommonResponseBean<MutableList<OrderBean>>
                ) {
                    super.onCodeError(mContext, t)
                    setFinishRefresh(
                        binding.refresh,
                        true
                    )
                    mContext?.let { adapter.setStateViewLayout(it, R.layout.empty_view) }
                }

                override fun onFailure(mContext: Context?, e: Throwable) {
                    super.onFailure(mContext, e)
                    setFinishRefresh(
                        binding.refresh,
                        true
                    )
                    mContext?.let { adapter.setStateViewLayout(it, R.layout.empty_view) }
                }
            }))
    }
}