<!DOCTYPE html>
<html lang="vi">

<head>
    <?php require_once('head.php');?>
    <title>AdminLTE 3 | Lịch Sử Tạo Logo</title>
    <?php require_once('nav.php');?>
    
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
    
    <style>
        body { font-family: 'Inter', sans-serif !important; background-color: #f8fafc; }
        .card { border-radius: 20px; border: none; box-shadow: 0 4px 15px rgba(0,0,0,0.05); }
        .table thead th { background: #f1f5f9; color: #475569; text-transform: uppercase; font-size: 0.75rem; letter-spacing: 0.05em; border: none !important; }
        .table td { padding: 1.2rem !important; vertical-align: middle !important; border-top: 1px solid #f1f5f9 !important; }
        
        /* Status Badges */
        .badge-done { background: #dcfce7; color: #166534; padding: 6px 12px; border-radius: 20px; font-weight: 600; }
        .badge-pending { background: #fef9c3; color: #854d0e; padding: 6px 12px; border-radius: 20px; font-weight: 600; }
        
        .btn-icon { padding: 8px 12px; border-radius: 10px; transition: 0.3s; }
        .btn-icon:hover { opacity: 0.8; transform: translateY(-2px); }
    </style>
</head>

<?php
// XỬ LÝ XÓA AN TOÀN
if (isset($_GET['delete'])) {
    $id = intval($_GET['delete']);
    $stmt = $ketnoi->prepare("DELETE FROM `ls_logo` WHERE `id` = ?");
    $stmt->bind_param("i", $id);
    if ($stmt->execute()) {
        echo '<script>Swal.fire("Thành Công", "Đã xóa lịch sử", "success").then(() => { location.href="chuyen-muc.php"; });</script>';
    }
}
?>

<body class="hold-transition sidebar-mini layout-fixed">
    <div class="wrapper">
        <div class="content-wrapper p-4">
            <div class="mb-4">
                <h1 class="h3 font-weight-bold">Lịch Sử Tạo Logo</h1>
                <p class="text-muted">Quản lý và theo dõi trạng thái các đơn hàng logo.</p>
            </div>

            <div class="card p-3">
                <table id="datatable1" class="table table-hover">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Tên Logo</th>
                            <th>Người Tạo</th>
                            <th>Link Kết Quả</th>
                            <th>Trạng Thái</th>
                            <th class="text-center">Hành Động</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $result = mysqli_query($ketnoi, "SELECT * FROM `ls_logo` ORDER BY id DESC");
                        while($row = mysqli_fetch_assoc($result)) {
                            // Giả sử hàm status() trả về 1 cho Done, 0 cho Pending
                            $statusClass = ($row['status'] == 1) ? 'badge-done' : 'badge-pending';
                        ?>
                        <tr>
                            <td class="text-muted font-weight-bold">#<?=$row['id'];?></td>
                            <td class="font-weight-600"><?=$row['name'];?></td>
                            <td><span class="badge badge-light"><?=$row['username'];?></span></td>
                            <td>
                                <a href="<?=$row['logo_output'];?>" target="_blank" class="text-primary font-weight-bold">
                                    <i class="fas fa-external-link-alt mr-1"></i>Xem kết quả
                                </a>
                            </td>
                            <td><span class="<?= $statusClass ?>"><?=status($row['status']);?></span></td>
                            <td class="text-center">
                                <a href="edit-logo.php?id=<?=$row['id'];?>" class="btn btn-outline-info btn-icon mr-2">
                                    <i class="fas fa-edit"></i>
                                </a>
                                <button onclick="confirmDelete(<?=$row['id'];?>)" class="btn btn-outline-danger btn-icon">
                                    <i class="fas fa-trash-alt"></i>
                                </button>
                            </td>
                        </tr>
                        <?php } ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <script>
    function confirmDelete(id) {
        Swal.fire({
            title: 'Xóa đơn này?',
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#e11d48',
            confirmButtonText: 'Xóa'
        }).then((result) => {
            if (result.isConfirmed) { window.location.href = "?delete=" + id; }
        });
    }

    $(document).ready(function() {
        $('#datatable1').DataTable({
            "language": { "url": "//cdn.datatables.net/plug-ins/1.10.21/i18n/Vietnamese.json" }
        });
    });
    </script>
    <?php require_once('foot.php');?>
</body>
</html>