<?php

session_start();

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

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

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

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

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

$id = intval($_SESSION['id']);

$total = mysqli_num_rows(

mysqli_query($conn,

"SELECT * FROM laporan
WHERE pelapor_id='$id'")

);

$pending = mysqli_num_rows(

mysqli_query($conn,

"SELECT * FROM laporan

WHERE pelapor_id='$id'
AND status='pending'")

);

$disetujui = mysqli_num_rows(

mysqli_query($conn,

"SELECT * FROM laporan

WHERE pelapor_id='$id'
AND status='disetujui'")

);

$diproses = mysqli_num_rows(

mysqli_query($conn,

"SELECT * FROM laporan

WHERE pelapor_id='$id'
AND status='diproses'")

);

$selesai = mysqli_num_rows(

mysqli_query($conn,

"SELECT * FROM laporan

WHERE pelapor_id='$id'
AND status='selesai'")

);

$ditolak = mysqli_num_rows(

mysqli_query($conn,

"SELECT * FROM laporan

WHERE pelapor_id='$id'
AND status='ditolak'")

);

$persentase = 0;

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

<!DOCTYPE html>
<html>

<head>

    <title>Dashboard Warga</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">
                WebGIS Kemiskinan
            </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-cyan-600 to-blue-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-cyan-100">
                Laporkan kondisi warga yang membutuhkan bantuan sosial
                di sekitar lingkungan Anda.
            </p>

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

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

                    <i class="fa-solid fa-plus mr-2"></i>
                    Buat Laporan

                </a>

                <a href="status.php"
                    class="bg-slate-800 text-white font-semibold px-6 py-3 rounded-xl shadow hover:bg-slate-900 transition">

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

                </a>

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

                <p class="font-semibold">
                    Progress Bantuan
                </p>

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

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

                </div>

                <p class="mt-2 text-sm text-slate-500">
                    <?= $persentase ?>% laporan telah selesai
                </p>

            </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-file text-4xl text-blue-600"></i>
                <p class="text-slate-500 mt-3">Total Laporan</p>
                <h2 class="text-4xl font-bold"><?= $total ?></h2>
            </div>

            <!-- Pending -->
            <div class="bg-white rounded-2xl shadow p-6">
                <i class="fa-solid fa-clock text-4xl text-yellow-500"></i>
                <p class="text-slate-500 mt-3">Pending</p>
                <h2 class="text-4xl font-bold"><?= $pending ?></h2>
            </div>

            <!-- Disetujui -->
            <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">Disetujui</p>
                <h2 class="text-4xl font-bold"><?= $disetujui ?></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"><?= $diproses ?></h2>
            </div>

            <!-- Selesai -->
            <div class="bg-white rounded-2xl shadow p-6">
                <i class="fa-solid fa-hand-holding-heart text-4xl text-purple-500"></i>
                <p class="text-slate-500 mt-3">Selesai</p>
                <h2 class="text-4xl font-bold"><?= $selesai ?></h2>
            </div>

            <!-- Ditolak -->
            <div class="bg-white rounded-2xl shadow p-6">
                <i class="fa-solid fa-circle-xmark text-4xl text-red-500"></i>
                <p class="text-slate-500 mt-3">Ditolak</p>
                <h2 class="text-4xl font-bold"><?= $ditolak ?></h2>
            </div>

        </div>

    </div>

</body>

</html>