package com.zj365.health.act

import android.content.Intent
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.GsonUtils
import com.xty.base.act.BaseVmAct
import com.xty.base.vp2.VpAdapter
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.weight.tablayout.TabEntity
import com.xty.common.weight.tablayout.listener.CustomTabEntity
import com.xty.common.weight.tablayout.listener.OnTabSelectListener
import com.zj365.health.R
import com.zj365.health.adapter.AnswerAdapter
import com.zj365.health.databinding.ActUserReportBinding
import com.zj365.health.fragment.*
import com.zj365.health.vm.PayCancerVm
import com.xty.network.model.CancerGoodsBean
import com.zj365.health.fragment.EstimateReportFrag
import com.zj365.health.fragment.QuestionIdFrag
import com.zj365.health.fragment.UploadCheckFrag

/**
 */
@Route(path = ARouterUrl.USER_REPORT)
class UserReportAct : BaseVmAct<PayCancerVm>() {
    val mAdapter by lazy { AnswerAdapter() }
    val binding by lazy { ActUserReportBinding.inflate(layoutInflater) }

    val listFragment= mutableListOf<Fragment>()
    var orderNo:String? = ""
    var order:CancerGoodsBean? = null
    override fun setLayout() = binding.root
    override fun initData() {
        super.initData()
        orderNo = intent.getStringExtra("orderNo")
        var result  = intent.getStringExtra("order")
        result?.let {
            order = GsonUtils.fromJson(it,CancerGoodsBean::class.java)
        }
    }
    var tabName = arrayOf<String>()
    private val mTabEntities: java.util.ArrayList<CustomTabEntity> = java.util.ArrayList()
    private val mTitles = arrayOf(
        "调查问卷", "检验单", "评估报告"
    )
    private fun initVp2() {
        listFragment.add(QuestionIdFrag().newInstance(order))
        listFragment.add(UploadCheckFrag().newInstance(order))
        listFragment.add(EstimateReportFrag().newInstance(order))
        tabName= arrayOf("调查问卷","检验单","评估报告")
        mTabEntities.clear()
        mTitles.forEach {
            mTabEntities.add(
                TabEntity(
                    it,
                    android.R.drawable.ic_delete,
                    android.R.drawable.ic_delete
                )
            )

        }
        binding.title.mTvRight.apply {
            visibility = View.VISIBLE
            text = "预约采血"
            setTextColor(resources.getColor(R.color.col_02c))
            setOnClickListener {
                bundle.clear()
                RouteManager.goAct(ARouterUrl.COLLECT_BLOOD_list,bundle)
            }
        }
        binding.commonTabLayout.setTabData(mTabEntities)
        binding.commonTabLayout.setOnTabSelectListener(object :
            OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                binding.commonTabLayout.currentTab = position
                binding.mVp2.currentItem = position
            }

            override fun onTabReselect(position: Int) {
            }

        })


        binding.mVp2.adapter = VpAdapter(listFragment, this)
        binding.mVp2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                binding.commonTabLayout.currentTab = position
            }
        })
        binding.mVp2.currentItem = 1
        binding.mVp2.offscreenPageLimit = 2
        binding.mVp2.isUserInputEnabled = false
        binding.commonTabLayout.currentTab = 1
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text = "用户报告"
        binding.title.mIvBack.setOnClickListener { finish() }
        initVp2()
        mViewModel.isCanReservation()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        listFragment?.forEach {
            it.onActivityResult(requestCode,resultCode,data)
        }
    }

    override fun liveObserver() {
        mViewModel.uploadSign.observe(this) {
            bundle.clear()
            bundle.putString("orderNo", orderNo)
            RouteManager.goAct(ARouterUrl.UPLOAD_CHECK, bundle)
        }

        mViewModel.isCanReservationOb.observe(this) {
            it.data?.let {
                if (it.equals("1")) {
                    //可预约
                    binding.title.mTvRight.visibility = View.VISIBLE
                } else{
                    //不可预约
                    binding.title.mTvRight.visibility = View.GONE
                }
            }
        }
    }
}