nhance/app/Views/UserList.php
2024-02-10 14:44:07 +05:30

317 lines
12 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="margin-bottom:1rem;">
<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="btnExport" class="btn btn-primary waves-effect waves-light" data-toggle="tooltip" data-placement="top" title="CSV" data-trigger="hover"><i class="ri-file-download-line"></i></button>
<button type="button" id="btnAdd" class="btn btn-primary waves-effect waves-light" data-toggle="tooltip" data-placement="top" title="Add" data-trigger="hover"><i class="ri-user-add-line"></i></button>
</div>
</div>
<!-- <?php print_r($UserList);?> -->
<table class="table table-sm table-hover m-0 table-centered dt-responsive nowrap w-100" cellspacing="0" id="tickets-table">
<thead class="bg-light">
<tr>
<th class="font-weight-medium">First Name</th>
<th class="font-weight-medium">Last Name</th>
<th class="font-weight-medium">Email</th>
<th class="font-weight-medium">Mobile No</th>
<th class="font-weight-medium">Status</th>
<th class="font-weight-medium">Action</th>
</tr>
</thead>
<tbody>
<?php foreach($UserList as $row) { ?>
<tr id="<?php echo $row['id'];?>">
<!-- <td>
<a href="javascript: void(0);" class="text-dark">
<img src="../assets/images/users/avatar-2.jpg" alt="contact-img" title="contact-img" class="avatar-sm rounded-circle img-thumbnail" />
<span class="ml-2">Robert K. Joseph</span>
</a>
</td> -->
<td class="text-dark"><?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>
<span class="<?php if($row['is_active'] == 1){ echo 'badge badge-success'; }else{ echo 'badge badge-danger'; } ?>">
<?php if($row['is_active'] == 1){ echo 'Active'; }else{ echo 'In-Active'; } ?>
</span>
</td>
<td>
<div class="btn-group dropdown" style="position: relative !important;left:0px !important;top:0px !important;">
<a href="javascript: void(0);" class="dropdown-toggle arrow-none btn btn-light btn-sm" data-toggle="dropdown" aria-expanded="false"><i class="mdi mdi-dots-horizontal"></i></a>
<div class="dropdown-menu dropdown-menu-right" style="cursor: pointer;">
<a class="dropdown-item btnEdit" data-id="<?php echo $row['id']; ?>"><i data-id="<?php echo $row['id']; ?>" class="mdi mdi-pencil mr-2 text-muted font-18 vertical-middle btnEdit"></i>Edit</a>
<a class="dropdown-item btnDelete" data-id="<?php echo $row['id']; ?>"><i data-id="<?php echo $row['id']; ?>" class="mdi mdi-delete mr-2 text-muted font-18 vertical-middle btnDelete"></i>Delete</a>
</div>
</div>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</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 : true,
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('');
$('#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) {
$('#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');
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);
$('#tickets-table tbody #'+ student_id).remove();
})
}
});
});
// $('#tickets-table').DataTable({
// "dom": 'lBfrtip', // Show export buttons
// "paging": true,
// "lengthMenu": [ [10, 20, 50, 100], [10, 20, 50, 100] ], // Define length menu options
// "pageLength":20,
// "paging"
// buttons: [
// {
// 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
}
const btnExport = document.querySelector("#btnExport");
const tableElement = document.querySelector("#tickets-table");
btnExport.addEventListener("click", () => {
const obj = new csvExport(tableElement);
const csvData = obj.exportCsv();
const blob = new Blob([csvData], { type: "text/csv" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "UserList.csv";
a.click();
setTimeout(() => {
URL.revokeObjectURL(url);
}, 500);
});
class csvExport {
constructor(table, header = true) {
this.table = table;
this.rows = Array.from(table.querySelectorAll("tr"));
if (!header && this.rows[0].querySelectorAll("th").length) {
this.rows.shift();
}
// console.log(this.rows);
// console.log(this._longestRow());
}
exportCsv() {
const lines = [];
const ncols = this._longestRow();
for (const row of this.rows) {
let line = "";
for (let i = 0; i < ncols; i++) {
if (row.children[i] !== undefined) {
line += csvExport.safeData(row.children[i]);
}
line += i !== ncols - 1 ? "," : "";
}
lines.push(line);
}
//console.log(lines);
return lines.join("\n");
}
_longestRow() {
return this.rows.reduce((length, row) => (row.childElementCount > length ? row.childElementCount : length), 0);
}
static safeData(td) {
let data = td.textContent;
//Replace all double quote to two double quotes
data = data.replace(/"/g, `""`);
//Replace , and \n to double quotes
data = /[",\n"]/.test(data) ? `"${data}"` : data;
return data;
}
}
</script>