"use client"
import useSWR, { SWRConfig } from 'swr'
import { useEffect, useState } from 'react';
import { useSearchParams, useRouter, usePathname } from 'next/navigation'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faSearch } from "@fortawesome/free-solid-svg-icons";
import { LoaderJo } from '../component/loader';
import { NotFound } from '../component/notFound';
import { ProdukCard } from '../library/card';
import { capitalize } from '@/app/library/global';
const fetcher = (...args) => fetch(...args).then((res) => res.json());
export default function Search({ params }) {
const Allowed = ['sale', 'rent', 'indekos'];
let slug = params.name;
const pageAllow = slug.some((url) => Allowed.includes(url));
if (!pageAllow) {
return ;
} else {
const searchParams = useSearchParams();
const router = useRouter();
const pathname = usePathname();
const keyword = (searchParams.get('keyword') ? searchParams.get('keyword') : '');
const type = (searchParams.get('type') ? searchParams.get('type') : '');
const order = (searchParams.get('order') ? searchParams.get('order') : '');
const [Label, setLabel] = useState('');
const [Keyword, setKeyword] = useState(keyword);
const [Type, setType] = useState(type);
const [Order, setOrder] = useState(order);
const keyDownAction = function(event){
if (event.code == 'NumpadEnter' || event.code == 'Enter'){
goSeach();
}
}
let search = '?status='+(slug[0]==='sale' ? '1' : slug[0]==='rent' ? '2' : '0')+'&';
searchParams.forEach((value, key) => {
search=search+key+'='+value+'&';
});
let url = '';
const goSeach = function(){
if (Keyword!=='' || Type!=='' || Order!=='') {
url = '?' +
(Keyword!=='' ? 'keyword='+Keyword+'&' : '') +
(Type!=='' ? 'type='+Type+'&' : '') +
(Order!=='' ? 'order='+Order+'&' : '');
url = url.slice(0, -1)
}
router.push(pathname + url);
}
const searchButton = function(){
goSeach();
}
const fillterButton = function(){
setOrder(document.getElementById("filter").value);
//goSeach();
}
const { data : dListing } = useSWR(`/api/listing`+search.slice(0, -1), fetcher);
const [dataListing, setListing] = useState([]);
useEffect(() => {
if(dListing){
setListing(dListing.message)
}
}, [dListing]);
return (
<>
Cari Properti {slug[0]==='sale' ? 'Dijual' : slug[0]==='rent' ? 'Disewa' : 'Indekos'}
{slug[0]!=='indekos'
?
-
:
}
{capitalize(keyword=='' ? 'Cari Properti' : Keyword.replace(/\-/g, ' '))} - RumahJo Cara Tepat Jual Cepat
{dataListing.map((z, k) => {
z.key = k;
return (
z.reg==='1'
?
:
)
})}
>
)
}
}