<?php
/**
 * Mini-Dashboard for: confirmer / shipping roles
 * Variables: $role, $adminId, $currency,
 *            $todayPerf, $monthPerf, $confirmRate,
 *            $myPendingOrders, $myPendingCount,
 *            $readyToShip, $readyCount,
 *            $teamUnreadCount, $upcomingMeetings
 */

$roleColors = [
    'confirmer'  => ['color' => 'primary',  'icon' => 'bi-telephone-fill'],
    'shipping'   => ['color' => 'warning',   'icon' => 'bi-truck'],
    'accountant' => ['color' => 'success',   'icon' => 'bi-calculator-fill'],
];
$rc    = $roleColors[$role] ?? ['color' => 'secondary', 'icon' => 'bi-person-fill'];
$color = $rc['color'];
$icon  = $rc['icon'];

$adminName = htmlspecialchars($_SESSION['admin_username'] ?? 'Admin');
$roleLabel = __('team.role_'.$role, ucfirst($role));
?>
<div class="content-wrapper">
<section class="content pt-3">
<div class="container-fluid pb-5">

    <!-- Welcome header -->
    <div class="d-flex flex-wrap justify-content-between align-items-start gap-3 mb-4">
        <div>
            <h3 class="mb-1 fw-bold">
                <i class="bi <?= $icon ?> me-2 text-<?= $color ?>"></i>
                <?= __('dashboard_member.welcome', 'Welcome back') ?>, <?= $adminName ?>
            </h3>
            <p class="text-muted mb-0 small">
                <span class="badge bg-<?= $color ?>-subtle text-<?= $color ?> me-2"><?= $roleLabel ?></span>
                <?= __('dashboard_member.subtitle', 'Here\'s your workspace for today') ?>
            </p>
        </div>
        <div class="d-flex gap-2">
            <a href="/admin/team/communication" class="btn btn-outline-primary btn-sm position-relative">
                <i class="bi bi-chat-dots-fill me-1"></i><?= __('sidebar.team_communication','Messages') ?>
                <?php if ($teamUnreadCount > 0): ?>
                <span class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-danger" style="font-size:.6rem">
                    <?= min(99, $teamUnreadCount) ?>
                </span>
                <?php endif; ?>
            </a>
            <?php if ($role === 'confirmer'): ?>
            <a href="/admin/orders" class="btn btn-primary btn-sm">
                <i class="bi bi-list-check me-1"></i><?= __('dashboard_member.my_orders', 'My Orders') ?>
            </a>
            <?php elseif ($role === 'shipping'): ?>
            <a href="/admin/orders" class="btn btn-warning btn-sm text-dark">
                <i class="bi bi-truck me-1"></i><?= __('dashboard_member.ready_to_ship', 'Orders to Ship') ?>
            </a>
            <?php endif; ?>
        </div>
    </div>

    <!-- KPI Cards -->
    <div class="row g-3 mb-4">

        <?php if ($role === 'confirmer'): ?>
        <!-- Today confirmed -->
        <div class="col-6 col-md-3">
            <div class="card border-0 shadow-sm h-100" style="border-top:3px solid var(--bs-success) !important">
                <div class="card-body py-3 text-center">
                    <div class="fs-2 fw-bold text-success"><?= (int)($todayPerf['confirmed'] ?? 0) ?></div>
                    <div class="small text-muted mt-1"><?= __('dashboard_member.today_confirmed','Today Confirmed') ?></div>
                </div>
            </div>
        </div>
        <!-- Today rejected -->
        <div class="col-6 col-md-3">
            <div class="card border-0 shadow-sm h-100" style="border-top:3px solid var(--bs-danger) !important">
                <div class="card-body py-3 text-center">
                    <div class="fs-2 fw-bold text-danger"><?= (int)($todayPerf['rejected'] ?? 0) ?></div>
                    <div class="small text-muted mt-1"><?= __('dashboard_member.today_rejected','Today Rejected') ?></div>
                </div>
            </div>
        </div>
        <!-- Monthly confirm rate -->
        <div class="col-6 col-md-3">
            <div class="card border-0 shadow-sm h-100" style="border-top:3px solid var(--bs-primary) !important">
                <div class="card-body py-3 text-center">
                    <div class="fs-2 fw-bold text-primary"><?= $confirmRate ?>%</div>
                    <div class="small text-muted mt-1"><?= __('dashboard_member.confirm_rate_30d','30-Day Confirm Rate') ?></div>
                </div>
            </div>
        </div>
        <!-- Pending assigned -->
        <div class="col-6 col-md-3">
            <div class="card border-0 shadow-sm h-100" style="border-top:3px solid var(--bs-warning) !important">
                <div class="card-body py-3 text-center">
                    <div class="fs-2 fw-bold text-warning"><?= (int)($myPendingCount) ?></div>
                    <div class="small text-muted mt-1"><?= __('dashboard_member.my_pending','My Pending Orders') ?></div>
                </div>
            </div>
        </div>

        <?php elseif ($role === 'shipping'): ?>
        <!-- Ready to ship -->
        <div class="col-6 col-md-3">
            <div class="card border-0 shadow-sm h-100" style="border-top:3px solid var(--bs-warning) !important">
                <div class="card-body py-3 text-center">
                    <div class="fs-2 fw-bold text-warning"><?= (int)$readyCount ?></div>
                    <div class="small text-muted mt-1"><?= __('dashboard_member.ready_to_ship','Ready to Ship') ?></div>
                </div>
            </div>
        </div>
        <!-- Today total confirmed (shipped today) -->
        <div class="col-6 col-md-3">
            <div class="card border-0 shadow-sm h-100" style="border-top:3px solid var(--bs-success) !important">
                <div class="card-body py-3 text-center">
                    <div class="fs-2 fw-bold text-success"><?= (int)($todayPerf['confirmed'] ?? 0) ?></div>
                    <div class="small text-muted mt-1"><?= __('dashboard_member.today_shipped','Shipped Today') ?></div>
                </div>
            </div>
        </div>
        <!-- Monthly total -->
        <div class="col-6 col-md-3">
            <div class="card border-0 shadow-sm h-100" style="border-top:3px solid var(--bs-primary) !important">
                <div class="card-body py-3 text-center">
                    <div class="fs-2 fw-bold text-primary"><?= (int)($monthPerf['total'] ?? 0) ?></div>
                    <div class="small text-muted mt-1"><?= __('dashboard_member.month_total','This Month Total') ?></div>
                </div>
            </div>
        </div>
        <!-- Messages -->
        <div class="col-6 col-md-3">
            <div class="card border-0 shadow-sm h-100" style="border-top:3px solid var(--bs-info) !important">
                <div class="card-body py-3 text-center">
                    <div class="fs-2 fw-bold text-info"><?= (int)$teamUnreadCount ?></div>
                    <div class="small text-muted mt-1"><?= __('dashboard_member.unread_messages','Unread Messages') ?></div>
                </div>
            </div>
        </div>
        <?php endif; ?>

    </div>

    <div class="row g-3">

        <!-- Left: Main work list -->
        <div class="col-lg-8">
            <?php if ($role === 'confirmer'): ?>
            <div class="card border-0 shadow-sm">
                <div class="card-header bg-white border-bottom d-flex align-items-center justify-content-between py-3">
                    <h6 class="mb-0 fw-semibold">
                        <i class="bi bi-list-check me-2 text-primary"></i>
                        <?= __('dashboard_member.my_assigned_orders','My Assigned Orders') ?>
                    </h6>
                    <a href="/admin/orders" class="btn btn-sm btn-outline-primary"><?= __('dashboard_member.view_all','View All') ?></a>
                </div>
                <div class="card-body p-0">
                    <?php if (empty($myPendingOrders)): ?>
                    <div class="text-center text-muted py-5">
                        <i class="bi bi-check-circle fs-1 d-block mb-2 text-success opacity-50"></i>
                        <span class="small"><?= __('dashboard_member.no_pending','No pending orders assigned to you') ?></span>
                    </div>
                    <?php else: ?>
                    <div class="table-responsive">
                        <table class="table table-hover align-middle mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th class="ps-3"><?= __('orders.customer','Customer') ?></th>
                                    <th><?= __('orders.product','Product') ?></th>
                                    <th><?= __('orders.total','Total') ?></th>
                                    <th class="text-end pe-3"><?= __('actions.actions','Actions') ?></th>
                                </tr>
                            </thead>
                            <tbody>
                            <?php foreach ($myPendingOrders as $o): ?>
                            <tr>
                                <td class="ps-3">
                                    <div class="fw-semibold small"><?= htmlspecialchars($o['full_name']) ?></div>
                                    <div class="text-muted" style="font-size:.72rem"><?= htmlspecialchars($o['phone']) ?></div>
                                </td>
                                <td class="small text-truncate" style="max-width:150px"><?= htmlspecialchars($o['product_name'] ?? '—') ?></td>
                                <td class="small fw-semibold"><?= number_format((float)$o['total_amount'], 2) ?> <?= $currency ?></td>
                                <td class="text-end pe-3">
                                    <a href="/admin/orders/view/<?= $o['id'] ?>" class="btn btn-sm btn-outline-primary">
                                        <i class="bi bi-eye"></i>
                                    </a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                    <?php endif; ?>
                </div>
            </div>

            <?php elseif ($role === 'shipping'): ?>
            <div class="card border-0 shadow-sm">
                <div class="card-header bg-white border-bottom d-flex align-items-center justify-content-between py-3">
                    <h6 class="mb-0 fw-semibold">
                        <i class="bi bi-truck me-2 text-warning"></i>
                        <?= __('dashboard_member.orders_ready_ship','Confirmed Orders — Ready to Ship') ?>
                    </h6>
                    <a href="/admin/orders" class="btn btn-sm btn-outline-warning"><?= __('dashboard_member.view_all','View All') ?></a>
                </div>
                <div class="card-body p-0">
                    <?php if (empty($readyToShip)): ?>
                    <div class="text-center text-muted py-5">
                        <i class="bi bi-truck fs-1 d-block mb-2 opacity-50"></i>
                        <span class="small"><?= __('dashboard_member.no_ready','All orders have been shipped!') ?></span>
                    </div>
                    <?php else: ?>
                    <div class="table-responsive">
                        <table class="table table-hover align-middle mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th class="ps-3"><?= __('orders.customer','Customer') ?></th>
                                    <th><?= __('orders.product','Product') ?></th>
                                    <th><?= __('orders.total','Total') ?></th>
                                    <th class="text-end pe-3"><?= __('actions.actions','Actions') ?></th>
                                </tr>
                            </thead>
                            <tbody>
                            <?php foreach ($readyToShip as $o): ?>
                            <tr>
                                <td class="ps-3">
                                    <div class="fw-semibold small"><?= htmlspecialchars($o['full_name']) ?></div>
                                    <div class="text-muted" style="font-size:.72rem"><?= htmlspecialchars($o['phone']) ?></div>
                                </td>
                                <td class="small text-truncate" style="max-width:150px"><?= htmlspecialchars($o['product_name'] ?? '—') ?></td>
                                <td class="small fw-semibold"><?= number_format((float)$o['total_amount'], 2) ?> <?= $currency ?></td>
                                <td class="text-end pe-3">
                                    <a href="/admin/orders/view/<?= $o['id'] ?>" class="btn btn-sm btn-outline-warning">
                                        <i class="bi bi-eye"></i>
                                    </a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
            <?php endif; ?>
        </div>

        <!-- Right: Upcoming meetings + Quick links -->
        <div class="col-lg-4">

            <!-- Upcoming Meetings -->
            <div class="card border-0 shadow-sm mb-3">
                <div class="card-header bg-white border-bottom d-flex align-items-center justify-content-between py-2">
                    <span class="fw-semibold small"><i class="bi bi-calendar-event-fill me-2 text-info"></i><?= __('team_comm.upcoming_meetings','Upcoming Meetings') ?></span>
                    <a href="/admin/team/meetings" class="btn btn-sm btn-outline-info"><?= __('team_comm.view_all','View All') ?></a>
                </div>
                <div class="card-body p-0">
                    <?php if (empty($upcomingMeetings)): ?>
                    <div class="text-center text-muted py-3 small">
                        <i class="bi bi-calendar-x fs-3 d-block mb-1 opacity-50"></i>
                        <?= __('team_comm.no_upcoming_meetings','No upcoming meetings') ?>
                    </div>
                    <?php else: ?>
                    <?php foreach ($upcomingMeetings as $m): ?>
                    <div class="d-flex align-items-center gap-3 p-3 border-bottom">
                        <div class="text-center flex-shrink-0" style="width:38px">
                            <div class="fw-bold text-primary" style="font-size:1rem"><?= date('d', strtotime($m['scheduled_at'])) ?></div>
                            <div class="text-muted" style="font-size:.6rem;text-transform:uppercase"><?= date('M', strtotime($m['scheduled_at'])) ?></div>
                        </div>
                        <div class="flex-grow-1">
                            <div class="fw-semibold small"><?= htmlspecialchars($m['title']) ?></div>
                            <div class="text-muted" style="font-size:.7rem">
                                <i class="bi bi-clock me-1"></i><?= date('H:i', strtotime($m['scheduled_at'])) ?>
                            </div>
                        </div>
                    </div>
                    <?php endforeach; ?>
                    <?php endif; ?>
                </div>
            </div>

            <!-- Quick links -->
            <div class="card border-0 shadow-sm">
                <div class="card-header bg-white border-bottom py-2">
                    <span class="fw-semibold small"><i class="bi bi-lightning-charge-fill me-2 text-warning"></i><?= __('dashboard_member.quick_links','Quick Links') ?></span>
                </div>
                <div class="list-group list-group-flush">
                    <a href="/admin/team/communication" class="list-group-item list-group-item-action d-flex align-items-center gap-2 py-2">
                        <i class="bi bi-chat-dots-fill text-primary"></i>
                        <span class="small"><?= __('sidebar.team_communication','Messages') ?></span>
                        <?php if ($teamUnreadCount > 0): ?>
                        <span class="badge bg-danger ms-auto rounded-pill"><?= min(99, $teamUnreadCount) ?></span>
                        <?php endif; ?>
                    </a>
                    <a href="/admin/team/meetings" class="list-group-item list-group-item-action d-flex align-items-center gap-2 py-2">
                        <i class="bi bi-calendar-event-fill text-info"></i>
                        <span class="small"><?= __('sidebar.team_meetings','Meetings') ?></span>
                    </a>
                    <a href="/admin/orders" class="list-group-item list-group-item-action d-flex align-items-center gap-2 py-2">
                        <i class="bi bi-bag-check-fill text-success"></i>
                        <span class="small"><?= __('sidebar.orders','Orders') ?></span>
                    </a>
                    <a href="/admin/profile" class="list-group-item list-group-item-action d-flex align-items-center gap-2 py-2">
                        <i class="bi bi-person-circle text-secondary"></i>
                        <span class="small"><?= __('sidebar.profile','My Profile') ?></span>
                    </a>
                </div>
            </div>

        </div>
    </div>

</div>
</section>
</div>
