<?php include __DIR__ . '/../header.php'; ?>

<div class="content-wrapper">
<section class="content pt-3">
<div class="container-fluid pb-5">

<?php if (!empty($_SESSION['success'])): ?>
<div class="alert alert-success alert-dismissible fade show py-2">
    <?= htmlspecialchars($_SESSION['success']) ?>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php unset($_SESSION['success']); endif; ?>

<?php if (!empty($_SESSION['error'])): ?>
<div class="alert alert-danger alert-dismissible fade show py-2">
    <?= htmlspecialchars($_SESSION['error']) ?>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php unset($_SESSION['error']); endif; ?>

<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-people-fill me-2 text-primary"></i><?= __('team.title','Team Management') ?></h3>
        <p class="text-muted mb-0 small"><?= __('team.subtitle','Manage team members and their roles') ?></p>
    </div>
    <div class="d-flex gap-2">
        <a href="/admin/team/workflow" class="btn btn-outline-primary btn-sm">
            <i class="bi bi-diagram-3 me-1"></i><?= __('team.workflow_title','Order Workflow Automation') ?>
        </a>
        <a href="/admin/team/stats" class="btn btn-outline-info btn-sm">
            <i class="bi bi-bar-chart me-1"></i><?= __('team.view_stats','Performance Stats') ?>
        </a>
        <a href="/admin/team/permissions" class="btn btn-outline-secondary btn-sm">
            <i class="bi bi-shield-check me-1"></i><?= __('team.manage_permissions','Permissions') ?>
        </a>
        <a href="/admin/team/create" class="btn btn-primary btn-sm">
            <i class="bi bi-person-plus me-1"></i><?= __('team.add_member','Add Member') ?>
        </a>
    </div>
</div>

<!-- Role KPIs -->
<?php
$roleColors=['owner'=>'primary','manager'=>'info','confirmer'=>'success','shipping'=>'warning','accountant'=>'secondary'];
$roleCounts=[];
foreach($members as $m) $roleCounts[$m['role']]=($roleCounts[$m['role']]??0)+1;
?>
<div class="row g-3 mb-4">
<?php foreach(\Core\App\Models\TeamMemberModel::ROLES as $role=>$label): ?>
<div class="col-md-2 col-sm-4">
    <div class="card border-0 shadow-sm text-center h-100" style="border-top:3px solid var(--bs-<?= $roleColors[$role]??'secondary' ?>) !important">
        <div class="card-body py-3">
            <div class="fs-2 fw-bold text-<?= $roleColors[$role]??'secondary' ?>"><?= $roleCounts[$role]??0 ?></div>
            <div class="small text-muted mt-1"><?= __('team.role_'.$role,$label) ?></div>
        </div>
    </div>
</div>
<?php endforeach; ?>
</div>

<!-- Members Table -->
<div class="card border-0 shadow-sm">
    <div class="card-header bg-white border-bottom d-flex justify-content-between align-items-center py-3">
        <h6 class="mb-0 fw-semibold"><i class="bi bi-list-ul me-2 text-muted"></i><?= __('team.members_list','Team Members') ?></h6>
        <span class="badge bg-secondary rounded-pill"><?= count($members) ?></span>
    </div>
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="table-light">
                    <tr>
                        <th class="ps-3"><?= __('team.member','Member') ?></th>
                        <th><?= __('team.role','Role') ?></th>
                        <th class="text-center"><?= __('team.confirmed_30d','Confirmed (30d)') ?></th>
                        <th class="text-center"><?= __('team.rejected_30d','Rejected (30d)') ?></th>
                        <th><?= __('team.confirm_rate','Confirm Rate') ?></th>
                        <th><?= __('team.status','Status') ?></th>
                        <th class="text-end pe-3"><?= __('actions.actions','Actions') ?></th>
                    </tr>
                </thead>
                <tbody>
                <?php foreach($members as $m):
                    $stat=$statsMap[$m['id']]??[];
                    $color=$roleColors[$m['role']]??'secondary';
                    $rate=(float)($stat['confirm_rate']??0);
                    $rateColor=$rate>=70?'success':($rate>=40?'warning':'danger');
                ?>
                <tr>
                    <td class="ps-3">
                        <div class="d-flex align-items-center gap-2">
                            <div class="rounded-circle bg-<?= $color ?>-subtle d-flex align-items-center justify-content-center flex-shrink-0"
                                 style="width:38px;height:38px">
                                <i class="bi bi-person text-<?= $color ?>"></i>
                            </div>
                            <div>
                                <div class="fw-semibold"><?= htmlspecialchars($m['username']) ?></div>
                                <div class="text-muted" style="font-size:.73rem"><?= htmlspecialchars($m['email']??'') ?></div>
                            </div>
                        </div>
                    </td>
                    <td>
                        <span class="badge bg-<?= $color ?>-subtle text-<?= $color ?> border border-<?= $color ?>-subtle px-2 py-1">
                            <?= \Core\App\Models\TeamMemberModel::roleLabel($m['role']) ?>
                        </span>
                    </td>
                    <td class="text-center fw-bold text-success"><?= (int)($stat['confirmed']??0) ?></td>
                    <td class="text-center fw-bold text-danger"><?= (int)($stat['rejected']??0) ?></td>
                    <td>
                        <?php if($m['role']==='confirmer'): ?>
                        <div class="d-flex align-items-center gap-2">
                            <div class="progress flex-grow-1" style="height:6px">
                                <div class="progress-bar bg-<?= $rateColor ?>" style="width:<?= $rate ?>%"></div>
                            </div>
                            <small class="text-<?= $rateColor ?> fw-bold"><?= $rate ?>%</small>
                        </div>
                        <?php else: ?><span class="text-muted">—</span><?php endif; ?>
                    </td>
                    <td>
                        <?php if($m['is_active']): ?>
                            <span class="badge bg-success-subtle text-success border border-success-subtle"><?= __('team.active','Active') ?></span>
                        <?php else: ?>
                            <span class="badge bg-secondary-subtle text-secondary border border-secondary-subtle"><?= __('team.inactive','Inactive') ?></span>
                        <?php endif; ?>
                    </td>
                    <td class="text-end pe-3">
                        <div class="d-flex gap-1 justify-content-end">
                            <a href="/admin/team/member/<?= $m['id'] ?>" class="btn btn-sm btn-outline-info" title="<?= __('team.view_member_details','View details') ?>"><i class="bi bi-eye"></i></a>
                            <a href="/admin/team/edit/<?= $m['id'] ?>" class="btn btn-sm btn-outline-primary"><i class="bi bi-pencil"></i></a>
                            <?php if($m['role']!=='owner'): ?>
                            <form method="POST" action="/admin/team/toggle/<?= $m['id'] ?>">
                                <?= \Core\System\Csrf::field() ?>
                                <button type="submit" class="btn btn-sm btn-outline-<?= $m['is_active']?'warning':'success' ?>">
                                    <i class="bi bi-<?= $m['is_active']?'pause':'play' ?>"></i>
                                </button>
                            </form>
                            <form method="POST" action="/admin/team/delete/<?= $m['id'] ?>"
                                  onsubmit="return confirm('<?= __('team.confirm_delete','Are you sure?') ?>')">
                                <?= \Core\System\Csrf::field() ?>
                                <button type="submit" class="btn btn-sm btn-outline-danger"><i class="bi bi-trash3"></i></button>
                            </form>
                            <?php endif; ?>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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

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