package com.cheng.blzb.ui.fragment.mine.coupon.list

import android.os.Bundle
import com.example.base.common.RxBus
import com.example.base.ui.list.ListFragment
import com.cheng.blzb.R
import com.cheng.blzb.databinding.FragmentCouponListBinding
import com.cheng.blzb.event.CouponRefreshEvent
import com.cheng.blzb.ui.activity.PublicActivity
import com.cheng.blzb.ui.fragment.mine.vip.VipFragment

class CouponListFragment : ListFragment<FragmentCouponListBinding, CouponListViewModel, com.cheng.blzb.bean.CouponEntity>() {
    private val status by lazy { arguments?.getString("status") ?: "1" }
    private val expire by lazy { arguments?.getString("expire") ?: "1" }

    companion object {
        fun newInstance(status: String, expire: String): CouponListFragment {
            val args = Bundle()
            args.putString("status", status)
            args.putString("expire", expire)
            val fragment = CouponListFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun bindAdapter() = CouponAdapter(expire == "2")

    override fun noDataClick() {

    }

    override fun initView() {
        super.initView()
        mEmptyView.setBtnVisible(false)
    }

    override fun initData() {
        super.initData()
        mViewModel.params["status"] = status
        mViewModel.params["expire"] = expire
        firstLoad()
    }

    override fun initListener() {
        super.initListener()
        mAdapter.setOnItemChildClickListener { _, view, _ ->
            when(view.id) {
                R.id.btn_next -> {
                    PublicActivity.start(requireContext(), VipFragment::class.java)
                }
            }
        }
    }

    override fun initObserve() {
        super.initObserve()
        val couponRefreshEvent = RxBus.defaultInstance.toObservable(CouponRefreshEvent::class.java).subscribe {
            firstLoad()
        }
        addDisposable(couponRefreshEvent)
    }
}