<?php
$u=current_user();
$q=db()->prepare('SELECT ci.*,p.name,p.description,p.price,p.stock,p.image,c.name company_name,c.logo company_logo,c.pickup_scheduling_enabled,c.pickup_weekdays,c.pickup_start_time,c.pickup_end_time,c.pickup_interval_minutes FROM cart_items ci JOIN products p ON p.id=ci.product_id JOIN companies c ON c.id=ci.company_id WHERE ci.user_id=? ORDER BY ci.id');
$q->execute([$u['id']]);$items=$q->fetchAll();$subtotal=0;foreach($items as $it)$subtotal+=(float)$it['price']*(float)$it['qty'];
?>
<section class="page-intro"><div><span class="mini-label">COMPRAS</span><h2>Meu carrinho</h2><p>Junte vários produtos da mesma empresa e finalize tudo em um único pedido.</p></div><a class="btn btn-secondary" href="dashboard.php?page=catalog">Continuar comprando</a></section>
<?php if(!$items):?><section class="panel"><div class="empty-state"><strong>Seu carrinho está vazio</strong>Escolha uma empresa e adicione os produtos desejados.</div></section><?php else:?>
<section class="panel"><div class="panel-header"><div><h3><?= e($items[0]['company_name']) ?></h3><p><?= count($items) ?> produto(s) no carrinho.</p></div><strong class="item-price"><?= money($subtotal) ?></strong></div>
<div class="table-wrap"><table><thead><tr><th>Produto</th><th>Preço</th><th>Quantidade</th><th>Total</th><th>Ação</th></tr></thead><tbody><?php foreach($items as $it):?><tr><td><div class="table-title"><span class="table-thumb"><?php if($it['image']):?><img src="<?= e(media_url($it['image'])) ?>" alt=""><?php else:?>🛍️<?php endif;?></span><div><strong><?= e($it['name']) ?></strong><small style="display:block">Estoque: <?= e($it['stock']) ?></small></div></div></td><td><?= money($it['price']) ?></td><td><form class="actions" method="post" action="actions.php"><?= csrf_field() ?><input type="hidden" name="action" value="update_cart_item"><input type="hidden" name="return_page" value="cart"><input type="hidden" name="id" value="<?= (int)$it['id'] ?>"><input class="input" style="width:90px" type="number" min="0" max="<?= max(1,(int)$it['stock']) ?>" name="qty" value="<?= e($it['qty']) ?>"><button class="btn btn-secondary btn-sm">Atualizar</button></form></td><td><?= money((float)$it['price']*(float)$it['qty']) ?></td><td><form method="post" action="actions.php"><?= csrf_field() ?><input type="hidden" name="action" value="remove_cart_item"><input type="hidden" name="return_page" value="cart"><input type="hidden" name="id" value="<?= (int)$it['id'] ?>"><button class="btn btn-danger btn-sm">Remover</button></form></td></tr><?php endforeach;?></tbody></table></div></section>
<section class="panel"><div class="panel-header"><div><h3>Finalizar pedido</h3><p>Escolha entrega ou retirada no estabelecimento.</p></div></div><form method="post" action="actions.php" data-fulfillment-form><?= csrf_field() ?><input type="hidden" name="action" value="checkout_cart"><input type="hidden" name="return_page" value="cart"><div class="form-grid"><div class="form-group"><label>Forma de recebimento</label><select name="fulfillment_type" data-fulfillment-select><option value="delivery">Entrega</option><option value="pickup">Retirar no estabelecimento/estacionamento</option></select></div><div class="form-group"><label>Pagamento</label><select name="payment_method"><option value="pix">Pix</option><option value="card">Cartão</option><option value="cash">Dinheiro</option></select></div><div class="form-group full" data-delivery-address><label>Endereço</label><input class="input" name="address" value="<?= e(full_user_address($u)) ?>"></div><?php if(!empty($items[0]['pickup_scheduling_enabled'])):$pickupTimes=company_pickup_time_options($items[0]);?><div class="form-group full" data-pickup-schedule hidden data-pickup-days="<?= e(implode(',',company_pickup_weekdays($items[0]))) ?>"><div class="notice"><strong>🎂 Agendar retirada</strong><br><small><?= e(company_pickup_schedule_summary($items[0])) ?></small></div><div class="form-grid" style="margin-top:10px"><div class="form-group"><label>Data para buscar</label><input class="input" type="date" name="pickup_date" min="<?= e(date('Y-m-d')) ?>"></div><div class="form-group"><label>Horário para buscar</label><select name="pickup_time"><option value="">Escolha o horário</option><?php foreach($pickupTimes as $pt):?><option value="<?= e($pt) ?>"><?= e($pt) ?></option><?php endforeach;?></select></div></div><small data-pickup-schedule-message></small></div><?php endif;?><div class="form-group full"><label>Observações</label><textarea name="notes"></textarea></div></div><div class="form-actions"><strong>Total dos produtos: <?= money($subtotal) ?></strong><button class="btn btn-primary">Finalizar compra</button></div></form></section>
<?php endif;?>
