<?php
$typeLabels = [
    'ENT' => 'Entrée',
    'SOR' => 'Sortie',
    'RET' => 'Retour',
    'INV' => 'Inventaire',
    'ADJ' => 'Ajustement',
    'IMP' => 'Import',
];

$typeBadges = [
    'ENT' => 'darlink-badge-success',
    'SOR' => 'darlink-badge-danger',
    'RET' => 'darlink-badge-info',
    'INV' => 'darlink-badge-primary',
    'ADJ' => 'darlink-badge-warning',
    'IMP' => 'darlink-badge-light',
];

$stock = (float)($article['quantite'] ?? 0);
$mini = (float)($article['stock_minimum'] ?? 0);
$achat = (float)($article['prix_achat_ht'] ?? 0);
$valeur = (float)($article['valeur_stock'] ?? 0);
$lastEntry = null;
$lastExit = null;
$totalEntries = 0;
$totalExits = 0;
$totalAdjustments = 0;
$totalInventory = 0;
$totalMovements = count($movements);

foreach ($movements as $movement) {
    $type = (string)($movement['type_mouvement'] ?? '');
    $qte = (float)($movement['quantite'] ?? 0);

    if ($type === 'ENT') {
        $totalEntries += $qte;
        if ($lastEntry === null) {
            $lastEntry = $movement;
        }
    }

    if ($type === 'SOR') {
        $totalExits += abs($qte);
        if ($lastExit === null) {
            $lastExit = $movement;
        }
    }

    if ($type === 'ADJ') {
        $totalAdjustments++;
    }

    if ($type === 'INV') {
        $totalInventory++;
    }
}

$pmp = $stock > 0 ? $valeur / $stock : 0;
$lastMovement = $movements[0] ?? null;

$etatLabel = 'OK';
$etatClass = 'darlink-badge-success';

if ($stock <= 0) {
    $etatLabel = 'Rupture';
    $etatClass = 'darlink-badge-danger';
} elseif ($mini > 0 && $stock <= $mini) {
    $etatLabel = 'Alerte';
    $etatClass = 'darlink-badge-warning';
}
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="darlink-title mb-1">
			
            <i class="bi bi-box-seam me-2"></i><?= htmlspecialchars($article['designation']) ?>
        </h1>
        <p class="text-muted mb-0">Référence <?= htmlspecialchars($article['reference']) ?></p>
		<div class="darlink-card darlink-card-body mb-4">

    <div class="d-flex flex-wrap gap-2">

        <a href="index.php?page=stock_entry"
           class="btn darlink-btn-primary">
            <i class="bi bi-plus-circle"></i>
            Entrée
        </a>

        <a href="index.php?page=stock_adjust&id=<?= (int)$article['id'] ?>"
           class="btn btn-outline-warning">
            <i class="bi bi-sliders"></i>
            Ajustement
        </a>

        <a href="index.php?page=stock_inventory"
           class="btn btn-outline-primary">
            <i class="bi bi-clipboard-check"></i>
            Inventaire
        </a>

        <a href="index.php?page=stock_history"
           class="btn btn-outline-secondary">
            <i class="bi bi-clock-history"></i>
            Historique complet
        </a>

        <a href="index.php?page=articles_show&id=<?= (int)$article['id'] ?>"
           class="btn btn-outline-success">
            <i class="bi bi-box"></i>
            Fiche article
        </a>

    </div>

</div>
    </div>

    <div class="d-flex gap-2">
        <a href="index.php?page=stock_entry" class="btn darlink-btn-primary">
            <i class="bi bi-plus-lg me-1"></i> Entrée
        </a>
        <a href="index.php?page=stock" class="btn btn-outline-secondary">
            <i class="bi bi-arrow-left me-1"></i> Retour
        </a>
    </div>
</div>

<div class="row g-3 mb-4">
    <div class="col-md-3">
        <div class="darlink-stat">
            <span>Stock actuel</span>
            <strong><?= number_format($stock, 0, ',', ' ') ?></strong>
        </div>
    </div>

    <div class="col-md-3">
        <div class="darlink-stat">
            <span>Stock minimum</span>
            <strong><?= number_format($mini, 0, ',', ' ') ?></strong>
        </div>
    </div>

    <div class="col-md-3">
        <div class="darlink-stat">
            <span>Achat HT</span>
            <strong><?= number_format($achat, 2, ',', ' ') ?> MAD</strong>
        </div>
    </div>

    <div class="col-md-3">
        <div class="darlink-stat">
            <span>Valeur stock</span>
            <strong><?= number_format($valeur, 2, ',', ' ') ?> MAD</strong>
        </div>
    </div>
