theloftstore/public/userarea/client_files.php

249 lines
9.8 KiB
PHP

<?php
include('include/headscript.php');
require_once(__DIR__ . '/class/db-functions.php');
$db = DBHandlerSelect::getInstance();
$pdo = $db->getConnection();
// === Carica elenco clienti ===
$clients = $pdo->query("SELECT idclient, client_name FROM clients ORDER BY client_name")->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Client Files Management</title>
<?php include('cssinclude.php'); ?>
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap5.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css">
<style>
.upload-box {
background: #f9f9f9;
border-radius: 8px;
padding: 20px;
margin-bottom: 20px;
}
.select2-container {
width: 100% !important;
}
</style>
</head>
<body>
<?php include('include/navbar.php'); ?>
<div class="dashboard-main-wrapper wrapper">
<?php include('include/topbar.php'); ?>
<div class="dashboard-body">
<div class="card shadow-sm">
<div class="card-body">
<h4 class="mb-4">📁 Client File Management</h4>
<!-- Client selection -->
<div class="row mb-4">
<div class="col-md-4">
<label for="idclient" class="form-label">Select Client</label>
<select id="idclient" class="form-select select2" required>
<option value="">-- Choose Client --</option>
<?php foreach ($clients as $cl): ?>
<option value="<?= htmlspecialchars($cl['idclient']) ?>">
<?= htmlspecialchars($cl['client_name']) ?>
</option>
<?php endforeach; ?>
</select>
</div>
</div>
<!-- Upload sections -->
<div class="row">
<div class="col-md-6">
<div class="upload-box">
<h6>📄 Upload Mapping File (PDF)</h6>
<form id="uploadMappingForm" enctype="multipart/form-data">
<input type="hidden" name="file_type" value="mapping">
<div class="mb-3">
<input type="file" name="file" accept=".pdf" class="form-control" required>
</div>
<button type="submit" class="btn btn-primary w-100">Upload Mapping</button>
</form>
</div>
</div>
<div class="col-md-6">
<div class="upload-box">
<h6>💰 Upload Price List (PDF)</h6>
<form id="uploadPriceForm" enctype="multipart/form-data">
<input type="hidden" name="file_type" value="pricelist">
<div class="mb-3">
<input type="file" name="file" accept=".pdf" class="form-control" required>
</div>
<button type="submit" class="btn btn-success w-100">Upload Pricelist</button>
</form>
</div>
</div>
</div>
<hr>
<h5 class="mt-4">📋 Uploaded Files</h5>
<div class="table-responsive">
<table id="clientFilesTable" class="table table-striped table-bordered w-100">
<thead>
<tr>
<th>ID</th>
<th>Client</th>
<th>File Type</th>
<th>Filename</th>
<th>Source ID</th>
<th>Uploaded By</th>
<th>Upload Date</th>
<th>Actions</th>
</tr>
</thead>
</table>
</div>
</div>
</div>
</div>
<?php include('include/footer.php'); ?>
</div>
<?php include('jsinclude.php'); ?>
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap5.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<script>
$(document).ready(function() {
$('.select2').select2();
const table = $('#clientFilesTable').DataTable({
ajax: {
url: 'client_files_list.php',
dataSrc: ''
},
columns: [{
data: 'id'
},
{
data: 'client_name'
},
{
data: 'file_type'
},
{
data: 'filename'
},
{
data: 'source_id'
},
{
data: 'uploaded_by'
},
{
data: 'upload_date'
},
{
data: null,
orderable: false,
render: function(data, type, row) {
return `
<button class="btn btn-danger btn-sm delete-file" data-id="${row.id}">
<i class="fas fa-trash"></i>
</button>`;
}
}
],
pageLength: 10,
order: [
[0, 'desc']
]
});
// Upload handlers
function handleUpload(formSelector) {
$(formSelector).on('submit', function(e) {
e.preventDefault();
const idclient = $('#idclient').val();
if (!idclient) {
Swal.fire('Warning', 'Select a client first.', 'warning');
return;
}
const formData = new FormData(this);
formData.append('idclient', idclient);
$.ajax({
url: 'client_files_upload.php',
type: 'POST',
data: formData,
processData: false,
contentType: false,
beforeSend: () => Swal.showLoading(),
success: function(res) {
Swal.close();
let json;
try {
json = JSON.parse(res);
} catch (e) {
Swal.fire('Error', res, 'error');
return;
}
if (json.status === 'ok') {
Swal.fire('Success', 'File uploaded successfully.', 'success');
table.ajax.reload(null, false);
} else {
Swal.fire('Error', json.message, 'error');
}
},
error: function() {
Swal.fire('Error', 'Upload failed.', 'error');
}
});
});
}
handleUpload('#uploadMappingForm');
handleUpload('#uploadPriceForm');
// Delete
$('#clientFilesTable').on('click', '.delete-file', function() {
const id = $(this).data('id');
Swal.fire({
title: 'Delete file?',
text: 'This will remove the file record.',
icon: 'warning',
showCancelButton: true,
confirmButtonText: 'Yes, delete it'
}).then(result => {
if (result.isConfirmed) {
$.post('client_files_delete.php', {
id
}, res => {
let json;
try {
json = JSON.parse(res);
} catch (e) {
Swal.fire('Error', res, 'error');
return;
}
if (json.status === 'ok') {
Swal.fire('Deleted', 'File removed.', 'success');
table.ajax.reload(null, false);
} else {
Swal.fire('Error', json.message, 'error');
}
});
}
});
});
});
</script>
</body>
</html>