import React, { useEffect, useRef, useState } from 'react'
import { MultiSelect } from 'react-multi-select-component'
import { Col, Label, Row } from 'reactstrap'
import Select from 'react-select'
import { Button as ButtonP } from 'primereact/button'
import { isObjEmpty } from '../util'
import $ from 'jquery'
import collect from 'collect.js'
import { setSelectedOpsi } from '../store/KpdlStore'
import { useDispatch, useSelector } from 'react-redux'
const NipPengampu = ({ dataSend, setDataSend, activeTab, toast, setHiddenGraphMatoa, dataOpsi }) => {
const dispatch = useDispatch()
const storeKpdl = useSelector((state) => state.kpdl)
const [kanwil, setKanwil] = useState({})
const [kpp, setKpp] = useState({})
const [seksi, setSeksi] = useState([])
const [ar, setAr] = useState([])
const [kanwilSelected, setKanwilSelected] = useState(null)
const [kppSelected, setKppSelected] = useState({})
const [seksiSelected, setSeksiSelected] = useState([])
const [arSelected, setArSelected] = useState([])
const base_url = '=base_url()?>'
useEffect(() => {
$.ajax({
url: base_url + 'kewilayahan/ref/kanwil',
method: 'GET',
dataType: 'json',
success: (data) => {
setKanwil(data)
}
})
}, [])
useEffect(() => {
setKpp(null)
setSeksi([])
setAr([])
setKppSelected(null)
setSeksiSelected([])
setArSelected([])
if (kanwilSelected && !isObjEmpty(kanwilSelected)) {
const kanwil = kanwilSelected.value
$.ajax({
url: base_url + 'kewilayahan/ref/kpp',
method: 'GET',
dataType: 'json',
data: { kanwil },
success: (data) => {
setKpp(data)
}
})
}
}, [kanwilSelected])
useEffect(() => {
setSeksi([])
setAr([])
setSeksiSelected([])
setArSelected([])
if (kppSelected && !isObjEmpty(kppSelected)) {
const kpp = kppSelected.value
$.ajax({
url: base_url + 'kewilayahan/ref/seksi',
method: 'GET',
dataType: 'json',
data: { kpp },
success: (data) => {
setSeksi(data)
}
})
}
}, [kppSelected])
useEffect(() => {
setAr([])
setArSelected([])
const seksi = collect(seksiSelected).pluck('value').all()
if (seksi.length && !isObjEmpty(seksiSelected)) {
const kpp = kppSelected.value
$.ajax({
url: base_url + 'kewilayahan/ref/ar',
method: 'POST',
dataType: 'json',
data: { kpp, seksi },
success: (data) => {
setAr(data)
}
})
}
}, [seksiSelected])
const buttonProsesOnClick = () => {
const nip_ar_pengampu = collect(arSelected).pluck('value').all()
if (nip_ar_pengampu.length) {
dispatch(setSelectedOpsi(dataOpsi.pengampu))
setDataSend({ opsiWilZona: dataOpsi.pengampu.key, nip_ar_pengampu })
setHiddenGraphMatoa(true)
} else {
toast.current.show({ severity: 'info', summary: 'Info', detail: 'AR Pengampu harus dipilih' })
}
}
return (
<>
buttonProsesOnClick()} label="Proses" severity="" rounded className="w-10rem text-white text-base" />
>
)
}
export default NipPengampu