import React, { useEffect, useRef, useState } from 'react'
import { Link, Head, useForm } from '@inertiajs/inertia-react'
import { toast } from 'react-toastify'
import { statuses } from '@/utils'
import { useModalState } from '@/Hooks'
import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout'
import PrimaryButton from '@/Components/PrimaryButton'
import InputLabel from '@/Components/InputLabel'
import TextInput from '@/Components/TextInput'
import InputError from '@/Components/InputError'
import InputFile from '@/Components/InputFile'
import ModalReminder from './ModalReminder'
import { IconCross } from '@/Icons'
export default function FormDocument(props) {
const inputDocument = useRef()
const { types, departments, doc }= props
const [reminders, setReminders] = useState([])
const { data, setData, post, processing, errors, reset } = useForm({
no_doc: '',
email: '',
type_doc_id: '1',
department_id: '1',
company_name: '',
first_person_name: '',
second_person_name: '',
start_date: '',
end_date: '',
pic_name: '',
note: '',
document: null,
document_name: '',
status: 0,
reminders: []
});
useEffect(() => {
if(doc !== undefined) {
setData({
no_doc: doc.no_doc,
email: doc.email,
type_doc_id: doc.type_doc_id,
department_id: doc.department_id,
company_name: doc.company_name,
first_person_name: doc.first_person_name,
second_person_name: doc.second_person_name,
start_date: doc.start_date,
end_date: doc.end_date,
pic_name: doc.pic_name,
note: doc.note,
document: null,
document_name: doc.document,
status: doc.status,
reminders: doc.reminders.map(r => r.date)
})
setReminders(doc.reminders.map(r => r.date))
}
}, [doc]);
const reminderModal = useModalState(false)
const handleAddReminder = (date) => {
setReminders(reminders.concat(date))
setData('reminders', reminders.concat(date))
}
const handleRemoveReminder = (index) => {
const r = reminders.filter((_, i) => i !== index)
setReminders(r)
setData('reminders', r)
}
const onHandleChange = (event) => {
setData(event.target.name, event.target.type === 'checkbox' ? event.target.checked : event.target.value);
};
const submit = (e) => {
e.preventDefault();
if(doc !== undefined) {
post(route('docs.update', doc), {
onError: () => toast.error('please recheck the data')
});
return
}
post(route('docs.store'), {
onError: () => toast.error('please recheck the data')
});
};
return (
Dokumen