<?php
session_start();
if(!isset($_SESSION['usuario'])) {
    header('Location: login.php');
    exit();
}
include 'conexao.php';

// Atualiza status para entregue
if (isset($_GET['entregar']) && is_numeric($_GET['entregar'])) {
    $id = intval($_GET['entregar']);
    // Nota de Segurança: Para aplicações reais, use Prepared Statements aqui.
    mysqli_query($conn, "UPDATE pedidos SET status = 'Entregue' WHERE id = $id");
    header('Location: pedidos.php');
    exit();
}

// Busca todos os pedidos
$resPedidos = mysqli_query($conn, "SELECT * FROM pedidos ORDER BY data DESC");
if (!$resPedidos) {
    die('Erro na consulta SQL: ' . mysqli_error($conn));
}
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <title>Pedidos - Mercadinho Pajeú</title>
    <link href="https://fonts.googleapis.com/css?family=Roboto:400,500,700&display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <link rel="shortcut icon" href="logo.png"/>
    <style>
        /* Cores Base: Vermelho (#A20000, #8B0000), Amarelo/Dourado (#ffc107), Fundo Escuro (#191c24, #1a2332) */
        body { font-family: 'Roboto', Arial, sans-serif; margin: 0; background: #1a2332; color: #fff;}
        
        /* --- Sidebar --- */
        .sidebar { position: fixed; left: 0; top: 0; bottom: 0; width: 240px; background: linear-gradient(180deg, #A20000 0%, #8B0000 100%); border-right: 1px solid #8B0000; z-index: 100; min-height: 100vh;}
        .sidebar .logo-area { display: flex; align-items: center; padding: 24px 28px 20px 28px; border-bottom: 1px solid #ffc107;}
        .sidebar .logo-area img { width: 32px; height: 32px; object-fit: contain; border-radius: 8px; margin-right: 14px; background: #fff;}
        .sidebar .logo-title { font-weight: 700; font-size: 1.15em; color: #ffc107; letter-spacing: 1px;}
        .sidebar nav { flex: 1;}
        .sidebar ul { padding: 0; margin: 0; list-style: none;}
        .sidebar li { margin: 7px 0;}
        .sidebar a {display: flex; align-items: center; color: #fff; text-decoration: none; padding: 12px 30px; font-size: 1.07em; border-left: 4px solid transparent; transition: background 0.18s, border-color 0.18s, color 0.18s;}
        .sidebar a.active, .sidebar a:hover {background: rgba(255, 255, 255, 0.1); color: #ffc107; border-left: 4px solid #ffc107;}
        .sidebar .material-icons {margin-right: 18px; font-size: 23px;}
        .sidebar .user-box {padding: 22px 24px 16px 24px; border-top: 1px solid #ffc107; background: #8B0000; text-align: center;}
        .sidebar .user-box strong {color: #fff; font-size: 1.08em; font-weight: 500;}
        .sidebar .user-box small {color: #ffc107; font-size: 0.98em;}

        /* --- Main Content --- */
        .main-content {margin-left: 240px; padding: 40px 20px 20px 20px; background: #191c24;}
        h1 {color: #ffc107; font-weight: 500;} /* Título em Amarelo/Dourado */
        
        /* --- Tabela --- */
        table {width: 100%; border-collapse: collapse; background: #23263a; border-radius: 8px; overflow: hidden; box-shadow: 0 4px 10px rgba(0,0,0,0.3);}
        th, td {padding: 12px 8px; border-bottom: 1px solid #380000; text-align: center;}
        th {color: #ffc107; background: #A20000; font-weight: bold;} /* Cabeçalho Vermelho */
        tr:hover {background: #380000;} /* Hover em Vermelho Escuro */
        
        /* --- Botões --- */
        .btn {padding: 8px 16px; border: none; border-radius: 6px; cursor: pointer; font-weight: 500; margin: 2px;}
        .btn-entregar {background: #43a047; color: #fff; transition: background 0.2s;} /* Verde para Entregar */
        .btn-entregar:hover {background: #388e3c;}
        .btn-entregar:disabled {background: #555; color: #aaa; cursor: default;}
        .btn-print {background: #ffc107; color: #8B0000; transition: background 0.2s;} /* Imprimir em Amarelo/Dourado com texto Vermelho */
        .btn-print:hover {background: #e0ac00;}

        /* --- Responsividade --- */
        @media (max-width: 1100px) {
            .main-content { margin-left: 0; }
            .sidebar { position: static; width: 100%; height: auto; border-right: none; box-shadow: none;}
        }
        @media (max-width: 768px) {
            .main-content { padding: 20px 10px; }
            table, thead, tbody, th, td, tr { display: block; }
            thead tr { position: absolute; top: -9999px; left: -9999px; }
            tr { border: 1px solid #380000; margin-bottom: 10px; }
            td { border: none; position: relative; padding-left: 50%; text-align: right; }
            td:before { 
                content: attr(data-label); 
                position: absolute; left: 6px; width: 45%; 
                padding-right: 10px; white-space: nowrap; text-align: left; 
                font-weight: bold; color: #ffc107;
            }
        }
    </style>
</head>
<body>
    <aside class="sidebar">
        <div class="logo-area">
            <img src="logo.png" alt="Logo">
            <span class="logo-title">Mercadinho Pajeú</span>
        </div>
        <nav>
            <ul>
                <li><a href="dashboard.php"><span class="material-icons">dashboard</span>Dashboard</a></li>
                <li><a href="produtos.php"><span class="material-icons">edit</span>Editar Produtos</a></li>
                <li><a href="pedidos.php" class="active"><span class="material-icons">list_alt</span>Pedidos</a></li>
                <li><a href="logout.php"><span class="material-icons">logout</span>Sair</a></li>
            </ul>
        </nav>
        <div class="user-box">
            <strong><?php echo htmlspecialchars($_SESSION['usuario']); ?></strong><br>
            <small>Administrador</small>
        </div>
    </aside>
    <div class="main-content">
        <h1>Pedidos</h1>
        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Cliente</th>
                    <th>Data</th>
                    <th>Itens</th>
                    <th>Total (R$)</th>
                    <th>Status</th>
                    <th>Ações</th>
                </tr>
            </thead>
            <tbody>
            <?php while($pedido = $resPedidos->fetch_assoc()): ?>
                <tr>
                    <td data-label="ID"><?php echo $pedido['id']; ?></td>
                    <td data-label="Cliente"><?php echo htmlspecialchars($pedido['cliente']); ?></td>
                    <td data-label="Data"><?php echo date('d/m/Y H:i', strtotime($pedido['data'])); ?></td>
                    <td data-label="Itens"><?php echo htmlspecialchars($pedido['itens']); ?></td>
                    <td data-label="Total (R$)"><?php echo number_format($pedido['total'],2,',','.'); ?></td>
                    <td data-label="Status"><?php echo htmlspecialchars($pedido['status']); ?></td>
                    <td data-label="Ações">
                        <button class="btn btn-print" onclick="window.open('imprimir_pedido.php?id=<?php echo $pedido['id']; ?>','_blank')">
                            <span class="material-icons" style="vertical-align:middle;">print</span> Imprimir
                        </button>
                        <a href="pedidos.php?entregar=<?php echo $pedido['id']; ?>">
                        <button class="btn btn-entregar" <?php if($pedido['status']=='Entregue') echo 'disabled'; ?>>
                            <span class="material-icons" style="vertical-align:middle;">check_circle</span> Entregar
                        </button></a>
                    </td>
                </tr>
            <?php endwhile; ?>
            </tbody>
        </table>
    </div>
</body>
</html>