<?php include __DIR__ . '/../header.php'; ?>
<div class="content-wrapper">
<section class="content pt-3">
<div class="container-fluid pb-5">
<?php
$roleColors=['owner'=>'primary','manager'=>'info','confirmer'=>'success','shipping'=>'warning','accountant'=>'secondary'];
$color=$roleColors[$member['role']] ?? 'secondary';
?>
<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 bi-person-badge-fill me-2 text-<?= $color ?>"></i><?= htmlspecialchars($member['username']) ?></h3>
        <p class="text-muted mb-0 small"><?= \Core\App\Models\TeamMemberModel::roleLabel($member['role']) ?> · <?= __('team.member_performance_subtitle','Detailed performance, shipping, returns and outcomes for this user') ?></p>
    </div>
    <div class="d-flex gap-2 align-items-center">
        <?php foreach([7,30,90] as $d): ?>
        <a href="?days=<?= $d ?>" class="btn btn-sm <?= $days==$d?'btn-primary':'btn-outline-secondary' ?>"><?= $d ?> <?= __('team.days','days') ?></a>
        <?php endforeach; ?>
        <a href="/admin/team" class="btn btn-outline-secondary btn-sm"><i class="bi bi-arrow-right me-1"></i><?= __('actions.back','Back') ?></a>
    </div>
</div>

<div class="row g-3 mb-4">
    <?php
    $cards = [
        ['key'=>'assigned','icon'=>'bi-inboxes','border'=>'#0d6efd','iconbg'=>'bg-primary-subtle','iconcolor'=>'text-primary','label'=>__('team.assigned_orders','Assigned Orders')],
        ['key'=>'confirmed','icon'=>'bi-check-circle','border'=>'#198754','iconbg'=>'bg-success-subtle','iconcolor'=>'text-success','label'=>__('team.confirmed','Confirmed')],
        ['key'=>'shipment_sent','icon'=>'bi-truck','border'=>'#fd7e14','iconbg'=>'bg-warning-subtle','iconcolor'=>'text-warning','label'=>__('team.shipments_sent','Shipments Sent')],
        ['key'=>'returned_manual','icon'=>'bi-arrow-return-left','border'=>'#6f42c1','iconbg'=>'','iconcolor'=>'','label'=>__('team.manual_returns','Manual Returns')],
    ];
    foreach($cards as $card): ?>
    <div class="col-6 col-lg-3">
        <div class="card border-0 shadow-sm h-100" style="border-top:3px solid <?= $card['border'] ?> !important">
            <div class="card-body px-4 py-3">
                <div class="d-flex justify-content-between align-items-start mb-2">
                    <div class="rounded-circle d-flex align-items-center justify-content-center <?= $card['iconbg'] ?: '' ?>" style="width:42px;height:42px;<?= $card['iconbg'] ? '' : 'background:'. $card['border'].'20' ?>">
                        <i class="bi <?= $card['icon'] ?> fs-5 <?= $card['iconcolor'] ?: '' ?>" style="<?= $card['iconcolor'] ? '' : 'color:'.$card['border'] ?>"></i>
                    </div>
                </div>
                <div class="fw-bold fs-2 lh-1 mb-1"><?= (int)($summary[$card['key']] ?? 0) ?></div>
                <div class="text-muted small"><?= $card['label'] ?></div>
            </div>
        </div>
    </div>
    <?php endforeach; ?>
</div>

