<?php

require_once '../includes/auth.php';

require_login();

/*
|--------------------------------------------------------------------------
| FETCH MEMBERS
|--------------------------------------------------------------------------
*/

$stmt = $db->query("

SELECT

cm.*,

p.platoon_name,

h.hostel_name,

cg.group_name

FROM corps_members cm

LEFT JOIN platoons p
ON p.id = cm.platoon_id

LEFT JOIN hostels h
ON h.id = cm.hostel_id

LEFT JOIN cds_group_members cgm
ON cgm.corps_member_id = cm.id

LEFT JOIN cds_groups cg
ON cg.id = cgm.cds_group_id

ORDER BY

cm.firstname,
cm.lastname

");

$members = $stmt->fetchAll(PDO::FETCH_ASSOC);

$totalMembers = count($members);

$male = 0;
$female = 0;

foreach($members as $member){

    if($member['gender']=="Male"){

        $male++;

    }else{

        $female++;

    }

}

?>

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>

        Corps Members Report

    </title>

    <style>
    body {

        font-family: Arial, Helvetica, sans-serif;

        font-size: 13px;

        color: #222;

        margin: 30px;

    }

    .header {

        text-align: center;

        margin-bottom: 20px;

    }

    .header img {

        width: 90px;

        margin-bottom: 10px;

    }

    .header h2 {

        margin: 3px 0;

    }

    .header h3 {

        margin: 3px 0;

    }

    .summary {

        margin: 20px 0;

    }

    .summary table {

        width: 100%;

        border-collapse: collapse;

    }

    .summary td {

        border: 1px solid #000;

        padding: 8px;

    }

    table.report {

        width: 100%;

        border-collapse: collapse;

        margin-top: 20px;

    }

    table.report th {

        background: #0d6efd;

        color: #fff;

        border: 1px solid #000;

        padding: 8px;

        font-size: 12px;

    }

    table.report td {

        border: 1px solid #000;

        padding: 7px;

        font-size: 12px;

    }

    .footer {

        margin-top: 40px;

        display: flex;

        justify-content: space-between;

    }

    @media print {

        button {

            display: none;

        }

        body {

            margin: 10px;

        }

    }
    </style>

</head>

<body>

    <div class="header">

        <img src="../assets/images/nysc-logo.png">

        <h2>

            NATIONAL YOUTH SERVICE CORPS

        </h2>

        <h3>

            Corps Members Report

        </h3>

        <p>

            Generated on

            <strong>

                <?= date('d F Y h:i A') ?>

            </strong>

        </p>

    </div>

    <div class="summary">

        <table>

            <tr>

                <td>

                    <strong>Total Members</strong>

                    <br>

                    <?= $totalMembers ?>

                </td>

                <td>

                    <strong>Male</strong>

                    <br>

                    <?= $male ?>

                </td>

                <td>

                    <strong>Female</strong>

                    <br>

                    <?= $female ?>

                </td>

                <td>

                    <strong>Printed By</strong>

                    <br>

                    <?= htmlspecialchars($user['firstname']." ".$user['lastname']) ?>

                </td>

            </tr>

        </table>

    </div>

    <table class="report">

        <thead>

            <tr>

                <th>#</th>

                <th>State Code</th>

                <th>Name</th>

                <th>Gender</th>

                <th>Phone</th>

                <th>Platoon</th>

                <th>Hostel</th>

                <th>CDS</th>

                <th>Status</th>

            </tr>

        </thead>

        <tbody>

            <?php

$count=1;

foreach($members as $member):

?>

            <tr>

                <td>

                    <?= $count++ ?>

                </td>

                <td>

                    <?= htmlspecialchars($member['state_code']) ?>

                </td>

                <td>

                    <?= htmlspecialchars(

$member['firstname']." ".$member['lastname']

) ?>

                </td>

                <td>

                    <?= htmlspecialchars($member['gender']) ?>

                </td>

                <td>

                    <?= htmlspecialchars($member['phone']) ?>

                </td>

                <td>

                    <?= htmlspecialchars($member['platoon_name']) ?>

                </td>

                <td>

                    <?= htmlspecialchars($member['hostel_name']) ?>

                </td>

                <td>

                    <?= htmlspecialchars($member['group_name']) ?>

                </td>

                <td>

                    <?= htmlspecialchars($member['status']) ?>

                </td>

            </tr>

            <?php endforeach; ?>

        </tbody>

    </table>

    <div class="footer">

        <div>

            _____________________________

            Camp Commandant

        </div>

        <div>

            _____________________________

            Local Government Inspector

        </div>

    </div>

    <script>
    window.print();
    </script>

</body>

</html>