mirror of
https://github.com/LD-Reborn/Berufsschule_HAM.git
synced 2025-12-20 06:51:55 +00:00
1278 lines
55 KiB
Plaintext
1278 lines
55 KiB
Plaintext
@using Microsoft.AspNetCore.Mvc.Localization
|
|
@using Berufsschule_HAM.Models
|
|
@model HomeIndexViewModel
|
|
@inject IViewLocalizer T
|
|
@{
|
|
ViewData["Title"] = T["Assets"];
|
|
}
|
|
|
|
|
|
<button id="openPrintModal"
|
|
class="btn btn-primary position-fixed bottom-0 start-0 m-4"
|
|
style="width: 3rem; height: 3rem;"
|
|
data-bs-toggle="modal"
|
|
data-bs-target="#printModal"
|
|
title="Open Print Page">
|
|
🖨️
|
|
</button>
|
|
|
|
<div class="container py-4">
|
|
<h2 class="mb-3">@T["Assets"]</h2>
|
|
|
|
|
|
<div class="mb-4 d-flex flex-wrap gap-2">
|
|
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#createAssetModal">
|
|
@T["Create asset"]
|
|
</button>
|
|
</div>
|
|
|
|
|
|
<div class="table-responsive">
|
|
<table class="table table-striped align-middle">
|
|
<thead>
|
|
<tr>
|
|
<th>@T["Owner"]</th>
|
|
<th>@T["Asset ID"]</th>
|
|
<th>@T["Asset Name"]</th>
|
|
<th>@T["Location"]</th>
|
|
<th class="text-center">@T["Action"]</th>
|
|
</tr>
|
|
<tr>
|
|
<th><input type="text" class="form-control form-control-sm column-filter" placeholder="@T["Owner"]" data-column="0" /></th>
|
|
<th><input type="text" class="form-control form-control-sm column-filter" placeholder="@T["Asset ID"]" data-column="1" /></th>
|
|
<th><input type="text" class="form-control form-control-sm column-filter" placeholder="@T["Asset Name"]" data-column="2" /></th>
|
|
<th><input type="text" class="form-control form-control-sm column-filter" placeholder="@T["Location"]" data-column="3" /></th>
|
|
<th class="text-center">-</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
@{
|
|
foreach (AssetsTableViewModel assetsTableViewModel in Model.AssetsTableViewModels)
|
|
{
|
|
<tr class="asset-row" data-asset-id="@assetsTableViewModel.AssetCn">
|
|
<td>@assetsTableViewModel.UserUID</td>
|
|
<td>@assetsTableViewModel.AssetCn</td>
|
|
<td>@assetsTableViewModel.AssetName</td>
|
|
<td>@assetsTableViewModel.LocationName</td>
|
|
<td>
|
|
<div class="d-flex gap-2 justify-content-center">
|
|
<button class="btn btn-sm btn-warning btn-update"
|
|
data-asset-id="@assetsTableViewModel.AssetCn"
|
|
data-bs-toggle="modal"
|
|
data-bs-target="#updateAssetModal">
|
|
@T["Update"]
|
|
</button>
|
|
<button class="btn btn-sm btn-danger btn-delete"
|
|
data-asset-id="@assetsTableViewModel.AssetCn"
|
|
data-bs-toggle="modal"
|
|
data-bs-target="#deleteModal">
|
|
@T["Delete"]
|
|
</button>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
}
|
|
}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Table filter -->
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
const filters = document.querySelectorAll('.column-filter');
|
|
const table = document.querySelector('table');
|
|
const tbody = table.querySelector('tbody');
|
|
|
|
filters.forEach(filter => {
|
|
filter.addEventListener('input', () => {
|
|
const rows = tbody.querySelectorAll('tr');
|
|
const filterValues = Array.from(filters).map(f => f.value.toLowerCase().trim());
|
|
|
|
rows.forEach(row => {
|
|
const cells = row.querySelectorAll('td');
|
|
let visible = true;
|
|
|
|
filterValues.forEach((val, i) => {
|
|
if (val && !cells[i].textContent.toLowerCase().includes(val)) {
|
|
visible = false;
|
|
}
|
|
});
|
|
|
|
row.style.display = visible ? '' : 'none';
|
|
});
|
|
});
|
|
});
|
|
});
|
|
</script>
|
|
|
|
<!-- Asset Delete Confirmation Modal -->
|
|
<div class="modal fade" id="deleteModal" tabindex="-1" aria-labelledby="deleteModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog modal-dialog-centered">
|
|
<div class="modal-content">
|
|
<div class="modal-header bg-danger text-white">
|
|
<h5 class="modal-title" id="deleteModalLabel">@T["Confirm Delete"]</h5>
|
|
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<p>@T["AssetDeleteConfirmation1"] <strong id="assetName"></strong> (ID: <span id="assetId"></span>)@T["AssetDeleteConfirmation2"]</p>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">@T["Cancel"]</button>
|
|
<form id="deleteForm" method="post" action="">
|
|
<button type="submit" class="btn btn-danger">@T["Yes, Delete"]</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<script>
|
|
function createToastContainer() {
|
|
const container = document.createElement('div');
|
|
container.id = 'toastContainer';
|
|
container.className = 'toast-container position-fixed bottom-0 end-0 p-3';
|
|
document.body.appendChild(container);
|
|
return container;
|
|
}
|
|
|
|
// Simple toast helper
|
|
function showToast(message, type) {
|
|
const toastContainer = document.getElementById('toastContainer') || createToastContainer();
|
|
const toast = document.createElement('div');
|
|
toast.className = `toast align-items-center text-white bg-${type} border-0`;
|
|
toast.role = 'alert';
|
|
toast.innerHTML = `
|
|
<div class="d-flex">
|
|
<div class="toast-body">${message}</div>
|
|
<button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast"></button>
|
|
</div>
|
|
`;
|
|
toastContainer.appendChild(toast);
|
|
const bsToast = new bootstrap.Toast(toast, { delay: 3000 });
|
|
bsToast.show();
|
|
toast.addEventListener('hidden.bs.toast', () => toast.remove());
|
|
}
|
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
const deleteModal = document.getElementById('deleteModal');
|
|
let currentButton = null; // The delete button that opened the modal
|
|
|
|
deleteModal.addEventListener('show.bs.modal', event => {
|
|
currentButton = event.relatedTarget; // Button that triggered the modal
|
|
const assetId = currentButton.getAttribute('data-asset-id');
|
|
const assetName = currentButton.getAttribute('data-asset-name');
|
|
|
|
deleteModal.querySelector('#assetId').textContent = assetId;
|
|
deleteModal.querySelector('#assetName').textContent = assetName;
|
|
|
|
// Store the delete URL for later use
|
|
deleteModal.querySelector('#deleteForm').dataset.url = `/Assets/Delete?cn=${assetId}`;
|
|
});
|
|
|
|
// Handle submit of deleteForm via fetch()
|
|
const deleteForm = document.getElementById('deleteForm');
|
|
deleteForm.addEventListener('submit', async e => {
|
|
e.preventDefault();
|
|
|
|
const url = deleteForm.dataset.url;
|
|
const assetId = deleteModal.querySelector('#assetId').textContent;
|
|
|
|
try {
|
|
const response = await fetch(url, {
|
|
method: 'DELETE',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Accept': 'application/json'
|
|
}//,
|
|
//body: JSON.stringify({ id: assetId }) // Use this for Post requests with [FromBody] parameters like in /Groups/Update
|
|
});
|
|
|
|
const result = await response.json();
|
|
|
|
if (result.success) {
|
|
// Close the modal
|
|
const modal = bootstrap.Modal.getInstance(deleteModal);
|
|
modal.hide();
|
|
|
|
// Remove the row from the table
|
|
const row = currentButton.closest('tr');
|
|
row.classList.add('table-danger');
|
|
setTimeout(() => row.remove(), 300);
|
|
|
|
showToast('@T["Asset deleted successfully"]', 'success');
|
|
} else {
|
|
showToast(`${result.reason}: ${result.exception || '@T["Unknown error"]'}`, 'danger');
|
|
}
|
|
} catch (error) {
|
|
console.error(error);
|
|
showToast('@T["Error contacting server"]', 'danger');
|
|
}
|
|
});
|
|
});
|
|
</script>
|
|
|
|
<!-- Asset Create Modal -->
|
|
<div class="modal fade" id="createAssetModal" tabindex="-1" aria-labelledby="createAssetModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog modal-lg modal-dialog-centered">
|
|
<div class="modal-content">
|
|
<div class="modal-header bg-primary text-white">
|
|
<h5 class="modal-title" id="createAssetModalLabel">@T["Create Asset"]</h5>
|
|
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
|
|
<form id="createAssetForm">
|
|
<div class="modal-body">
|
|
<div class="row g-3">
|
|
<!-- Basic Info -->
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Name"]</label>
|
|
<input type="text" class="form-control" name="Name" />
|
|
</div>
|
|
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Location"]</label>
|
|
<input type="text" class="form-control" name="Location" />
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Owner"]</label>
|
|
<input type="text" class="form-control" name="Owner" />
|
|
</div>
|
|
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Serial Number"]</label>
|
|
<input type="text" class="form-control" name="SerialNumber" />
|
|
</div>
|
|
|
|
<hr class="my-3" />
|
|
|
|
<!-- Description Section -->
|
|
<h6 class="fw-bold">@T["Description"]</h6>
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Type"]</label>
|
|
<input type="text" class="form-control" name="Description.Type" />
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Make"]</label>
|
|
<input type="text" class="form-control" name="Description.Make" />
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Model"]</label>
|
|
<input type="text" class="form-control" name="Description.Model" />
|
|
</div>
|
|
|
|
|
|
<!-- Attributes Section -->
|
|
<div class="col-12 mt-3">
|
|
<div class="d-flex justify-content-between align-items-center mb-2">
|
|
<h6 class="fw-bold mb-0">@T["Attributes"]</h6>
|
|
</div>
|
|
<div id="attributesContainer" class="d-flex flex-column gap-2">
|
|
<!-- Dynamic attribute rows will appear here -->
|
|
</div>
|
|
<button type="button" class="btn btn-sm btn-success mt-3" id="addAttributeBtn">
|
|
@T["Add Attribute"]
|
|
</button>
|
|
</div>
|
|
|
|
<hr class="my-3" />
|
|
|
|
<!-- Purchase Info -->
|
|
<h6 class="fw-bold">@T["Purchase Information"]</h6>
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Purchase Date"]</label>
|
|
<input type="date" class="form-control" name="Description.Purchase.PurchaseDate" />
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Purchase Value"]</label>
|
|
<input type="text" class="form-control" name="Description.Purchase.PurchaseValue" />
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Purchased At"]</label>
|
|
<input type="text" class="form-control" name="Description.Purchase.PurchaseAt" />
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Purchased By"]</label>
|
|
<input type="text" class="form-control" name="Description.Purchase.PurchaseBy" />
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">@T["Cancel"]</button>
|
|
<button type="submit" class="btn btn-primary">@T["Create"]</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
const attributesContainer = document.getElementById('attributesContainer');
|
|
const addAttributeBtn = document.getElementById('addAttributeBtn');
|
|
|
|
addAttributeBtn.addEventListener('click', () => {
|
|
const row = document.createElement('div');
|
|
row.className = 'd-flex gap-2 align-items-center attribute-row';
|
|
row.innerHTML = `
|
|
<input type="text" class="form-control" placeholder="@T["Attribute name"]" data-attr-name />
|
|
<input type="text" class="form-control" placeholder="@T["Attribute value"]" data-attr-value />
|
|
<button type="button" class="btn btn-danger btn-sm btn-remove-attribute">@T["Remove"]</button>
|
|
`;
|
|
attributesContainer.appendChild(row);
|
|
});
|
|
|
|
attributesContainer.addEventListener('click', (e) => {
|
|
if (e.target.classList.contains('btn-remove-attribute')) {
|
|
e.target.closest('.attribute-row').remove();
|
|
}
|
|
});
|
|
|
|
const createForm = document.getElementById('createAssetForm');
|
|
const originalHandler = createForm.onsubmit;
|
|
createForm.addEventListener('submit', (e) => {
|
|
const formData = new FormData(createForm);
|
|
const jsonData = {};
|
|
|
|
for (const [key, value] of formData.entries()) {
|
|
if (!value) continue;
|
|
const keys = key.split('.');
|
|
let target = jsonData;
|
|
for (let i = 0; i < keys.length - 1; i++) {
|
|
target[keys[i]] = target[keys[i]] || {};
|
|
target = target[keys[i]];
|
|
}
|
|
target[keys[keys.length - 1]] = value;
|
|
}
|
|
|
|
const attributes = {};
|
|
document.querySelectorAll('#attributesContainer .attribute-row').forEach(row => {
|
|
const name = row.querySelector('[data-attr-name]').value.trim();
|
|
const value = row.querySelector('[data-attr-value]').value.trim();
|
|
if (name) attributes[name] = value;
|
|
});
|
|
|
|
if (Object.keys(attributes).length > 0) {
|
|
jsonData.Description = jsonData.Description || {};
|
|
jsonData.Description.Attributes = attributes;
|
|
}
|
|
|
|
e.preventDefault();
|
|
|
|
fetch('/Assets/Create', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Accept': 'application/json'
|
|
},
|
|
body: JSON.stringify(jsonData)
|
|
})
|
|
.then(res => res.json())
|
|
.then(result => {
|
|
const createModalEl = document.getElementById('createAssetModal');
|
|
if (result.success) {
|
|
bootstrap.Modal.getInstance(createModalEl).hide();
|
|
createForm.reset();
|
|
attributesContainer.innerHTML = '';
|
|
const tableBody = document.querySelector('table tbody');
|
|
if (tableBody) {
|
|
const newRow = document.createElement('tr');
|
|
newRow.innerHTML = `
|
|
<td>${jsonData.Owner || ''}</td>
|
|
<td>${result.assetId || ''}</td>
|
|
<td>${jsonData.Name || ''}</td>
|
|
<td>${jsonData.Location || ''}</td>
|
|
<td>
|
|
<div class="d-flex gap-2">
|
|
<button class="btn btn-sm btn-warning btn-update"
|
|
data-asset-id="${result.assetId}"
|
|
data-bs-toggle="modal"
|
|
data-bs-target="#updateAssetModal">
|
|
@T["Update"]
|
|
</button>
|
|
<button class="btn btn-sm btn-danger btn-delete"
|
|
data-asset-id="${result.assetId}"
|
|
data-asset-name="${jsonData.Name || ''}"
|
|
data-bs-toggle="modal"
|
|
data-bs-target="#deleteModal">
|
|
@T["Delete"]
|
|
</button>
|
|
</div>
|
|
</td>
|
|
`;
|
|
|
|
newRow.classList.add('table-success');
|
|
setTimeout(() => {
|
|
newRow.classList.toggle('table-success');
|
|
}, 500);
|
|
tableBody.append(newRow);
|
|
}
|
|
showToast('@T["Asset created successfully"]', 'success');
|
|
} else {
|
|
showToast(`${result.reason || '@T["Error creating asset"]'}`, 'danger');
|
|
}
|
|
})
|
|
.catch(err => {
|
|
console.error(err);
|
|
showToast('@T["Error contacting server"]', 'danger');
|
|
});
|
|
});
|
|
});
|
|
</script>
|
|
|
|
|
|
<div class="modal fade" id="updateAssetModal" tabindex="-1" aria-labelledby="updateAssetModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog modal-lg modal-dialog-centered">
|
|
<div class="modal-content">
|
|
<div class="modal-header bg-warning text-dark">
|
|
<h5 class="modal-title" id="updateAssetModalLabel">@T["Update Asset"]</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
|
|
<form id="updateAssetForm">
|
|
<div class="modal-body">
|
|
<div class="row g-3">
|
|
<!-- Same fields as in Create -->
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Name"]</label>
|
|
<input type="text" class="form-control" name="Name" />
|
|
</div>
|
|
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Location"]</label>
|
|
<input type="text" class="form-control" name="Location" />
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Owner"]</label>
|
|
<input type="text" class="form-control" name="Owner" />
|
|
</div>
|
|
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Serial Number"]</label>
|
|
<input type="text" class="form-control" name="SerialNumber" />
|
|
</div>
|
|
|
|
<hr class="my-3" />
|
|
|
|
<h6 class="fw-bold">@T["Description"]</h6>
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Type"]</label>
|
|
<input type="text" class="form-control" name="Description.Type" />
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Make"]</label>
|
|
<input type="text" class="form-control" name="Description.Make" />
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Model"]</label>
|
|
<input type="text" class="form-control" name="Description.Model" />
|
|
</div>
|
|
|
|
<div class="col-12 mt-3">
|
|
<div class="d-flex justify-content-between align-items-center mb-2">
|
|
<h6 class="fw-bold mb-0">@T["Attributes"]</h6>
|
|
</div>
|
|
<div id="updateAttributesContainer" class="d-flex flex-column gap-2"></div>
|
|
<button type="button" class="btn btn-sm btn-success mt-3" id="updateAddAttributeBtn">
|
|
@T["Add Attribute"]
|
|
</button>
|
|
</div>
|
|
|
|
<hr class="my-3" />
|
|
|
|
<h6 class="fw-bold">@T["Purchase Information"]</h6>
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Purchase Date"]</label>
|
|
<input type="date" class="form-control" name="Description.Purchase.PurchaseDate" />
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Purchase Value"]</label>
|
|
<input type="text" class="form-control" name="Description.Purchase.PurchaseValue" />
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Purchased At"]</label>
|
|
<input type="text" class="form-control" name="Description.Purchase.PurchaseAt" />
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Purchased By"]</label>
|
|
<input type="text" class="form-control" name="Description.Purchase.PurchaseBy" />
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">@T["Cancel"]</button>
|
|
<button type="submit" class="btn btn-warning">@T["Save Changes"]</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
const updateButtons = document.querySelectorAll('.btn-update');
|
|
const updateModal = document.getElementById('updateAssetModal');
|
|
const updateForm = document.getElementById('updateAssetForm');
|
|
const updateAttributesContainer = document.getElementById('updateAttributesContainer');
|
|
const addAttrBtn = document.getElementById('updateAddAttributeBtn');
|
|
let assetId = null;
|
|
|
|
addAttrBtn.addEventListener('click', () => {
|
|
const row = document.createElement('div');
|
|
row.className = 'd-flex gap-2 align-items-center attribute-row';
|
|
row.innerHTML = `
|
|
<input type="text" class="form-control" placeholder="@T["Attribute name"]" data-attr-name />
|
|
<input type="text" class="form-control" placeholder="@T["Attribute value"]" data-attr-value />
|
|
<button type="button" class="btn btn-danger btn-sm btn-remove-attribute">@T["Remove"]</button>
|
|
`;
|
|
updateAttributesContainer.appendChild(row);
|
|
});
|
|
|
|
updateAttributesContainer.addEventListener('click', e => {
|
|
if (e.target.classList.contains('btn-remove-attribute')) {
|
|
e.target.closest('.attribute-row').remove();
|
|
}
|
|
});
|
|
|
|
updateModal.addEventListener('show.bs.modal', async event => {
|
|
const button = event.relatedTarget;
|
|
assetId = button.getAttribute('data-asset-id');
|
|
|
|
updateAttributesContainer.innerHTML = '';
|
|
updateForm.reset();
|
|
|
|
try {
|
|
const response = await fetch(`/Assets/Get?cn=${assetId}`);
|
|
const responseJson = await response.json();
|
|
const asset = responseJson.assetsModel;
|
|
|
|
for (const [key, value] of Object.entries(asset)) {
|
|
const input = updateForm.querySelector(`[name="${key}"]`);
|
|
if (input) input.value = value;
|
|
}
|
|
|
|
// Handle nested description fields
|
|
if (asset.Description) {
|
|
for (const [descKey, descVal] of Object.entries(asset.Description)) {
|
|
const field = updateForm.querySelector(`[name="Description.${descKey}"]`);
|
|
if (field && typeof descVal === 'string') field.value = descVal;
|
|
}
|
|
|
|
// Attributes
|
|
if (asset.Description.Attributes) {
|
|
for (const [attrName, attrValue] of Object.entries(asset.Description.Attributes)) {
|
|
const row = document.createElement('div');
|
|
row.className = 'd-flex gap-2 align-items-center attribute-row';
|
|
row.innerHTML = `
|
|
<input type="text" class="form-control" value="${attrName}" data-attr-name />
|
|
<input type="text" class="form-control" value="${attrValue}" data-attr-value />
|
|
<button type="button" class="btn btn-danger btn-sm btn-remove-attribute">@T["Remove"]</button>
|
|
`;
|
|
updateAttributesContainer.appendChild(row);
|
|
}
|
|
}
|
|
|
|
// Purchase info
|
|
if (asset.Description.Purchase) {
|
|
for (const [pKey, pValue] of Object.entries(asset.Description.Purchase)) {
|
|
const field = updateForm.querySelector(`[name="Description.Purchase.${pKey}"]`);
|
|
if (field) field.value = pValue;
|
|
}
|
|
}
|
|
}
|
|
} catch (err) {
|
|
console.error(err);
|
|
showToast('@T["Error loading asset data"]', 'danger');
|
|
}
|
|
});
|
|
|
|
updateForm.addEventListener('submit', async e => {
|
|
e.preventDefault();
|
|
|
|
const formData = new FormData(updateForm);
|
|
const jsonData = {"Cn": assetId};
|
|
for (const [key, value] of formData.entries()) {
|
|
if (!value) continue;
|
|
const keys = key.split('.');
|
|
let target = jsonData;
|
|
for (let i = 0; i < keys.length - 1; i++) {
|
|
target[keys[i]] = target[keys[i]] || {};
|
|
target = target[keys[i]];
|
|
}
|
|
target[keys[keys.length - 1]] = value;
|
|
}
|
|
|
|
const attributes = {};
|
|
document.querySelectorAll('#updateAttributesContainer .attribute-row').forEach(row => {
|
|
const name = row.querySelector('[data-attr-name]').value.trim();
|
|
const value = row.querySelector('[data-attr-value]').value.trim();
|
|
if (name) attributes[name] = value;
|
|
});
|
|
|
|
if (Object.keys(attributes).length > 0) {
|
|
jsonData.Description = jsonData.Description || {};
|
|
jsonData.Description.Attributes = attributes;
|
|
}
|
|
|
|
try {
|
|
const response = await fetch('/Assets/Update', {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' },
|
|
body: JSON.stringify(jsonData)
|
|
});
|
|
const result = await response.json();
|
|
|
|
if (result.success) {
|
|
bootstrap.Modal.getInstance(updateModal).hide();
|
|
showToast('@T["Asset updated successfully"]', 'success');
|
|
|
|
// Optionally refresh the row
|
|
const row = [...document.querySelectorAll('tr')]
|
|
.find(r => r.querySelector(`[data-asset-id="${jsonData.Cn}"]`));
|
|
if (row) {
|
|
row.children[0].textContent = jsonData.Owner || '';
|
|
row.children[1].textContent = jsonData.Cn || '';
|
|
row.children[2].textContent = jsonData.Name || '';
|
|
row.children[3].textContent = jsonData.Location || '';
|
|
}
|
|
} else {
|
|
showToast(result.reason || '@T["Error updating asset"]', 'danger');
|
|
}
|
|
} catch (err) {
|
|
console.error(err);
|
|
showToast('@T["Error contacting server"]', 'danger');
|
|
}
|
|
});
|
|
});
|
|
</script>
|
|
|
|
<!-- Detail view -->
|
|
<div class="modal fade" id="viewAssetModal" tabindex="-1" aria-labelledby="viewAssetModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog modal-lg modal-dialog-centered">
|
|
<div class="modal-content">
|
|
<div class="modal-header bg-info text-white">
|
|
<h5 class="modal-title" id="viewAssetModalLabel">@T["Asset Details"]</h5>
|
|
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
|
|
<div class="modal-body">
|
|
<div id="viewAssetContent">
|
|
<p class="text-center text-muted">@T["Loading..."]</p>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">@T["Close"]</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<style>
|
|
.asset-row > td:not(:last-child) {
|
|
cursor: pointer;
|
|
}
|
|
.asset-row > td {
|
|
transition: 0.1s ease;
|
|
}
|
|
.asset-row:has(td:not(:last-child):hover) > td {
|
|
background-color: #17a2b8;
|
|
}
|
|
</style>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/jsbarcode@3.11.6/dist/JsBarcode.all.min.js" defer></script>
|
|
|
|
<script>
|
|
function downloadBarcode(svgId, filename = "barcode.png") {
|
|
const svg = document.getElementById(svgId);
|
|
const serializer = new XMLSerializer();
|
|
const svgString = serializer.serializeToString(svg);
|
|
|
|
const canvas = document.createElement("canvas");
|
|
const ctx = canvas.getContext("2d");
|
|
|
|
// Optionally set canvas size
|
|
const bbox = svg.getBBox();
|
|
canvas.width = bbox.width + 20; // padding
|
|
canvas.height = bbox.height + 20;
|
|
|
|
const img = new Image();
|
|
const svgBlob = new Blob([svgString], { type: "image/svg+xml;charset=utf-8" });
|
|
const url = URL.createObjectURL(svgBlob);
|
|
|
|
img.onload = () => {
|
|
ctx.drawImage(img, 0, 0);
|
|
URL.revokeObjectURL(url);
|
|
|
|
const pngUrl = canvas.toDataURL("image/png");
|
|
const a = document.createElement("a");
|
|
a.href = pngUrl;
|
|
a.download = filename;
|
|
a.click();
|
|
};
|
|
|
|
img.src = url;
|
|
}
|
|
|
|
function printBarcode(svgId) {
|
|
const svg = document.getElementById(svgId);
|
|
if (!svg) return;
|
|
|
|
// Serialize the SVG
|
|
const serializer = new XMLSerializer();
|
|
const svgString = serializer.serializeToString(svg);
|
|
|
|
// Open a new window for printing
|
|
const printWindow = window.open('', '_blank');
|
|
printWindow.document.write(`
|
|
<html>
|
|
<head>
|
|
<title>Print Barcode</title>
|
|
<style>
|
|
body { text-align: center; margin: 0; padding: 20px; }
|
|
svg { width: 300px; height: auto; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
${svgString}
|
|
</body>
|
|
</html>
|
|
`);
|
|
printWindow.document.close();
|
|
printWindow.focus();
|
|
printWindow.print();
|
|
printWindow.close();
|
|
}
|
|
|
|
function idToEAN13(id) {
|
|
const padded = id.toString().padStart(12, "0"); // 12 digits
|
|
return padded;
|
|
}
|
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
const rows = document.querySelectorAll('.asset-row');
|
|
const viewModal = document.getElementById('viewAssetModal');
|
|
const viewContent = document.getElementById('viewAssetContent');
|
|
|
|
rows.forEach(row => {
|
|
row.addEventListener('click', async (e) => {
|
|
// Avoid clicks on buttons inside the row
|
|
if (e.target.closest('button')) return;
|
|
|
|
const assetId = row.getAttribute('data-asset-id');
|
|
viewContent.innerHTML = '<p class="text-center text-muted">@T["Loading..."]</p>';
|
|
|
|
const modal = new bootstrap.Modal(viewModal);
|
|
modal.show();
|
|
|
|
try {
|
|
const response = await fetch(`/Assets/Get?cn=${assetId}`);
|
|
const json = await response.json();
|
|
const asset = json.assetsModel;
|
|
|
|
if (!asset) {
|
|
viewContent.innerHTML = `<p class="text-danger text-center">@T["Asset not found."]</p>`;
|
|
return;
|
|
}
|
|
|
|
const html = `
|
|
<div class="row g-3">
|
|
<h6 class="fw-bold">@T["Barcode"]</h6>
|
|
<div class="col-md-6">
|
|
<svg id="ean13" class="form-control" name="Barcode" />
|
|
</div>
|
|
<div class="col-md-6">
|
|
<button id="downloadBtn" class="form-control my-2 btn btn-primary">@T["Download Barcode"]</button>
|
|
<button id="printBtn" class="form-control my-2 btn btn-primary">@T["Add Barcode to print batch"]</button>
|
|
</div>
|
|
</div>
|
|
<div class="row g-3">
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Name"]</label>
|
|
<input type="text" class="form-control" name="Name" value="${asset.Name || ''}" disabled />
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Location"]</label>
|
|
<input type="text" class="form-control" name="Location" value="${asset.Location || ''}" disabled />
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Owner"]</label>
|
|
<input type="text" class="form-control" name="Owner" value="${asset.Owner || ''}" disabled />
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Serial Number"]</label>
|
|
<input type="text" class="form-control" name="SerialNumber" value="${asset.SerialNumber || ''}" disabled />
|
|
</div>
|
|
</div>
|
|
<hr class="my-3" />
|
|
<div class="row g-3">
|
|
<h6 class="fw-bold">@T["Description"]</h6>
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Type"]</label>
|
|
<input type="text" class="form-control" name="Description.Type" value="${asset.Description?.Type || ''}" disabled />
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Make"]</label>
|
|
<input type="text" class="form-control" name="Description.Make" value="${asset.Description?.Make || ''}" disabled />
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Model"]</label>
|
|
<input type="text" class="form-control" name="Description.Model" value="${asset.Description?.Model || ''}" disabled />
|
|
</div>
|
|
</div>
|
|
${asset.Description?.Attributes ? `
|
|
<hr class="my-3" />
|
|
<div class="row g-3">
|
|
<h6 class="fw-bold">@T["Attributes"]</h6>
|
|
${Object.entries(asset.Description.Attributes)
|
|
.map(([k,v]) => `
|
|
<div class="d-flex gap-2 align-items-center attribute-row">
|
|
<input type="text" class="form-control w-50" placeholder="@T["Attribute name"]" data-attr-name disabled value="${k}" />:
|
|
<input type="text" class="form-control" placeholder="@T["Attribute value"]" data-attr-value disabled value="${v}" />
|
|
</div>`)
|
|
.join('')}
|
|
</div>` : ''}
|
|
|
|
${asset.Description?.Purchase ? `
|
|
<hr class="my-3" />
|
|
<div class="row g-3">
|
|
<h6 class="fw-bold">@T["Purchase Information"]</h6>
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Purchase Date"]</label>
|
|
<input type="date" class="form-control" name="Description.Purchase.PurchaseDate" value="${asset.Description.Purchase.PurchaseDate || ''}" disabled />
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Purchase Value"]</label>
|
|
<input type="text" class="form-control" name="Description.Purchase.PurchaseValue" value="${asset.Description.Purchase.PurchaseValue || ''}" disabled />
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Purchased At"]</label>
|
|
<input type="text" class="form-control" name="Description.Purchase.PurchaseAt" value="${asset.Description.Purchase.PurchaseAt || ''}" disabled />
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label">@T["Purchased By"]</label>
|
|
<input type="text" class="form-control" name="Description.Purchase.PurchaseBy" value="${asset.Description.Purchase.PurchaseBy || ''}" disabled />
|
|
</div>
|
|
</div>` : ''}
|
|
</div>`;
|
|
viewContent.innerHTML = html;
|
|
JsBarcode("#ean13", idToEAN13(asset.Cn), {
|
|
format: "EAN13",
|
|
lineColor: "#000",
|
|
width: 2,
|
|
height: 80,
|
|
displayValue: true
|
|
});
|
|
document.getElementById("downloadBtn").addEventListener("click", () => {
|
|
downloadBarcode("ean13", idToEAN13(asset.Cn));
|
|
});
|
|
document.getElementById("printBtn").addEventListener("click", () => {
|
|
addAssetIdToBatch(asset.Cn);
|
|
showToast("@T["Successfully added barcode to print batch"]", "success");
|
|
bootstrap.Modal.getInstance('#viewAssetModal').hide();
|
|
});
|
|
} catch (err) {
|
|
console.error(err);
|
|
viewContent.innerHTML = `<p class="text-danger text-center">@T["Error loading asset details"]</p>`;
|
|
}
|
|
});
|
|
});
|
|
});
|
|
</script>
|
|
|
|
<!-- Print Modal -->
|
|
<div class="modal fade" id="printModal" tabindex="-1" aria-labelledby="printModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog modal-lg modal-dialog-centered">
|
|
<div class="modal-content">
|
|
<div class="modal-header bg-primary text-white">
|
|
<h5 class="modal-title" id="printModalLabel">Print Page</h5>
|
|
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
const printModal = document.getElementById('printModal');
|
|
printModal.addEventListener('show.bs.modal', async () => {
|
|
var assetIds = getAssetIdsFromBatch();
|
|
padNulls(assetIds, 24);
|
|
const modalBody = document.querySelector('#printModal .modal-body');
|
|
modalBody.innerHTML = '';
|
|
if (!assetIds || assetIds.length === 0) {
|
|
modalBody.innerHTML = '<p>No assets selected for printing.</p>';
|
|
return;
|
|
}
|
|
modalBody.innerHTML = `
|
|
<div class="row g-3">
|
|
<h6 class="fw-bold">@T["Print"]</h6>
|
|
<button class="btn btn-primary" onclick="printBatch();" type="button">
|
|
@T["Print batch"]
|
|
</button>
|
|
<div id="printPreviewContainer">
|
|
</div>
|
|
</div>
|
|
<div class="row g-3 mt-3">
|
|
<button class="btn btn-warning" onclick="clearBatch();" type="button" style="width: auto; margin: auto;">
|
|
@T["Clear batch"]
|
|
</button>
|
|
</div>
|
|
<hr class="my-3">
|
|
<h6 class="fw-bold">@T["Layout"]</h6>
|
|
<br/>
|
|
`;
|
|
await renderBarcodePreview("printPreviewContainer");
|
|
for (let i = 0; i < assetIds.length; i++) {
|
|
const assetId = assetIds[i];
|
|
try {
|
|
const assetCard = document.createElement('div');
|
|
assetCard.className = 'card mb-3';
|
|
assetCard.setAttribute("data-card-index", i);
|
|
|
|
if (assetId) {
|
|
const response = await fetch(`/Assets/Get?cn=${assetId}`);
|
|
const json = await response.json();
|
|
const asset = json.assetsModel;
|
|
assetCard.innerHTML = `
|
|
<div class="card-body" data-cn="${asset.Cn}">
|
|
<h6 class="card-title"><strong>Asset ${i + 1}:</strong> ${asset.Name}</h6>
|
|
<div class="row">
|
|
<div class="col-md-5">
|
|
<p><strong>Asset ID:</strong> ${asset.Cn}</p>
|
|
<p><strong>Owner:</strong> ${asset.Owner}</p>
|
|
<p><strong>Type:</strong> ${asset.Description.Type}</p>
|
|
</div>
|
|
<div class="col-md-5">
|
|
<p><strong>Make:</strong> ${asset.Description.Make}</p>
|
|
<p><strong>Model:</strong> ${asset.Description.Model}</p>
|
|
<p><strong>Serial:</strong> ${asset.SerialNumber}</p>
|
|
</div>
|
|
<div id="printPreviewBatchButtons${i}" class="col-md-1 justify-content-end" style="margin-left: auto; width: auto;">
|
|
</div>
|
|
</div>
|
|
</div>
|
|
`;
|
|
} else {
|
|
assetCard.innerHTML = `
|
|
<div class="card-body">
|
|
<h6 class="card-title"><strong>Asset ${i}:</strong> @T["Empty"]</h6>
|
|
<div class="row">
|
|
<div id="printPreviewBatchButtons${i}" class="col-md-1 justify-content-end" style="margin-left: auto; width: auto;">
|
|
</div>
|
|
</div>
|
|
</div>
|
|
`;
|
|
}
|
|
modalBody.appendChild(assetCard);
|
|
const container = document.getElementById(`printPreviewBatchButtons${i}`);
|
|
container.innerHTML = '';
|
|
let upButton = document.createElement("button");
|
|
upButton.setAttribute('data-action', 'up');
|
|
upButton.setAttribute('data-batchId', i);
|
|
upButton.className = 'btn btn-sm btn-primary mb-1';
|
|
upButton.textContent = '↑';
|
|
let downButton = document.createElement('button');
|
|
downButton.setAttribute('data-action', 'down');
|
|
downButton.setAttribute('data-batchId', i);
|
|
downButton.className = 'btn btn-sm btn-primary';
|
|
downButton.textContent = '↓';
|
|
container.appendChild(upButton);
|
|
container.appendChild(document.createElement('br'));
|
|
container.appendChild(downButton);
|
|
upButton.addEventListener('click', function() {
|
|
moveCardInBatch(upButton.parentElement.parentElement.parentElement.parentElement.attributes["data-card-index"].value, "up");
|
|
});
|
|
|
|
downButton.addEventListener('click', function() {
|
|
moveCardInBatch(downButton.parentElement.parentElement.parentElement.parentElement.attributes["data-card-index"].value, "down");
|
|
});
|
|
} catch (error) {
|
|
console.error(`Error fetching asset ${assetId}:`, error);
|
|
const errorDiv = document.createElement('div');
|
|
errorDiv.className = 'alert alert-danger';
|
|
errorDiv.textContent = `Error loading asset ${assetId}`;
|
|
modalBody.appendChild(errorDiv);
|
|
}
|
|
}
|
|
});
|
|
|
|
async function renderBarcodePreview(containerId = "printPreviewContainer") {
|
|
const container = document.getElementById(containerId);
|
|
if (!container) return console.error(`Container #${containerId} not found.`);
|
|
|
|
container.innerHTML = `<p class="text-muted text-center">Loading preview...</p>`;
|
|
|
|
let batch = JSON.parse(localStorage.getItem("printBatch")) || [];
|
|
padNulls(batch, 24);
|
|
|
|
container.innerHTML = "";
|
|
|
|
// Create the grid
|
|
const grid = document.createElement("div");
|
|
grid.className = "barcode-grid d-grid gap-3";
|
|
grid.style.gridTemplateColumns = "repeat(3, 1fr)";
|
|
grid.style.gridTemplateRows = "repeat(8, auto)";
|
|
grid.style.textAlign = "center";
|
|
|
|
container.appendChild(grid);
|
|
|
|
for (let i = 0; i < batch.length; i++) {
|
|
const cell = document.createElement("div");
|
|
cell.className = "barcode-cell border rounded p-1 d-flex flex-column align-items-center justify-content-center";
|
|
cell.style.minHeight = "120px";
|
|
var isMobileM = window.innerWidth < 992 && window.innerWidth >= 576;
|
|
var isMobileS = window.innerWidth < 576;
|
|
|
|
const assetId = batch[i];
|
|
if (!assetId) {
|
|
cell.innerHTML = `<div class="text-muted small">Empty Slot</div>`;
|
|
grid.appendChild(cell);
|
|
continue;
|
|
}
|
|
|
|
try {
|
|
const response = await fetch(`/Assets/Get?cn=${assetId}`);
|
|
const json = await response.json();
|
|
const asset = json.assetsModel;
|
|
|
|
const label = document.createElement("div");
|
|
label.className = "small fw-bold";
|
|
label.textContent = asset?.Name || assetId;
|
|
|
|
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
|
|
svg.id = `barcodePreview_${i}`;
|
|
svg.style.width = "100%";
|
|
svg.style.height = "60px";
|
|
|
|
cell.appendChild(label);
|
|
cell.appendChild(svg);
|
|
|
|
grid.appendChild(cell);
|
|
JsBarcode(svg, idToEAN13(asset.Cn), {
|
|
format: "EAN13",
|
|
lineColor: "#000",
|
|
width: isMobileM ? 1 : (isMobileS ? 0.5 : 2),
|
|
height: isMobileM ? 20 : (isMobileS ? 20 : 40),
|
|
displayValue: true,
|
|
fontSize: 12,
|
|
});
|
|
} catch (error) {
|
|
console.error(`Error rendering asset ${assetId}:`, error);
|
|
cell.innerHTML = `<div class="text-danger small">Error loading ${assetId}</div>`;
|
|
grid.appendChild(cell);
|
|
}
|
|
}
|
|
}
|
|
|
|
function moveCardInBatch(index, direction) {
|
|
let batch = getAssetIdsFromBatch();
|
|
padNulls(batch, 24);
|
|
|
|
if (!batch || batch.length === 0) return;
|
|
|
|
const newIndex = direction === 'up' ? index - 1 : index - 0 + 1;
|
|
// Prevent out-of-bounds movement
|
|
if (newIndex < 0 || newIndex >= batch.length) return;
|
|
|
|
let cardAtIndex = document.querySelector(`[data-card-index="${index}"]`)
|
|
let cardBodyAtIndex = cardAtIndex.children[0];
|
|
cardBodyAtIndex.children[0].children[0].textContent = `Asset ${newIndex + 1}`;
|
|
let cardAtTarget = document.querySelector(`[data-card-index="${newIndex}"]`)
|
|
let cardBodyAtTarget = cardAtTarget.children[0];
|
|
cardBodyAtTarget.children[0].children[0].textContent = `Asset ${index - 0 + 1}`;
|
|
cardAtIndex.insertBefore(cardBodyAtTarget, null);
|
|
cardAtTarget.insertBefore(cardBodyAtIndex, null);
|
|
|
|
// Swap items
|
|
[batch[index], batch[newIndex]] = [batch[newIndex], batch[index]];
|
|
|
|
// Save new order
|
|
localStorage.setItem("printBatch", JSON.stringify(batch));
|
|
renderBarcodePreview();
|
|
}
|
|
|
|
|
|
function getAssetIdsFromBatch() {
|
|
let currentData = JSON.parse(localStorage.getItem("printBatch"));
|
|
if (currentData != null)
|
|
{
|
|
return currentData;
|
|
}
|
|
return [];
|
|
}
|
|
|
|
function padNulls(array, length) {
|
|
while (array.length < length)
|
|
{
|
|
array.push(null);
|
|
}
|
|
}
|
|
|
|
function addAssetIdToBatch(assetId) {
|
|
let currentData = JSON.parse(localStorage.getItem("printBatch"));
|
|
if (!Array.isArray(currentData)) {
|
|
currentData = [];
|
|
}
|
|
|
|
// Find first null index
|
|
const nullIndex = currentData.indexOf(null);
|
|
|
|
if (nullIndex !== -1) {
|
|
// Replace first null with assetId
|
|
currentData[nullIndex] = assetId;
|
|
} else {
|
|
// Append if no nulls found
|
|
currentData.push(assetId);
|
|
}
|
|
|
|
localStorage.setItem("printBatch", JSON.stringify(currentData));
|
|
}
|
|
|
|
|
|
function removeAssetIdFromBatch(uid) {
|
|
let content = JSON.parse(localStorage.getItem("printBatch"));
|
|
const index = content.indexOf(uid);
|
|
if (index > -1) {
|
|
content.splice(index, 1);
|
|
localStorage.setItem("printBatch", JSON.stringify(content));
|
|
}
|
|
}
|
|
|
|
|
|
async function printBatch() {
|
|
const batch = JSON.parse(localStorage.getItem("printBatch")) || [];
|
|
padNulls(batch, 24);
|
|
|
|
if (!batch.length || batch.every(x => x === null)) {
|
|
alert("No assets in print batch.");
|
|
return;
|
|
}
|
|
|
|
// Open a new tab for print preview
|
|
const printWindow = window.open("", "_blank");
|
|
printWindow.document.write(`
|
|
<html>
|
|
<head>
|
|
<title>Print Preview - Barcodes</title>
|
|
<style>
|
|
@@page {
|
|
size: A4;
|
|
margin-top: 0.45cm;
|
|
margin-bottom: 0.45cm;
|
|
margin-left: 0;
|
|
margin-right: 0;
|
|
}
|
|
body {
|
|
font-family: Arial, sans-serif;
|
|
text-align: center;
|
|
margin-top: 0.45cm;
|
|
margin-bottom: 0.45cm;
|
|
height: 28.8cm;
|
|
width: 21cm;
|
|
}
|
|
.barcode-grid {
|
|
display: grid;
|
|
grid-template-columns: repeat(3, 1fr);
|
|
grid-template-rows: repeat(8, auto);
|
|
gap: 0mm;
|
|
justify-items: center;
|
|
align-items: center;
|
|
}
|
|
.barcode-cell {
|
|
border: 1px solid #ccc;
|
|
padding: 6mm;
|
|
width: 100%;
|
|
height: 3.6cm;
|
|
box-sizing: border-box;
|
|
}
|
|
.barcode-label {
|
|
font-weight: bold;
|
|
font-size: 10pt;
|
|
margin-bottom: 4mm;
|
|
text-align: center;
|
|
word-break: break-word;
|
|
}
|
|
svg {
|
|
width: 100%;
|
|
height: 60px;
|
|
}
|
|
@@media print {
|
|
.barcode-cell {
|
|
border: none;
|
|
}
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div id="barcodeGrid" class="barcode-grid"></div>
|
|
<script src="https://cdn.jsdelivr.net/npm/jsbarcode@3.11.6/dist/JsBarcode.all.min.js"><\/script>
|
|
</body>
|
|
</html>
|
|
`);
|
|
const grid = printWindow.document.getElementById("barcodeGrid");
|
|
|
|
for (let i = 0; i < batch.length; i++) {
|
|
const assetId = batch[i];
|
|
const cell = printWindow.document.createElement("div");
|
|
cell.className = "barcode-cell";
|
|
|
|
if (!assetId) {
|
|
cell.innerHTML = `<div class="barcode-label text-muted"></div>`;
|
|
grid.appendChild(cell);
|
|
continue;
|
|
}
|
|
|
|
try {
|
|
const response = await fetch(`/Assets/Get?cn=${assetId}`);
|
|
const json = await response.json();
|
|
const asset = json.assetsModel;
|
|
|
|
const label = printWindow.document.createElement("div");
|
|
label.className = "barcode-label";
|
|
label.textContent = "HAM"; //asset?.Name || assetId;
|
|
|
|
const svg = printWindow.document.createElementNS("http://www.w3.org/2000/svg", "svg");
|
|
svg.id = `barcode_${i}`;
|
|
|
|
cell.appendChild(label);
|
|
cell.appendChild(svg);
|
|
grid.appendChild(cell);
|
|
|
|
// Generate barcode
|
|
printWindow.JsBarcode(`#barcode_${i}`, idToEAN13(asset.Cn), {
|
|
format: "EAN13",
|
|
lineColor: "#000",
|
|
width: 2,
|
|
height: 50,
|
|
displayValue: true,
|
|
fontSize: 12,
|
|
});
|
|
svg.viewBox.baseVal.x += 10;
|
|
} catch (err) {
|
|
console.error(err);
|
|
const errDiv = printWindow.document.createElement("div");
|
|
errDiv.className = "barcode-label text-danger";
|
|
errDiv.textContent = `Error loading ${assetId}`;
|
|
cell.appendChild(errDiv);
|
|
grid.appendChild(cell);
|
|
}
|
|
}
|
|
|
|
|
|
printWindow.print();
|
|
}
|
|
|
|
function clearBatch() {
|
|
localStorage.setItem("printBatch", JSON.stringify([]));
|
|
showToast("@T["Batch cleared successfully"]", "success");
|
|
bootstrap.Modal.getInstance('#printModal').hide();
|
|
}
|
|
</script>
|