import React, { useEffect, useState } from 'react'; import { Modal, Select, Typography, Button, ConfigProvider } from 'antd'; import { NotifOk } from '../../../components/Global/ToastNotif'; import { createJadwalShift, updateJadwalShift } from '../../../api/jadwal-shift'; import { getAllUser } from '../../../api/user'; import { getAllShift } from '../../../api/master-shift'; import { validateRun } from '../../../Utils/validate'; const { Text } = Typography; const { Option } = Select; const DetailJadwalShift = (props) => { const [confirmLoading, setConfirmLoading] = useState(false); const [employees, setEmployees] = useState([]); const [shifts, setShifts] = useState([]); const [loadingData, setLoadingData] = useState(false); const defaultData = { id: '', user_id: null, shift_id: null, schedule_id: '', user_phone: null, }; const [formData, setFormData] = useState(defaultData); const handleSelectChange = (name, value) => { const updates = { [name]: value }; if (name === 'user_id') { const selectedEmployee = employees.find((emp) => emp.user_id === value); updates.user_phone = selectedEmployee?.user_phone || '-'; } setFormData({ ...formData, ...updates, }); }; const handleCancel = () => { props.setSelectedData(null); props.setActionMode('list'); }; const fetchData = async () => { setLoadingData(true); try { const params = new URLSearchParams({ page: 1, limit: 100, }); const [usersResponse, shiftsResponse] = await Promise.all([ getAllUser(params), getAllShift(params), ]); const userData = usersResponse?.data || usersResponse || []; const shiftData = shiftsResponse?.data || shiftsResponse || []; setEmployees(Array.isArray(userData) ? userData : []); setShifts(Array.isArray(shiftData) ? shiftData : []); } catch (error) { NotifOk({ icon: 'error', title: 'Gagal', message: 'Gagal memuat data karyawan atau shift.', }); } finally { setLoadingData(false); } }; const handleSave = async () => { setConfirmLoading(true); // Daftar aturan validasi const validationRules = [ { field: 'user_id', label: 'Nama Karyawan', required: true }, { field: 'shift_id', label: 'Shift', required: true }, ]; if ( validateRun(formData, validationRules, (errorMessages) => { NotifOk({ icon: 'warning', title: 'Peringatan', message: errorMessages, }); setConfirmLoading(false); }) ) return; try { const payload = { user_id: formData.user_id, shift_id: formData.shift_id, }; // Add schedule_id only if editing and it exists if (props.actionMode === 'edit' && formData.schedule_id) { payload.schedule_id = formData.schedule_id; } const response = props.actionMode === 'edit' ? await updateJadwalShift(formData.id, payload) : await createJadwalShift(payload); if (response && (response.statusCode === 200 || response.statusCode === 201)) { const action = props.actionMode === 'edit' ? 'diubah' : 'ditambahkan'; NotifOk({ icon: 'success', title: 'Berhasil', message: `Jadwal 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); } }; useEffect(() => { if (props.showModal) { fetchData(); } if (props.selectedData) { setFormData({ id: props.selectedData.id || '', user_id: props.selectedData.user_id || null, shift_id: props.selectedData.shift_id || null, schedule_id: props.selectedData.schedule_id || '', user_phone: props.selectedData.whatsapp || props.selectedData.user_phone || null, }); } else { setFormData(defaultData); } }, [props.showModal, props.selectedData, props.actionMode]); return ( {!props.readOnly && ( )} , ]} > {formData && (
Nama Karyawan *
No. Telepon
{formData.user_phone || 'Pilih karyawan terlebih dahulu'}
Shift *
)}
); }; export default DetailJadwalShift;