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

$sql = "SELECT * FROM produtos";
$result = mysqli_query($conn, $sql);

// Conta produtos sem foto e armazena todos em array
$total_sem_foto = 0;
$produtos = [];
if ($result) {
    while($row = mysqli_fetch_assoc($result)) {
        if (empty($row['foto'])) {
            $total_sem_foto++;
        }
        $produtos[] = $row;
    }
} else {
    $produtos = [];
}

// Ordena produtos: sem foto primeiro
usort($produtos, function($a, $b) {
    $afoto = empty($a['foto']);
    $bfoto = empty($b['foto']);
    if ($afoto == $bfoto) return 0;
    return $afoto ? -1 : 1;
});
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <title>Editar Produtos - Lojão da Construção</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <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>
        html, body {
            font-family: 'Roboto', Arial, sans-serif;
            background: #191c24 !important;
            color: #fff;
            margin: 0;
            padding: 0;
        }
        .sidebar {
            position: fixed;
            left: 0; top: 0; bottom: 0;
            width: 240px;
            background: #161925;
            border-right: 1px solid #23263a;
            z-index: 100;
            display: flex;
            flex-direction: column;
            min-height: 100vh;
        }
        .sidebar .logo-area {
            display: flex;
            align-items: center;
            padding: 24px 28px 20px 28px;
            border-bottom: 1px solid #252943;
        }
        .sidebar .logo-area img {
            width: 44px; height: 44px; object-fit: contain;
            border-radius: 8px; margin-right: 14px; background: #fff;
        }
        .sidebar .logo-title {
            font-weight: 700; font-size: 1.22em; color: #1976d2;
            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: #cfd8dc;
            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: #23263a;
            color: #42a5f5;
            border-left: 4px solid #1976d2;
        }
        .sidebar .material-icons {
            margin-right: 18px;
            font-size: 23px;
        }
        .sidebar .user-box {
            padding: 22px 24px 16px 24px;
            border-top: 1px solid #252943;
            background: #23263a;
            text-align: center;
        }
        .sidebar .user-box strong {
            color: #fff;
            font-size: 1.08em;
            font-weight: 500;
        }
        .sidebar .user-box small {
            color: #90caf9; font-size: 0.98em;
        }
        .main-content {
            margin-left: 240px;
            min-height: 100vh;
            background: #191c24;
            padding: 0;
        }
        .appbar {
            background: #23263a;
            height: 64px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            box-shadow: 0 2px 7px 0 #1976d20b;
            padding: 0 32px;
            position: sticky;
            top: 0;
            z-index: 10;
        }
        .appbar .title {
            font-weight: 500;
            color: #42a5f5;
            font-size: 1.13em;
        }
        .appbar .material-icons {
            color: #42a5f5;
            vertical-align: middle;
        }
        .produtos-header {
            margin: 36px 0 20px 0;
            font-size: 1.4em;
            color: #fff;
            font-weight: 500;
            letter-spacing: 0.5px;
            padding-left: 18px;
        }
        .search-bar-container-fixed {
            position: sticky;
            top: 64px;
            z-index: 11;
            background: #191c24;
            display: flex;
            align-items: center;
            justify-content: flex-start;
            padding: 10px 0 10px 18px;
            border-bottom: 1px solid #23263a;
            gap: 30px;
        }
        .info-sem-foto {
            background: #ffcdd2;
            color: #b71c1c;
            border: 1px solid #d32f2f;
            padding: 8px 14px;
            border-radius: 8px;
            font-weight: 500;
            display: inline-block;
            margin: 0;
        }
        .search-bar-input {
            padding:8px 12px;
            border-radius:6px;
            border:1px solid #1976d2;
            width:260px;
            background:#23263a;
            color:#fff;
            font-size:1em;
        }
        .table-responsive {
            margin: 0 0 38px 16px;
            overflow-x: auto;
        }
        table {
            width: 100%;
            min-width: 780px;
            border-collapse: collapse;
            background: #23263a;
            color: #fff;
            border-radius: 10px;
            overflow: hidden;
            box-shadow: 8px 8px 16px #161925, -8px -8px 16px #23263a;
        }
        th, td {
            padding: 12px 8px;
            text-align: center;
        }
        th {
            background: #1976d2;
            color: #fff;
            font-weight: bold;
            font-size: 1em;
            border: none;
        }
        tr:nth-child(even) { background: #21253a; }
        tr:nth-child(odd) { background: #23263a; }
        tr:hover { background: #27345c; }
        .foto-produto {
            width: 50px;
            height: 50px;
            object-fit: cover;
            border-radius: 6px;
            box-shadow: 0 2px 6px #1976d211;
        }
        .btn-editar {
            background: #1976d2;
            color: #fff;
            text-decoration: none;
            padding: 7px 16px;
            border-radius: 6px;
            font-weight: 500;
            transition: background 0.2s;
            border: none;
            cursor: pointer;
        }
        .btn-editar:hover { background: #1252a3; }
    </style>
</head>
<body>
    <aside class="sidebar">
        <div class="logo-area">
            <img src="logo.png" alt="Logo">
            <span class="logo-title">Lojão Admin</span>
        </div>
        <nav>
            <ul>
                <li><a href="dashboard.php"><span class="material-icons">dashboard</span>Dashboard</a></li>
                <li><a href="produtos.php" class="active"><span class="material-icons">edit</span>Editar Produtos</a></li>
				<li><a href="pedidos.php"><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">
        <header class="appbar">
            <span class="title">Editar Produtos</span>
            <span class="material-icons">account_circle</span>
        </header>
        <div class="produtos-header">
            Gerencie seus produtos cadastrados
        </div>
        <div class="search-bar-container-fixed">
            <div class="info-sem-foto">
                Total de produtos sem foto: <strong><?= $total_sem_foto ?></strong>
            </div>
            <input type="text" id="search-bar" class="search-bar-input" placeholder="Escaneie ou digite o código de barras...">
        </div>
        <div class="table-responsive">
            <table>
                <thead>
                <tr>
                    <th>ID</th>
                    <th>Código</th>
                    <th>Código de Barras</th>
                    <th>Nome</th>
                    <th>Preço</th>
                    <th>Foto</th>
                    <th>Ações</th>
                </tr>
                </thead>
                <tbody>
                <?php foreach($produtos as $row) { ?>
                <tr>
                    <td><?= $row['id'] ?></td>
                    <td><?= htmlspecialchars($row['codigo']) ?></td>
                    <td><?= htmlspecialchars($row['codigo_barra']) ?></td>
                    <td><?= htmlspecialchars($row['nome']) ?></td>
                    <td>R$ <?= number_format($row['preco'], 2, ',', '.') ?></td>
                    <td>
                        <?php if($row['foto']) { ?>
                            <img src="fotos/<?= htmlspecialchars($row['foto']) ?>" class="foto-produto" alt="Foto">
                        <?php } else { ?>
                            <span style="color:#aaa;">Sem foto</span>
                        <?php } ?>
                    </td>
                    <td>
                        <a href="editar_produto.php?id=<?= $row['id'] ?>" class="btn-editar">Editar</a>
                    </td>
                </tr>
                <?php } ?>
                </tbody>
            </table>
        </div>
    </div>
    <script>
    function pesquisarPorCodigoBarras() {
        const valor = document.getElementById('search-bar').value.trim().toLowerCase();
        document.querySelectorAll('table tbody tr').forEach(function(row) {
            const codBarras = row.children[2]?.textContent?.toLowerCase() || '';
            if (codBarras.includes(valor)) {
                row.style.display = '';
            } else {
                row.style.display = 'none';
            }
        });
    }

    document.getElementById('search-bar').addEventListener('input', pesquisarPorCodigoBarras);

    // Foco automático para leitor de código de barras
    document.getElementById('search-bar').focus();
    </script>
</body>
</html>
