admin and vendor backends features

This commit is contained in:
2025-10-19 16:16:13 +02:00
parent 7b8e31a198
commit cbe1ab09d1
25 changed files with 5787 additions and 1540 deletions

View File

@@ -6,342 +6,396 @@
<title>Create Vendor - Admin Portal</title>
<link rel="stylesheet" href="/static/css/shared/base.css">
<link rel="stylesheet" href="/static/css/admin/admin.css">
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
</head>
<body>
<header class="header">
<h1>Create New Vendor</h1>
<a href="/static/admin/dashboard.html" class="btn-back">← Back to Dashboard</a>
<body x-data="vendorCreation()" x-init="init()" x-cloak>
<!-- Header -->
<header class="admin-header">
<div class="header-left">
<h1>🔐 Admin Dashboard</h1>
</div>
<div class="header-right">
<span class="user-info">Welcome, <strong x-text="currentUser.username"></strong></span>
<button class="btn-logout" @click="handleLogout">Logout</button>
</div>
</header>
<div class="container">
<div class="form-card">
<h2 class="form-title">Vendor Information</h2>
<!-- Main Container -->
<div class="admin-container">
<!-- Sidebar -->
<aside class="admin-sidebar">
<nav>
<ul class="nav-menu">
<li class="nav-item">
<a href="/static/admin/dashboard.html" class="nav-link">
📊 Dashboard
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link active">
🏪 Vendors
</a>
</li>
<li class="nav-item">
<a href="/static/admin/dashboard.html#users" class="nav-link">
👥 Users
</a>
</li>
<li class="nav-item">
<a href="/static/admin/dashboard.html#imports" class="nav-link">
📦 Import Jobs
</a>
</li>
</ul>
</nav>
</aside>
<div id="alertBox" class="alert"></div>
<!-- Main Content -->
<main class="admin-content">
<!-- Form View -->
<div x-show="!showCredentials">
<div class="content-section">
<div class="section-header">
<h2 class="section-title">Create New Vendor</h2>
<a href="/static/admin/dashboard.html" class="btn btn-secondary">
← Back to Dashboard
</a>
</div>
<form id="createVendorForm">
<!-- Vendor Code -->
<div class="form-group">
<label for="vendorCode">
Vendor Code <span class="required">*</span>
</label>
<input
type="text"
id="vendorCode"
name="vendor_code"
required
placeholder="e.g., TECHSTORE"
pattern="[A-Z0-9_-]+"
maxlength="50"
>
<div class="form-help">Uppercase letters, numbers, underscores, and hyphens only</div>
<div class="error-message" id="vendorCodeError"></div>
<form @submit.prevent="handleSubmit">
<div class="form-grid">
<!-- Left Column -->
<div class="form-column">
<h3 class="form-section-title">Basic Information</h3>
<!-- Vendor Code -->
<div class="form-group">
<label for="vendorCode">
Vendor Code <span class="required">*</span>
</label>
<input
type="text"
id="vendorCode"
x-model="formData.vendor_code"
@input="formatVendorCode"
:class="{ 'error': errors.vendor_code }"
required
placeholder="e.g., TECHSTORE"
maxlength="50"
:disabled="loading"
>
<div class="form-help">Uppercase letters, numbers, underscores, and hyphens only</div>
<div x-show="errors.vendor_code"
x-text="errors.vendor_code"
class="error-message show"></div>
</div>
<!-- Vendor Name -->
<div class="form-group">
<label for="name">
Vendor Name <span class="required">*</span>
</label>
<input
type="text"
id="name"
x-model="formData.name"
:class="{ 'error': errors.name }"
required
placeholder="e.g., Tech Store Luxembourg"
maxlength="255"
:disabled="loading"
>
<div class="form-help">Display name for the vendor</div>
<div x-show="errors.name"
x-text="errors.name"
class="error-message show"></div>
</div>
<!-- Subdomain -->
<div class="form-group">
<label for="subdomain">
Subdomain <span class="required">*</span>
</label>
<input
type="text"
id="subdomain"
x-model="formData.subdomain"
@input="formatSubdomain"
:class="{ 'error': errors.subdomain }"
required
placeholder="e.g., techstore"
maxlength="100"
:disabled="loading"
>
<div class="form-help">Lowercase letters, numbers, and hyphens only</div>
<div x-show="errors.subdomain"
x-text="errors.subdomain"
class="error-message show"></div>
</div>
<!-- Description -->
<div class="form-group">
<label for="description">Description</label>
<textarea
id="description"
x-model="formData.description"
placeholder="Brief description of the vendor's business"
rows="3"
:disabled="loading"
></textarea>
<div class="form-help">Optional description of the vendor</div>
</div>
</div>
<!-- Right Column -->
<div class="form-column">
<h3 class="form-section-title">Contact & Business Information</h3>
<!-- Owner Email -->
<div class="form-group">
<label for="ownerEmail">
Owner Email <span class="required">*</span>
</label>
<input
type="email"
id="ownerEmail"
x-model="formData.owner_email"
:class="{ 'error': errors.owner_email }"
required
placeholder="owner@example.com"
:disabled="loading"
>
<div class="form-help">Login credentials will be sent to this email</div>
<div x-show="errors.owner_email"
x-text="errors.owner_email"
class="error-message show"></div>
</div>
<!-- Contact Phone -->
<div class="form-group">
<label for="contactPhone">Contact Phone</label>
<input
type="tel"
id="contactPhone"
x-model="formData.business_phone"
placeholder="+352 123 456 789"
:disabled="loading"
>
<div class="form-help">Optional contact phone number</div>
</div>
<!-- Website -->
<div class="form-group">
<label for="website">Website</label>
<input
type="url"
id="website"
x-model="formData.website"
placeholder="https://example.com"
:disabled="loading"
>
<div class="form-help">Optional website URL</div>
</div>
<!-- Business Address -->
<div class="form-group">
<label for="businessAddress">Business Address</label>
<textarea
id="businessAddress"
x-model="formData.business_address"
placeholder="Street, City, Country"
rows="3"
:disabled="loading"
></textarea>
</div>
<!-- Tax Number -->
<div class="form-group">
<label for="taxNumber">Tax Number</label>
<input
type="text"
id="taxNumber"
x-model="formData.tax_number"
placeholder="LU12345678"
:disabled="loading"
>
</div>
</div>
</div>
<div class="form-actions">
<button type="button"
class="btn btn-secondary"
@click="window.location.href='/static/admin/dashboard.html'"
:disabled="loading">
Cancel
</button>
<button type="submit"
class="btn btn-primary"
:disabled="loading">
<span x-show="!loading">Create Vendor</span>
<span x-show="loading">
<span class="loading-spinner"></span>
Creating vendor...
</span>
</button>
</div>
</form>
</div>
<!-- Vendor Name -->
<div class="form-group">
<label for="name">
Vendor Name <span class="required">*</span>
</label>
<input
type="text"
id="name"
name="name"
required
placeholder="e.g., Tech Store Luxembourg"
maxlength="255"
>
<div class="form-help">Display name for the vendor</div>
<div class="error-message" id="nameError"></div>
</div>
<!-- Subdomain -->
<div class="form-group">
<label for="subdomain">
Subdomain <span class="required">*</span>
</label>
<input
type="text"
id="subdomain"
name="subdomain"
required
placeholder="e.g., techstore"
pattern="[a-z0-9][a-z0-9-]*[a-z0-9]"
maxlength="100"
>
<div class="form-help">Lowercase letters, numbers, and hyphens only (e.g., techstore.platform.com)</div>
<div class="error-message" id="subdomainError"></div>
</div>
<!-- Description -->
<div class="form-group">
<label for="description">Description</label>
<textarea
id="description"
name="description"
placeholder="Brief description of the vendor's business"
></textarea>
<div class="form-help">Optional description of the vendor</div>
</div>
<!-- Owner Email -->
<div class="form-group">
<label for="ownerEmail">
Owner Email <span class="required">*</span>
</label>
<input
type="email"
id="ownerEmail"
name="owner_email"
required
placeholder="owner@example.com"
>
<div class="form-help">Email for the vendor owner (login credentials will be sent here)</div>
<div class="error-message" id="ownerEmailError"></div>
</div>
<!-- Contact Phone -->
<div class="form-group">
<label for="contactPhone">Contact Phone</label>
<input
type="tel"
id="contactPhone"
name="contact_phone"
placeholder="+352 123 456 789"
>
<div class="form-help">Optional contact phone number</div>
</div>
<!-- Website -->
<div class="form-group">
<label for="website">Website</label>
<input
type="url"
id="website"
name="website"
placeholder="https://example.com"
>
<div class="form-help">Optional website URL</div>
</div>
<!-- Business Address -->
<div class="form-group">
<label for="businessAddress">Business Address</label>
<textarea
id="businessAddress"
name="business_address"
placeholder="Street, City, Country"
></textarea>
</div>
<!-- Tax Number -->
<div class="form-group">
<label for="taxNumber">Tax Number</label>
<input
type="text"
id="taxNumber"
name="tax_number"
placeholder="LU12345678"
>
</div>
<div class="form-actions">
<button type="button" class="btn btn-secondary" onclick="window.history.back()">
Cancel
</button>
<button type="submit" class="btn btn-primary" id="submitButton">
Create Vendor
</button>
</div>
</form>
</div>
<!-- Success credentials display -->
<div id="credentialsDisplay" style="display: none;">
<div class="credentials-card">
<h3>✅ Vendor Created Successfully!</h3>
<div class="credential-item">
<label>Vendor Code:</label>
<span class="value" id="displayVendorCode"></span>
<div x-show="showCredentials">
<div class="content-section">
<div class="credentials-success">
<div class="success-icon"></div>
<h2>Vendor Created Successfully!</h2>
<p class="success-message">
The vendor has been created and credentials have been generated.
Please save these credentials securely.
</p>
</div>
<div class="credential-item">
<label>Subdomain:</label>
<span class="value" id="displaySubdomain"></span>
<!-- Debug info (remove in production) -->
<div x-show="credentials" style="display: none;">
<pre x-text="JSON.stringify(credentials, null, 2)"></pre>
</div>
<div class="credential-item">
<label>Owner Username:</label>
<span class="value" id="displayUsername"></span>
<div class="credentials-grid">
<div class="credential-card">
<div class="credential-label">Vendor Code</div>
<div class="credential-value-group">
<span class="credential-value" x-text="credentials?.vendor_code || 'N/A'"></span>
<button type="button"
class="btn-icon"
@click="copyToClipboard(credentials?.vendor_code, 'Vendor code')"
title="Copy to clipboard"
:disabled="!credentials?.vendor_code">
📋
</button>
</div>
</div>
<div class="credential-card">
<div class="credential-label">Vendor Name</div>
<div class="credential-value-group">
<span class="credential-value" x-text="credentials?.name || 'N/A'"></span>
<button type="button"
class="btn-icon"
@click="copyToClipboard(credentials?.name, 'Vendor name')"
title="Copy to clipboard"
:disabled="!credentials?.name">
📋
</button>
</div>
</div>
<div class="credential-card">
<div class="credential-label">Subdomain</div>
<div class="credential-value-group">
<span class="credential-value" x-text="credentials?.subdomain || 'N/A'"></span>
<button type="button"
class="btn-icon"
@click="copyToClipboard(credentials?.subdomain, 'Subdomain')"
title="Copy to clipboard"
:disabled="!credentials?.subdomain">
📋
</button>
</div>
</div>
<div class="credential-card">
<div class="credential-label">Owner Email</div>
<div class="credential-value-group">
<span class="credential-value" x-text="credentials?.owner_email || 'N/A'"></span>
<button type="button"
class="btn-icon"
@click="copyToClipboard(credentials?.owner_email, 'Email')"
title="Copy to clipboard"
:disabled="!credentials?.owner_email">
📋
</button>
</div>
</div>
<div class="credential-card">
<div class="credential-label">Owner Username</div>
<div class="credential-value-group">
<span class="credential-value" x-text="credentials?.owner_username || 'N/A'"></span>
<button type="button"
class="btn-icon"
@click="copyToClipboard(credentials?.owner_username, 'Username')"
title="Copy to clipboard"
:disabled="!credentials?.owner_username">
📋
</button>
</div>
</div>
<div class="credential-card credential-card-highlight">
<div class="credential-label">
<span>⚠️ Temporary Password</span>
</div>
<div class="credential-value-group">
<template x-if="credentials?.temporary_password && credentials.temporary_password !== 'PASSWORD_NOT_RETURNED'">
<span class="credential-value" x-text="credentials.temporary_password"></span>
</template>
<template x-if="!credentials?.temporary_password || credentials.temporary_password === 'PASSWORD_NOT_RETURNED'">
<span class="credential-value text-danger">
⚠️ Password not returned - Check server logs
</span>
</template>
<button type="button"
class="btn-icon"
@click="copyToClipboard(credentials?.temporary_password, 'Password')"
title="Copy to clipboard"
:disabled="!credentials?.temporary_password || credentials.temporary_password === 'PASSWORD_NOT_RETURNED'">
📋
</button>
</div>
<div class="credential-warning">
This password will not be shown again. Make sure to save it securely.
</div>
</div>
<div class="credential-card credential-card-full">
<div class="credential-label">Login URL</div>
<div class="credential-value-group">
<span class="credential-value credential-value-url" x-text="credentials?.login_url || 'N/A'"></span>
<button type="button"
class="btn-icon"
@click="copyToClipboard(credentials?.login_url, 'Login URL')"
title="Copy to clipboard"
:disabled="!credentials?.login_url">
📋
</button>
</div>
</div>
</div>
<div class="credential-item">
<label>Owner Email:</label>
<span class="value" id="displayEmail"></span>
<!-- Warning if password not returned -->
<div x-show="!credentials?.temporary_password || credentials.temporary_password === 'PASSWORD_NOT_RETURNED'"
class="alert alert-warning mt-3">
<strong>⚠️ Warning:</strong> The temporary password was not returned by the server.
This might be a backend configuration issue. Please check the server logs or contact the system administrator.
</div>
<div class="credential-item">
<label>Temporary Password:</label>
<span class="value" id="displayPassword"></span>
</div>
<div class="credential-item">
<label>Login URL:</label>
<span class="value" id="displayLoginUrl"></span>
</div>
<p class="warning-text">
⚠️ Important: Save these credentials! The password will not be shown again.
</p>
<div class="form-actions" style="margin-top: 20px;">
<button class="btn btn-primary" onclick="window.location.href='/static/admin/dashboard.html'">
Go to Dashboard
<div class="form-actions" style="margin-top: 24px;">
<button class="btn btn-secondary"
@click="resetForm(); showCredentials = false">
Create Another Vendor
</button>
<button class="btn btn-secondary" onclick="location.reload()">
Create Another Vendor
<button class="btn btn-primary"
@click="window.location.href='/static/admin/dashboard.html'">
← Back to Dashboard
</button>
</div>
</div>
</div>
</div>
</main>
</div>
<script>
const API_BASE_URL = '/api/v1';
// Check authentication
function checkAuth() {
const token = localStorage.getItem('admin_token');
const user = localStorage.getItem('admin_user');
if (!token || !user) {
window.location.href = '/static/admin/login.html';
return false;
}
try {
const userData = JSON.parse(user);
if (userData.role !== 'admin') {
alert('Access denied. Admin privileges required.');
window.location.href = '/static/admin/login.html';
return false;
}
return true;
} catch (e) {
window.location.href = '/static/admin/login.html';
return false;
}
}
// Show alert
function showAlert(message, type = 'error') {
const alertBox = document.getElementById('alertBox');
alertBox.textContent = message;
alertBox.className = `alert alert-${type} show`;
// Scroll to top
window.scrollTo({ top: 0, behavior: 'smooth' });
}
// Clear all errors
function clearErrors() {
document.querySelectorAll('.error-message').forEach(el => {
el.classList.remove('show');
});
document.querySelectorAll('input, textarea').forEach(el => {
el.classList.remove('error');
});
document.getElementById('alertBox').classList.remove('show');
}
// Show field error
function showFieldError(fieldName, message) {
const input = document.querySelector(`[name="${fieldName}"]`);
const errorDiv = document.getElementById(`${fieldName.replace('_', '')}Error`);
if (input) input.classList.add('error');
if (errorDiv) {
errorDiv.textContent = message;
errorDiv.classList.add('show');
}
}
// Auto-format inputs
document.getElementById('vendorCode').addEventListener('input', function(e) {
this.value = this.value.toUpperCase().replace(/[^A-Z0-9_-]/g, '');
});
document.getElementById('subdomain').addEventListener('input', function(e) {
this.value = this.value.toLowerCase().replace(/[^a-z0-9-]/g, '');
});
// Handle form submission
document.getElementById('createVendorForm').addEventListener('submit', async function(e) {
e.preventDefault();
clearErrors();
const submitButton = document.getElementById('submitButton');
submitButton.disabled = true;
submitButton.innerHTML = '<span class="loading-spinner"></span>Creating vendor...';
// Collect form data
const formData = {
vendor_code: document.getElementById('vendorCode').value.trim(),
name: document.getElementById('name').value.trim(),
subdomain: document.getElementById('subdomain').value.trim(),
description: document.getElementById('description').value.trim() || null,
owner_email: document.getElementById('ownerEmail').value.trim(),
contact_phone: document.getElementById('contactPhone').value.trim() || null,
website: document.getElementById('website').value.trim() || null,
business_address: document.getElementById('businessAddress').value.trim() || null,
tax_number: document.getElementById('taxNumber').value.trim() || null,
};
try {
const token = localStorage.getItem('admin_token');
const response = await fetch(`${API_BASE_URL}/admin/vendors`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify(formData)
});
const data = await response.json();
if (!response.ok) {
throw new Error(data.detail || 'Failed to create vendor');
}
// Display success and credentials
document.getElementById('createVendorForm').style.display = 'none';
document.getElementById('credentialsDisplay').style.display = 'block';
document.getElementById('displayVendorCode').textContent = data.vendor_code;
document.getElementById('displaySubdomain').textContent = data.subdomain;
document.getElementById('displayUsername').textContent = data.owner_username;
document.getElementById('displayEmail').textContent = data.owner_email;
document.getElementById('displayPassword').textContent = data.temporary_password;
document.getElementById('displayLoginUrl').textContent = data.login_url || `${data.subdomain}.platform.com/vendor/login`;
showAlert('Vendor created successfully!', 'success');
} catch (error) {
console.error('Error creating vendor:', error);
showAlert(error.message || 'Failed to create vendor');
submitButton.disabled = false;
submitButton.innerHTML = 'Create Vendor';
}
});
// Initialize
window.addEventListener('DOMContentLoaded', () => {
checkAuth();
});
</script>
<script src="/static/js/shared/api-client.js"></script>
<script src="/static/js/admin/vendors.js"></script>
</body>
</html>