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.AppealsOldAdapter
import com.example.doctor.Appeals.AppealsOldModel
import com.example.doctor.DoctorViewModel
import com.example.doctor.databinding.FragmentOldAppealsBinding


class OldAppealsFragment : Fragment(), AppealsOldAdapter.Listener {
    private lateinit var binding:FragmentOldAppealsBinding
    private val model: DoctorViewModel by activityViewModels()
    lateinit var adapter: AppealsOldAdapter

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

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

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

        addAllAppeals()
    }

    private fun addAllAppeals() {
        val list = ArrayList<AppealsOldModel>()

        for (i in 1..7){
            val itemPatient = AppealsOldModel(
                i,
                "Patient_$i",
                " 81 23891 23812 37817411 sdasds adnas kjhdkl ashdlk ashdkjalks ashdlkashdkjalks hdjlkasjdlk ajldkhas hdjlkasjdlk ajldkhasas hdlkashdkjalks hdjlkasjdlk ajldkhasashdlkashdkjalks hdjlkasjdlk ajldkhas j djask hsfldksjfksdf",
                i,
            )
            list.add(itemPatient)//Передали заполненый список
        }
        model.appealsAllList.value = list
    }

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

    }

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

        fun newInstance() = OldAppealsFragment()
    }

    override fun onClickAppeals(item: AppealsOldModel) {

    }
}