nhance/app/Views/UserList.php

281 lines
10 KiB
PHP

<!-- End ADD and EDIT Page HTML -->
<div class="row" id="List-page">
<div class="col-12">
<div class="card">
<div class="card-body">
<div class="row" style="padding-bottom: 10px;">
<div class="col-6" style="align-self: center;">
<h4 class="header-title" style="position: relative;">User List</h4>
</div>
<div class="col-6" style="text-align: right;">
<button type="button" id="btnAdd" class="btn btn-primary waves-effect waves-light">Add</button>
</div>
</div>
<table id="datatable-buttons" class="table table-striped dt-responsive nowrap w-100">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Mobile No</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach($UserList as $row) { ?>
<tr id="<?php echo $row['id'];?>">
<td><?php echo $row['first_name']; ?></td>
<td><?php echo $row['last_name']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['mobile']; ?></td>
<td>
<a class="btnEdit" data-id="<?php echo $row['id']; ?>"><i data-id="<?php echo $row['id']; ?>" class="mdi mdi-lead-pencil btnEdit" style="font-size:18px;"></i></a>
<a class="btnDelete" data-id="<?php echo $row['id']; ?>"><i data-id="<?php echo $row['id']; ?>" class="mdi mdi-delete btnDelete" style="font-size:18px;"></i></a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div> <!-- end card body-->
</div> <!-- end card -->
</div><!-- end col-->
</div>
<!-- end row-->
<script>
$(document).ready(function () {
$('#Add-Edit-Page').hide();
$('.btnBack').hide();
$('#btnAdd').click(function(){
$('#Add-Edit-Page').show();
$('.header-title').html('Add User');
$('#btnSubmit').html('Submit');
$('#first_name').val('');
$('#last_name').val('');
$('#email').val('');
$('#mobile').val('');
$('#emp_code').val('');
$('#profile').val('');
$('#ajaxImgUpload').attr('src', 'https://via.placeholder.com/300').width(100);
$('#List-page').hide();
$('.btnBack').show();
$('#btnAdd').hide();
$('#UserForm').attr('action', '<?php echo base_url('/user/create');?>');
$.ajax({
url: '<?php echo base_url('user/rolesandteams/');?>',
type: "GET",
dataType: 'json',
success: function (res) {
console.log(res)
$.each(res.roleData, function(index, item) {
console.log(item.role)
$('#role').append($('<option>', {
value: item.id,
text : item.role
}));
});
$.each(res.teamData, function(index, item) {
$('#team').append($('<option>', {
value: item.id,
text : item.name
}));
});
$('#team').multiselect({
nonSelectedText: 'Select Team',
enableFiltering: false,
enableCaseInsensitiveFiltering: false,
includeSelectAllOption : false,
buttonWidth:'100%'
});
},
error: function (xhr, status, error) {
console.error(xhr.responseText);
console.error(status, error);
}
});
})
$('.btnBack').click(function(){
$('#Add-Edit-Page').hide();
$('.header-title').html('User List');
$('#UserId').val('');
$('#first_name').val('');
$('#last_name').val('');
$('#email').val('');
$('#mobile').val('');
$('#emp_code').val('');
$('#profile').val('');
$('#role').val('')
$('#ajaxImgUpload').attr('src', 'https://via.placeholder.com/300').width(100);
$('#List-page').show();
$('.btnBack').hide();
$('#btnAdd').show();
})
$('body').on('click', '.btnEdit', function () {
var student_id = $(this).attr('data-id');
$.ajax({
url: '<?php echo base_url('user/getuser/');?>'+student_id,
type: "GET",
dataType: 'json',
success: function (res) {
console.log(res)
$('#role').val('')
$('#Add-Edit-Page').show();
$('.header-title').html('Edit User');
$('#List-page').hide();
$('.btnBack').show();
$('#btnAdd').hide();
$('#UserForm').attr('action', '<?php echo base_url('user/edit');?>');
$('#UserId').val(res.data.id);
$('#first_name').val(res.data.first_name);
$('#last_name').val(res.data.last_name);
$('#email').val(res.data.email);
$('#mobile').val(res.data.mobile);
$('#emp_code').val(res.data.emp_code);
$('#btnSubmit').html('Update');
var roleOptionHTML = '';
$.each(res.roleData, function(index, item) {
var isSelected = res.userTeamData.some(function(userTeamItem) {
return res.data.role === item.id;
});
roleOptionHTML += '<option value="' + item.id + '" ' + (isSelected ? 'selected="selected"' : '') + '>' + item.role + '</option>';
});
$('#role').html(roleOptionHTML);
var optionsHTML = '';
$.each(res.teamData, function(index, item) {
var isSelected = res.userTeamData.some(function(userTeamItem) {
return userTeamItem.team_id === item.id;
});
optionsHTML += '<option value="' + item.id + '" ' + (isSelected ? 'selected="selected"' : '') + '>' + item.name + '</option>';
});
$('#team').html(optionsHTML);
$('#team').multiselect({
nonSelectedText: 'Select Team',
enableFiltering: false,
enableCaseInsensitiveFiltering: false,
includeSelectAllOption : false,
buttonWidth:'100%'
});
if(res.data.profile){
$('#ajaxImgUpload').attr('src', '<?php echo base_url();?>public/uploads/'+res.data.profile).width(100);
}else{
$('#ajaxImgUpload').attr('src', 'https://via.placeholder.com/300').width(100);
}
},
error: function (xhr, status, error) {
console.error(xhr.responseText);
console.error(status, error);
}
});
});
$('body').on('click', '.btnDelete', function () {
Swal.fire({
title: "Are you sure?",
text: "You won't be able to revert this!",
icon: "warning",
showCancelButton: true,
confirmButtonColor: "#3085d6",
cancelButtonColor: "#d33",
confirmButtonText: "Yes, delete it!"
}).then((result) => {
if (result.isConfirmed) {
var student_id = $(this).attr('data-id');
$.get('<?php echo base_url('user/deactive/');?>'+student_id, function (data) {
console.log(data);
$('#datatable-buttons tbody #'+ student_id).remove();
})
}
});
});
$('#datatable-buttons').DataTable({
"dom": 'Bfrtip', // Show export buttons
"paging": true,
"lengthMenu": [ [10, 25, 50, 100], [10, 25, 50, 100] ], // Define length menu options
buttons: [
{
extend: 'pdfHtml5',
title: 'UserList', // Set your custom PDF title here
text: 'PDF',
customize: function(doc) {
// You can further customize the PDF here if needed
doc.content[1].table.body.forEach(function(row) {
row.pop(); // Remove the last column
});
}
},
{
extend: 'csv',
text: 'CSV', // Set the title for the CSV button
title: 'UserList', // Set your custom print title here
exportOptions: {
columns: ':not(:last-child)' // Exclude the first and last column (ID column)
}
},
]
});
});
function onFileUpload(input, id) {
id = id || '#ajaxImgUpload';
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
console.log(e.target.result);
$(id).attr('src', e.target.result).width(100);
};
reader.readAsDataURL(input.files[0]);
}
}
function isFieldEmptyOrNull(formData, fieldName) {
let value = formData.get(fieldName);
return value === '' || value === null;
}
function areFieldsEmptyOrFileEmpty(id) {
console.log(id)
let element = document.getElementById(id);
if (element.type === 'file') {
console.log(element.files.length)
if (element.files.length === 0) {
return true; // File input is empty
}
} else {
if (element.value === '') {
return true; // Other input field is empty
}
}
return false; // All fields are non-empty
}
</script>