</div>

<div class="darlink-card darlink-card-body mb-4">
	<div class="darlink-card darlink-card-body mb-4">

    <h2 class="h5 mb-4">
        <i class="bi bi-box me-2"></i>Informations article
    </h2>

    <div class="row">

        <div class="col-lg-6">

            <table class="table table-borderless mb-0">

                <tr>
                    <th width="40%">Référence</th>
                    <td><?= htmlspecialchars($article['reference']) ?></td>
                </tr>

                <tr>
                    <th>Désignation</th>
                    <td><?= htmlspecialchars($article['designation']) ?></td>
                </tr>

                <tr>
                    <th>Unité</th>
                    <td><?= htmlspecialchars($article['unite'] ?? '-') ?></td>
                </tr>

                <tr>
                    <th>Stock minimum</th>
                    <td><?= number_format($mini,0,',',' ') ?></td>
                </tr>

                <tr>
                    <th>État</th>
                    <td>
                        <span class="darlink-badge <?= $etatClass ?>">
                            <?= $etatLabel ?>
                        </span>
                    </td>
                </tr>

            </table>

        </div>

        <div class="col-lg-6">

            <table class="table table-borderless mb-0">

                <tr>
                    <th width="40%">Prix achat HT</th>
                    <td><?= number_format($achat,2,',',' ') ?> MAD</td>
                </tr>

                <tr>
                    <th>PMP</th>
                    <td><?= number_format($pmp,2,',',' ') ?> MAD</td>
                </tr>

                <tr>
                    <th>Stock actuel</th>
                    <td><?= number_format($stock,0,',',' ') ?></td>
                </tr>

                <tr>
                    <th>Valorisation</th>
                    <td class="fw-bold text-success">
                        <?= number_format($valeur,2,',',' ') ?> MAD
                    </td>
                </tr>

                <tr>
                    <th>Mouvements</th>
                    <td><?= $totalMovements ?></td>
                </tr>

            </table>

        </div>

    </div>

</div>
    <div class="d-flex justify-content-between align-items-center flex-wrap gap-3">
        <div>
            <div class="text-muted small text-uppercase fw-bold">État du stock</div>
            <span class="darlink-badge <?= $etatClass ?>"><?= $etatLabel ?></span>
        </div>

        <div>
            <div class="text-muted small text-uppercase fw-bold">Unité</div>
            <div class="fw-semibold"><?= htmlspecialchars($article['unite'] ?? '-') ?></div>
        </div>

        <div>
            <div class="text-muted small text-uppercase fw-bold">Valeur unitaire</div>
            <div class="fw-semibold"><?= number_format($achat, 2, ',', ' ') ?> MAD</div>
        </div>

        <div>
            <div class="text-muted small text-uppercase fw-bold">Valorisation</div>
            <div class="fw-semibold text-success"><?= number_format($valeur, 2, ',', ' ') ?> MAD</div>
        </div>
    </div>
</div>

