package com.nurulwafa.nufakuproject;

import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class InformasiSPPActivity extends AppCompatActivity {
    private RecyclerView rvInformasiSPP;
    private InformasiSPPAdapter adapter;
    private ArrayList<InformasiSPP> informasiSPPList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_informasi_spp_santri);

        rvInformasiSPP = findViewById(R.id.rvInformasiSPP);
        rvInformasiSPP.setLayoutManager(new LinearLayoutManager(this));

        informasiSPPList = new ArrayList<>();
        adapter = new InformasiSPPAdapter(informasiSPPList);
        rvInformasiSPP.setAdapter(adapter);

        String loginKey = getIntent().getStringExtra("login_key");
        String loginValue = getIntent().getStringExtra("login_value");

        if (loginKey != null && loginValue != null) {
            getDataSantri(loginKey, loginValue);
        } else {
            Toast.makeText(this, "Data login tidak ditemukan", Toast.LENGTH_SHORT).show();
        }
    }

    private void getDataSantri(String key, String value) {
        String baseUrl = ApiHelper.getBaseUrl();
        String requestUrl = baseUrl + "getDataSantri?" + key + "=" + value;

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, requestUrl, null,
                response -> {
                    try {
                        if (response.getBoolean("status")) {
                            informasiSPPList.clear();

                            // Ambil objek "data"
                            JSONObject dataObject = response.getJSONObject("data");

                            // Ambil array "pelanggaran_santri" dari dalam "data"
                            JSONArray informasiSPPArray = dataObject.getJSONArray("informasi_spp_santri");

                            for (int i = 0; i < informasiSPPArray.length(); i++) {
                                JSONObject informasiSPP = informasiSPPArray.getJSONObject(i);

                                String nama = informasiSPP.getString("nama_santri");
                                String bulan = informasiSPP.optString("bulan", "-");
                                String tahun = informasiSPP.optString("tahun", "-");
                                String jumlahPembayaran = informasiSPP.optString("jumlah_pembayaran", "-");
                                String jatuhTempo = informasiSPP.optString("jatuh_tempo", "-");
                                String keterangan = informasiSPP.optString("keterangan", "-");

                                InformasiSPP p = new InformasiSPP(nama, bulan, tahun, jumlahPembayaran, jatuhTempo, keterangan);
                                informasiSPPList.add(p);
                            }

                            adapter.notifyDataSetChanged();
                        } else {
                            Toast.makeText(this, "Data tidak ditemukan", Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(this, "Gagal parsing data", Toast.LENGTH_SHORT).show();
                    }
                },
                error -> Toast.makeText(this, "Gagal ambil data: " + error.getMessage(), Toast.LENGTH_SHORT).show()
        );

        RequestQueue queue = Volley.newRequestQueue(this);
        queue.add(request);
    }
}
