import React, { useEffect, useState } from 'react'; import { Modal, Input, Divider, Typography, Switch, Button, ConfigProvider, Select } from 'antd'; import { NotifAlert, NotifOk } from '../../../../components/Global/ToastNotif'; import { createDevice, updateDevice } from '../../../../api/master-device'; import { getAllBrands } from '../../../../api/master-brand'; import { validateRun } from '../../../../Utils/validate'; const { Text } = Typography; const { TextArea } = Input; const DetailDevice = (props) => { const [confirmLoading, setConfirmLoading] = useState(false); const [brands, setBrands] = useState([]); const [loadingBrands, setLoadingBrands] = useState(false); const defaultData = { device_id: '', device_code: '', device_name: '', brand_id: '', brand_code: '', is_active: true, device_location: '', device_description: '', ip_address: '', }; const [formData, setFormData] = useState(defaultData); const handleCancel = () => { props.setSelectedData(null); props.setActionMode('list'); }; const handleSave = async () => { setConfirmLoading(true); // Daftar aturan validasi const validationRules = [ { field: 'device_name', label: 'Device Name', required: true }, { field: 'ip_address', label: 'Ip Address', required: true, ip: true }, { field: 'brand_id', label: 'Brand Device', required: true }, ]; if ( validateRun(formData, validationRules, (errorMessages) => { NotifOk({ icon: 'warning', title: 'Peringatan', message: errorMessages, }); setConfirmLoading(false); }) ) return; try { const payload = { device_name: formData.device_name, is_active: formData.is_active, device_location: formData.device_location, device_description: formData.device_description, ip_address: formData.ip_address, brand_id: formData.brand_id, }; const response = formData.device_id ? await updateDevice(formData.device_id, payload) : await createDevice(payload); // Check if response is successful if (response && (response.statusCode === 200 || response.statusCode === 201)) { const deviceName = response.data?.device_name || formData.device_name; NotifOk({ icon: 'success', title: 'Berhasil', message: `Data Device "${deviceName}" berhasil ${ formData.device_id ? 'diubah' : 'ditambahkan' }.`, }); props.setActionMode('list'); } else { NotifAlert({ icon: 'error', title: 'Gagal', message: response?.message || 'Terjadi kesalahan saat menyimpan data.', }); } } catch (error) { console.error('Save Device Error:', error); NotifAlert({ icon: 'error', title: 'Error', message: error.message || 'Terjadi kesalahan pada server. Coba lagi nanti.', }); } setConfirmLoading(false); }; const handleInputChange = (e) => { const { name, value } = e.target; setFormData({ ...formData, [name]: value, }); }; const handleSelectChange = (name, value) => { setFormData({ ...formData, [name]: value, }); }; const handleStatusToggle = (event) => { const isChecked = event; setFormData({ ...formData, is_active: isChecked ? true : false, }); }; // Fungsi untuk mengambil daftar brand const fetchBrands = async () => { setLoadingBrands(true); try { const response = await getAllBrands(new URLSearchParams()); if (response && response.data) { setBrands(response.data || []); } } catch (error) { console.error('Error fetching brands:', error); NotifAlert({ icon: 'error', title: 'Error', message: error.message || 'Gagal mengambil data brand', }); } finally { setLoadingBrands(false); } }; useEffect(() => { if (props.showModal && (props.actionMode === 'add' || props.actionMode === 'edit')) { fetchBrands(); } }, [props.showModal, props.actionMode]); useEffect(() => { if (props.selectedData) { setFormData(props.selectedData); } else { setFormData(defaultData); } }, [props.showModal, props.selectedData, props.actionMode]); return ( {!props.readOnly && ( )} , ]} > {formData && (
Device Status
{formData.is_active === true ? 'Running' : 'Offline'}
{/* Device Code - Auto Increment & Read Only */}
Device Code
Device Name *
Brand Device *
Device Location
IP Address *
Device Description