package com.zj365.health.act.datamanage

import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.GsonUtils
import com.xty.common.arouter.ARouterUrl
import com.zj365.health.vm.DataManageVm
import com.xty.network.model.MedicalDataBean

/**
 */
@Route(path = ARouterUrl.UPLOAD_Medical_EDIT)
class UploadMedicalEditAct : UploadMedicalAct<DataManageVm>() {
    var resultData: MedicalDataBean? = null
    var mType = "6"
    override fun initData() {
        super.initData()
        var result = intent.getStringExtra("data")
        mType = intent.getStringExtra("type").toString()
        result?.let {
            resultData = GsonUtils.fromJson(it, MedicalDataBean::class.java)
        }

        binding.title.mTvTitle.text =
            if (mType == "6") "上传体检" else if (mType == "7") "上传用药" else "上传住院"
        if (mType == "7") {
            binding.llTijianUpload.visibility = View.GONE
            binding.llMedicalLayout.visibility = View.VISIBLE

            resultData?.apply {
                //用药初始值
                binding.etMedicalName.setText(medicineName)

                val sTime =  if (startTime !=null && startTime.length>9){
                    startTime.subSequence(0,10)
                }else{
                    ""
                }
                val eTime = if (startTime !=null && endTime.length>9){
                    endTime.subSequence(0,10)
                }else{
                    ""
                }
                binding.tvMedicalStartTime.text = sTime
                binding.tvMedicalEndTime.text = eTime
            }
        } else if (mType == "6") {
            resultData?.name?.let {
                binding.etName.setText(it)
            }
            resultData?.time?.let {
                binding.tvTime.text = it.replace("00:00:00", "").trim()
            }
            resultData?.orgName?.let {
                binding.etOrg.setText(it)
            }
        } else {
            resultData?.apply {

                val sTime =  if (startTime !=null && startTime.length>9){
                    startTime.subSequence(0,10)
                }else{
                    ""
                }
                val eTime = if (startTime !=null && endTime.length>9){
                    endTime.subSequence(0,10)
                }else{
                    ""
                }
                binding.llHospitalLayout.tvStartTime.text = sTime
                binding.llHospitalLayout.tvEndTime.text = eTime
                binding.llHospitalLayout.etHospitalName.setText(hospital)
                binding.llHospitalLayout.etHospitalInstruction.setText(hospitalExplain)
            }
        }

        var data1 = mutableListOf<String>()
        var data2 = mutableListOf<String>()

        resultData?.examinationList?.forEach {
            data1.add(it.imageUrl!!)
            mAdapter.setNewInstance(data1)
        }

        resultData?.medicationList?.forEach {
            data2.add(it.imageUrl!!)
            mAdapter1.setNewInstance(data2)
        }

        resultData?.hospitalList?.forEach {
            data1.add(it.imageUrl!!)
            mAdapter.setNewInstance(data1)
        }

        binding.mZyDesc.setText(resultData?.remark)

    }

    override fun uploadData(
        list: MutableList<String>,
        list1: MutableList<String>,
        name: String,
        time: String
    ) {
        if (type == "7") {  //用药
            mViewModel.edittUseMedical(
                resultData?.id,
                list,
                list1,
                binding.mZyDesc.text.toString(),
                binding.etName.text.toString(),
                binding.etOrg.text.toString(),
                binding.tvMedicalStartTime.text.toString() + " 00:00:00",
                binding.tvMedicalEndTime.text.toString() + " 00:00:00",
                binding.etMedicalName.text.toString()
            )
        } else if (type == "6") {  //体检
            mViewModel.edittMedical(
                resultData?.id,
                list,
                list1,
                binding.mZyDesc.text.toString(),
                binding.etName.text.toString(),
                binding.tvTime.text.toString() + " 00:00:00",
                binding.etOrg.text.toString()
            )
        }
    }

    override fun uploadHospitalDate(list: MutableList<String>, list1: MutableList<String>) {
        mViewModel.edittHospitalMedical(
            resultData?.id,
            list,
            list1,
            binding.mZyDesc.text.toString(),
            binding.llHospitalLayout.tvStartTime.text.toString()+ " 00:00:00",
            binding.llHospitalLayout.tvEndTime.text.toString() + " 00:00:00",
            binding.llHospitalLayout.etHospitalName.text.toString(),
            binding.llHospitalLayout.etHospitalInstruction.text.toString(),
        )
    }
    override fun uploadData(list: MutableList<String>, list1: MutableList<String>) {
        if (type == "7") {
            binding.tvTime.text.toString()
            mViewModel.edittUseMedical(
                resultData?.id,
                list,
                list1,
                binding.mZyDesc.text.toString(),
                binding.etName.text.toString(),
                binding.etOrg.text.toString(),
                binding.tvMedicalStartTime.text.toString() + " 00:00:00",
                binding.tvMedicalEndTime.text.toString() + " 00:00:00",
                binding.etMedicalName.text.toString()
            )
        } else if(type == "6"){
            mViewModel.edittMedical(
                resultData?.id,
                list,
                list1,
                binding.mZyDesc.text.toString(),
                binding.etName.text.toString(),
                binding.tvTime.text.toString() + " 00:00:00",
                binding.etOrg.text.toString()
            )
        }
    }

    override fun deleteData(list: MutableList<String>) {
        var pathList = arrayListOf<String>()
        list.forEachIndexed { index, path ->
            var listTop = resultData?.examinationList?.filter { it.imageUrl == path }
            var listBottom = resultData?.medicationList?.filter { it.imageUrl == path }
            if (listTop?.size!! > 0) {
                pathList.add(listTop[0].id.toString())
            }
            if (listBottom?.size!! > 0) {
                pathList.add(listBottom[0].id.toString())
            }
        }
        mViewModel.deleteMedicalFile(pathList)
    }
}