import React, { useState, useEffect } from 'react';
import { Head } from '@inertiajs/react';
import ModalInput from '@/Components/ModalInput';
import { useDispatch } from 'react-redux';
import { setPageTitle } from '@/Components/features/common/headerSlice';
import { CurrencyDollarIcon } from '@heroicons/react/24/outline'


export default function ManualPayment({ wallet }) {
    const [searchTerm, setSearchTerm] = useState('');
    const [filteredSantri, setFilteredSantri] = useState([]);

    const dispatch = useDispatch();

    useEffect(() => {
        dispatch(setPageTitle("Data Dompet Santri"));
    }, [dispatch]);

    // console.log(santri.id)
    useEffect(() => {
        if (wallet?.data) {
            setFilteredSantri(
                wallet.data.filter(item =>
                    item.nama.toLowerCase().includes(searchTerm.toLowerCase()) ||
                    item.nis.toString().includes(searchTerm)
                )
            );
        }
    }, [searchTerm, wallet]);

    const handleSearch = (e) => {
        setSearchTerm(e.target.value);
    };


    return (
        <div>
            <Head title="Dompet Santri" />
            <div className="card bg-base-100 shadow-xl">
                <div className="card-body">
                    <div className="flex items-center mb-6">
                        <div className="bg-gradient-to-tr from-blue-400 to-blue-600 p-3 rounded-lg mr-3">
                            <CurrencyDollarIcon className="h-6 w-6 text-white" />
                        </div>
                        <h1 className="text-2xl font-bold">Data Dompet Santri</h1>
                        <div className="ml-auto">
                            <span className="text-gray-700">Overview</span>
                            <span className="ml-2 text-gray-500">
                                <svg xmlns="http://www.w3.org/2000/svg" className="h-5 w-5 inline" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
                                </svg>
                            </span>
                        </div>
                    </div>
                    <div className="flex justify-between items-center mb-4">
                        <div className="form-control">
                            <div className="input-group">
                                <input
                                    type="text"
                                    placeholder="Cari Santri..."
                                    className="input input-bordered"
                                    value={searchTerm}
                                    onChange={handleSearch}
                                />
                            </div>
                        </div>
                    </div>

                    <div className="overflow-x-auto">
                        <table className="table table-zebra w-full">
                            <thead>
                                <tr>
                                    <th>Nama</th>
                                    <th>Nis</th>
                                    <th>Status</th>
                                    <th>Saldo</th>
                                </tr>
                            </thead>
                            <tbody>
                                {filteredSantri.length > 0 ? (
                                    filteredSantri.map((item, i) => (
                                        <tr key={i}>
                                            <td>
                                                <div className="flex items-center space-x-3">
                                                    <div className="avatar">
                                                        <div className="mask mask-squircle w-12 h-12">
                                                            <img src={`https://ui-avatars.com/api/?name=${item.nama}`} alt="Avatar" />
                                                        </div>
                                                    </div>
                                                    <div>
                                                        <div className="font-bold">{item.nama}</div>
                                                        <div className="text-sm opacity-50">{item.level === 1 ? 'Admin' : 'Santri'}</div>
                                                    </div>
                                                </div>
                                            </td>
                                            <td>{item.nis}</td>
                                            <td>
                                                <div className={`badge ${item.status_santri === 'aktif' ? 'badge-success' : 'badge-warning'} gap-2 text-white`}>
                                                    {item.status_santri || "Open"}
                                                </div>
                                            </td>
                                            <td>
                                                Rp. {item.wallet ? item.wallet.saldo : 'Tidak ada saldo'}
                                            </td>
                                        </tr>
                                    ))
                                ) : (
                                    <tr>
                                        <td colSpan="4" className="text-center">Tidak ada data santri.</td>
                                    </tr>
                                )}
                            </tbody>
                        </table>
                        <div className="flex justify-center mt-4">
                            {wallet.current_page > 1 && (
                                <button
                                    className="btn btn-sm mx-1"
                                    onClick={() => (window.location.href = `?page=1`)}
                                >
                                    &laquo;
                                </button>
                            )}

                            {wallet.current_page > 3 && (
                                <span className="btn btn-sm mx-1 pointer-events-none">...</span>
                            )}

                            {Array.from({ length: wallet.last_page }, (_, i) => i + 1)
                                .filter(
                                    (page) =>
                                        page === 1 ||
                                        page === wallet.last_page ||
                                        Math.abs(page - wallet.current_page) <= 2
                                )
                                .map((page) => (
                                    <button
                                        key={page}
                                        className={`btn btn-sm mx-1 ${wallet.current_page === page ? 'btn-active btn-primary text-white' : ''
                                            }`}
                                        onClick={() => {
                                            window.location.href = `?page=${page}`;
                                        }}
                                    >
                                        {page}
                                    </button>
                                ))}

                            {wallet.current_page < wallet.last_page - 2 && (
                                <span className="btn btn-sm mx-1 pointer-events-none">...</span>
                            )}

                            {wallet.current_page < wallet.last_page && (
                                <button
                                    className="btn btn-sm mx-1"
                                    onClick={() => (window.location.href = `?page=${wallet.last_page}`)}
                                >
                                    &raquo;
                                </button>
                            )}
                        </div>

                    </div>
                </div>
            </div>

        </div>
    );
}
