import React, { useEffect, useState } from 'react';
import { View, Text, StyleSheet, ActivityIndicator, TouchableOpacity, Alert, ScrollView, SafeAreaView } from 'react-native';
import { Ionicons } from '@expo/vector-icons';
import { useNavigation, useRoute } from '@react-navigation/native';
import apiService from '../../services/apiService';
import { COLORS } from '../../constants/colors';
import { StatusBar } from 'expo-status-bar';
// Helper formatters
const formatDate = (dateStr) => {
if (!dateStr) return '';
const d = new Date(dateStr);
if (isNaN(d)) return dateStr;
return d.toLocaleDateString('tk-TM');
};
const formatDateTime = (dateStr) => {
if (!dateStr) return '';
const d = new Date(dateStr);
if (isNaN(d)) return dateStr;
return d.toLocaleString('tk-TM', {
hour: '2-digit',
minute: '2-digit',
day: '2-digit',
month: '2-digit',
year: 'numeric',
});
};
// Key/value row
const DetailRow = ({ label, value, showBorder = true }) => (
{label}
{String(value)}
);
const LoanOrderDetailsScreen = () => {
const navigation = useNavigation();
const route = useRoute();
const { orderId } = route.params || {};
const [loading, setLoading] = useState(true);
const [order, setOrder] = useState(null);
const fetchDetails = async () => {
setLoading(true);
const res = await apiService.getLoanOrder(orderId);
if (res.success) {
setOrder(res.data);
} else {
Alert.alert('Error', res.error || 'Could not fetch details');
}
setLoading(false);
};
useEffect(() => {
fetchDetails();
}, []);
const handleDelete = () => {
Alert.alert('Tassykla', 'Bu sargydy pozmak isleýärsiňizmi?', [
{ text: 'Goýbolsun', style: 'cancel' },
{ text: 'Poz', style: 'destructive', onPress: deleteOrder },
]);
};
const deleteOrder = async () => {
const res = await apiService.deleteLoanOrder(orderId);
if (res.success) {
Alert.alert('Pozuldy', res.message || 'Sargyt pozuldy', [
{ text: 'Bolýar', onPress: () => navigation.goBack() },
]);
} else {
Alert.alert('Ýalňyşlyk', res.error || 'Pozup bolmady');
}
};
if (loading) {
return (
);
}
if (!order) {
return (
No data
);
}
// Pre-computed helpers
const fullName = `${order.customer_name ?? ''} ${order.customer_surname ?? ''} ${order.customer_patronic_name ?? ''}`.trim();
const guarantorFullName = `${order.guarantor_name ?? ''} ${order.guarantor_surname ?? ''} ${order.guarantor_patronic_name ?? ''}`.trim();
const guarantor2FullName = `${order.guarantor_2_name ?? ''} ${order.guarantor_2_surname ?? ''} ${order.guarantor_2_patronic_name ?? ''}`.trim();
return (
navigation.goBack()}>
{/* Ensure enough bottom padding so content is not hidden behind the tab bar */}
Karz sargyt: {order.unique_id}
{/* Basic order info */}
{order.unique_id && }
{order.created_at && }
{order.updated_at && }
{order.status && }
{/* Loan details */}
Karz barada maglumatlar
{order.loan_amount && }
{order.loan_type && }
{order.satisfiable && }
{/* Location */}
{(order.region || order.branch) && (
<>
Lokasiýa
{order.region && }
{order.branch && }
>
)}
{/* Personal information */}
Şahsy maglumatlar
{order.born_at && }
{order.born_place && }
{order.marriage_status && }
{order.education && }
{order.phone && }
{order.phone_additional && }
{order.phone_home && }
{order.email && }
{order.passport_serie && order.passport_id && }
{order.passport_given_at && }
{order.passport_given_by && }
{order.passport_address && }
{/* Work info */}
{(order.work_company || order.work_position) && (
<>
Iş barada maglumatlar
{order.work_company && }
{order.work_position && }
{order.work_salary && }
{order.work_started_at && }
{order.work_company_accountant_number && }
{order.work_region && }
{order.work_province && }
>
)}
{/* Applicant card info */}
{order.card_number && (
<>
Kart maglumatlary (Arza beriji)
{order.card_name && }
{order.card_month && order.card_year && }
>
)}
{/* Guarantor 1 */}
{guarantorFullName.trim() && (
<>
Zamun (1)
{order.guarantor_card_name && }
{order.guarantor_card_number && }
{order.guarantor_card_month && order.guarantor_card_year && }
>
)}
{/* Guarantor 2 */}
{guarantor2FullName.trim() && (
<>
Zamun (2)
{order.guarantor_2_card_name && }
{order.guarantor_2_card_number && }
{order.guarantor_2_card_month && order.guarantor_2_card_year && }
>
)}
Poz
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: COLORS.backgroundSecondary,
paddingHorizontal: 24,
paddingTop: 40,
},
centered: { flex: 1, alignItems: 'center', justifyContent: 'center' },
backBtn: { alignSelf: 'flex-end', marginBottom: 16, marginRight: 24 },
title: { fontSize: 24, fontWeight: 'bold', color: COLORS.textPrimary, marginBottom: 24 },
sectionTitle: { fontSize: 18, fontWeight: '700', color: COLORS.textPrimary, marginBottom: 12 },
detailCard: { backgroundColor: COLORS.white, borderRadius: 12, padding: 20, marginBottom: 32 },
detailRow: { flexDirection: 'row', justifyContent: 'space-between', paddingVertical: 12 },
detailRowBorder: { borderBottomWidth: 1, borderBottomColor: COLORS.border },
detailKey: { fontWeight: '600', color: COLORS.textSecondary },
detailValue: { color: COLORS.textPrimary, maxWidth: '60%', textAlign: 'right' },
deleteBtn: { backgroundColor: COLORS.error, paddingVertical: 14, borderRadius: 8, alignItems: 'center' },
deleteText: { color: COLORS.white, fontSize: 16, fontWeight: '600' },
});
export default LoanOrderDetailsScreen;