<div class="darlink-card darlink-table-card">
	<div class="darlink-card darlink-card-body mb-4">
    <h2 class="h5 mb-3">
        <i class="bi bi-clipboard-data me-2"></i>Informations de gestion
    </h2>

    <div class="row g-3">
        <div class="col-md-3">
            <div class="text-muted small text-uppercase fw-bold">Dernière entrée</div>
            <div class="fw-semibold">
                <?= $lastEntry ? htmlspecialchars($lastEntry['date_mouvement']) : '-' ?>
            </div>
        </div>

        <div class="col-md-3">
            <div class="text-muted small text-uppercase fw-bold">Dernière sortie</div>
            <div class="fw-semibold">
                <?= $lastExit ? htmlspecialchars($lastExit['date_mouvement']) : '-' ?>
            </div>
        </div>

        <div class="col-md-3">
            <div class="text-muted small text-uppercase fw-bold">PMP</div>
            <div class="fw-semibold">
                <?= number_format($pmp, 2, ',', ' ') ?> MAD
            </div>
        </div>

        <div class="col-md-3">
            <div class="text-muted small text-uppercase fw-bold">Nombre de mouvements</div>
            <div class="fw-semibold">
                <?= (int)$totalMovements ?>
            </div>
        </div>

        <div class="col-md-3">
            <div class="text-muted small text-uppercase fw-bold">Total entrées</div>
            <div class="fw-semibold text-success">
                <?= number_format($totalEntries, 0, ',', ' ') ?>
            </div>
        </div>

        <div class="col-md-3">
            <div class="text-muted small text-uppercase fw-bold">Total sorties</div>
            <div class="fw-semibold text-danger">
                <?= number_format($totalExits, 0, ',', ' ') ?>
            </div>
        </div>

        <div class="col-md-3">
            <div class="text-muted small text-uppercase fw-bold">Ajustements</div>
            <div class="fw-semibold">
                <?= (int)$totalAdjustments ?>
            </div>
        </div>

        <div class="col-md-3">
            <div class="text-muted small text-uppercase fw-bold">Inventaires</div>
            <div class="fw-semibold">
                <?= (int)$totalInventory ?>
            </div>
        </div>

        <div class="col-md-12">
            <div class="text-muted small text-uppercase fw-bold">Dernier mouvement</div>
            <div class="fw-semibold">
                <?= $lastMovement ? htmlspecialchars(($typeLabels[$lastMovement['type_mouvement']] ?? $lastMovement['type_mouvement']) . ' — ' . $lastMovement['date_mouvement']) : '-' ?>
            </div>
        </div>
    </div>
</div>
    <div class="darlink-table-toolbar">
        <div class="darlink-table-toolbar-left">
            <strong><i class="bi bi-clock-history me-1"></i> Historique des mouvements</strong>
        </div>

        <div class="darlink-table-toolbar-right">
            <span class="text-muted small"><?= count($movements) ?> mouvement(s)</span>
        </div>
    </div>

    <div class="table-responsive darlink-table-wrapper">
        <table class="table align-middle darlink-table darlink-table-modern" id="stockMovementsTable">
            <thead>
            <tr>
                <th class="darlink-sortable" data-column="0">Date <span class="sort-indicator">↕</span></th>
                <th class="darlink-sortable" data-column="1">Type <span class="sort-indicator">↕</span></th>
                <th class="text-end darlink-sortable" data-column="2">Quantité <span class="sort-indicator">↕</span></th>
                <th class="text-end darlink-sortable" data-column="3">Prix achat <span class="sort-indicator">↕</span></th>
                <th class="darlink-sortable" data-column="4">Origine <span class="sort-indicator">↕</span></th>
                <th class="darlink-sortable" data-column="5">Commentaire <span class="sort-indicator">↕</span></th>
            </tr>
            </thead>

            <tbody>
            <?php if (empty($movements)): ?>
                <tr>
                    <td colspan="6" class="text-center text-muted py-5">
                        Aucun mouvement pour cet article.
                    </td>
                </tr>
            <?php endif; ?>

            <?php foreach ($movements as $movement): ?>
                <?php
                    $type = (string)($movement['type_mouvement'] ?? '');
                    $badgeClass = $typeBadges[$type] ?? 'darlink-badge-light';
                    $qte = (float)$movement['quantite'];
                ?>
                <tr>
                    <td><?= htmlspecialchars($movement['date_mouvement']) ?></td>

                    <td>
                        <span class="darlink-badge <?= htmlspecialchars($badgeClass) ?>">
                            <?= htmlspecialchars($typeLabels[$type] ?? $type) ?>
                        </span>
                    </td>

                    <td class="text-end <?= $qte < 0 ? 'text-danger' : 'text-success' ?> fw-semibold">
                        <?= number_format($qte, 0, ',', ' ') ?>
                    </td>

                    <td class="text-end">
                        <?= number_format((float)$movement['prix_achat_ht'], 2, ',', ' ') ?>
                    </td>

                    <td><?= htmlspecialchars($movement['origine'] ?? '-') ?></td>

                    <td><?= htmlspecialchars($movement['commentaire'] ?? '') ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>