<?php

$pageTitle = "View Corps Member";

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

require_login();

$user = current_user();


/*
|--------------------------------------------------------------------------
| GET CORPS MEMBER ID
|--------------------------------------------------------------------------
*/

$corpsMemberId = isset($_GET['id'])
    ? (int) $_GET['id']
    : 0;


if ($corpsMemberId <= 0) {

    $_SESSION['error_message'] =
        "Invalid Corps Member selected.";

    header(
        "Location: all-corps-members.php"
    );

    exit;
}


/*
|--------------------------------------------------------------------------
| FETCH CORPS MEMBER
|--------------------------------------------------------------------------
*/

$stmt = $db->prepare("

    SELECT

        cm.*,

        u.status AS account_status,
        u.last_login,
        u.created_at AS account_created_at,

        s.state_name,
        s.state_code AS state_abbreviation,

        lga.lga_name,

        p.platoon_name,
        p.platoon_code,

        h.hostel_name,
        h.hostel_code

    FROM corps_members cm

    LEFT JOIN users u
        ON cm.user_id = u.id

    LEFT JOIN states s
        ON cm.state_id = s.id

    LEFT JOIN local_governments lga
        ON cm.lga_id = lga.id

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

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

    WHERE cm.id = ?

    LIMIT 1

");

$stmt->execute([
    $corpsMemberId
]);

$corpsMember =
    $stmt->fetch(PDO::FETCH_ASSOC);


/*
|--------------------------------------------------------------------------
| CHECK IF CORPS MEMBER EXISTS
|--------------------------------------------------------------------------
*/

if (!$corpsMember) {

    $_SESSION['error_message'] =
        "Corps Member not found.";

    header(
        "Location: all-corps-members.php"
    );

    exit;
}


/*
|--------------------------------------------------------------------------
| PREPARE FULL NAME
|--------------------------------------------------------------------------
*/

$fullName = trim(

    $corpsMember['firstname']

    . ' '

    . ($corpsMember['othername'] ?? '')

    . ' '

    . $corpsMember['lastname']

);


/*
|--------------------------------------------------------------------------
| PASSPORT
|--------------------------------------------------------------------------
*/

if (!empty($corpsMember['passport'])) {

    $passportUrl =
        '../' .
        ltrim(
            $corpsMember['passport'],
            '/'
        );

} else {

    $passportUrl =
        '../assets/images/user-grid/user-grid-img13.png';
}


/*
|--------------------------------------------------------------------------
| STATUS BADGE
|--------------------------------------------------------------------------
*/

$statusClass =
    'bg-neutral-200 text-neutral-600';


switch ($corpsMember['status']) {

    case 'Registered':

        $statusClass =
            'bg-info-100 text-info-600';

        break;

    case 'In Camp':

        $statusClass =
            'bg-warning-100 text-warning-600';

        break;

    case 'Serving':

        $statusClass =
            'bg-success-100 text-success-600';

        break;

    case 'Completed':

        $statusClass =
            'bg-primary-100 text-primary-600';

        break;
}


/*
|--------------------------------------------------------------------------
| ACCOUNT STATUS BADGE
|--------------------------------------------------------------------------
*/

$accountStatusClass =
    'bg-neutral-200 text-neutral-600';


switch ($corpsMember['account_status']) {

    case 'active':

        $accountStatusClass =
            'bg-success-100 text-success-600';

        break;

    case 'inactive':

        $accountStatusClass =
            'bg-warning-100 text-warning-600';

        break;

    case 'suspended':

        $accountStatusClass =
            'bg-danger-100 text-danger-600';

        break;
}


/*
|--------------------------------------------------------------------------
| LOAD HEADER
|--------------------------------------------------------------------------
*/

require_once '../includes/header.php';

require_once '../includes/sidebar.php';

?>


<div class="overlay bg-black bg-opacity-50 w-100 h-100 position-fixed z-9 visibility-hidden opacity-0 duration-300">
</div>


<main class="dashboard-main">


    <?php require_once '../includes/topbar.php'; ?>


    <div class="dashboard-main-body">


        <!-- =========================================================
        BREADCRUMB
        ========================================================== -->

        <div class="breadcrumb d-flex flex-wrap align-items-center justify-content-between gap-3 mb-24">


            <div>

                <h6 class="fw-semibold mb-0">

                    <?= htmlspecialchars($pageTitle); ?>

                </h6>


                <p class="text-neutral-600 mt-4 mb-0">

                    <?= htmlspecialchars(APP_SHORT_NAME); ?>

                    >

                    Corps Members

                    >

                    <?= htmlspecialchars($fullName); ?>

                </p>

            </div>


            <div class="d-flex align-items-center gap-8">


                <a href="corps_members.php" class="btn btn-outline-primary">

                    <i class="ri-arrow-left-line"></i>

                    Back

                </a>


                <a href="edit_corps_member.php?id=<?= (int) $corpsMember['id']; ?>" class="btn btn-primary-600">

                    <i class="ri-edit-2-line"></i>

                    Edit Corps Member

                </a>


            </div>


        </div>



        <!-- =========================================================
        PROFILE HEADER
        ========================================================== -->

        <div class="card radius-12 mb-24">

            <div class="card-body p-24">


                <div class="d-flex flex-wrap align-items-center gap-24">


                    <!-- PASSPORT -->

                    <div>

                        <img src="<?= htmlspecialchars($passportUrl); ?>" alt="Corps Member Passport" width="120"
                            height="120" class="rounded-circle border" style="object-fit: cover;">

                    </div>



                    <!-- PROFILE INFO -->

                    <div class="flex-grow-1">


                        <h4 class="mb-8">

                            <?= htmlspecialchars($fullName); ?>

                        </h4>


                        <div class="d-flex flex-wrap align-items-center gap-12">


                            <span class="<?= $statusClass; ?> px-16 py-6 radius-4 fw-medium text-sm">

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

                            </span>


                            <span class="<?= $accountStatusClass; ?> px-16 py-6 radius-4 fw-medium text-sm">

                                Account:

                                <?= ucfirst(
                                    htmlspecialchars(
                                        $corpsMember['account_status']
                                    )
                                ); ?>

                            </span>


                        </div>


                        <div class="mt-16">


                            <p class="mb-4">

                                <strong>State Code:</strong>

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

                            </p>


                            <p class="mb-4">

                                <strong>Call-Up Number:</strong>

                                <?= htmlspecialchars(
                                    $corpsMember['call_up_number']
                                    ?? '-'
                                ); ?>

                            </p>


                            <p class="mb-0">

                                <strong>Batch / Stream:</strong>

                                <?= htmlspecialchars(
                                    ($corpsMember['batch'] ?? '-')
                                ); ?>

                                /

                                <?= htmlspecialchars(
                                    ($corpsMember['stream'] ?? '-')
                                ); ?>

                            </p>


                        </div>


                    </div>


                </div>


            </div>

        </div>



        <div class="row gy-4">


            <!-- =====================================================
            PERSONAL INFORMATION
            ====================================================== -->

            <div class="col-lg-6">


                <div class="card radius-12 h-100">


                    <div class="card-header">

                        <h6 class="mb-0">

                            <i class="ri-user-line me-8"></i>

                            Personal Information

                        </h6>

                    </div>


                    <div class="card-body">


                        <div class="row gy-3">


                            <div class="col-md-6">

                                <span class="text-secondary-light text-sm">
                                    First Name
                                </span>

                                <p class="fw-semibold mb-0">

                                    <?= htmlspecialchars(
                                        $corpsMember['firstname']
                                    ); ?>

                                </p>

                            </div>


                            <div class="col-md-6">

                                <span class="text-secondary-light text-sm">
                                    Last Name
                                </span>

                                <p class="fw-semibold mb-0">

                                    <?= htmlspecialchars(
                                        $corpsMember['lastname']
                                    ); ?>

                                </p>

                            </div>


                            <div class="col-md-6">

                                <span class="text-secondary-light text-sm">
                                    Other Name
                                </span>

                                <p class="fw-semibold mb-0">

                                    <?= htmlspecialchars(
                                        $corpsMember['othername']
                                        ?? '-'
                                    ); ?>

                                </p>

                            </div>


                            <div class="col-md-6">

                                <span class="text-secondary-light text-sm">
                                    Gender
                                </span>

                                <p class="fw-semibold mb-0">

                                    <?= htmlspecialchars(
                                        $corpsMember['gender']
                                        ?? '-'
                                    ); ?>

                                </p>

                            </div>


                            <div class="col-md-6">

                                <span class="text-secondary-light text-sm">
                                    Date of Birth
                                </span>

                                <p class="fw-semibold mb-0">

                                    <?php

                                    if (
                                        !empty(
                                            $corpsMember['date_of_birth']
                                        )
                                    ) {

                                        echo date(
                                            'd M Y',
                                            strtotime(
                                                $corpsMember[
                                                    'date_of_birth'
                                                ]
                                            )
                                        );

                                    } else {

                                        echo '-';
                                    }

                                    ?>

                                </p>

                            </div>


                            <div class="col-md-6">

                                <span class="text-secondary-light text-sm">
                                    Marital Status
                                </span>

                                <p class="fw-semibold mb-0">

                                    <?= htmlspecialchars(
                                        $corpsMember['marital_status']
                                        ?? '-'
                                    ); ?>

                                </p>

                            </div>


                        </div>


                    </div>


                </div>


            </div>



            <!-- =====================================================
            CONTACT INFORMATION
            ====================================================== -->

            <div class="col-lg-6">


                <div class="card radius-12 h-100">


                    <div class="card-header">

                        <h6 class="mb-0">

                            <i class="ri-phone-line me-8"></i>

                            Contact Information

                        </h6>

                    </div>


                    <div class="card-body">


                        <div class="row gy-3">


                            <div class="col-md-6">

                                <span class="text-secondary-light text-sm">
                                    Email Address
                                </span>

                                <p class="fw-semibold mb-0">

                                    <?= htmlspecialchars(
                                        $corpsMember['email']
                                        ?? '-'
                                    ); ?>

                                </p>

                            </div>


                            <div class="col-md-6">

                                <span class="text-secondary-light text-sm">
                                    Phone Number
                                </span>

                                <p class="fw-semibold mb-0">

                                    <?= htmlspecialchars(
                                        $corpsMember['phone']
                                        ?? '-'
                                    ); ?>

                                </p>

                            </div>


                            <div class="col-12">

                                <span class="text-secondary-light text-sm">
                                    Home Address
                                </span>

                                <p class="fw-semibold mb-0">

                                    <?= nl2br(
                                        htmlspecialchars(
                                            $corpsMember['home_address']
                                            ?? '-'
                                        )
                                    ); ?>

                                </p>

                            </div>


                        </div>


                    </div>


                </div>


            </div>



            <!-- =====================================================
            ACADEMIC INFORMATION
            ====================================================== -->

            <div class="col-lg-6">


                <div class="card radius-12 h-100">


                    <div class="card-header">

                        <h6 class="mb-0">

                            <i class="ri-graduation-cap-line me-8"></i>

                            Academic Information

                        </h6>

                    </div>


                    <div class="card-body">


                        <div class="row gy-3">


                            <div class="col-12">

                                <span class="text-secondary-light text-sm">
                                    Institution
                                </span>

                                <p class="fw-semibold mb-0">

                                    <?= htmlspecialchars(
                                        $corpsMember['institution']
                                        ?? '-'
                                    ); ?>

                                </p>

                            </div>


                            <div class="col-md-6">

                                <span class="text-secondary-light text-sm">
                                    Course of Study
                                </span>

                                <p class="fw-semibold mb-0">

                                    <?= htmlspecialchars(
                                        $corpsMember['course_of_study']
                                        ?? '-'
                                    ); ?>

                                </p>

                            </div>


                            <div class="col-md-6">

                                <span class="text-secondary-light text-sm">
                                    Qualification
                                </span>

                                <p class="fw-semibold mb-0">

                                    <?= htmlspecialchars(
                                        $corpsMember['qualification']
                                        ?? '-'
                                    ); ?>

                                </p>

                            </div>


                            <div class="col-md-6">

                                <span class="text-secondary-light text-sm">
                                    Graduation Year
                                </span>

                                <p class="fw-semibold mb-0">

                                    <?= htmlspecialchars(
                                        $corpsMember['graduation_year']
                                        ?? '-'
                                    ); ?>

                                </p>

                            </div>


                        </div>


                    </div>


                </div>


            </div>



            <!-- =====================================================
            NYSC INFORMATION
            ====================================================== -->

            <div class="col-lg-6">


                <div class="card radius-12 h-100">


                    <div class="card-header">

                        <h6 class="mb-0">

                            <i class="ri-government-line me-8"></i>

                            NYSC Information

                        </h6>

                    </div>


                    <div class="card-body">


                        <div class="row gy-3">


                            <div class="col-md-6">

                                <span class="text-secondary-light text-sm">
                                    State Code
                                </span>

                                <p class="fw-semibold mb-0">

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

                                </p>

                            </div>


                            <div class="col-md-6">

                                <span class="text-secondary-light text-sm">
                                    Call-Up Number
                                </span>

                                <p class="fw-semibold mb-0">

                                    <?= htmlspecialchars(
                                        $corpsMember['call_up_number']
                                        ?? '-'
                                    ); ?>

                                </p>

                            </div>


                            <div class="col-md-6">

                                <span class="text-secondary-light text-sm">
                                    Batch
                                </span>

                                <p class="fw-semibold mb-0">

                                    <?= htmlspecialchars(
                                        $corpsMember['batch']
                                        ?? '-'
                                    ); ?>

                                </p>

                            </div>


                            <div class="col-md-6">

                                <span class="text-secondary-light text-sm">
                                    Stream
                                </span>

                                <p class="fw-semibold mb-0">

                                    <?= htmlspecialchars(
                                        $corpsMember['stream']
                                        ?? '-'
                                    ); ?>

                                </p>

                            </div>


                            <div class="col-md-6">

                                <span class="text-secondary-light text-sm">
                                    Service Year
                                </span>

                                <p class="fw-semibold mb-0">

                                    <?= htmlspecialchars(
                                        $corpsMember['year_of_service']
                                        ?? '-'
                                    ); ?>

                                </p>

                            </div>


                            <div class="col-md-6">

                                <span class="text-secondary-light text-sm">
                                    NYSC Status
                                </span>

                                <p class="fw-semibold mb-0">

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

                                </p>

                            </div>


                        </div>


                    </div>


                </div>


            </div>



            <!-- =====================================================
            CAMP ASSIGNMENT
            ====================================================== -->

            <div class="col-lg-6">


                <div class="card radius-12 h-100">


                    <div class="card-header">

                        <h6 class="mb-0">

                            Camp Assignment

                        </h6>

                    </div>


                    <div class="card-body">


                        <div class="row gy-3">


                            <div class="col-md-6">

                                <span class="text-secondary-light text-sm">
                                    State
                                </span>

                                <p class="fw-semibold mb-0">

                                    <?= htmlspecialchars(
                                        $corpsMember['state_name']
                                        ?? '-'
                                    ); ?>

                                </p>

                            </div>


                            <div class="col-md-6">

                                <span class="text-secondary-light text-sm">
                                    LGA
                                </span>

                                <p class="fw-semibold mb-0">

                                    <?= htmlspecialchars(
                                        $corpsMember['lga_name']
                                        ?? '-'
                                    ); ?>

                                </p>

                            </div>


                            <div class="col-md-6">

                                <span class="text-secondary-light text-sm">
                                    Platoon
                                </span>

                                <p class="fw-semibold mb-0">

                                    <?= htmlspecialchars(
                                        $corpsMember['platoon_name']
                                        ?? '-'
                                    ); ?>

                                </p>

                            </div>


                            <div class="col-md-6">

                                <span class="text-secondary-light text-sm">
                                    Hostel
                                </span>

                                <p class="fw-semibold mb-0">

                                    <?= htmlspecialchars(
                                        $corpsMember['hostel_name']
                                        ?? '-'
                                    ); ?>

                                </p>

                            </div>


                        </div>


                    </div>


                </div>


            </div>



            <!-- =====================================================
            NEXT OF KIN
            ====================================================== -->

            <div class="col-lg-6">


                <div class="card radius-12 h-100">


                    <div class="card-header">

                        <h6 class="mb-0">

                            Next of Kin

                        </h6>

                    </div>


                    <div class="card-body">


                        <div class="row gy-3">


                            <div class="col-md-6">

                                <span class="text-secondary-light text-sm">
                                    Name
                                </span>

                                <p class="fw-semibold mb-0">

                                    <?= htmlspecialchars(
                                        $corpsMember['next_of_kin']
                                        ?? '-'
                                    ); ?>

                                </p>

                            </div>


                            <div class="col-md-6">

                                <span class="text-secondary-light text-sm">
                                    Phone Number
                                </span>

                                <p class="fw-semibold mb-0">

                                    <?= htmlspecialchars(
                                        $corpsMember['next_of_kin_phone']
                                        ?? '-'
                                    ); ?>

                                </p>

                            </div>


                        </div>


                    </div>


                </div>


            </div>


        </div>


    </div>


</main>


<?php

require_once "../includes/footer.php";

?>