package com.example.doctor.Appeals.TabLayout

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.example.doctor.Appeals.AppealsNewAdapter
import com.example.doctor.Appeals.AppealsNewModel
import com.example.doctor.DoctorViewModel
import com.example.doctor.databinding.FragmentNewAppealsBinding


class NewAppealsFragment : Fragment(), AppealsNewAdapter.Listener {
    private lateinit var binding:FragmentNewAppealsBinding
    private val model: DoctorViewModel by activityViewModels()
    lateinit var adapter: AppealsNewAdapter



    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentNewAppealsBinding.inflate(layoutInflater,container,false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        initRcViewAppeals()
        patientListCurrent()
        model.appealsNewList.observe(viewLifecycleOwner) {//viewLifecycleOwner - следит за циклом жизни fragment
            adapter.submitList(it)//Напрямую переадем созданный список в adapter(ProductAdapter)
        }

        addNewAppeals()
    }

    private fun addNewAppeals() {
        val list = ArrayList<AppealsNewModel>()

        for (i in 1..5){
            val itemPatient = AppealsNewModel(
                i,
                "Patient_$i",
                "asdasds adnas kjhdkl ashdlkashdkjalks hdjlkasjdlk ajldkhas j djask hsfldksjfksdf",
                i,
            )
            list.add(itemPatient)//Передали заполненый список
        }
        model.appealsNewList.value = list
    }

    //Вывод прогресса на один день
    private fun patientListCurrent() = with(binding) {
//        model.appealsAllCurrent.observe(viewLifecycleOwner) {//viewLifecycleOwner - следит за циклом жизни fragment
//
//        }

    }

    //Инициализация списка
    private fun initRcViewAppeals() = with(binding) {
        rcView.layoutManager = GridLayoutManager(requireContext(),1)//По вертикали будет выводить по умолчанию
        adapter = AppealsNewAdapter(this@NewAppealsFragment)
        rcView.adapter = adapter
    }

    companion object {

        fun newInstance() = NewAppealsFragment()
    }

    override fun onClickAppeals(item: AppealsNewModel) {

    }
}