147 lines
4.5 KiB
JavaScript
147 lines
4.5 KiB
JavaScript
/**
|
|
* Main Application - Alpine.js Sales Panel App
|
|
*
|
|
* This file combines all components into the main Alpine.js application.
|
|
*/
|
|
|
|
function salesApp() {
|
|
return {
|
|
// Authentication state
|
|
isLoggedIn: false,
|
|
isLoading: true,
|
|
isLoadingProducts: false,
|
|
loadingMessage: '',
|
|
currentUser: '',
|
|
userRole: '',
|
|
loginForm: { username: '', password: '' },
|
|
|
|
// Order state
|
|
submitting: false,
|
|
orderComplete: false,
|
|
lastOrder: { id: '', name: '', total: '' },
|
|
|
|
// Include state from components
|
|
...CartComponent.getInitialState(),
|
|
...ProductsComponent.getInitialState(),
|
|
...FormsComponent.getInitialState(),
|
|
|
|
// Computed properties
|
|
get total() {
|
|
return CartComponent.getComputed().total.call(this);
|
|
},
|
|
|
|
get subtotal() {
|
|
return CartComponent.getComputed().subtotal.call(this);
|
|
},
|
|
|
|
get freeItemsCount() {
|
|
return CartComponent.getComputed().freeItemsCount.call(this);
|
|
},
|
|
|
|
get filteredProducts() {
|
|
return ProductsComponent.getComputed().filteredProducts.call(this);
|
|
},
|
|
|
|
get filteredExtraProducts() {
|
|
return ProductsComponent.getComputed().filteredExtraProducts.call(this);
|
|
},
|
|
|
|
/**
|
|
* Initialize the application
|
|
* Runs automatically on page load
|
|
*/
|
|
async init() {
|
|
this.isLoading = true;
|
|
try {
|
|
const data = await ApiService.checkSession();
|
|
if (data.authenticated) {
|
|
this.currentUser = data.user || localStorage.getItem('telvero_user') || 'Agent';
|
|
this.userRole = data.role || localStorage.getItem('telvero_role') || 'agent';
|
|
await this.loadProducts();
|
|
this.isLoggedIn = true;
|
|
}
|
|
} catch (e) {
|
|
console.error("Session check failed");
|
|
} finally {
|
|
this.isLoading = false;
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Handle user login
|
|
*/
|
|
async doLogin() {
|
|
const result = await ApiService.login(this.loginForm.username, this.loginForm.password);
|
|
if (result.ok) {
|
|
this.currentUser = result.data.user;
|
|
this.userRole = result.data.role || 'agent';
|
|
localStorage.setItem('telvero_user', result.data.user);
|
|
localStorage.setItem('telvero_role', result.data.role || 'agent');
|
|
|
|
// Show loading indicator while fetching products
|
|
this.isLoadingProducts = true;
|
|
this.loadingMessage = 'Productgegevens ophalen...';
|
|
await this.loadProducts();
|
|
this.isLoadingProducts = false;
|
|
this.loadingMessage = '';
|
|
|
|
this.isLoggedIn = true;
|
|
} else {
|
|
alert("Login mislukt");
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Handle user logout
|
|
*/
|
|
async doLogout() {
|
|
await ApiService.logout();
|
|
localStorage.removeItem('telvero_user');
|
|
localStorage.removeItem('telvero_role');
|
|
location.reload();
|
|
},
|
|
|
|
/**
|
|
* Submit order to API
|
|
*/
|
|
async submitOrder() {
|
|
this.submitting = true;
|
|
|
|
try {
|
|
const payload = this.buildOrderPayload();
|
|
const result = await ApiService.createOrder(payload);
|
|
|
|
if (result.success) {
|
|
this.lastOrder = {
|
|
id: result.order_id,
|
|
name: this.form.initials + ' ' + this.form.lastname,
|
|
total: result.total
|
|
};
|
|
this.orderComplete = true;
|
|
} else {
|
|
alert("Fout: " + result.error);
|
|
}
|
|
} catch (e) {
|
|
alert("Systeemfout");
|
|
}
|
|
|
|
this.submitting = false;
|
|
},
|
|
|
|
/**
|
|
* Reset everything for a new order
|
|
*/
|
|
resetForNewOrder() {
|
|
this.resetCart();
|
|
this.resetProducts();
|
|
this.resetForm();
|
|
this.orderComplete = false;
|
|
},
|
|
|
|
// Include methods from components
|
|
...CartComponent.getMethods(),
|
|
...ProductsComponent.getMethods(),
|
|
...FormsComponent.getMethods()
|
|
};
|
|
}
|