package com.nurulwafa.nufakuproject;

import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
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.JSONException;
import org.json.JSONObject;

public class DataWaliSantriActivity extends AppCompatActivity {

    private TextView tvEmail, tvPassword, tvNamaWaliSantri, tvAlamat, tvNoTelepon, tvPekerjaan;

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

        // Inisialisasi TextView
        tvEmail = findViewById(R.id.tvEmail);
        tvPassword = findViewById(R.id.tvPassword);
        tvNamaWaliSantri = findViewById(R.id.tvNamaWaliSantri);
        tvAlamat = findViewById(R.id.tvAlamat);
        tvNoTelepon = findViewById(R.id.tvNoTelepon);
        tvPekerjaan = findViewById(R.id.tvPekerjaan);

        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(); // Ambil base URL fleksibel

        String requestUrl = baseUrl + "getDataSantri?" + key + "=" + value;

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, requestUrl, null,
                response -> {
                    try {
                        if (response.getBoolean("status")) {
                            // Ambil objek wali_santri
                            JSONObject data = response.getJSONObject("data");
                            JSONObject waliSantri = data.getJSONObject("wali_santri");

                            tvEmail.setText(waliSantri.getString("email"));
                            tvPassword.setText(waliSantri.getString("password")); // Tidak ada di response, bisa dikosongkan atau ditambah di API jika perlu
                            tvNamaWaliSantri.setText(waliSantri.getString("nama_wali_santri"));
                            tvAlamat.setText(waliSantri.getString("alamat"));
                            tvNoTelepon.setText(waliSantri.getString("no_telepon"));
                            tvPekerjaan.setText(waliSantri.getString("pekerjaan"));
                        } 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);
    }
}
