<?php
include 'template/header.php';
session_start();

$user_id = $_SESSION['id'];
$type = isset($_GET['type']) && $_GET['type'] == 2 ? 'sent' : 'inbox';

if(isset($_GET['type'])) {
    $query = $koneksi->prepare("SELECT * FROM tb_memo WHERE user_id = ? AND type = ? ORDER BY created_at DESC");
    $query->bind_param('is', $user_id, $type);
    $query->execute();
    $result = $query->get_result();
} else {
    $query = $koneksi->prepare("SELECT * FROM tb_memo WHERE user_id = ? ORDER BY created_at DESC");
    $query->bind_param('i', $user_id);
    $query->execute();
    $result = $query->get_result();
}
?>
<style>
        .page-link {
            background: #333848 !important;
            border-color: #695b56 !important;
            font-weight: bold;
            color: #fff !important;
        }
        .page-item.active .page-link {
            background: #0065E6 !important;
            color: #fff !important;
        }
        .tab-active {
            cursor: pointer;
            background-color: var(--primary-color);
            color: #001739;
            border-radius: 4px;
            padding: 6px 12px;
            margin-right: 6px;
        }
        .tab-nonactive {
            cursor: pointer;
            box-shadow: 0 0 0 1px var(--primary-color) inset;
            border-radius: 4px;
            padding: 6px 18px;
            margin-right: 6px;
        }
    </style>
<div style="margin-top: 50px;">
    <div style="padding: 20px 0; background-color:var(--secondary-color);">
        <div
            style="width: 90%; margin: 0 auto 10px auto; color: transparent !important; -webkit-background-clip: text; background-image: radial-gradient(ellipse farthest-corner at right bottom, #FEDB37 0%, #FDB931 8%, #9f7928 30%, #8A6E2F 40%, transparent 80%), radial-gradient(ellipse farthest-corner at left top, #e99c05 0%, #f4ae00 8%, #f8bf33 25%, #f8ce19 62.5%, #ffdc02 100%); font-family: Teko-SemiBold; text-align: center; font-size: 24px;">
            PESAN
        </div>

        <div style="width: 90%; margin: 0 auto 20px auto; display: flex;">
            <div onclick="window.location='pesan.php?type=1'"
                class="<?= $type == 'inbox' ? 'tab-active' : 'tab-nonactive' ?>">Inbox</div>
            <div onclick="window.location='pesan.php?type=2'"
                class="<?= $type == 'sent' ? 'tab-active' : 'tab-nonactive' ?>">Terkirim</div>
            <div onclick="window.location='add.php'"
                style="cursor: pointer; margin: auto 0 auto auto; font-size: 13px; border-radius: 4px; background-color: #1BB143; padding: 4px 16px;">
                + Tulis
            </div>
        </div>

        <div style="width: 90%; margin: 0 auto 20px auto; display: block; overflow: auto;">
            <table width="100%" style="font-size: 11px;">
                <thead>
                    <tr style="background-color: #494848; color: #fff; font-weight: bold;">
                        <td style="padding: 12px 5px;">Tanggal</td>
                        <td style="padding: 12px 5px;">Subjek</td>
                        <td style="padding: 12px 5px;">Dari</td>
                    </tr>
                </thead>
                <tbody>
                    <?php while ($row = $result->fetch_assoc()): ?>
                        <tr style="background-color: <?= $row['id'] % 2 ? 'var(--secondary-color)' : '#000' ?>; color: #fff;">
                            <td style="padding: 12px 5px; font-size: 10px; vertical-align: top;">
                                <?= date('d/m/y H:i', strtotime($row['created_at'])) ?>
                            </td>
                            <td style="padding: 12px 5px;">
                                <div style="font-weight: bold; font-size: 11px;">
                                    <?= htmlspecialchars($row['subject']) ?>
                                </div>
                                <div style="font-size: 9px; color: #C4C4C4; margin-top: 4px;">
                                    <?= nl2br(htmlspecialchars(substr($row['message'], 0, 50))) ?>...
                                </div>
                                <div style="margin-top: 4px;">
                                    <?php if ($row['is_read'] == 0): ?>
                                        <span style="background-color: #ffc107; color: #000; padding: 2px 6px; border-radius: 3px; font-size: 9px; display: inline-block;">Belum Dibaca</span>
                                    <?php else: ?>
                                        <span style="background-color: #28a745; color: #fff; padding: 2px 6px; border-radius: 3px; font-size: 9px; display: inline-block;">Dibaca</span>
                                    <?php endif; ?>
                                </div>
                            </td>
                            <td style="padding: 12px 5px; font-size: 10px; vertical-align: top;">
                                <?= htmlspecialchars($row['from_user']) ?>
                            </td>
                        </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php include 'template/footer.php'; ?>
