<?php

session_start();

if(!isset($_SESSION['role'])){

    header("Location: ../index.php");
    exit;
}

if($_SESSION['role'] != 'cs'){

    header("Location: ../index.php");
    exit;
}

include '../config/koneksi.php';

$user_id = $_SESSION['id'];

$cs = mysqli_fetch_assoc(
mysqli_query(
$conn,
"SELECT * FROM cs_rumah_ibadah
WHERE user_id='$user_id'"
)
);

$cs_id = $cs['id'];

/* TOTAL */

$total = mysqli_num_rows(

mysqli_query($conn,

"SELECT * FROM bantuan
WHERE cs_id='$cs_id'")

);

/* PROSES */

$proses = mysqli_num_rows(

mysqli_query($conn,

"SELECT * FROM bantuan

WHERE cs_id='$cs_id'
AND status='proses'")

);

/* SELESAI */

$selesai = mysqli_num_rows(

mysqli_query($conn,

"SELECT * FROM bantuan

WHERE cs_id='$cs_id'
AND status='selesai'")

);

$persentase = 0;

if($total > 0){
    $persentase = round(($selesai / $total) * 100);
}
?>

<!DOCTYPE html>
<html>

<head>

    <title>Dashboard CS</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" />


</head>

<body class="bg-slate-100 min-h-screen">

    <!-- NAVBAR -->
    <nav class="bg-slate-800 text-white shadow">

        <div class="max-w-7xl mx-auto px-4 py-4 flex flex-col md:flex-row justify-between items-center gap-3">

            <h2 class="font-bold text-xl">
                Dashboard CS
            </h2>

            <div class="flex flex-col sm:flex-row items-center gap-3">

                <span>
                    Halo,
                    <?= htmlspecialchars($_SESSION['nama']) ?>
                </span>

                <a href="../logout.php" onclick="return confirm('Logout sekarang?')"
                    class="bg-red-500 hover:bg-red-600 px-4 py-2 rounded-lg transition">

                    Logout

                </a>

            </div>

        </div>

    </nav>

    <!-- CONTENT -->
    <div class="max-w-7xl mx-auto p-6">

        <!-- HERO -->
        <div class="bg-gradient-to-r from-emerald-600 to-teal-600 text-white rounded-3xl p-8 shadow-lg">

            <h1 class="text-3xl font-bold">
                Selamat Datang 👋
                <?= htmlspecialchars($_SESSION['nama']) ?>
            </h1>

            <p class="mt-3 text-emerald-100">
                Kelola tugas bantuan sosial yang telah diberikan oleh admin.
            </p>

            <div class="flex flex-col sm:flex-row gap-3 mt-6">

                <a href="tugas.php"
                    class="bg-white text-emerald-600 font-semibold px-6 py-3 rounded-xl shadow hover:shadow-lg transition">

                    <i class="fa-solid fa-list-check mr-2"></i>
                    Lihat Tugas

                </a>

            </div>

        </div>

        <!-- CARDS -->
        <div class="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-6 mt-8">

            <!-- Total -->
            <div class="bg-white rounded-2xl shadow p-6">

                <i class="fa-solid fa-list text-4xl text-emerald-600"></i>

                <p class="text-slate-500 mt-3">
                    Total Tugas
                </p>

                <h2 class="text-4xl font-bold">
                    <?= $total ?>
                </h2>

            </div>

            <!-- Diproses -->
            <div class="bg-white rounded-2xl shadow p-6">

                <i class="fa-solid fa-spinner animate-spin text-4xl text-blue-500"></i>

                <p class="text-slate-500 mt-3">
                    Diproses
                </p>

                <h2 class="text-4xl font-bold">
                    <?= $proses ?>
                </h2>

            </div>

            <!-- Selesai -->
            <div class="bg-white rounded-2xl shadow p-6">

                <i class="fa-solid fa-circle-check text-4xl text-green-500"></i>

                <p class="text-slate-500 mt-3">
                    Selesai
                </p>

                <h2 class="text-4xl font-bold">
                    <?= $selesai ?>
                </h2>

            </div>

        </div>

        <div class="bg-white rounded-2xl shadow p-6 mt-8">

            <h3 class="font-semibold text-slate-700">
                Progress Penyelesaian Bantuan
            </h3>

            <div class="w-full bg-slate-200 rounded-full h-4 mt-4">

                <div class="bg-green-500 h-4 rounded-full" style="width: <?= $persentase ?>%">
                </div>

            </div>

            <p class="mt-3 text-slate-500">

                <?= $persentase ?>% tugas bantuan telah selesai disalurkan

            </p>

        </div>

    </div>

</body>


</html>