package com.zj365.health.act

import android.view.View
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.EstimateRecordAdapter
import com.zj365.health.databinding.ActEstimateRecordBinding
import com.zj365.health.vm.PayCancerVm
import com.xty.network.model.CancerGoodsBean

/**
 * 已填问卷列表
 */
@Route(path = ARouterUrl.ESTIMATE_RECORD)
class EstimateRecordAct : BaseListAct<PayCancerVm>() {
    val mAdapter by lazy { EstimateRecordAdapter() }
    val binding by lazy { ActEstimateRecordBinding.inflate(layoutInflater) }


    override fun setLayout() = binding.root
    override fun initData() {
        super.initData()
    }

    override fun onResume() {
        super.onResume()
        loadData()
    }
    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text = "评估记录"
        binding.title.mIvBack.setOnClickListener { finish() }
        mViewModel.getEstimateRecord()
    }

    override fun initAdapter() {
        setRecycleRefresh(binding.mRecycle,binding.mRefresh)
        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.get(position) as CancerGoodsBean
            when(view.id){
                R.id.llUploadSuccess -> {
                    //跳转用户报告
                    bundle.clear()
                    bundle.putString("orderNo",indexOf.orderNo)
                    bundle.putString("order",GsonUtils.toJson(indexOf))
                    RouteManager.goAct(ARouterUrl.USER_REPORT,bundle)
                }
                R.id.llUpload -> {
                    // 根据 有无 签名 问卷id 上传图片，跳转到对应页面
                    val esignature = indexOf.esignature
                    val questionnaireId = indexOf.questionnaireId
                    val images = indexOf.images
                    if (esignature.isNullOrEmpty()){
                        bundle.clear()
                        bundle.putString("orderNo",indexOf.orderNo)
                        RouteManager.goAct(ARouterUrl.AGREE,bundle)
                    } else if (questionnaireId.isNullOrEmpty()){
                        bundle.clear()
                        bundle.putString("orderNo",indexOf.orderNo)
                        RouteManager.goAct(ARouterUrl.AGREE,bundle)
                    } else if (images.isNullOrEmpty()){
                        bundle.clear()
                        bundle.putString("orderNo",indexOf.orderNo)
                        RouteManager.goAct(ARouterUrl.UPLOAD_CHECK,bundle)
                    }
                }
            }
        }
    }

    override fun loadData() {
        mViewModel.getEstimateRecord()
    }

    override fun liveObserver() {
        mViewModel.cancerGoodsBeanObservable.observe(this) {
            it.data?.let {
                if (it.isEmpty()){
                    binding.viewNone.rlRoot.visibility = View.VISIBLE
                } else {
                    binding.viewNone.rlRoot.visibility = View.GONE
                }

               mAdapter.setNewInstance(it)
            }
        }
    }

}