import React, { useState, useCallback } from 'react' import { View, Text, ScrollView, TouchableOpacity, StyleSheet, ActivityIndicator, Alert, } from 'react-native' import { useFocusEffect } from 'expo-router' import { COLORS } from '../../constants/Config' import { getReportData } from '../../services/api' export default function PDFShareScreen() { const [data, setData] = useState(null) const [loading, setLoading] = useState(false) const [exporting, setExporting] = useState(false) const load = useCallback(async () => { setLoading(true) try { const r = await getReportData(); setData(r.data) } catch { setData(null) } finally { setLoading(false) } }, []) useFocusEffect(useCallback(() => { load() }, [load])) const exportPDF = async () => { setExporting(true) try { // eslint-disable-next-line @typescript-eslint/no-var-requires let Print: any = null; let Sharing: any = null try { Print = require('expo-print') } catch {} try { Sharing = require('expo-sharing') } catch {} if (!Print || !Sharing) { Alert.alert('알림', '현재 환경에서 PDF 내보내기가 지원되지 않습니다.') return } const html = buildHTML(data) const { uri } = await Print.printToFileAsync({ html }) if (await Sharing.isAvailableAsync()) { await Sharing.shareAsync(uri, { mimeType: 'application/pdf', dialogTitle: 'GUARDiA 리포트 공유' }) } else { Alert.alert('완료', `PDF가 저장됐습니다:\n${uri}`) } } catch { Alert.alert('오류', 'PDF 생성에 실패했습니다.') } finally { setExporting(false) } } if (loading) return if (!data) return 데이터를 불러올 수 없습니다. return ( 월간 운영 리포트 {data.period ?? ''} SR 현황 배포 이력 {exporting ? : PDF 내보내기 · 공유} ) } function Row({ label, value }: { label: string; value: any }) { return ( {label} {value} ) } function buildHTML(data: any): string { return `

GUARDiA 월간 리포트

${data?.period ?? ''}

SR 현황

전체 SR${data?.total_sr ?? 0}
완료${data?.completed_sr ?? 0}
SLA 준수율${data?.sla_compliance_rate ?? 0}%
CSAP 점수${data?.csap_score ?? 0}점

배포

성공${data?.deploy_success ?? 0}
실패${data?.deploy_failure ?? 0}
` } const s = StyleSheet.create({ title: { fontSize: 20, fontWeight: '800', color: COLORS.text, marginBottom: 4 }, period: { fontSize: 13, color: COLORS.muted, marginBottom: 16 }, section: { backgroundColor: '#fff', borderRadius: 10, padding: 14, marginBottom: 10, elevation: 1 }, sectionTitle:{ fontSize: 14, fontWeight: '700', color: COLORS.text, marginBottom: 10 }, dataRow: { flexDirection: 'row', justifyContent: 'space-between', paddingVertical: 6, borderBottomWidth: 1, borderBottomColor: COLORS.light }, dataLabel: { fontSize: 13, color: COLORS.muted }, dataValue: { fontSize: 13, fontWeight: '700', color: COLORS.text }, exportBtn: { margin: 12, backgroundColor: COLORS.accent, borderRadius: 12, padding: 16, alignItems: 'center' }, exportText: { color: '#fff', fontWeight: '800', fontSize: 15 }, empty: { textAlign: 'center', color: COLORS.muted, marginTop: 40 }, })