<?php
require_once 'includes/session.php';
require_once 'includes/conn.php';

if(!isset($_SESSION['admin']) || trim($_SESSION['admin']) == ''){
    header('location: index.php');
    exit();
}

$conn = $pdo->open();

if(isset($_POST['reset'])) {
    try {
        // Réinitialiser les ventes
        $stmt = $conn->prepare("TRUNCATE TABLE sales");
        $stmt->execute();
        
        // Réinitialiser les dettes
        $stmt = $conn->prepare("UPDATE customers SET debt = 0");
        $stmt->execute();
        
        $_SESSION['success'] = 'Toutes les statistiques ont été réinitialisées avec succès';
    }
    catch(PDOException $e) {
        $_SESSION['error'] = $e->getMessage();
    }
    
    $pdo->close();
    header('location: reset_stats.php');
    exit();
}
?>
<?php include 'includes/header.php'; ?>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">

    <?php include 'includes/navbar.php'; ?>
    <?php include 'includes/menubar.php'; ?>

    <div class="content-wrapper">
        <section class="content-header">
            <h1>Réinitialisation des Statistiques</h1>
            <ol class="breadcrumb">
                <li><a href="home.php"><i class="fa fa-dashboard"></i> Accueil</a></li>
                <li class="active">Réinitialisation</li>
            </ol>
        </section>

        <section class="content">
            <?php
            if(isset($_SESSION['error'])){
                echo "
                    <div class='alert alert-danger alert-dismissible'>
                        <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                        <h4><i class='icon fa fa-warning'></i> Erreur!</h4>
                        ".$_SESSION['error']."
                    </div>
                ";
                unset($_SESSION['error']);
            }
            if(isset($_SESSION['success'])){
                echo "
                    <div class='alert alert-success alert-dismissible'>
                        <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                        <h4><i class='icon fa fa-check'></i> Succès!</h4>
                        ".$_SESSION['success']."
                    </div>
                ";
                unset($_SESSION['success']);
            }
            ?>
            <div class="row">
                <div class="col-xs-12">
                    <div class="box">
                        <div class="box-header with-border">
                            <h3 class="box-title">Réinitialiser toutes les statistiques</h3>
                        </div>
                        <div class="box-body">
                            <div class="alert alert-warning">
                                <strong>Attention!</strong> Cette action va:
                                <ul>
                                    <li>Supprimer tout l'historique des ventes</li>
                                    <li>Remettre à zéro toutes les dettes des clients</li>
                                </ul>
                                Cette action est irréversible.
                            </div>
                            <form class="form-horizontal" method="POST" action="reset_stats.php">
                                <div class="form-group">
                                    <div class="col-sm-12">
                                        <button type="submit" class="btn btn-danger btn-flat" name="reset" onclick="return confirm('Êtes-vous sûr de vouloir réinitialiser toutes les statistiques? Cette action est irréversible.');">
                                            <i class="fa fa-refresh"></i> Réinitialiser les statistiques
                                        </button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </div>

    <?php include 'includes/footer.php'; ?>
</div>
<?php include 'includes/scripts.php'; ?>
</body>
</html>
