package com.zj365.health.act

import android.annotation.SuppressLint
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.GsonUtils
import com.xty.base.act.BaseListAct
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.zj365.health.R
import com.zj365.health.adapter.OrderListAdapter
import com.zj365.health.databinding.ActOrderListBinding
import com.zj365.health.vm.PayCancerVm
import com.xty.network.model.CancerGoodsBean

/**
 * 已填问卷列表
 */
@Route(path = ARouterUrl.ORDER_LIST)
class OrderListAct : BaseListAct<PayCancerVm>() {

    val binding by lazy { ActOrderListBinding.inflate(layoutInflater) }
    override fun setLayout() = binding.root

    var userId: String? = null
    val mAdapter by lazy { OrderListAdapter() }

    var type:Int = 0 //默认全部 1 是从糖医进来

    override fun initData() {
        super.initData()
        type = intent.getIntExtra("type",0)
        if(intent.hasExtra("id")){
            userId = intent.getStringExtra("id")

        }
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text = "我的订单"
        binding.title.mIvBack.setOnClickListener { finish() }
    }

    override fun initAdapter() {
        setRecycleRefresh(binding.mRecycle, binding.mRefresh, true)
        binding.mRecycle.layoutManager = LinearLayoutManager(this)
        binding.mRecycle.adapter = mAdapter
        mAdapter.addChildClickViewIds(R.id.llUploadSuccess, R.id.llUpload)
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            val indexOf = adapter.data[position] as CancerGoodsBean
            when (view.id) {
                R.id.llUploadSuccess -> {
                    bundle.clear()
                    bundle.putString("data", GsonUtils.toJson(indexOf))
                    RouteManager.goAct(ARouterUrl.ORDER_DETAIL, bundle)
                }
            }
        }
    }

    override fun loadData() {
        if (type == 1){
            mViewModel.getSugarMedicineOrderList( page)

        }else {
            mViewModel.getOrderList(userId, page)

        }
    }

    @SuppressLint("SetTextI18n")
    override fun liveObserver() {
        mViewModel.orderListData.observe(this) {
            it.data?.let { listData ->
                setDate(mAdapter, listData.records)
                if (listData.total <= 0){
                    binding.title.mTvTitle.text = "我的订单"
                }else{
                    binding.title.mTvTitle.text = "我的订单(${listData.total})"
                }
            }
        }

        mViewModel.sugarMedicalListLiveData.observe(this){
            it.data?.let { listData ->
                setDate(mAdapter, listData)
                /*if (listData.size <= 0){
                    binding.title.mTvTitle.text = "我的订单"
                }else{
                    binding.title.mTvTitle.text = "我的订单(${listData.size})"
                }*/
            }
        }
    }
}