<?php include('include/headscript.php'); ?>
<?php
// Connessione al database
$conn = new mysqli($servername, $username, $password, $database);

// Query 1: Numero totale di prodotti
$totalProductsQuery = "SELECT COUNT(DISTINCT p.idproducts) AS totalProducts FROM products p WHERE 1=1";
$totalProductsResult = $conn->query($totalProductsQuery);
$totalProducts = $totalProductsResult->fetch_assoc()['totalProducts'];

// Query 2: Numero totale di report
$totalReportsQuery = "
    SELECT COUNT(DISTINCT r.idreports) AS totalReports 
    FROM reports r 
    LEFT JOIN products p ON r.idproducts = p.idproducts";
$totalReportsResult = $conn->query($totalReportsQuery);
$totalReports = $totalReportsResult->fetch_assoc()['totalReports'];

// Query 3: Numero di report "fail"
$failedReportsQuery = "
    SELECT COUNT(DISTINCT r.idreports) AS failedReports 
    FROM reports r 
    LEFT JOIN products p ON r.idproducts = p.idproducts 
    WHERE UPPER(r.reportsRating) IN ('FAIL', 'F', 'DOESN\'T COMPLY')";
$failedReportsResult = $conn->query($failedReportsQuery);
$failedReports = $failedReportsResult->fetch_assoc()['failedReports'];

$totalTestsQuery = "
    SELECT COUNT(DISTINCT ap.idreports, ap.idPart, ap.result_TestName) AS totalTests 
    FROM analysis_project ap
    LEFT JOIN result_project rp ON ap.idAnalysis_Project = rp.idanalysis_project
    LEFT JOIN reports r ON ap.idreports = r.idreports
    LEFT JOIN products p ON r.idproducts = p.idproducts";
$totalTestsResult = $conn->query($totalTestsQuery);
$totalTests = $totalTestsResult->fetch_assoc()['totalTests'];
// Verifica connessione
if ($conn->connect_error) {
    die("Connessione fallita: " . $conn->connect_error);
}
?>
<?php
// Connessione al database
$conn = new mysqli($servername, $username, $password, $database);
// Query per ottenere i moduli attivi e disattivi

$query = "
    SELECT idmodules, activemod
    FROM activemodules
    WHERE idcompany = ?
";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $idcompany);
$stmt->execute();
$result = $stmt->get_result();

