<?php
include('session_handler.php');

// Pastikan hanya admin dan staf boleh akses
if (!checkRoles(['superadmin', 'admin', 'staf'])) {
    // Jika bukan admin atau staf, redirect atau boleh tunjuk mesej tak dibenarkan
    header('Location: unauthorized.php'); 
    exit;
}

// Dapatkan jumlah rekod dari setiap jadual
$queryStaff = "SELECT COUNT(*) as total FROM users";
$resultStaff = $conn->query($queryStaff);
$rowStaff = $resultStaff->fetch_assoc();
$totalStaff = $rowStaff['total'];

$queryJabatan = "SELECT COUNT(*) as total FROM jabatan";
$resultJabatan = $conn->query($queryJabatan);
$rowJabatan = $resultJabatan->fetch_assoc();
$totalJabatan = $rowJabatan['total'];

$queryBahagian = "SELECT COUNT(*) as total FROM bahagian";
$resultBahagian = $conn->query($queryBahagian);
$rowBahagian = $resultBahagian->fetch_assoc();
$totalBahagian = $rowBahagian['total'];

$querySurat = "SELECT COUNT(*) as total FROM surat";
$resultSurat = $conn->query($querySurat);
$rowSurat = $resultSurat->fetch_assoc();
$totalSurat = $rowSurat['total'];

$conn->close(); // Tutup sambungan database
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <!-- Header part -->
    <?php include('header.php'); ?>
</head>
<body class="skin-blue fixed-layout">
    <!-- ============================================================== -->
    <!-- Preloader - style you can find in spinners.css -->
    <!-- ============================================================== -->
    <div class="preloader">
        <div class="loader">
            <div class="loader__figure"></div>
            <p class="loader__label">SPNR Surat/Memo</p>
        </div>
    </div>
    <!-- ============================================================== -->
    <!-- Main wrapper - style you can find in pages.scss -->
    <!-- ============================================================== -->
    <div id="main-wrapper">
        <!-- ============================================================== -->
        <!-- Topbar header - style you can find in pages.scss -->
        <!-- ============================================================== -->
       <?php
include('topbar.php');
?>
        <!-- ============================================================== -->
        <!-- End Topbar header -->
        <!-- ============================================================== -->
        <!-- ============================================================== -->
        <!-- Left Sidebar - style you can find in sidebar.scss  -->
        <!-- ============================================================== -->
         <?php
include('leftsidebar.php');
?>
        <!-- ============================================================== -->
        <!-- End Left Sidebar - style you can find in sidebar.scss  -->
        <!-- ============================================================== -->
        <!-- ============================================================== -->
        <!-- Page wrapper  -->
        <!-- ============================================================== -->
        <div class="page-wrapper">
            <!-- ============================================================== -->
            <!-- Container fluid  -->
            <!-- ============================================================== -->
            <div class="container-fluid">
                <!-- ============================================================== -->
                <!-- Bread crumb and right sidebar toggle -->
                <!-- ============================================================== -->
                <div class="row page-titles">
                    <div class="col-md-5 align-self-center">
                        <h4 class="text-themecolor">Halaman Utama</h4>
                    </div>
                    <div class="col-md-7 align-self-center text-end">
                        <div class="d-flex justify-content-end align-items-center">
                            <ol class="breadcrumb justify-content-end">
                                <li class="breadcrumb-item"><a href="javascript:void(0)">Home</a></li>
                                <li class="breadcrumb-item active">Dashboard </li>
                            </ol>
                            <!-- <button type="button" class="btn btn-info d-none d-lg-block m-l-15 text-white"><i class="fa fa-plus-circle"></i> Create New</button> -->
                        </div>
                    </div>
                </div>
                <!-- Info box -->
                <!-- ============================================================== -->
                <div class="row g-0">
                    <div class="col-lg-3 col-md-3 col-sm-6 col-6">
                        <div class="card border">
                            <div class="card-body">
                                <div class="row">
                                    <div class="col-md-12">
                                        <div class="d-flex no-block align-items-center">
                                            <div>
                                                <h3><i class="far fa-user"></i></h3>
                                                <p class="text-muted">STAFF</p>
                                            </div>
                                            <div class="ms-auto">
                            <h2 class="counter text-primary"><?php echo $totalStaff; ?></h2>
                                    </div>
                                        </div>
                                    </div>
                                    <div class="col-12">
                                        <div class="progress">
                                            <div class="progress-bar bg-primary" role="progressbar" style="width: 85%; height: 6px;" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100"></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                   <div class="col-lg-3 col-md-3 col-sm-6 col-6">
                        <div class="card border">
                            <div class="card-body">
                                <div class="row">
                                    <div class="col-md-12">
                                        <div class="d-flex no-block align-items-center">
                                            <div>
                                                <h3><i class="icon-note"></i></h3>
                                                <p class="text-muted">JABATAN</p>
                                            </div>
                                            <div class="ms-auto">
                            <h2 class="counter text-cyan"><?php echo $totalJabatan; ?></h2>
                        </div>
                                        </div>
                                    </div>
                                    <div class="col-12">
                                        <div class="progress">
                                            <div class="progress-bar bg-cyan" role="progressbar" style="width: 85%; height: 6px;" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100"></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="col-lg-3 col-md-3 col-sm-6 col-6">
                        <div class="card border">
                            <div class="card-body">
                                <div class="row">
                                    <div class="col-md-12">
                                        <div class="d-flex no-block align-items-center">
                                            <div>
                                                <h3><i class="icon-doc"></i></h3>
                                                <p class="text-muted">BAHAGIAN</p>
                                            </div>
                                            <div class="ms-auto">
                            <h2 class="counter text-purple"><?php echo $totalBahagian; ?></h2>
                        </div>
                                        </div>
                                    </div>
                                    <div class="col-12">
                                        <div class="progress">
                                            <div class="progress-bar bg-purple" role="progressbar" style="width: 85%; height: 6px;" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100"></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="col-lg-3 col-md-3 col-sm-6 col-6">
                        <div class="card border">
                            <div class="card-body">
                                <div class="row">
                                    <div class="col-md-12">
                                        <div class="d-flex no-block align-items-center">
                                            <div>
                                                <h3><i class="ti-email"></i></h3>
                                                <p class="text-muted">NO. RUJUKAN</p>
                                            </div>
                                             <div class="ms-auto">
                            <h2 class="counter text-success"><?php echo $totalSurat; ?></h2>
                        </div>
                                        </div>
                                    </div>
                                    <div class="col-12">
                                        <div class="progress">
                                            <div class="progress-bar bg-success" role="progressbar" style="width: 85%; height: 6px;" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100"></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                
            </div>
        </div>
       
        <!-- footer -->
        <!-- ============================================================== -->
        <?php
include('footer.php');
?>
        <!-- ============================================================== -->
        <!-- End footer -->
        <!-- ============================================================== -->
    </div>
    
    <!-- All Jquery -->
    <!-- ============================================================== -->
     <?php
include('jquery.php');
?>
</body>
</html>