// ================================================ // data.js — Versão Final com Token Bearer (MySQL) // ================================================ 'use strict'; var SisprimeDB = (function () { var API_URL = './api/'; async function apiFetch(endpoint, options = {}) { // Recuperar sessão para pegar o Token var session = JSON.parse(localStorage.getItem('sp_session') || 'null'); var token = session ? session.token : ''; options.headers = options.headers || {}; if (token) { options.headers['Authorization'] = 'Bearer ' + token; } try { const response = await fetch(API_URL + endpoint, { ...options, headers: { 'Content-Type': 'application/json', ...options.headers } }); const result = await response.json(); if (response.status === 401) { localStorage.removeItem('sp_session'); if (!window.location.href.includes('login.html')) { window.location.replace('login.html'); } return null; } if (!result.ok) { throw new Error(result.error || 'Erro na API'); } // O seu PHP retorna os dados dentro da chave "data" return result.data; } catch (error) { console.error('Erro na requisição:', error); throw error; } } return { // ---- Auth ---- login: async function(email, password) { // CORREÇÃO: endpoint correto é auth.php var res = await apiFetch('auth.php?action=login', { method: 'POST', body: JSON.stringify({ email: email, password: password }) }); if (res && res.token) { // Guardar o token e dados do usuário var session = { token: res.token, user_id: res.user.id, nombre: res.user.nombre, perfil: res.user.perfil, es_socio: res.user.es_socio, expires: res.user.expires }; localStorage.setItem('sp_session', JSON.stringify(session)); return session; } return null; }, getSession: function() { try { var s = JSON.parse(localStorage.getItem('sp_session') || 'null'); if (!s || !s.expires || Date.now() > s.expires) { localStorage.removeItem('sp_session'); return null; } return s; } catch(e) { return null; } }, logout: async function() { try { await apiFetch('auth.php?action=logout', { method: 'POST' }); } catch(e){} localStorage.removeItem('sp_session'); }, // ---- Pedidos ---- getPedidos: async function() { return await apiFetch('pedidos.php'); }, getPedido: async function(id) { return await apiFetch('pedidos.php?id=' + id); }, savePedido: async function(pedido) { var method = pedido.id ? 'PUT' : 'POST'; var url = 'pedidos.php' + (pedido.id ? '?id=' + pedido.id : ''); return await apiFetch(url, { method: method, body: JSON.stringify(pedido) }); }, deletePedido: async function(id) { return await apiFetch('pedidos.php?id=' + id, { method: 'DELETE' }); }, // ---- Clientes ---- getClientes: async function() { return await apiFetch('clientes.php'); }, saveCliente: async function(cliente) { var method = cliente.id ? 'PUT' : 'POST'; var url = 'clientes.php' + (cliente.id ? '?id=' + cliente.id : ''); return await apiFetch(url, { method: method, body: JSON.stringify(cliente) }); }, // ---- Utilitários ---- getConfig: function() { try { return JSON.parse(localStorage.getItem('sp_config') || '{}'); } catch(e){ return {}; } } }; })();