diff --git a/app/src/main/java/com/example/bdkipoc/bantuan/BantuanFormActivity.java b/app/src/main/java/com/example/bdkipoc/bantuan/BantuanFormActivity.java index f756fc8..f422d39 100644 --- a/app/src/main/java/com/example/bdkipoc/bantuan/BantuanFormActivity.java +++ b/app/src/main/java/com/example/bdkipoc/bantuan/BantuanFormActivity.java @@ -21,6 +21,7 @@ import androidx.activity.result.contract.ActivityResultContracts; import com.example.bdkipoc.R; import com.example.bdkipoc.LoginActivity; +import org.json.JSONArray; import org.json.JSONObject; import java.io.BufferedReader; @@ -29,7 +30,9 @@ import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Calendar; +import java.util.List; import java.util.Locale; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -48,6 +51,27 @@ public class BantuanFormActivity extends AppCompatActivity { private ExecutorService executor = Executors.newSingleThreadExecutor(); private Handler mainHandler = new Handler(Looper.getMainLooper()); + // Dynamic data lists + private List sourceList = new ArrayList<>(); + private List issueList = new ArrayList<>(); + private List assignList = new ArrayList<>(); + + // Inner class for parameter details + public static class ParameterDetail { + public int id; + public String name; + + public ParameterDetail(int id, String name) { + this.id = id; + this.name = name; + } + + @Override + public String toString() { + return name; + } + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -61,7 +85,11 @@ public class BantuanFormActivity extends AppCompatActivity { setContentView(R.layout.activity_bantuan_form); initViews(); - setupSpinners(); + + // Load dynamic data first, then setup spinners + loadHeaderParams(); + loadUsers(); + setupListeners(); // Initialize button state @@ -87,31 +115,220 @@ public class BantuanFormActivity extends AppCompatActivity { backNavigation = findViewById(R.id.back_navigation); } + private void loadHeaderParams() { + String authToken = LoginActivity.getToken(this); + if (authToken == null || authToken.isEmpty()) { + LoginActivity.logout(this); + return; + } + + executor.execute(() -> { + HttpURLConnection connection = null; + try { + URL url = new URL("https://be-edc.msvc.app/header-params/list"); + connection = (HttpURLConnection) url.openConnection(); + + connection.setRequestMethod("GET"); + connection.setRequestProperty("Authorization", "Bearer " + authToken); + connection.setConnectTimeout(15000); + connection.setReadTimeout(15000); + + int responseCode = connection.getResponseCode(); + + if (responseCode == 200) { + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + StringBuilder response = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + response.append(line); + } + reader.close(); + + // Parse response + JSONObject jsonResponse = new JSONObject(response.toString()); + JSONArray dataArray = jsonResponse.getJSONArray("data"); + + // Clear existing lists + sourceList.clear(); + issueList.clear(); + + // Process each header parameter + for (int i = 0; i < dataArray.length(); i++) { + JSONObject headerParam = dataArray.getJSONObject(i); + String code = headerParam.getString("code"); + String name = headerParam.getString("name"); + + // Check if this is ticket_sources (code "01") + if ("01".equals(code) && "ticket_sources".equals(name)) { + JSONArray details = headerParam.getJSONArray("details"); + for (int j = 0; j < details.length(); j++) { + JSONObject detail = details.getJSONObject(j); + int id = detail.getInt("id"); + String detailName = detail.getString("name"); + String status = detail.getString("status"); + + // Only add active items + if ("1".equals(status)) { + sourceList.add(new ParameterDetail(id, detailName)); + } + } + } + // Check if this is issue_categories (code "02") + else if ("02".equals(code) && "issue_categories".equals(name)) { + JSONArray details = headerParam.getJSONArray("details"); + for (int j = 0; j < details.length(); j++) { + JSONObject detail = details.getJSONObject(j); + int id = detail.getInt("id"); + String detailName = detail.getString("name"); + String status = detail.getString("status"); + + // Only add active items + if ("1".equals(status)) { + issueList.add(new ParameterDetail(id, detailName)); + } + } + } + } + + // Update UI on main thread + mainHandler.post(() -> { + setupSpinners(); + updateButtonState(); + }); + + } else if (responseCode == 401) { + mainHandler.post(() -> { + Toast.makeText(this, "Session expired. Please login again.", Toast.LENGTH_LONG).show(); + LoginActivity.logout(this); + }); + } else { + mainHandler.post(() -> { + Toast.makeText(this, "Failed to load data. Error: " + responseCode, Toast.LENGTH_LONG).show(); + // Setup with empty data + setupSpinners(); + }); + } + + } catch (Exception e) { + mainHandler.post(() -> { + Toast.makeText(this, "Network error: " + e.getMessage(), Toast.LENGTH_LONG).show(); + // Setup with empty data + setupSpinners(); + }); + } finally { + if (connection != null) { + connection.disconnect(); + } + } + }); + } + + private void loadUsers() { + String authToken = LoginActivity.getToken(this); + if (authToken == null || authToken.isEmpty()) { + LoginActivity.logout(this); + return; + } + + executor.execute(() -> { + HttpURLConnection connection = null; + try { + URL url = new URL("https://be-edc.msvc.app/users"); + connection = (HttpURLConnection) url.openConnection(); + + connection.setRequestMethod("GET"); + connection.setRequestProperty("Authorization", "Bearer " + authToken); + connection.setConnectTimeout(15000); + connection.setReadTimeout(15000); + + int responseCode = connection.getResponseCode(); + + if (responseCode == 200) { + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + StringBuilder response = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + response.append(line); + } + reader.close(); + + // Parse response + JSONArray usersArray = new JSONArray(response.toString()); + + // Clear existing list + assignList.clear(); + + // Process each user + for (int i = 0; i < usersArray.length(); i++) { + JSONObject user = usersArray.getJSONObject(i); + String role = user.getString("role"); + boolean isActive = user.getBoolean("is_active"); + + // Only add superadmin users who are active + if ("superadmin".equals(role) && isActive) { + int id = user.getInt("id"); + String name = user.getString("name"); + assignList.add(new ParameterDetail(id, name)); + } + } + + // Update UI on main thread + mainHandler.post(() -> { + setupSpinners(); + updateButtonState(); + }); + + } else if (responseCode == 401) { + mainHandler.post(() -> { + Toast.makeText(this, "Session expired. Please login again.", Toast.LENGTH_LONG).show(); + LoginActivity.logout(this); + }); + } else { + mainHandler.post(() -> { + Toast.makeText(this, "Failed to load users. Error: " + responseCode, Toast.LENGTH_LONG).show(); + // Setup with empty data + setupSpinners(); + }); + } + + } catch (Exception e) { + mainHandler.post(() -> { + Toast.makeText(this, "Network error loading users: " + e.getMessage(), Toast.LENGTH_LONG).show(); + // Setup with empty data + setupSpinners(); + }); + } finally { + if (connection != null) { + connection.disconnect(); + } + } + }); + } + private void setupSpinners() { - // Setup Source Spinner - String[] sourceOptions = { - "Pilih Source", - "WhatsApp", - "Website Hubungi Kami" - }; + // Setup Source Spinner (Dynamic) + List sourceOptions = new ArrayList<>(); + sourceOptions.add("Pilih Source"); + for (ParameterDetail source : sourceList) { + sourceOptions.add(source.name); + } ArrayAdapter sourceAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, sourceOptions); sourceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinnerSource.setAdapter(sourceAdapter); - // Setup Issue Spinner - String[] issueOptions = { - "Pilih Issue", - "Koneksi EDC Bermasalah", - "Permintaan Tambahan EDC", - "Pergantian EDC" - }; + // Setup Issue Spinner (Dynamic) + List issueOptions = new ArrayList<>(); + issueOptions.add("Pilih Issue"); + for (ParameterDetail issue : issueList) { + issueOptions.add(issue.name); + } ArrayAdapter issueAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, issueOptions); issueAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinnerIssue.setAdapter(issueAdapter); - // Setup Merchant Spinner + // Setup Merchant Spinner (Static - unchanged) String[] merchantOptions = { "Pilih Merchant", "Surabaya", @@ -133,16 +350,12 @@ public class BantuanFormActivity extends AppCompatActivity { merchantAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinnerMerchant.setAdapter(merchantAdapter); - // Setup Assign Spinner - String[] assignOptions = { - "Pilih Assign", - "welno hedi prabowo putro jenaka prima", - "rizqika", - "budi", - "binomo", - "budi", - "Irfan Muslim Saputra" - }; + // Setup Assign Spinner (Dynamic) + List assignOptions = new ArrayList<>(); + assignOptions.add("Pilih Assign"); + for (ParameterDetail assign : assignList) { + assignOptions.add(assign.name); + } ArrayAdapter assignAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, assignOptions); assignAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); @@ -325,20 +538,17 @@ public class BantuanFormActivity extends AppCompatActivity { } private int getSourceId(int position) { - switch (position) { - case 1: return 1; // WhatsApp - case 2: return 2; // Website Hubungi Kami - default: return 0; + if (position > 0 && position <= sourceList.size()) { + return sourceList.get(position - 1).id; } + return 0; } private int getIssueId(int position) { - switch (position) { - case 1: return 4; // Koneksi EDC Bermasalah - case 2: return 5; // Permintaan Tambahan EDC - case 3: return 6; // Pergantian EDC - default: return 0; + if (position > 0 && position <= issueList.size()) { + return issueList.get(position - 1).id; } + return 0; } private int getMerchantId(int position) { @@ -361,15 +571,10 @@ public class BantuanFormActivity extends AppCompatActivity { } private int getAssignId(int position) { - switch (position) { - case 1: return 1; // welno hedi prabowo putro jenaka prima - case 2: return 2; // rizqika - case 3: return 3; // budi - case 4: return 4; // binomo - case 5: return 5; // budi - case 6: return 6; // Irfan Muslim Saputra - default: return 0; + if (position > 0 && position <= assignList.size()) { + return assignList.get(position - 1).id; } + return 0; } private void submitForm() {