<div class="row g-3 mb-4">
    <div class="col-6 col-lg-3">
        <div class="card border-0 shadow-sm h-100" style="border-top:3px solid #20c997 !important"><div class="card-body px-4 py-3"><div class="d-flex justify-content-between align-items-start mb-2"><div class="rounded-circle d-flex align-items-center justify-content-center bg-success-subtle" style="width:42px;height:42px"><i class="bi bi-box-seam fs-5 text-success"></i></div></div><div class="fw-bold fs-2 lh-1 mb-1"><?= (int)($summary['confirmed_delivered'] ?? 0) ?></div><div class="text-muted small"><?= __('team.confirmed_delivered','Delivered from Confirmed Orders') ?></div></div></div>
    </div>
    <div class="col-6 col-lg-3">
        <div class="card border-0 shadow-sm h-100" style="border-top:3px solid #dc3545 !important"><div class="card-body px-4 py-3"><div class="d-flex justify-content-between align-items-start mb-2"><div class="rounded-circle d-flex align-items-center justify-content-center bg-danger-subtle" style="width:42px;height:42px"><i class="bi bi-arrow-counterclockwise fs-5 text-danger"></i></div></div><div class="fw-bold fs-2 lh-1 mb-1"><?= (int)($summary['confirmed_returned'] ?? 0) ?></div><div class="text-muted small"><?= __('team.confirmed_returned','Returned from Confirmed Orders') ?></div></div></div>
    </div>
    <div class="col-6 col-lg-3">
        <div class="card border-0 shadow-sm h-100" style="border-top:3px solid #0dcaf0 !important"><div class="card-body px-4 py-3"><div class="d-flex justify-content-between align-items-start mb-2"><div class="rounded-circle d-flex align-items-center justify-content-center bg-info-subtle" style="width:42px;height:42px"><i class="bi bi-check2-square fs-5 text-info"></i></div></div><div class="fw-bold fs-2 lh-1 mb-1"><?= (int)($summary['shipped_delivered'] ?? 0) ?></div><div class="text-muted small"><?= __('team.shipped_delivered','Delivered from Sent Shipments') ?></div></div></div>
    </div>
    <div class="col-6 col-lg-3">
        <div class="card border-0 shadow-sm h-100" style="border-top:3px solid #6f42c1 !important"><div class="card-body px-4 py-3"><div class="d-flex justify-content-between align-items-start mb-2"><div class="rounded-circle d-flex align-items-center justify-content-center" style="width:42px;height:42px;background:#6f42c120"><i class="bi bi-arrow-repeat fs-5" style="color:#6f42c1"></i></div></div><div class="fw-bold fs-2 lh-1 mb-1"><?= (int)($summary['returned_auto_provider'] ?? 0) ?></div><div class="text-muted small"><?= __('team.auto_returns','Auto Returns from Provider') ?></div></div></div>
    </div>
</div>

<div class="row g-3">
    <div class="col-lg-7">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-header bg-white border-bottom py-3 d-flex justify-content-between align-items-center">
                <h6 class="mb-0 fw-semibold"><?= __('team.member_financials','Financial and Outcome Summary') ?></h6>
                <span class="badge bg-light text-dark border"><?= $days ?> <?= __('team.days','days') ?></span>
            </div>
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-md-6"><div class="rounded-3 bg-light p-3 h-100"><div class="text-muted small mb-1"><?= __('team.calls_logged','Calls Logged') ?></div><div class="fw-bold fs-4"><?= (int)($summary['calls'] ?? 0) ?></div><div class="small text-muted"><?= __('team.callbacks_due','Callbacks') ?>: <?= (int)($summary['callbacks'] ?? 0) ?></div></div></div>
                    <div class="col-md-6"><div class="rounded-3 bg-light p-3 h-100"><div class="text-muted small mb-1"><?= __('team.provider_return_requests','Provider Return Requests') ?></div><div class="fw-bold fs-4"><?= (int)($summary['return_requested_to_provider'] ?? 0) ?></div><div class="small text-muted"><?= __('team.manual_returns','Manual Returns') ?>: <?= (int)($summary['returned_manual'] ?? 0) ?></div></div></div>
                    <div class="col-md-6"><div class="rounded-3 bg-light p-3 h-100"><div class="text-muted small mb-1"><?= __('team.delivered_revenue_confirmed','Delivered Revenue (Confirmed By User)') ?></div><div class="fw-bold fs-4"><?= number_format((float)($summary['delivered_revenue_confirmed'] ?? 0),2) ?></div></div></div>
                    <div class="col-md-6"><div class="rounded-3 bg-light p-3 h-100"><div class="text-muted small mb-1"><?= __('team.delivered_revenue_shipped','Delivered Revenue (Shipped By User)') ?></div><div class="fw-bold fs-4"><?= number_format((float)($summary['delivered_revenue_shipped'] ?? 0),2) ?></div></div></div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-lg-5">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-header bg-white border-bottom py-3"><h6 class="mb-0 fw-semibold"><?= __('team.recent_activity','Recent Activity') ?></h6></div>
            <div class="card-body p-0">
                <div class="list-group list-group-flush">
                    <?php if (empty($recentEvents)): ?>
                    <div class="p-4 text-muted text-center small"><?= __('team.no_recent_activity','No recent activity for this user yet.') ?></div>
                    <?php else: foreach($recentEvents as $ev): ?>
                    <div class="list-group-item border-0 border-bottom">
                        <div class="d-flex justify-content-between gap-3">
                            <div>
                                <div class="fw-semibold">#<?= (int)$ev['order_id'] ?> · <?= __('team.event_'.$ev['event_type'], ucfirst(str_replace('_',' ', $ev['event_type']))) ?></div>
                                <div class="text-muted small"><?= htmlspecialchars($ev['full_name'] ?? '') ?><?php if(!empty($ev['provider'])): ?> · <?= htmlspecialchars($ev['provider']) ?><?php endif; ?></div>
                            </div>
                            <div class="text-muted small text-nowrap"><?= htmlspecialchars($ev['created_at']) ?></div>
                        </div>
                    </div>
                    <?php endforeach; endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

</div></section></div>
<?php include __DIR__ . '/../footer.php'; ?>
