import { DatatablePrime } from '@/components/Datatables'
import { DialogDelete } from '@/components/Dialog'
import FormJenisBelanja from '@/components/Form/JenisBelanja'
import { Belakang } from '@/components/Layouts'
import { Judul } from '@/components/TextCustom'
import { JenisBelanjaDelete, JenisBelanjaGetOne, JenisBelanjaList } from '@/services/referensi/jenisBelanja-service'
import Head from 'next/head'
import { Button } from 'primereact/button'
import { Column } from 'primereact/column'
import { Toast } from 'primereact/toast'
import { useEffect, useRef, useState } from 'react'

export default function JenisBelanja() {
	const toast = useRef(null)
	const [data, setData] = useState([])
	const [dialogForm, setDialogForm] = useState(false)
	const [dataEdit, setDataEdit] = useState([])
	const [refresh, setRefresh] = useState(0)
	const [search, setSearch] = useState('')
	const [dialogDelete, setDialogDelete] = useState({})

	useEffect(() => {
		let params = {}
		search !== null && search !== '' ? (params.search = search) : ''
		JenisBelanjaList(params).then((res) => setData(res.data))
	}, [refresh, search])

	const editJenisBelanja = (data) => {
		JenisBelanjaGetOne({ ref_id: data.jenis_id }).then((res) => {
			if (res.status === 'ok') {
				setDataEdit(res.data)
				setDialogForm(true)
			} else {
				console.log(res.message)
			}
		})
	}

	const deleteJenisBelanja = () => {
		JenisBelanjaDelete({ ref_id: dialogDelete.ref_id }).then((res) => {
			if (res.status === 'success') {
				setRefresh(Math.random)
				setDialogDelete({ visible: false })
				toast.current.show({
					severity: 'success',
					detail: res.message,
					closable: false,
				})
			} else {
				setDialogDelete({ visible: false })
				toast.current.show({
					severity: 'error',
					detail: res.message,
					closable: false,
				})
			}
		})
	}

	const actionBodyTemplate = (rowData) => {
		return (
			<div className='flex gap-x-2'>
				<Button
					icon='pi pi-pencil'
					className='p-button-sm p-button-rounded'
					onClick={() => editJenisBelanja(rowData)}
				/>
				<Button
					icon='pi pi-trash'
					className='p-button-sm p-button-rounded'
					onClick={() => setDialogDelete({ ref_id: rowData.jenis_id, visible: true })}
				/>
			</div>
		)
	}

	return (
		<>
			<Toast ref={toast} />
			<Head>
				<title>Jenis Belanja</title>
			</Head>
			<Belakang>
				<Judul>Jenis Belanja</Judul>
				<DatatablePrime
					data={data}
					setSearch={setSearch}
					dialogForm={dialogForm}
					setDialogForm={setDialogForm}
					Form={FormJenisBelanja}
					buttonAdd={true}
					buttonAddLabel='Tambah data'
					dataEdit={dataEdit}
					setDataEdit={setDataEdit}
					toast={toast}
					refresh={setRefresh}
				>
					<Column field='nama' header='Nama' sortable></Column>
					<Column header='Action' body={actionBodyTemplate} exportable={false}></Column>
				</DatatablePrime>
			</Belakang>

			{dialogDelete.visible === true && (
				<DialogDelete data={dialogDelete} setDelete={setDialogDelete} onCallback={deleteJenisBelanja} />
			)}
		</>
	)
}
