'use client'; import { useState, useRef } from 'react'; import { api } from '@/lib/api'; import Navbar from '@/components/Navbar'; import { Upload, Loader2, CheckCircle, XCircle } from 'lucide-react'; interface ImportResult { success: boolean; message: string; created?: number; updated?: number; } export default function ImportPage() { const [kyosaiFile, setKyosaiFile] = useState(null); const [yoshidaFile, setYoshidaFile] = useState(null); const [chusankanFile, setChusankanFile] = useState(null); const [uploading, setUploading] = useState(false); const [kyosaiResult, setKyosaiResult] = useState(null); const [yoshidaResult, setYoshidaResult] = useState(null); const [chusankanResult, setChusankanResult] = useState(null); const kyosaiInputRef = useRef(null); const yoshidaInputRef = useRef(null); const chusankanInputRef = useRef(null); const handleKyosaiUpload = async () => { if (!kyosaiFile) { alert('ファイルを選択してください'); return; } setUploading(true); setKyosaiResult(null); try { const formData = new FormData(); formData.append('file', kyosaiFile); const response = await api.post('/fields/import/kyosai/', formData, { headers: { 'Content-Type': 'multipart/form-data', }, }); const data = response.data; setKyosaiResult({ success: true, message: data.message || 'インポートが完了しました', created: data.created, updated: data.updated, }); } catch (error: unknown) { console.error('Upload failed:', error); let errorMessage = 'アップロードに失敗しました'; if (error && typeof error === 'object' && 'response' in error) { const axiosError = error as { response?: { data?: { error?: string } } }; if (axiosError.response?.data?.error) { errorMessage = axiosError.response.data.error; } } setKyosaiResult({ success: false, message: errorMessage, }); } finally { setUploading(false); } }; const handleYoshidaUpload = async () => { if (!yoshidaFile) { alert('ファイルを選択してください'); return; } setUploading(true); setYoshidaResult(null); try { const formData = new FormData(); formData.append('file', yoshidaFile); const response = await api.post('/fields/import/yoshida/', formData, { headers: { 'Content-Type': 'multipart/form-data', }, }); const data = response.data; setYoshidaResult({ success: true, message: data.message || 'インポートが完了しました', created: data.created, updated: data.updated, }); } catch (error: unknown) { console.error('Upload failed:', error); let errorMessage = 'アップロードに失敗しました'; if (error && typeof error === 'object' && 'response' in error) { const axiosError = error as { response?: { data?: { error?: string } } }; if (axiosError.response?.data?.error) { errorMessage = axiosError.response.data.error; } } setYoshidaResult({ success: false, message: errorMessage, }); } finally { setUploading(false); } }; const handleChusankanUpload = async () => { if (!chusankanFile) { alert('ファイルを選択してください'); return; } setUploading(true); setChusankanResult(null); try { const formData = new FormData(); formData.append('file', chusankanFile); const response = await api.post('/fields/import/chusankan/', formData, { headers: { 'Content-Type': 'multipart/form-data', }, }); const data = response.data; setChusankanResult({ success: true, message: data.message || 'インポートが完了しました', created: data.created, updated: data.updated, }); } catch (error: unknown) { console.error('Upload failed:', error); let errorMessage = 'アップロードに失敗しました'; if (error && typeof error === 'object' && 'response' in error) { const axiosError = error as { response?: { data?: { error?: string } } }; if (axiosError.response?.data?.error) { errorMessage = axiosError.response.data.error; } } setChusankanResult({ success: false, message: errorMessage, }); } finally { setUploading(false); } }; return (

データインポート

{/* 共済マスタ取込 */}

共済マスタ取込

共済細目データをインポートします(k_num, s_num, address...)

setKyosaiFile(e.target.files?.[0] || null)} className="hidden" /> {kyosaiFile && ( )}
{kyosaiResult && (
{kyosaiResult.success ? ( ) : ( )}

{kyosaiResult.message}

{kyosaiResult.success && kyosaiResult.created !== undefined && (

作成: {kyosaiResult.created}件 / 更新: {kyosaiResult.updated}件

)}
)}
{/* 中山間マスタ取込 */}

中山間マスタ取込

中山間指定データをインポートします(ID, 大字, 字, 地番...)

setChusankanFile(e.target.files?.[0] || null)} className="hidden" /> {chusankanFile && ( )}
{chusankanResult && (
{chusankanResult.success ? ( ) : ( )}

{chusankanResult.message}

{chusankanResult.success && chusankanResult.created !== undefined && (

作成: {chusankanResult.created}件 / 更新: {chusankanResult.updated}件

)}
)}
{/* 実圃場データ取込 */}

実圃場データ取込

吉田農地台帳データをインポートします

setYoshidaFile(e.target.files?.[0] || null)} className="hidden" /> {yoshidaFile && ( )}
{yoshidaResult && (
{yoshidaResult.success ? ( ) : ( )}

{yoshidaResult.message}

{yoshidaResult.success && yoshidaResult.created !== undefined && (

作成: {yoshidaResult.created}件 / 更新: {yoshidaResult.updated}件

)}
)}
); }