import {useEffect, useState} from "react";
import entry from "./Entry.jsx";

function Result({listEntry, baseSalary}) {
    const [totalOvertimePay, setTotalOvertimePay] = useState(0);

    useEffect(() => {
        setTotalOvertimePay(getOvertimePayTotal(listEntry, baseSalary));
    }, [listEntry, baseSalary]);

    return (
        <div>
            Totalnye: Rp{totalOvertimePay}
            {listEntry.map((entry) => {
                return (
                    <div key={entry.id}>
                        <p>{entry.date.format('DD MMMM YYYY')} dapetnya Rp{calculatePerDay(entry, baseSalary)}</p>
                    </div>
                )
            })}
        </div>
    )
}

function calculatePerDay(entry, baseSalary) {
    const hourlyPay = baseSalary / 173;
    const timeDiff = entry.finish.diff(entry.start, 'hour', true);
    let multiplier = 1.5; // Jam pertama
    if (timeDiff > 1) {
        multiplier += (timeDiff - 1) * 2; // Jam jam berikutnya
    }

    return multiplier * hourlyPay;
}

function getOvertimePayTotal(listEntry, baseSalary) {
    let total = 0;
    listEntry.forEach((entry) => {
        total += calculatePerDay(entry, baseSalary)
    });
    return total;
}

export default Result;