const { Log, Core, Utils, Pages } = require('@orbito/api-lib'); const ShopConfig = require('../src/models/ShopConfig'); module.exports = async function(body, params) { const configId = params.shopId; const isEdit = !!configId; let config = null; if (isEdit) { config = await ShopConfig.findOne({ _id: configId, isRemoved: { $ne: true } }); if (!config) throw new Error('Config not found'); } const res = await Core.callModule( 'core/orders', 'status.search', {}); const orderStatuses = res.results || []; return { type: 'ove', template: { type: 'card', title: isEdit ? `Edytuj konfigurację: ${config.shopName || configId}` : 'Nowa konfiguracja sklepu', description: isEdit ? 'Zaktualizuj dane połączenia ze sklepem Prestashop.' : 'Uzupełnij dane połączenia z nowym sklepem Prestashop.', content: [ { type: 'row', cols: [ { size: 6, childs: [ { type: 'input', name: 'shopName', inputType: 'text', label: 'Nazwa sklepu', value: config?.shopName || '', groups: ['config-data'] } ] }, { size: 6, childs: [ { type: 'select', name: 'versionCode', label: 'Wersja sklepu', value: config?.versionCode || '', options: [ { id: '9', label: 'PrestaShop 9' }, { id: '8', label: 'PrestaShop 8' }, { id: '1.7', label: 'PrestaShop 1.7' }, { id: '1.6', label: 'PrestaShop 1.6 lub starsze' } ], groups: ['config-data'] } ] }, { size: 12, childs: [ { type: 'input', name: 'apiUrl', inputType: 'text', label: 'API URL', placeholder: 'https://sklep.pl/api', value: config?.apiUrl || '', groups: ['config-data'] }, { type: 'input', name: 'apiKey', inputType: 'text', label: 'API Key', placeholder: 'Klucz API Prestashop', value: config?.apiKey || '', groups: ['config-data'] }, { type: 'select', name: 'orderStatus', label: 'Domyślny status zamówienia po pobraniu z Prestashop', value: config?.orderStatus || '', options: orderStatuses.map(status => ({ id: status._id, label: status.name })), groups: ['config-data'] } ] } ] } ], footer: [ { type: 'button', label: 'Wróć do listy', variant: 'secondary', outline: true, icon: 'arrow-left', onClick: { action: 'emit', event: 'generatePage', data: { module: 'core/prestashop', page: 'config' } } }, { type: 'button', label: isEdit ? 'Zapisz zmiany' : 'Utwórz konfigurację', variant: 'primary', icon: 'device-floppy', groups: ['config-data'], onClick: { action: 'callMethod', module: 'core/prestashop', method: 'updateConfig', params: isEdit ? { _id: configId } : {}, withData: ['#config-data'], then: { action: 'emit', event: 'generatePage', data: { module: 'core/prestashop', page: 'config' } } } } ] } }; }