File: /var/www/html/hrms-production/resources/views/components/department-modal.blade.php
{{-- Add Department --}}
<div class="modal fade" id="addDepartment">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Add Department</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<!-- Main content -->
<form id="addDepartmentform">
<input type="hidden" name="_token" id="token_department" value="{{ csrf_token() }}">
<div class="card-body">
<div class="row">
<div class="col-md-12">
<div class="form-group mb-2">
<label class="mb-0" for="ActualWorkPlace">Actual Work Place<span class="text-danger">*</label>
<select class="form-control form-control-sm select2" style="width: 100%;" aria-hidden="true" name="ActualWorkPlaceId" id="AWPIdDepartment" data-placeholder="Select Actual Work Place" required>
<option selected="selected" disabled>Select Actual Work Place</option>
</select>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group mb-2">
<label class="mb-0" for="DepartmentName">Department Name<span class="text-danger">*</span></label>
<input type="text" class="form-control form-control-sm" id="DepartmentName" name="DepartmentName" required>
</div>
</div>
</div>
<hr />
<div class="row">
<div class="col-12">
<div class="d-flex align-items-center justify-content-center">
<button type="button" class="btn btn-sm btn-outline-secondary mx-1"
data-dismiss="modal" aria-label="Close">Cancel</button>
<button type="submit" id="btn-save-new-department"
class="btn btn-sm btn-outline-info mx-1">Save & New</button>
<button type="submit" id="btn-save-department"
class="btn btn-sm btn-info mx-1">Save</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<!-- /.modal -->
{{-- Update Department --}}
<div class="modal fade" id="updateDepartment">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Department <span id="DepartmentName" class="text-info"></span></h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<!-- Main content -->
<form id="updateDepartmentform">
<input type="hidden" name="_token" id="token_department_update" value="{{ csrf_token() }}">
<input type="hidden" id="DepartmentId" name="DepartmentId">
<div class="card-body">
<div class="row">
<div class="col-md-12">
<div class="form-group mb-2">
<label class="mb-0" for="name">Department Name<span
class="text-danger">*</span></label>
<input type="text" class="form-control form-control-sm" id="editDepartmentName"
name="DepartmentName" onkeyup="checkDuplicateDepartment(this);" required>
<span id='editCheckDepartment'></span>
</div>
</div>
</div>
<hr />
<div class="row">
<div class="col-12">
<div class="d-flex align-items-center justify-content-center">
<button type="button" class="btn btn-sm btn-outline-secondary mx-1"
data-dismiss="modal" aria-label="Close">Cancel</button>
<button type="button" id="btn-update-department" name="Update" value="Update"
class="btn btn-sm btn-info mx-1" disabled>Update</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<!-- /.modal -->
{{-- JS Start Here --}}
{{-- Actual Work Place List --}}
<script>
$(document).ready(function() {
$.ajax({
type: "GET",
url: "{{ url('/actual-work-place-list') }}",
dataType: "json",
success: function(response) {
var json = response.AWPList;
$.each(json, function(i, awp) {
$("#AWPIdDepartment, #AWPIdDepartmentEdit").append("<option value='" + awp.ActualWorkPlaceId + "'>" + awp.ActualWorkPlaceName + "</option>");
$('#AWPIdDepartment, #AWPIdDepartmentEdit').trigger('change');
});
}
});
});
</script>
{{-- Check Availablity --}}
<script>
function checkDuplicateDepartment(element, id){
var id = id;
var DepartmentName = $(element).val();
if (DepartmentName == "") {
$('span#CheckDepartment, #editCheckDepartment').css("display", "none");
} else {
$('span#CheckDepartment, #editCheckDepartment').css("display", "block");
}
//alert (Department);
$.ajax({
type: "GET",
url: '{{ url("/") }}/check-department/',
data: {DepartmentName:DepartmentName, id:id},
dataType: "json",
success: function(res) {
if(res.exists){
$('span#CheckDepartment, span#editCheckDepartment').html('<span class="text-danger">Department name is already exist!</span>');
$('#btn-save-new-department, #btn-save-department, #btn-update-department').attr('disabled', true);
} else {
$('span#CheckDepartment, span#editCheckDepartment').html('<span class="text-success">Department name is available!</span>');
$('#btn-save-new-department, #btn-save-department, #btn-update-department').attr('disabled', false);
}
},
error: function (jqXHR, exception) {
}
});
}
</script>
{{-- Store Department --}}
<script>
var reload = 0;
$(function () {
$.validator.setDefaults({
submitHandler: function () {
saveDepartmentRecord();
}
});
// Reload page after data save
$('#btn-save-department').click(function(){
reload = 1;
});
// Validate Fields
$('#addDepartmentform').validate({
rules: {
DepartmentName: {
required: true,
remote: {
type: "POST",
url:'{{ url("/") }}/check-duplicate-department',
data: {
DepartmentName: function() {
return $( "#DepartmentName" ).val();
},
ActualWorkPlaceId: function() {
return $('#AWPIdDepartment').find(":selected").val();
},
'_token':$('meta[name="csrf-token"]').attr('content')
}
},
},
},
messages: {
DepartmentName: {
required: "Please enter the department name",
remote: "The department already exists in the selected place.",
},
},
errorElement: 'span',
errorPlacement: function (error, element) {
error.addClass('invalid-feedback');
element.closest('.form-group').append(error);
},
highlight: function (element, errorClass, validClass) {
$(element).addClass('is-invalid');
},
unhighlight: function (element, errorClass, validClass) {
$(element).removeClass('is-invalid');
}
});
});
</script>
{{-- Save Data Function --}}
<script>
function saveDepartmentRecord() {
event.preventDefault();
$.ajax({
type: "post",
url: "{{ url('store-department') }}",
dataType: "json",
data: $('form#addDepartmentform').serialize(),
success: function(response) {
const Toast = Swal.mixin({
toast: true,
position: 'top-end',
showConfirmButton: false,
timer: 2000,
timerProgressBar: true,
didOpen: (toast) => {
toast.addEventListener('mouseenter', Swal.stopTimer)
toast.addEventListener('mouseleave', Swal.resumeTimer)
}
})
Toast.fire({
icon: 'success',
title: 'Department added successfully!'
});
$('form#addDepartmentform').each(function() {
this.reset();
$('#AWPIdDepartment').val(null).trigger('change');
});
if(reload)
{
location.reload();
}
},
error: function(response) {
const Toast = Swal.mixin({
toast: true,
position: 'top-end',
showConfirmButton: false,
timer: 3000,
timerProgressBar: true,
didOpen: (toast) => {
toast.addEventListener('mouseenter', Swal.stopTimer)
toast.addEventListener('mouseleave', Swal.resumeTimer)
}
})
Toast.fire({
icon: 'error',
title: 'Sorry, Something went wrong!'
})
}
});
}
</script>
{{-- Activate / De-Activate --}}
<script>
// Activate
function activateDepartment(DepartmentId) {
var DepartmentId = DepartmentId;
Swal.fire({
title: 'Activate Department?',
text: "Are you sure you want to activate Department!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, activate it!'
}).then((result) => {
if (result.isConfirmed) {
$.ajax({
type: "get",
url: "{{ url('/') }}/change-status-department/" + DepartmentId,
success: function(response) {
Swal.fire({
icon: 'success',
title: 'Department has been activated!',
showConfirmButton: false,
timer: 1500
}).then(function() {
location.reload();
});
}
});
}
});
}
// Deactivate
function deactivateDepartment(DepartmentId) {
var DepartmentId = DepartmentId;
Swal.fire({
title: 'De-activate Department?',
text: "Are you sure you want to De-activate Department!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, De-activate it!'
}).then((result) => {
if (result.isConfirmed) {
$.ajax({
type: "get",
url: "{{ url('/') }}/change-status-department/" + DepartmentId,
success: function(response) {
Swal.fire({
icon: 'success',
title: 'Department has been De-activated!',
showConfirmButton: false,
timer: 1500
}).then(function() {
location.reload();
});
}
});
}
});
}
</script>
{{-- Edit Department --}}
<script>
function editDepartment(DepartmentId)
{
var DepartmentId = DepartmentId;
$('#updateDepartment').modal('show');
$.ajax({
type: "GET",
url: "{{ url('/') }}/edit-department/" +DepartmentId,
success: function (response) {
$('#editDepartmentName').val(response.Department.DepartmentName);
$('#DepartmentId').val(DepartmentId);
$('span#DepartmentName').html('#' +response.Department.DepartmentName);
}
});
}
</script>
{{-- Update Department --}}
<script>
$("#btn-update-department").click(function (event) {
event.preventDefault();
var Name = $("#editDepartmentName").val();
if (Name == "") {
Swal.fire({
icon: "warning",
title: "Name is required!",
showConfirmButton: false,
timer: 2000,
});
} else {
$.ajax({
type: "post",
url: "{{ url('update-department') }}",
dataType: "json",
data: $("form#updateDepartmentform").serialize(),
success: function (response) {
Swal.fire({
icon: "success",
title: "Department has been updated!",
showConfirmButton: false,
timer: 2000,
}).then(function () {
location.reload();
});
},
error: function (response) {
Swal.fire({
icon: "error",
title: "Sorry, Some thing went wrong!",
showConfirmButton: false,
timer: 2000,
});
},
});
}
});
</script>
{{-- Delete Department --}}
<script>
//Delete Department
function deleteDepartment(DepartmentId) {
var DepartmentId = DepartmentId;
$.ajax({
type: "GET",
url: "{{ url('/') }}/check-employee-department/" + DepartmentId,
success: function (response) {
if (response.exists) {
Swal.fire({
icon: "warning",
title: "Can't be deleted!",
text: "This Department is associated with empolyee(s)!",
showConfirmButton: true,
});
} else {
Swal.fire({
title: "Delete Department?",
text: "Are you sure you want to delete this Department?",
icon: "warning",
showCancelButton: true,
confirmButtonColor: "#3085d6",
cancelButtonColor: "#d33",
confirmButtonText: "Yes, delete it!",
}).then((result) => {
if (result.isConfirmed) {
$.ajax({
type: "get",
url:
"{{ url('/') }}/delete-department/" +
DepartmentId,
success: function (response) {
Swal.fire({
icon: "success",
title: "Department has been deleted!",
showConfirmButton: false,
timer: 1500,
}).then(function () {
location.reload();
});
},
});
}
});
}
},
});
}
</script>