package com.cheng.blzb.ui.fragment.bid.publish.bid

import com.cheng.blzb.R
import com.cheng.blzb.bean.BidDetailEntity
import com.cheng.blzb.databinding.FragmentMyBidBinding
import com.cheng.blzb.event.BidEvent
import com.cheng.blzb.manager.DialogEnum
import com.cheng.blzb.ui.activity.PublicActivity
import com.cheng.blzb.ui.dialog.TipDialog
import com.cheng.blzb.ui.fragment.bid.detail.BidDetailFragment
import com.example.base.common.RxBus
import com.example.base.extensions.onClick
import com.example.base.ui.list.ListFragment

class MyBidFragment: ListFragment<FragmentMyBidBinding, MyBidViewModel, BidDetailEntity>() {

    override fun noDataClick() {
    }

    override fun bindAdapter() = MyBidAdapter()

    override fun initView() {
        super.initView()
        mEmptyView.setNoDataText("暂无数据")
        mEmptyView.setNoDataLogo(R.mipmap.ic_empty_seat)
    }

    override fun initData() {
        super.initData()
        firstLoad()
    }

    override fun initListener() {
        super.initListener()
        binding.btnNext.onClick {
            PublicActivity.start(requireActivity(), PublishBidMessageFragment::class.java)
        }

        mAdapter.setOnItemChildClickListener { _, view, position ->
            val item = mAdapter.getItem(position)
            when(view.id) {
                R.id.btn_delete -> {
                    val f = TipDialog.newInstance("温馨提示", "您确定要删除标书吗？")
                    f.setOnSelectListener {
                        if (it == DialogEnum.CLICK_OK) {
                            mViewModel.deleteBid(item.id)
                        }
                    }
                    f.show(childFragmentManager, TipDialog::class.java.simpleName)
                }

                R.id.btn_edit -> {
                    PublicActivity.start(requireContext(), PublishBidMessageFragment::class.java, Pair("item", item))
                }

                R.id.btn_view -> {
                    PublicActivity.start(
                        requireContext(),
                        BidDetailFragment::class.java,
                        Pair("id", item.id),
                        Pair("info_source", item.info_source)
                    )
                }
            }
        }
    }

    override fun initObserve() {
        super.initObserve()
        mViewModel.deleteLiveData.observe(this) {
            firstLoad()
        }

        val bidEvent = RxBus.defaultInstance.toObservable(BidEvent::class.java).subscribe {
            firstLoad()
        }
        addDisposable(bidEvent)
    }
}