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

import android.os.Bundle
import com.cheng.blzb.R
import com.cheng.blzb.databinding.FragmentOrderListBinding
import com.cheng.blzb.event.OrderEvent
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.mine.order.cert.CertificateFragment
import com.cheng.blzb.ui.fragment.mine.order.invoice.InvoiceFragment
import com.example.base.browser.BrowserActivity
import com.example.base.common.RxBus
import com.example.base.extensions.toast
import com.example.base.ui.BaseFragment
import com.example.base.widget.EmptyView
import com.example.base.widget.PageStatus

class OrderListFragment : BaseFragment<FragmentOrderListBinding, OrderListViewModel>() {
    private val status by lazy { arguments?.getString("status") ?: "" }

    private val mAdapter by lazy { OrderAdapter() }
    private val mEmptyView by lazy { EmptyView(requireContext()) }

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

    override fun initView() {
        super.initView()

        binding.mRecyclerView.adapter = mAdapter
        mEmptyView.setNoDataLogo(R.mipmap.ic_empty_order)
        mEmptyView.setNoDataText("暂无订单")
        mAdapter.setEmptyView(mEmptyView)
    }

    override fun initData() {
        super.initData()
        mViewModel.getOrderList(status)
    }

    override fun initListener() {
        super.initListener()
        binding.mRefreshLayout.setOnRefreshListener {
            mViewModel.getOrderList(status)
        }

        mAdapter.setOnItemChildClickListener { _, view, i ->
            val item = mAdapter.getItem(i)
            when (view.id) {
                R.id.btn_invoice -> { //1：未开票 2：开票中 3：催开票4：开票完成
                    when (item.invoice_status) {
                        "1" -> {
                            PublicActivity.start(requireContext(), InvoiceFragment::class.java, Pair("item", item))
                        }
                        "2" -> {
                            val f = TipDialog.newInstance("温馨提示", "发票正在开具中，确定要加急开票吗？")
                            f.setOnSelectListener {
                                if (it == DialogEnum.CLICK_OK) {
                                    mViewModel.updateOrder(item.id)
                                }
                            }
                            f.show(childFragmentManager, TipDialog::class.java.simpleName)
                        }
                        "3" -> {

                        }
                        "4" -> {
                            BrowserActivity.start(requireContext(), "查看发票", item.invoice_url)
                        }
                    }
                }

                R.id.btn_certificate -> {
                    PublicActivity.start(requireContext(), CertificateFragment::class.java, Pair("orderId", item.id))
                }
            }
        }
    }

    override fun initObserve() {
        super.initObserve()
        mViewModel.orderLiveData.observe(this) {
            mAdapter.setList(it)
            if (it.isNotEmpty()) {
                mEmptyView.setStatus(PageStatus.GONG)
            } else {
                mEmptyView.setStatus(PageStatus.NO_DATA)
            }
            binding.mRefreshLayout.finishRefresh()
        }

        mViewModel.updateLiveData.observe(this) {
            toast("已催促")
            RxBus.defaultInstance.post(OrderEvent())
        }

        val orderEvent = RxBus.defaultInstance.toObservable(OrderEvent::class.java).subscribe {
            binding.mRefreshLayout.autoRefresh()
        }
        addDisposable(orderEvent)
    }
}