package com.zj365.health.act.datamanage

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.GsonUtils
import com.xty.base.fragment.BaseFragList
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.util.CommonToastUtils
import com.zj365.health.databinding.FragDatamanagerMedicalBinding
import com.zj365.health.vm.DataManageVm
import com.xty.network.model.CancerGoodsBean
import com.xty.network.model.MedicalDataBean

class DataManageMedicalFrag : BaseFragList<DataManageVm>() {
    //val binding by lazy{FragWholeRankBinding.inflate(layoutInflater)}
    val binding by lazy { FragDatamanagerMedicalBinding.inflate(layoutInflater) }
    val mAdapter by lazy { DataManageMedicalAdapter() }
    override fun setLayout() = binding.root

    var result = mutableListOf<String>()
    override fun initView() {
        super.initView()
        initAdapter()
    }

    override fun initAdapter() {
        setRecycleRefresh(binding.mRecycle,binding.mRefresh,true)
        binding.mRecycle.layoutManager = LinearLayoutManager(activity)
        binding.mRecycle.adapter = mAdapter

        mAdapter.addChildClickViewIds(
            com.zj365.health.R.id.root,
        )
        mAdapter.setOnItemChildClickListener { mAdapter, view, position ->
            if (view.id == com.zj365.health.R.id.root) {
                val healthDataBean = mAdapter.data.get(position) as MedicalDataBean
                if (isManage) {
                    val findViewById = view.findViewById<ImageView>(com.zj365.health.R.id.ivSelected)
                    findViewById.isSelected = !findViewById.isSelected
                    healthDataBean.isSelectd = findViewById.isSelected
                    val selectData = this.mAdapter.getSelectData()
                    changeButtonStatus(selectData.size == 1)
                } else {
                    bundle.clear()
                    bundle.putString("data",GsonUtils.toJson(healthDataBean))
                    RouteManager.goAct(ARouterUrl.DATAMANAGE_DETAIL_ACT,bundle)
                }

            }
        }
        binding.mTvManage.setOnClickListener {
            toManage(!isManage)
        }
        binding.mDelete.setOnClickListener {
            val selectData = mAdapter.getSelectData()
            if (selectData.isEmpty()){
                CommonToastUtils.showToast("未勾选数据")
                return@setOnClickListener
            }
            mViewModel.deleteMedical(mAdapter.getSelectData())
        }
//        binding.title.mTvRight.apply {
//            text = "退出管理"
//            setTextColor(resources.getColor(R.color.col_02c))
//            setOnClickListener {
//
//                toManage(false)
//            }
//        }
        binding.tvChange.setOnClickListener {
            val selectData = mAdapter.getSelectData()
            if (selectData.isEmpty()) {
                return@setOnClickListener
            }
            val dataValue = selectData.get(0)
            bundle.clear()
            bundle.putString("data",GsonUtils.toJson(dataValue))
            RouteManager.goAct(ARouterUrl.UPLOAD_Medical_EDIT,bundle)

        }

        binding.mAddData.setOnClickListener {
            //上传图片
            RouteManager.goAct(ARouterUrl.UPLOAD_Medical)
        }
    }

    override fun refresh() {
        mViewModel.getMedicalDataList(page)
    }

    override fun onResume() {
        super.onResume()
        refresh()
    }
    var isEdit = false
    private fun changeButtonStatus(canChange: Boolean) {
        binding.tvChange.isEnabled = canChange
    }

    var isManage = false
    fun toManage(isManager: Boolean) {
        isManage = isManager
        mAdapter.isSelect = isManager
        mAdapter.notifyDataSetChanged()
        if (isManager) {
            binding.mTvManage.visibility = View.GONE
            binding.mDelete.visibility = View.VISIBLE
            binding.mAddData.visibility = View.GONE
            binding.tvChange.visibility = View.VISIBLE
            activity?.let {
                val dataManageTypeAct = it as DataManageAct
                dataManageTypeAct.binding.title.mTvRight.visibility = View.VISIBLE
            }
            changeButtonStatus(mAdapter.getSelectData().size ==1)
            isEdit = true
        } else {
            binding.mTvManage.visibility = View.VISIBLE
            binding.mDelete.visibility = View.GONE
            binding.mAddData.visibility = View.VISIBLE
            binding.tvChange.visibility = View.GONE
            activity?.let {
                val dataManageTypeAct = it as DataManageAct
                dataManageTypeAct.binding.title.mTvRight.visibility = View.GONE
            }
            mAdapter.reset()
            isEdit = false
        }

    }

    var order: CancerGoodsBean? = null
    var status: Int? = null
    private var filePath: String? = null
    override fun initData() {
        super.initData()
        var result = arguments?.getString("order")
        result?.let {
            order = GsonUtils.fromJson(it, CancerGoodsBean::class.java)
        }
    }


    fun newInstance(): DataManageMedicalFrag {

        val fragment = DataManageMedicalFrag()
        var bundle = Bundle()
        fragment.arguments = bundle
        return fragment
    }


    override fun setViewModel() = DataManageVm()
    override fun observer() {
        mViewModel.addDataOb.observe(this) {
            refresh()
            toManage(false)
        }
        mViewModel.getMedicalDataListOb.observe(this) {
            mAdapter.setNewInstance(it.data.records)
        }

    }
    fun exitManage(){
        toManage(false)
    }
}