Implement API ke Form Bantuan
This commit is contained in:
parent
f64779755a
commit
c033a26516
@ -44,8 +44,9 @@ public class BantuanFormActivity extends AppCompatActivity {
|
||||
|
||||
// UI Components
|
||||
private EditText etTicketCode;
|
||||
private Spinner spinnerSource, spinnerIssue, spinnerMerchant, spinnerAssign, spinnerResolvedDate;
|
||||
private TextView tvStatus;
|
||||
private Spinner spinnerSource, spinnerIssue, spinnerMerchant, spinnerAssign;
|
||||
private TextView tvStatus, tvResolvedDate;
|
||||
private LinearLayout llResolvedDate;
|
||||
private Button btnKirim;
|
||||
private LinearLayout backNavigation;
|
||||
private LinearLayout successScreen;
|
||||
@ -60,6 +61,7 @@ public class BantuanFormActivity extends AppCompatActivity {
|
||||
private List<ParameterDetail> sourceList = new ArrayList<>();
|
||||
private List<ParameterDetail> issueList = new ArrayList<>();
|
||||
private List<ParameterDetail> assignList = new ArrayList<>();
|
||||
private List<ParameterDetail> merchantList = new ArrayList<>();
|
||||
|
||||
// Inner class for parameter details
|
||||
public static class ParameterDetail {
|
||||
@ -94,6 +96,7 @@ public class BantuanFormActivity extends AppCompatActivity {
|
||||
// Load dynamic data first, then setup spinners
|
||||
loadHeaderParams();
|
||||
loadUsers();
|
||||
loadMerchants();
|
||||
|
||||
setupListeners();
|
||||
|
||||
@ -114,7 +117,10 @@ public class BantuanFormActivity extends AppCompatActivity {
|
||||
spinnerIssue = findViewById(R.id.spinner_issue);
|
||||
spinnerMerchant = findViewById(R.id.spinner_merchant);
|
||||
spinnerAssign = findViewById(R.id.spinner_assign);
|
||||
spinnerResolvedDate = findViewById(R.id.spinner_resolved_date);
|
||||
|
||||
// Resolved Date components
|
||||
llResolvedDate = findViewById(R.id.ll_resolved_date);
|
||||
tvResolvedDate = findViewById(R.id.tv_resolved_date);
|
||||
|
||||
// Status (read-only)
|
||||
tvStatus = findViewById(R.id.tv_status);
|
||||
@ -232,6 +238,93 @@ public class BantuanFormActivity extends AppCompatActivity {
|
||||
});
|
||||
}
|
||||
|
||||
private void loadMerchants() {
|
||||
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/merchants/list?location_id=0");
|
||||
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 list
|
||||
merchantList.clear();
|
||||
|
||||
// Process each merchant
|
||||
for (int i = 0; i < dataArray.length(); i++) {
|
||||
JSONObject merchant = dataArray.getJSONObject(i);
|
||||
String status = merchant.getString("status");
|
||||
|
||||
// Only add active merchants
|
||||
if ("1".equals(status)) {
|
||||
int id = merchant.getInt("id");
|
||||
String name = merchant.getString("name");
|
||||
String merchantCode = merchant.optString("merchant_code", "");
|
||||
|
||||
// Only add merchants that have merchant_code
|
||||
if (!merchantCode.isEmpty()) {
|
||||
merchantList.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 merchants. Error: " + responseCode, Toast.LENGTH_LONG).show();
|
||||
// Setup with empty data
|
||||
setupSpinners();
|
||||
});
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
mainHandler.post(() -> {
|
||||
Toast.makeText(this, "Network error loading merchants: " + e.getMessage(), Toast.LENGTH_LONG).show();
|
||||
// Setup with empty data
|
||||
setupSpinners();
|
||||
});
|
||||
} finally {
|
||||
if (connection != null) {
|
||||
connection.disconnect();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void showSuccessScreen() {
|
||||
Log.d(TAG, "Showing success screen");
|
||||
|
||||
@ -370,23 +463,12 @@ public class BantuanFormActivity extends AppCompatActivity {
|
||||
issueAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
||||
spinnerIssue.setAdapter(issueAdapter);
|
||||
|
||||
// Setup Merchant Spinner (Static - unchanged)
|
||||
String[] merchantOptions = {
|
||||
"Pilih Merchant",
|
||||
"Surabaya",
|
||||
"Jakarta Selatan",
|
||||
"Jakarta Pusat",
|
||||
"Jakarta Barat",
|
||||
"Palembang",
|
||||
"Bogor Timur",
|
||||
"Bandung Utara",
|
||||
"Yogyakarta Selatan",
|
||||
"Cilacap Barat",
|
||||
"Bekasi Timur",
|
||||
"Tangerang Selatan",
|
||||
"Depok Tengah",
|
||||
"Kediri Kota"
|
||||
};
|
||||
// Setup Merchant Spinner (Dynamic)
|
||||
List<String> merchantOptions = new ArrayList<>();
|
||||
merchantOptions.add("Pilih Merchant");
|
||||
for (ParameterDetail merchant : merchantList) {
|
||||
merchantOptions.add(merchant.name);
|
||||
}
|
||||
ArrayAdapter<String> merchantAdapter = new ArrayAdapter<>(this,
|
||||
android.R.layout.simple_spinner_item, merchantOptions);
|
||||
merchantAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
||||
@ -403,12 +485,7 @@ public class BantuanFormActivity extends AppCompatActivity {
|
||||
assignAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
||||
spinnerAssign.setAdapter(assignAdapter);
|
||||
|
||||
// Setup Resolved Date Spinner with date picker
|
||||
String[] dateOptions = {"Pilih Tanggal Resolved", "Pilih dari kalender"};
|
||||
ArrayAdapter<String> dateAdapter = new ArrayAdapter<>(this,
|
||||
android.R.layout.simple_spinner_item, dateOptions);
|
||||
dateAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
||||
spinnerResolvedDate.setAdapter(dateAdapter);
|
||||
// Setup Resolved Date components (no spinner setup needed)
|
||||
|
||||
// Set status to fixed value
|
||||
if (tvStatus != null) {
|
||||
@ -466,19 +543,10 @@ public class BantuanFormActivity extends AppCompatActivity {
|
||||
public void onNothingSelected(android.widget.AdapterView<?> parent) {}
|
||||
});
|
||||
|
||||
// Resolved Date spinner listener
|
||||
spinnerResolvedDate.setOnItemSelectedListener(new android.widget.AdapterView.OnItemSelectedListener() {
|
||||
@Override
|
||||
public void onItemSelected(android.widget.AdapterView<?> parent, View view, int position, long id) {
|
||||
if (position == 1) { // "Pilih dari kalender"
|
||||
showDatePicker();
|
||||
}
|
||||
updateButtonState();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNothingSelected(android.widget.AdapterView<?> parent) {}
|
||||
});
|
||||
// Resolved Date click listener
|
||||
if (llResolvedDate != null) {
|
||||
llResolvedDate.setOnClickListener(v -> showDatePicker());
|
||||
}
|
||||
|
||||
// Text watcher for EditText field
|
||||
setupTextWatcher();
|
||||
@ -560,13 +628,13 @@ public class BantuanFormActivity extends AppCompatActivity {
|
||||
SimpleDateFormat displayFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
|
||||
String displayDate = displayFormat.format(selectedCalendar.getTime());
|
||||
|
||||
// Update spinner to show selected date
|
||||
String[] updatedOptions = {"Pilih Tanggal Resolved", displayDate};
|
||||
ArrayAdapter<String> updatedAdapter = new ArrayAdapter<>(this,
|
||||
android.R.layout.simple_spinner_item, updatedOptions);
|
||||
updatedAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
||||
spinnerResolvedDate.setAdapter(updatedAdapter);
|
||||
spinnerResolvedDate.setSelection(1);
|
||||
// Update TextView to show selected date
|
||||
if (tvResolvedDate != null) {
|
||||
tvResolvedDate.setText(displayDate);
|
||||
tvResolvedDate.setTextColor(0xFF000000); // Black color for selected date
|
||||
}
|
||||
|
||||
Log.d(TAG, "Date selected: " + displayDate + " (API format: " + selectedResolvedDate + ")");
|
||||
|
||||
// Update button state after date selection
|
||||
updateButtonState();
|
||||
@ -576,6 +644,9 @@ public class BantuanFormActivity extends AppCompatActivity {
|
||||
calendar.get(Calendar.DAY_OF_MONTH)
|
||||
);
|
||||
|
||||
// Set minimum date to today to prevent past dates
|
||||
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
|
||||
|
||||
datePickerDialog.show();
|
||||
}
|
||||
|
||||
@ -594,22 +665,10 @@ public class BantuanFormActivity extends AppCompatActivity {
|
||||
}
|
||||
|
||||
private int getMerchantId(int position) {
|
||||
switch (position) {
|
||||
case 1: return 1; // Surabaya
|
||||
case 2: return 2; // Jakarta Selatan
|
||||
case 3: return 3; // Jakarta Pusat
|
||||
case 4: return 4; // Jakarta Barat
|
||||
case 5: return 5; // Palembang
|
||||
case 6: return 6; // Bogor Timur
|
||||
case 7: return 7; // Bandung Utara
|
||||
case 8: return 8; // Yogyakarta Selatan
|
||||
case 9: return 9; // Cilacap Barat
|
||||
case 10: return 10; // Bekasi Timur
|
||||
case 11: return 11; // Tangerang Selatan
|
||||
case 12: return 12; // Depok Tengah
|
||||
case 13: return 13; // Kediri Kota
|
||||
default: return 0;
|
||||
if (position > 0 && position <= merchantList.size()) {
|
||||
return merchantList.get(position - 1).id;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private int getAssignId(int position) {
|
||||
@ -655,6 +714,14 @@ public class BantuanFormActivity extends AppCompatActivity {
|
||||
return;
|
||||
}
|
||||
|
||||
if (merchantId == 0) {
|
||||
Log.e(TAG, "Invalid merchant ID: " + merchantId + ", position: " + spinnerMerchant.getSelectedItemPosition());
|
||||
Toast.makeText(this, "Error: Merchant tidak valid", Toast.LENGTH_SHORT).show();
|
||||
btnKirim.setEnabled(true);
|
||||
btnKirim.setText("Kirim Sekarang");
|
||||
return;
|
||||
}
|
||||
|
||||
if (assignId == 0) {
|
||||
Log.e(TAG, "Invalid assign ID: " + assignId + ", position: " + spinnerAssign.getSelectedItemPosition());
|
||||
Toast.makeText(this, "Error: Assign To tidak valid", Toast.LENGTH_SHORT).show();
|
||||
@ -853,8 +920,12 @@ public class BantuanFormActivity extends AppCompatActivity {
|
||||
if (spinnerIssue != null) spinnerIssue.setSelection(0);
|
||||
if (spinnerMerchant != null) spinnerMerchant.setSelection(0);
|
||||
if (spinnerAssign != null) spinnerAssign.setSelection(0);
|
||||
if (spinnerResolvedDate != null) spinnerResolvedDate.setSelection(0);
|
||||
|
||||
// Reset resolved date
|
||||
if (tvResolvedDate != null) {
|
||||
tvResolvedDate.setText("Pilih Tanggal Resolved");
|
||||
tvResolvedDate.setTextColor(0xFFAAAAAA); // Light gray color
|
||||
}
|
||||
selectedResolvedDate = "";
|
||||
|
||||
// Update button state after clearing form
|
||||
|
@ -200,13 +200,34 @@
|
||||
android:layout_marginBottom="8dp"
|
||||
android:textStyle="bold" />
|
||||
|
||||
<Spinner
|
||||
android:id="@+id/spinner_resolved_date"
|
||||
<LinearLayout
|
||||
android:id="@+id/ll_resolved_date"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="@android:drawable/editbox_background"
|
||||
android:orientation="horizontal"
|
||||
android:gravity="center_vertical"
|
||||
android:padding="16dp"
|
||||
android:layout_marginBottom="12dp"
|
||||
android:padding="16dp" />
|
||||
android:clickable="true"
|
||||
android:focusable="true">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_resolved_date"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="Pilih Tanggal Resolved"
|
||||
android:textSize="14sp"
|
||||
android:textColor="#AAAAAA" />
|
||||
|
||||
<ImageView
|
||||
android:layout_width="24dp"
|
||||
android:layout_height="24dp"
|
||||
android:src="@android:drawable/ic_menu_my_calendar"
|
||||
android:contentDescription="Calendar Icon" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
@ -262,7 +283,7 @@
|
||||
android:id="@+id/success_icon"
|
||||
android:layout_width="120dp"
|
||||
android:layout_height="120dp"
|
||||
android:src="@drawable/ic_check_circle"
|
||||
android:src="@drawable/ic_success_payment"
|
||||
android:layout_marginBottom="32dp"
|
||||
android:scaleType="centerInside"
|
||||
android:contentDescription="Success Icon" />
|
||||
|
Loading…
x
Reference in New Issue
Block a user