$modulesStatus = [];
while ($row = $result->fetch_assoc()) {
    $modulesStatus[$row['idmodules']] = $row['activemod'];
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
    <?php include('include/seo.php'); ?>

    <meta http-equiv="X-UA-Compatible" content="IE=edge" />

    <link rel="shortcut icon" href="assets/images/favicon.ico">

    <link href="assets/css/bootstrap.min.css" rel="stylesheet" type="text/css">
    <link href="assets/css/icons.css" rel="stylesheet" type="text/css">
    <link href="assets/css/style.css" rel="stylesheet" type="text/css">
    <link href="https://cdn.jsdelivr.net/npm/boxicons@2.0.7/css/boxicons.min.css" rel="stylesheet">

</head>


<body class="fixed-left">

    <!-- Loader -->
    <div id="preloader">
        <div id="status">
            <div class="spinner"></div>
        </div>
    </div>

    <!-- Begin page -->
    <div id="wrapper">

        <?php include('include/navigationbar.php'); ?>

        <!-- Start right Content here -->

        <div class="content-page">
            <!-- Start content -->
            <div class="content">

                <?php include('include/topbar.php'); ?>

                <div class="page-content-wrapper ">

                    <div class="container-fluid">

                        <div class="row">
                            <div class="col-sm-12">
                                <div class="page-title-box">
                                    <div class="btn-group float-right">
                                        <ol class="breadcrumb hide-phone p-0 m-0">
                                            <li class="breadcrumb-item"><a href="#">Reportify</a></li>
                                            <li class="breadcrumb-item active">Dashboard</li>
                                        </ol>
                                    </div>
                                    <h4 class="page-title">Dashboard</h4>
                                </div>
                            </div>
                        </div>
                        <!-- end page title end breadcrumb -->
                        <div class="row">
                            <div class="col-lg-3">
                                <div class="card">
                                    <div class="card-body">
                                        <div class="icon-contain">
                                            <div class="row">
                                                <div class="col-2 align-self-center">
                                                    <i class="fas fa-boxes text-gradient-success"></i>
                                                </div>
                                                <div class="col-10 text-right">
                                                    <h5 class="mt-0 mb-1"><?php echo $totalProducts; ?></h5>
                                                    <p class="mb-0 font-12 text-muted">Total Products</p>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="col-lg-3">
                                <div class="card">
                                    <div class="card-body">
                                        <div class="icon-contain">
                                            <div class="row">
                                                <div class="col-2 align-self-center">
                                                    <i class="fas fa-file-alt text-gradient-danger"></i>
                                                </div>
                                                <div class="col-10 text-right">
                                                    <h5 class="mt-0 mb-1"><?php echo $totalReports; ?></h5>
                                                    <p class="mb-0 font-12 text-muted">Total Reports</p>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <!-- Indicatore Report "Fail" -->
                            <div class="col-lg-3">
                                <div class="card">
                                    <div class="card-body">
                                        <div class="icon-contain">
                                            <div class="row">
                                                <div class="col-2 align-self-center">
                                                    <i class="fas fa-times-circle text-gradient-warning"></i>
                                                </div>
                                                <div class="col-10 text-right">
                                                    <h5 class="mt-0 mb-1"><?php echo $failedReports; ?></h5>
                                                    <p class="mb-0 font-12 text-muted">Failed Reports</p>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-lg-3">
                                <div class="card">
                                    <div class="card-body">
                                        <div class="icon-contain">
                                            <div class="row">
                                                <div class="col-2 align-self-center">
                                                    <i class="fas fa-vials text-gradient-primary"></i>
                                                </div>
                                                <div class="col-10 text-right">
                                                    <h5 class="mt-0 mb-1"><?php echo $totalTests; ?></h5>
                                                    <p class="mb-0 font-12 text-muted">Total Tests</p>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>



                        <div class="row">

                            <div class="col-lg-3">
                                <div class="card card-body">
                                    <a href="<?php echo USERAREA_PATH; ?>products/products.php">
                                        <div class="alert alert-warning alert-dismissible fade show px-4 mb-0 text-center" role="alert">
                                            <i class="mdi mdi-database-import d-block display-4 mt-2 mb-3 text-warning"></i>
                                            <h5 class="text-primary"><?php echo $products; ?></h5>
                                            <p class="text-primary"><?php echo $productsslogan; ?></p><br>

                                        </div>
                                    </a>
                                </div>
                            </div>
                            <div class="col-lg-3">
                                <div class="card card-body">
                                    <a href="<?php echo USERAREA_PATH; ?>reports/reports.php">
                                        <div class="alert alert-primary alert-dismissible fade show px-4 mb-0 text-center" role="alert">
                                            <i class="mdi mdi-cloud-sync d-block display-4 mt-2 mb-3 text-primary"></i>
                                            <h5 class="text-primary">Reports</h5>
                                            <p class="text-primary"><?php echo $reportsslogan; ?></p>

                                        </div>
                                    </a>
                                </div>
                            </div>
                            <div class="col-lg-3">
                                <div class="card card-body">
                                    <a href="<?php echo USERAREA_PATH; ?>statkpi/statkpi.php">
                                        <div class="alert alert-info alert-dismissible fade show px-4 mb-0 text-center" role="alert">
                                            <i class="mdi mdi-signal d-block display-4 mt-2 mb-3 text-info"></i>
                                            <h5 class="text-primary"><?php echo $statkpi; ?></h5>
                                            <p class="text-primary"><?php echo $statkpislogan; ?></p>

                                        </div>
                                    </a>
                                </div>
                            </div>
                            <div class="col-lg-3">
                                <div class="card card-body">
                                    <a href="<?php echo USERAREA_PATH; ?>importify/importifydashboard.php">
                                        <div class="alert alert-success alert-dismissible fade show px-4 mb-0 text-center" role="alert">
                                            <i class="mdi mdi-database-import d-block display-4 mt-2 mb-3 text-success"></i>
                                            <h5 class="text-primary"><?php echo $importify; ?></h5>
                                            <p class="text-primary"><?php echo $importifyslogan; ?></p><br>

                                        </div>
                                    </a>
                                </div>
                            </div>

                        </div>


                        <div class="row">
                            <div class="col-lg-3">
                                <div class="card card-body">
                                    <a href="<?php echo USERAREA_PATH; ?>easyspec/rsl.php">
                                        <div class="alert alert-danger alert-dismissible fade show px-4 mb-0 text-center" role="alert">
                                            <i class="mdi mdi-view-list d-block display-4 mt-2 mb-3 text-danger"></i>
                                            <h5 class="text-danger"><?php echo $rsl; ?></h5>
                                            <p><?php echo $easyspecslogan; ?></p><br>

                                        </div>
                                    </a>
                                </div>
                            </div>

                            <div class="col-lg-3">
                                <div class="card card-body">
                                    <a href="importify.php">
                                        <div class="alert alert-danger alert-dismissible fade show px-4 mb-0 text-center" role="alert">
                                            <i class="mdi mdi-database-import d-block display-4 mt-2 mb-3 text-danger"></i>
                                            <h5 class="text-danger">SayTRL</h5>
                                            <p class="text-danger"><?php echo $saytrlslogan; ?></p>

                                        </div>
                                    </a>
                                </div>
                            </div>

                            <div class="col-lg-3">
                                <div class="card card-body">
                                    <a href="importify.php">
                                        <div class="alert alert-info alert-dismissible fade show px-4 mb-0 text-center" role="alert">
                                            <i class="mdi mdi-file-document-box d-block display-4 mt-2 mb-3 text-info"></i>
                                            <h5 class="text-primary">Rate&Go</h5>
                                            <p class="text-primary"><?php echo $rategoslogan; ?></p>

                                        </div>
                                    </a>
                                </div>
                            </div>
                            <div class="col-lg-3">
                                <div class="card card-body">
                                    <a href="importify.php">
                                        <div class="alert alert-warning alert-dismissible fade show px-4 mb-0 text-center" role="alert">
                                            <i class="fas fa-random display-4 mt-2 mb-3 text-warning"></i>
                                            <h5 class="text-primary">ReEvaluate</h5>
                                            <p class="text-primary"><?php echo $reevaluateslogan; ?></p>

                                        </div>
                                    </a>
                                </div>
                            </div>

                        </div>


                        <!-- end row -->

                    </div><!-- container -->

                </div> <!-- Page content Wrapper -->

            </div> <!-- content -->

            <?php include('include/footer.php'); ?>

        </div>
        <!-- End Right content here -->

    </div>
    <!-- END wrapper -->


    <!-- jQuery  -->
    <script src="assets/js/jquery.min.js"></script>
    <script src="assets/js/popper.min.js"></script>
    <script src="assets/js/bootstrap.min.js"></script>
    <script src="assets/js/modernizr.min.js"></script>
    <script src="assets/js/detect.js"></script>
    <script src="assets/js/fastclick.js"></script>
    <script src="assets/js/jquery.slimscroll.js"></script>
    <script src="assets/js/jquery.blockUI.js"></script>
    <script src="assets/js/waves.js"></script>
    <script src="assets/js/jquery.nicescroll.js"></script>
    <script src="assets/js/jquery.scrollTo.min.js"></script>

    <script src="assets/plugins/chart.js/chart.min.js"></script>
    <script src="assets/pages/dashboard.js"></script>

    <!-- App js -->
    <script src="assets/js/app.js"></script>


</body>

</html>