document.head.insertAdjacentHTML('beforeend','')
PouchDB = require 'app/utils/pouch'
module.exports =
name: 'AdminSettings'
render: (new Function '_ctx', '_cache', renderFns['app/pages/Admin/Settings/index.pug'])()
data: ->
return {
activeTab: 'brand'
tabs: [
{ id: 'brand', name: 'Бренд' }
{ id: 'favicon', name: 'Фавикон' }
{ id: 'domains', name: 'Домены' }
{ id: 'languages', name: 'Языки' }
]
brandSettings: {
logo: ''
favicon: ''
companyName: ''
description: ''
email: ''
phone: ''
}
domains: []
currentDomain: window.location.hostname
}
methods:
getTabClass: (tab) ->
baseClass = 'admin-settings__tab'
isActive = @activeTab == tab.id
if isActive
return "#{baseClass} admin-settings__tab--active"
else
return "#{baseClass} admin-settings__tab--inactive"
loadBrandSettings: ->
PouchDB.getDocument("domain_settings:#{@currentDomain}")
.then (settings) =>
if settings
@brandSettings = { ...@brandSettings, ...settings }
.catch (error) =>
debug.log 'Настройки бренда не найдены'
onLogoUpload: (event) ->
file = event.target.files[0]
if file
# В реальном приложении здесь будет загрузка файла на сервер
# и получение пути к изображению
reader = new FileReader()
reader.onload = (e) =>
# Временное решение - используем data URL
# В продакшене нужно загружать на сервер и получать путь /d/[db]/[doc]/[filename]
@brandSettings.logo = e.target.result
reader.readAsDataURL(file)
removeLogo: ->
@brandSettings.logo = ''
onFaviconUpload: (event) ->
file = event.target.files[0]
if file
reader = new FileReader()
reader.onload = (e) =>
@brandSettings.favicon = e.target.result
reader.readAsDataURL(file)
removeFavicon: ->
@brandSettings.favicon = ''
saveBrandSettings: ->
domainDoc = {
_id: "domain_settings:#{@currentDomain}"
type: 'domain_settings'
domain: @currentDomain
...@brandSettings
updatedAt: new Date().toISOString()
}
# Проверяем существование документа
PouchDB.getDocument(domainDoc._id)
.then (existingDoc) =>
domainDoc._rev = existingDoc._rev
return PouchDB.saveToRemote(domainDoc)
.catch (error) =>
if error.status == 404
return PouchDB.saveToRemote(domainDoc)
else
throw error
.then (result) =>
@showNotification 'Настройки бренда сохранены'
# Обновляем данные в главном приложении
@$root.loadDomainSettings?()
.catch (error) =>
console.error 'Ошибка сохранения настроек бренда:', error
@showNotification 'Ошибка сохранения настроек бренда', 'error'
showNotification: (message, type = 'success') ->
@$root.showNotification?(message, type) || debug.log("#{type}: #{message}")
mounted: ->
@loadBrandSettings()