206 lines
7.6 KiB
JavaScript
206 lines
7.6 KiB
JavaScript
// static/admin/js/vendor-edit.js
|
||
|
||
// Log levels: 0 = None, 1 = Error, 2 = Warning, 3 = Info, 4 = Debug
|
||
const VENDOR_EDIT_LOG_LEVEL = 3;
|
||
|
||
const editLog = {
|
||
error: (...args) => VENDOR_EDIT_LOG_LEVEL >= 1 && console.error('❌ [VENDOR_EDIT ERROR]', ...args),
|
||
warn: (...args) => VENDOR_EDIT_LOG_LEVEL >= 2 && console.warn('⚠️ [VENDOR_EDIT WARN]', ...args),
|
||
info: (...args) => VENDOR_EDIT_LOG_LEVEL >= 3 && console.info('ℹ️ [VENDOR_EDIT INFO]', ...args),
|
||
debug: (...args) => VENDOR_EDIT_LOG_LEVEL >= 4 && console.log('🔍 [VENDOR_EDIT DEBUG]', ...args)
|
||
};
|
||
|
||
function adminVendorEdit() {
|
||
return {
|
||
// Inherit base layout functionality from init-alpine.js
|
||
...data(),
|
||
|
||
// Vendor edit page specific state
|
||
currentPage: 'vendor-edit',
|
||
vendor: null,
|
||
formData: {},
|
||
errors: {},
|
||
loadingVendor: false,
|
||
saving: false,
|
||
vendorCode: null,
|
||
|
||
// Initialize
|
||
async init() {
|
||
editLog.info('=== VENDOR EDIT PAGE INITIALIZING ===');
|
||
|
||
// Prevent multiple initializations
|
||
if (window._vendorEditInitialized) {
|
||
editLog.warn('Vendor edit page already initialized, skipping...');
|
||
return;
|
||
}
|
||
window._vendorEditInitialized = true;
|
||
|
||
// Get vendor code from URL
|
||
const path = window.location.pathname;
|
||
const match = path.match(/\/admin\/vendors\/([^\/]+)\/edit/);
|
||
|
||
if (match) {
|
||
this.vendorCode = match[1];
|
||
editLog.info('Editing vendor:', this.vendorCode);
|
||
await this.loadVendor();
|
||
} else {
|
||
editLog.error('No vendor code in URL');
|
||
Utils.showToast('Invalid vendor URL', 'error');
|
||
setTimeout(() => window.location.href = '/admin/vendors', 2000);
|
||
}
|
||
|
||
editLog.info('=== VENDOR EDIT PAGE INITIALIZATION COMPLETE ===');
|
||
},
|
||
|
||
// Load vendor data
|
||
async loadVendor() {
|
||
editLog.info('Loading vendor data...');
|
||
this.loadingVendor = true;
|
||
|
||
try {
|
||
const startTime = Date.now();
|
||
const response = await apiClient.get(`/admin/vendors/${this.vendorCode}`);
|
||
const duration = Date.now() - startTime;
|
||
|
||
this.vendor = response;
|
||
|
||
// Initialize form data
|
||
this.formData = {
|
||
name: response.name || '',
|
||
subdomain: response.subdomain || '',
|
||
description: response.description || '',
|
||
contact_email: response.contact_email || '',
|
||
contact_phone: response.contact_phone || '',
|
||
website: response.website || '',
|
||
business_address: response.business_address || '',
|
||
tax_number: response.tax_number || ''
|
||
};
|
||
|
||
editLog.info(`Vendor loaded in ${duration}ms`, {
|
||
vendor_code: this.vendor.vendor_code,
|
||
name: this.vendor.name
|
||
});
|
||
editLog.debug('Form data initialized:', this.formData);
|
||
|
||
} catch (error) {
|
||
editLog.error('Failed to load vendor:', error);
|
||
Utils.showToast('Failed to load vendor', 'error');
|
||
setTimeout(() => window.location.href = '/admin/vendors', 2000);
|
||
} finally {
|
||
this.loadingVendor = false;
|
||
}
|
||
},
|
||
|
||
// Format subdomain
|
||
formatSubdomain() {
|
||
this.formData.subdomain = this.formData.subdomain
|
||
.toLowerCase()
|
||
.replace(/[^a-z0-9-]/g, '');
|
||
editLog.debug('Subdomain formatted:', this.formData.subdomain);
|
||
},
|
||
|
||
// Submit form
|
||
async handleSubmit() {
|
||
editLog.info('=== SUBMITTING VENDOR UPDATE ===');
|
||
editLog.debug('Form data:', this.formData);
|
||
|
||
this.errors = {};
|
||
this.saving = true;
|
||
|
||
try {
|
||
const startTime = Date.now();
|
||
const response = await apiClient.put(
|
||
`/admin/vendors/${this.vendorCode}`,
|
||
this.formData
|
||
);
|
||
const duration = Date.now() - startTime;
|
||
|
||
this.vendor = response;
|
||
Utils.showToast('Vendor updated successfully', 'success');
|
||
editLog.info(`Vendor updated successfully in ${duration}ms`, response);
|
||
|
||
// Optionally redirect back to list
|
||
// setTimeout(() => window.location.href = '/admin/vendors', 1500);
|
||
|
||
} catch (error) {
|
||
editLog.error('Failed to update vendor:', error);
|
||
|
||
// Handle validation errors
|
||
if (error.details && error.details.validation_errors) {
|
||
error.details.validation_errors.forEach(err => {
|
||
const field = err.loc?.[1] || err.loc?.[0];
|
||
if (field) {
|
||
this.errors[field] = err.msg;
|
||
}
|
||
});
|
||
editLog.debug('Validation errors:', this.errors);
|
||
}
|
||
|
||
Utils.showToast(error.message || 'Failed to update vendor', 'error');
|
||
} finally {
|
||
this.saving = false;
|
||
editLog.info('=== VENDOR UPDATE COMPLETE ===');
|
||
}
|
||
},
|
||
|
||
// Toggle verification
|
||
async toggleVerification() {
|
||
const action = this.vendor.is_verified ? 'unverify' : 'verify';
|
||
editLog.info(`Toggle verification: ${action}`);
|
||
|
||
if (!confirm(`Are you sure you want to ${action} this vendor?`)) {
|
||
editLog.info('Verification toggle cancelled by user');
|
||
return;
|
||
}
|
||
|
||
this.saving = true;
|
||
try {
|
||
const response = await apiClient.put(
|
||
`/admin/vendors/${this.vendorCode}/verification`,
|
||
{ is_verified: !this.vendor.is_verified }
|
||
);
|
||
|
||
this.vendor = response;
|
||
Utils.showToast(`Vendor ${action}ed successfully`, 'success');
|
||
editLog.info(`Vendor ${action}ed successfully`);
|
||
|
||
} catch (error) {
|
||
editLog.error(`Failed to ${action} vendor:`, error);
|
||
Utils.showToast(`Failed to ${action} vendor`, 'error');
|
||
} finally {
|
||
this.saving = false;
|
||
}
|
||
},
|
||
|
||
// Toggle active status
|
||
async toggleActive() {
|
||
const action = this.vendor.is_active ? 'deactivate' : 'activate';
|
||
editLog.info(`Toggle active status: ${action}`);
|
||
|
||
if (!confirm(`Are you sure you want to ${action} this vendor?\n\nThis will affect their operations.`)) {
|
||
editLog.info('Active status toggle cancelled by user');
|
||
return;
|
||
}
|
||
|
||
this.saving = true;
|
||
try {
|
||
const response = await apiClient.put(
|
||
`/admin/vendors/${this.vendorCode}/status`,
|
||
{ is_active: !this.vendor.is_active }
|
||
);
|
||
|
||
this.vendor = response;
|
||
Utils.showToast(`Vendor ${action}d successfully`, 'success');
|
||
editLog.info(`Vendor ${action}d successfully`);
|
||
|
||
} catch (error) {
|
||
editLog.error(`Failed to ${action} vendor:`, error);
|
||
Utils.showToast(`Failed to ${action} vendor`, 'error');
|
||
} finally {
|
||
this.saving = false;
|
||
}
|
||
}
|
||
};
|
||
}
|
||
|
||
editLog.info('Vendor edit module loaded'); |