<?php

session_start();

if(
    !isset($_SESSION['role']) ||
    $_SESSION['role'] != "admin"
){
    die("No access");
}

include("../../../config/db.php");

include(
$_SERVER['DOCUMENT_ROOT'] .
"/SEAT/modules/Notification/notification_helper.php"
);

/* =====================================================
   TEMPLATE
===================================================== */

include("../../../templates/header.php");
include("../../../templates/sidebar.php");


/* =====================================================
   ADMIN DATA
===================================================== */

$user_id = (int)$_SESSION['id'];

$adminQuery = $conn->prepare("

SELECT

a.id,
a.user_id,
a.full_name,
a.phone_number,
a.created_at,

u.email,
u.status

FROM admins a

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

WHERE a.user_id = ?

LIMIT 1

");

$adminQuery->bind_param(
    "i",
    $user_id
);

$adminQuery->execute();

$admin =
$adminQuery
->get_result()
->fetch_assoc();


/* =====================================================
   DEACTIVATE ORGANIZER
===================================================== */

if(
    $_SERVER['REQUEST_METHOD'] == "POST"
    &&
    isset($_POST['deactivate_id'])
){

    $id = (int)$_POST['deactivate_id'];

    mysqli_query($conn,"
        UPDATE users
        SET status='inactive'
        WHERE id = $id
        AND role='organizer'
    ");

    /* =================================================
       NOTIFICATION
    ================================================= */

    notify(
        $conn,
        $id,
        "Account Deactivated",
        "Your account has been deactivated due to not posting any events for the last 6 months. Please contact admin for further assistance.",
        "account",
        $id
    );

    header("Location: dashboard.php");

    exit;
}


/* =====================================================
   SUMMARY COUNT
===================================================== */

/* TOTAL USERS */

$totalUsers =
mysqli_num_rows(
mysqli_query(
$conn,
"SELECT id FROM users"
)
);


/* PENDING USERS */

$pending =
mysqli_num_rows(
mysqli_query(
$conn,
"SELECT id
FROM users
WHERE status='pending'"
)
);


/* INACTIVE USERS */

$inactive =
mysqli_num_rows(
mysqli_query(
$conn,
"SELECT id
FROM users
WHERE status='inactive'"
)
);


/* UPCOMING EVENTS */

$upcoming =
mysqli_num_rows(
mysqli_query(
$conn,
"SELECT id
FROM events
WHERE event_date >= CURDATE()"
)
);

?>


<link
rel="stylesheet"
href="/SEAT/assets/css/admin/dashboard.css"
>

<link
rel="stylesheet"
href="/SEAT/assets/css/admin/profile.css"
>


<meta name="viewport" content="width=device-width, initial-scale=1.0">
<div class="content">


<!-- =====================================================
     PROFILE MODULE
===================================================== -->

<?php include("profile.php"); ?>



<!-- =====================================================
     SUMMARY CARDS
===================================================== -->

<div class="summaryRow">

    <!-- TOTAL USERS -->

    <div class="cardStat blue">

        <div class="cardTitle">
            Total Users
        </div>

        <div class="cardNum">
            <?= $totalUsers ?>
        </div>

    </div>



    <!-- PENDING -->

    <div class="cardStat orange">

        <div class="cardTitle">
            Pending Accounts
        </div>

        <div class="cardNum">
            <?= $pending ?>
        </div>

    </div>



    <!-- INACTIVE -->

    <div class="cardStat gray">

        <div class="cardTitle">
            Inactive Accounts
        </div>

        <div class="cardNum">
            <?= $inactive ?>
        </div>

    </div>



    <!-- UPCOMING -->

    <div class="cardStat green">

        <div class="cardTitle">
            Upcoming Events
        </div>

        <div class="cardNum">
            <?= $upcoming ?>
        </div>

    </div>

</div>



<!-- =====================================================
     TABLE SECTION
===================================================== -->

<div class="tableSection">

<div class="whiteSection">

    <!-- HEADER -->

    <div class="sectionHeader">

        <div class="sectionTitle">
            Organizer Inactive Monitor
        </div>

        <div class="sectionSub">
            No event for 6 months after register or last event
        </div>

    </div>



    <!-- TABLE -->

    <div class="tableBox">

    <table>

        <tr>

            <th>Bil</th>
            <th>Club</th>
            <th>Email</th>
            <th>Register</th>
            <th>Last Event</th>
            <th>Total</th>
            <th>Action</th>

        </tr>

        <?php

        $i = 1;

        $q = mysqli_query($conn, "

        SELECT 

        u.id,
        u.email,
        u.status,
        u.created_at,

        o.club_name,

        MAX(e.event_date) AS last_event,
        COUNT(e.id) AS total_event,

        GREATEST(
            IFNULL(MAX(e.event_date), u.created_at),
            u.created_at
        ) AS last_activity

        FROM users u

        LEFT JOIN organizers o
        ON o.user_id = u.id

        LEFT JOIN events e
        ON e.organizer_id = o.id

        WHERE u.role='organizer'
        AND u.status='active'

        GROUP BY u.id

        HAVING last_activity <
        DATE_SUB(CURDATE(), INTERVAL 6 MONTH)

        ORDER BY last_activity ASC

        ");

        while($d = mysqli_fetch_assoc($q)){

        ?>

        <tr>

            <td>
                <?= $i++ ?>
            </td>

            <td>
                <?= htmlspecialchars($d['club_name']) ?>
            </td>

            <td>
                <?= htmlspecialchars($d['email']) ?>
            </td>

            <td>

                <?= date(
                    "d M Y",
                    strtotime($d['created_at'])
                ) ?>

            </td>

            <td>

                <?php

                if($d['last_event']){

                    echo
                    "<span class='dateRed'>"
                    .
                    date(
                        "d M Y",
                        strtotime($d['last_event'])
                    )
                    .
                    "</span>";

                }else{

                    echo
                    "<span class='dateRed'>
                    Never
                    </span>";
                }

                ?>

            </td>

            <td>
                <?= $d['total_event'] ?>
            </td>

            <td>

                <form
                method="post"
                onsubmit="return confirmDeactivate()"
                >

                    <input
                    type="hidden"
                    name="deactivate_id"
                    value="<?= $d['id'] ?>"
                    >

                    <button class="btn btnRed">
                        Deactivate
                    </button>

                </form>

            </td>

        </tr>

        <?php } ?>

    </table>

    </div>

</div>

</div>

</div>



<script>

/* =====================================================
DEACTIVATE CONFIRM
===================================================== */

function confirmDeactivate(){

    return confirm(
        "Deactivate this organizer?\n\nNo event published for the previous 6 months."
    );

}

</script>