package com.example.daftarbuku

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.example.daftarbuku.databinding.FragmentProfileBinding

class ProfileFragment : Fragment() {
    private var _binding: FragmentProfileBinding? = null
    private val binding get() = _binding!!

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

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

        val username = activity?.intent?.getStringExtra("USERNAME") ?: "User"
        val email = activity?.intent?.getStringExtra("EMAIL") ?: "-"

        binding.tvDisplayUsername.text = username
        binding.tvDisplayEmail.text = email

        binding.btnLogout.setOnClickListener {
            val sharedPref = requireContext().getSharedPreferences("USER_SESSION", Context.MODE_PRIVATE)
            with(sharedPref.edit()) {
                putBoolean("IS_LOGGED_IN", false)
                remove("USERNAME")
                remove("EMAIL")
                apply()
            }

            val intent = Intent(requireContext(), RegisterActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
            startActivity(intent)
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}
