3import { useState } from 'react';
4import { cwaApi } from '@/lib/cwa-gnap-api';
5import { cwaPosApi, setCWAPosApiKey } from '@/lib/cwa-pos-api';
7export default function CWADualTestPage() {
8 const [apiKey, setApiKey] = useState('');
9 const [results, setResults] = useState<Record<string, any>>({});
10 const [loading, setLoading] = useState<Record<string, boolean>>({});
11 const [searchTerm, setSearchTerm] = useState('test');
13 const handleSetApiKey = () => {
14 // Set API key for both systems
15 cwaApi.setApiKey(apiKey);
16 setCWAPosApiKey(apiKey);
17 alert('API Key set for both Management and POS APIs');
20 const testEndpoint = async (name: string, apiCall: () => Promise<any>) => {
21 setLoading(prev => ({ ...prev, [name]: true }));
23 const result = await apiCall();
24 setResults(prev => ({ ...prev, [name]: result }));
28 [name]: { error: error instanceof Error ? error.message : 'Unknown error' }
31 setLoading(prev => ({ ...prev, [name]: false }));
35 <div className="min-h-screen bg-bg p-8">
36 <div className="max-w-6xl mx-auto">
37 <h1 className="text-4xl font-bold text-text mb-8">
38 CWA Dual API Test Dashboard
41 {/* API Key Section */}
42 <div className="bg-surface p-6 rounded-lg shadow-md mb-8">
43 <h2 className="text-xl font-semibold mb-4">Authentication</h2>
44 <div className="flex gap-4">
48 onChange={(e) => setApiKey(e.target.value)}
49 placeholder="Enter FieldpineApiKey (e.g., BjjKmbw23IskPjxVGVMiqRhMcJBqtLYQRVHhYfnvMNp3IK)"
50 className="flex-1 p-2 border border-border rounded"
53 onClick={handleSetApiKey}
54 className="px-4 py-2 bg-brand text-white rounded hover:bg-brand"
59 <p className="text-sm text-muted mt-2">
60 Use the FieldpineApiKey cookie value from your browser when authenticated to CWA
64 {/* API Systems Grid */}
65 <div className="grid grid-cols-1 lg:grid-cols-2 gap-8 mb-8">
67 {/* Management API Section */}
68 <div className="bg-surface p-6 rounded-lg shadow-lg">
69 <h2 className="text-2xl font-semibold mb-6 text-brand">
72 <p className="text-sm text-muted mb-4">
73 Base: https://iig.cwanz.online/GNAP/j/BUCK?3=retailmax.elink.*
76 <div className="grid grid-cols-1 gap-4">
77 {/* Management API Endpoints */}
79 onClick={() => testEndpoint('mgmt-locations', () => cwaApi.getLocations())}
80 disabled={loading['mgmt-locations']}
81 className="w-full p-3 bg-brand text-white rounded hover:bg-brand disabled:opacity-50"
83 {loading['mgmt-locations'] ? 'Loading...' : 'Get Locations'}
87 onClick={() => testEndpoint('mgmt-stats', () => cwaApi.getStatsToday())}
88 disabled={loading['mgmt-stats']}
89 className="w-full p-3 bg-brand text-white rounded hover:bg-brand disabled:opacity-50"
91 {loading['mgmt-stats'] ? 'Loading...' : 'Get Stats Today'}
95 onClick={() => testEndpoint('mgmt-server-status', () => cwaApi.getServerStatus())}
96 disabled={loading['mgmt-server-status']}
97 className="w-full p-3 bg-brand text-white rounded hover:bg-brand disabled:opacity-50"
99 {loading['mgmt-server-status'] ? 'Loading...' : 'Get Server Status'}
103 onClick={() => testEndpoint('mgmt-config', () => cwaApi.getRetailConfig())}
104 disabled={loading['mgmt-config']}
105 className="w-full p-3 bg-brand text-white rounded hover:bg-brand disabled:opacity-50"
107 {loading['mgmt-config'] ? 'Loading...' : 'Get Retail Config'}
112 {/* POS API Section */}
113 <div className="bg-surface p-6 rounded-lg shadow-lg">
114 <h2 className="text-2xl font-semibold mb-6 text-green-600">
117 <p className="text-sm text-muted mb-4">
118 Base: https://murwillumbah.cwanz.online/openapi/*
121 <div className="grid grid-cols-1 gap-4">
122 {/* Product Search Input */}
123 <div className="mb-4">
124 <label className="block text-sm font-medium mb-2">Product Search:</label>
128 onChange={(e) => setSearchTerm(e.target.value)}
129 placeholder="Enter search term..."
130 className="w-full p-2 border border-border rounded"
134 {/* POS API Endpoints */}
136 onClick={() => testEndpoint('pos-config', () => cwaPosApi.getRetailConfig())}
137 disabled={loading['pos-config']}
138 className="w-full p-3 bg-success text-white rounded hover:bg-green-600 disabled:opacity-50"
140 {loading['pos-config'] ? 'Loading...' : 'Get POS Config'}
144 onClick={() => testEndpoint('pos-products', () => cwaPosApi.searchProducts(searchTerm))}
145 disabled={loading['pos-products']}
146 className="w-full p-3 bg-success text-white rounded hover:bg-green-600 disabled:opacity-50"
148 {loading['pos-products'] ? 'Loading...' : `Search Products: "${searchTerm}"`}
152 onClick={() => testEndpoint('pos-payments', () => cwaPosApi.getPaymentTypes())}
153 disabled={loading['pos-payments']}
154 className="w-full p-3 bg-success text-white rounded hover:bg-green-600 disabled:opacity-50"
156 {loading['pos-payments'] ? 'Loading...' : 'Get Payment Types'}
160 onClick={() => testEndpoint('pos-sale-template', () => cwaPosApi.getSaleListTemplate())}
161 disabled={loading['pos-sale-template']}
162 className="w-full p-3 bg-success text-white rounded hover:bg-green-600 disabled:opacity-50"
164 {loading['pos-sale-template'] ? 'Loading...' : 'Get Sale Template'}
170 {/* Results Section */}
171 <div className="bg-surface p-6 rounded-lg shadow-lg">
172 <h2 className="text-xl font-semibold mb-4">API Responses</h2>
174 {Object.keys(results).length === 0 ? (
175 <p className="text-muted">No API calls made yet. Click a button above to test an endpoint.</p>
177 <div className="space-y-6">
178 {Object.entries(results).map(([name, result]) => (
179 <div key={name} className="border border-border rounded p-4">
180 <h3 className="font-semibold text-lg mb-2 capitalize flex items-center gap-2">
181 {name.startsWith('mgmt-') && <span className="text-brand">📊</span>}
182 {name.startsWith('pos-') && <span className="text-green-500">🛒</span>}
183 {name.replace(/^(mgmt|pos)-/, '').replace(/-/g, ' ')}
185 <pre className="bg-surface-2 p-3 rounded text-sm overflow-x-auto max-h-64 overflow-y-auto">
186 {JSON.stringify(result, null, 2)}
194 {/* Information Section */}
195 <div className="mt-8 grid grid-cols-1 md:grid-cols-2 gap-8">
197 {/* Management API Info */}
198 <div className="bg-info/10 p-6 rounded-lg">
199 <h3 className="text-xl font-semibold mb-4 text-info">Management API</h3>
200 <div className="space-y-2 text-sm">
201 <p><strong>Purpose:</strong> Store management, reporting, admin functions</p>
202 <p><strong>Base URL:</strong> https://iig.cwanz.online/GNAP/j</p>
203 <p><strong>Pattern:</strong> /BUCK?3=retailmax.elink.*</p>
204 <p><strong>Auth:</strong> FieldpineApiKey cookie</p>
207 <div className="mt-4">
208 <h4 className="font-semibold mb-2">Available Endpoints:</h4>
209 <ul className="text-sm text-text space-y-1">
210 <li>• retailmax.elink.locations</li>
211 <li>• retailmax.elink.stats.today</li>
212 <li>• retailmax.elink.sale.totals</li>
213 <li>• retailmax.elink.userinterface</li>
214 <li>• gds.server.status</li>
215 <li>• retailmax.elink.config.setting</li>
221 <div className="bg-green-50 p-6 rounded-lg">
222 <h3 className="text-xl font-semibold mb-4 text-green-700">POS API</h3>
223 <div className="space-y-2 text-sm">
224 <p><strong>Purpose:</strong> Point of sale operations, product search, transactions</p>
225 <p><strong>Base URL:</strong> https://murwillumbah.cwanz.online/openapi</p>
226 <p><strong>Pattern:</strong> Direct endpoints + BUCK for products</p>
227 <p><strong>Auth:</strong> FieldpineApiKey cookie</p>
230 <div className="mt-4">
231 <h4 className="font-semibold mb-2">Available Endpoints:</h4>
232 <ul className="text-sm text-text space-y-1">
233 <li>• /retailconfig</li>
234 <li>• BUCK?3=retailmax.elink.products.list</li>
235 <li>• /PaymentTypes</li>
236 <li>• /salelist1.htm</li>
237 <li>• /saleheader1.htm</li>
238 <li>• /paymentsingle.htm</li>