import React from "react";
import UserLayout from "./Layout/UserLayout";
import { Link, router } from "@inertiajs/react";
import { IoGrid } from "react-icons/io5";
import Swal from "sweetalert2";

const Materi = (props) => {
    const { module } = props;

    // console.log(props);

    const image = module.name.toLowerCase().includes("baca")
        ? "bgbaca.jpg"
        : module.name.toLowerCase().includes("hitung")
        ? "bghitung.jpg"
        : "bgkuis.jpg";

    const goToMateriQuiz = (item) => {
        if (item.kategori == "materi") {
            router.get(`/user/materi/${item.id}`);
        } else {
            Swal.fire({
                title: "Mulai Quiz",
                text: "Apakah anda yakin ingin memulai quiz?",
                icon: "question",
                showCancelButton: true,
                confirmButtonText: "Ya",
                cancelButtonText: "Tidak",
            }).then(async (result) => {
                if (result.isConfirmed) {
                    router.get(`/user/quiz/${item.id}`);
                }
            });
        }
    };

    return (
        <UserLayout bg={image} showBack={true} showNavbar={false}>
            <div className="w-full flex flex-col py-4 container">
                <h1 className="font-semibold text-xl py-4">
                    Pilih Materi Pembelajaran
                </h1>
                <div className="grid grid-cols-1 md:grid-cols-2 gap-3">
                    {props.data.map((item, index) => (
                        <div onClick={() => goToMateriQuiz(item)} key={index}>
                            <CardMaterial
                                title={item.name}
                                kategori={item.kategori}
                            />
                        </div>
                    ))}
                </div>
            </div>
        </UserLayout>
    );
};

const CardMaterial = ({ title, kategori }) => {
    return (
        <div className="flex flex-row p-4 border-2 rounded-md shadow-md gap-3 hover:bg-gray-100 bg-white">
            <img className="w-16" src="/assets/images/book2.png" alt="" />
            <div className="flex flex-col">
                <h1 className="font-bold text-blue-800">{title}</h1>
                <p className="line-clamp-2 text-xs mt-2 text-orange-500">
                    Kategori: {kategori}
                </p>
            </div>
        </div>
    );
};

export default Materi;
