File size: 4,402 Bytes
7b3aac2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// ========================================
// API CLIENT — Frontend → Backend Communication
// ========================================
const API_BASE = (window.location.hostname === '127.0.0.1' || window.location.port === '5500')
    ? 'http://localhost:3000/api'
    : '/api';

const API = {
    // === PRODUCTS ===
    async getProducts() {
        try {
            const res = await fetch(`${API_BASE}/products`);
            if (!res.ok) throw new Error('Products fetch failed');
            return await res.json();
        } catch (err) {
            console.warn('API unavailable, using local data:', err.message);
            return null; // Fallback to local DEFAULT_PRODUCTS
        }
    },

    async getProduct(id) {
        try {
            const res = await fetch(`${API_BASE}/products/${id}`);
            if (!res.ok) return null;
            return await res.json();
        } catch {
            return null;
        }
    },

    async createProduct(data) {
        const res = await fetch(`${API_BASE}/products`, {
            method: 'POST', headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(data)
        });
        return await res.json();
    },

    async updateProduct(id, data) {
        const res = await fetch(`${API_BASE}/products/${id}`, {
            method: 'PUT', headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(data)
        });
        return await res.json();
    },

    async deleteProduct(id) {
        const res = await fetch(`${API_BASE}/products/${id}`, { method: 'DELETE' });
        return await res.json();
    },

    // === AUTH ===
    async register(name, phone, password, email) {
        const res = await fetch(`${API_BASE}/auth/register`, {
            method: 'POST', headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ name, phone, password, email })
        });
        return await res.json();
    },

    async login(phone, password) {
        const res = await fetch(`${API_BASE}/auth/login`, {
            method: 'POST', headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ phone, password })
        });
        return await res.json();
    },

    async getProfile(userId) {
        const res = await fetch(`${API_BASE}/auth/profile/${userId}`);
        return await res.json();
    },

    async updateProfile(userId, data) {
        const res = await fetch(`${API_BASE}/auth/profile/${userId}`, {
            method: 'PUT', headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(data)
        });
        return await res.json();
    },

    // === ORDERS ===
    async getOrders() {
        const res = await fetch(`${API_BASE}/orders`);
        return await res.json();
    },

    async getUserOrders(userId) {
        const res = await fetch(`${API_BASE}/orders/user/${userId}`);
        return await res.json();
    },

    async createOrder(data) {
        const res = await fetch(`${API_BASE}/orders`, {
            method: 'POST', headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(data)
        });
        return await res.json();
    },

    async updateOrderStatus(orderId, status) {
        const res = await fetch(`${API_BASE}/orders/${orderId}/status`, {
            method: 'PUT', headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ status })
        });
        return await res.json();
    },

    // === SETTINGS ===
    async getSettings() {
        try {
            const res = await fetch(`${API_BASE}/settings`);
            return await res.json();
        } catch {
            return {};
        }
    },

    async updateSettings(data) {
        const res = await fetch(`${API_BASE}/settings`, {
            method: 'PUT', headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(data)
        });
        return await res.json();
    },

    async adminLogin(password) {
        const res = await fetch(`${API_BASE}/settings/admin-login`, {
            method: 'POST', headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ password })
        });
        return await res.json();
    }
};