import React, { useEffect, useState } from 'react'; import { Modal, Input, Typography, Switch, Button, ConfigProvider, Divider } from 'antd'; import { NotifAlert, NotifOk } from '../../../../components/Global/ToastNotif'; import { createPlantSection, updatePlantSection } from '../../../../api/master-plant-section'; import { validateRun } from '../../../../Utils/validate'; import TextArea from 'antd/es/input/TextArea'; const { Text } = Typography; const DetailPlantSubSection = (props) => { const [confirmLoading, setConfirmLoading] = useState(false); const defaultData = { plant_sub_section_id: '', plant_sub_section_code: '', plant_sub_section_name: '', table_name_value: '', // Fix field name plant_sub_section_description: '', is_active: true, }; const [formData, setFormData] = useState(defaultData); const handleInputChange = (e) => { // Handle different input types let name, value; if (e && e.target) { // Standard input name = e.target.name; value = e.target.value; } else if (e && e.type === 'change') { // Switch or other components name = e.name || e.target?.name; value = e.value !== undefined ? e.value : e.checked; } else { // Fallback return; } console.log(`📝 Input change: ${name} = ${value}`); if (name) { setFormData((prev) => ({ ...prev, [name]: value, })); } }; const handleCancel = () => { props.setSelectedData(null); props.setActionMode('list'); }; const handleSave = async () => { setConfirmLoading(true); // Daftar aturan validasi const validationRules = [ { field: 'plant_sub_section_name', label: 'Plant Sub Section Name', required: true }, ]; if ( validateRun(formData, validationRules, (errorMessages) => { NotifOk({ icon: 'warning', title: 'Peringatan', message: errorMessages, }); setConfirmLoading(false); }) ) return; try { console.log('💾 Current formData before save:', formData); const payload = { plant_sub_section_name: formData.plant_sub_section_name, plant_sub_section_description: formData.plant_sub_section_description, table_name_value: formData.table_name_value, // Fix field name is_active: formData.is_active, }; console.log('📤 Payload to be sent:', payload); const response = props.actionMode === 'edit' ? await updatePlantSection(formData.plant_sub_section_id, payload) : await createPlantSection(payload); if (response && (response.statusCode === 200 || response.statusCode === 201)) { const action = props.actionMode === 'edit' ? 'diubah' : 'ditambahkan'; NotifOk({ icon: 'success', title: 'Berhasil', message: `Data Plant Section berhasil ${action}.`, }); props.setActionMode('list'); } else { NotifOk({ icon: 'error', title: 'Gagal', message: response?.message || 'Terjadi kesalahan saat menyimpan data.', }); } } catch (error) { NotifOk({ icon: 'error', title: 'Error', message: error.message || 'Terjadi kesalahan pada server.', }); } finally { setConfirmLoading(false); } }; const handleStatusToggle = (checked) => { setFormData({ ...formData, is_active: checked, }); }; useEffect(() => { console.log('🔄 Modal state changed:', { showModal: props.showModal, actionMode: props.actionMode, selectedData: props.selectedData, }); if (props.selectedData) { console.log('📋 Setting form data from selectedData:', props.selectedData); setFormData(props.selectedData); } else { console.log('📋 Resetting to default data'); setFormData(defaultData); } }, [props.showModal, props.selectedData, props.actionMode]); return ( {!props.readOnly && ( )} , ]} > {formData && (
Status
{formData.is_active ? 'Active' : 'Inactive'}
{/* Plant Section Code - Auto Increment & Read Only */}
Plant Sub Section Code
Plant Sub Section Name *
Table Name Value
Description