first commit
This commit is contained in:
@@ -0,0 +1,314 @@
|
||||
@extends('layouts.main')
|
||||
|
||||
@section('breadcrumb')
|
||||
<ol class="breadcrumb m-0">
|
||||
<li class="breadcrumb-item"><a href="javascript:void(0);">Manajemen</a></li>
|
||||
<li class="breadcrumb-item active" aria-current="page">{{ $title }}</li>
|
||||
</ol>
|
||||
@endsection
|
||||
|
||||
@section('content')
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
<div class="card border">
|
||||
<div class="card-body">
|
||||
@include('components.datatable-header', [
|
||||
'title' => 'Daftar Permission',
|
||||
'description' => 'Berikut adalah daftar permission yang tersedia dalam sistem',
|
||||
'actionButton' => $canTambah ? '<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#permissionModal" onclick="resetForm()">
|
||||
<i class="fas fa-plus"></i> Tambah Permission
|
||||
</button>' : ''
|
||||
])
|
||||
|
||||
<div class="table-responsive">
|
||||
<table class="table table-striped table-bordered w-100" id="permissionTable">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>No</th>
|
||||
<th>Name</th>
|
||||
<th>Guard Name</th>
|
||||
<th>Type</th>
|
||||
@if ($canAksi)
|
||||
<th>Aksi</th>
|
||||
@endif
|
||||
</tr>
|
||||
</thead>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Modal Form -->
|
||||
<div class="modal fade" id="permissionModal" tabindex="-1" role="dialog" aria-labelledby="permissionModalLabel" aria-hidden="true">
|
||||
<div class="modal-dialog" role="document">
|
||||
<div class="modal-content">
|
||||
<form id="permissionForm">
|
||||
@csrf
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title" id="permissionModalLabel">Tambah Permission</h5>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<input type="hidden" id="permission_id" name="permission_id">
|
||||
|
||||
<div class="form-group">
|
||||
<label for="name" class="form-label">Name <span class="text-danger">*</span></label>
|
||||
<input type="text" class="form-control" id="name" name="name" required>
|
||||
<div class="invalid-feedback"></div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="guard_name" class="form-label">Guard Name <span class="text-danger">*</span></label>
|
||||
<input type="text" class="form-control" id="guard_name" name="guard_name" required>
|
||||
<div class="invalid-feedback"></div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="type" class="form-label">Type <span class="text-danger">*</span></label>
|
||||
<select class="form-control select2" id="type" name="type" required
|
||||
data-placeholder="Pilih Type" data-dropdown-parent="#permissionModal">
|
||||
<option value=""></option>
|
||||
<option value="Create">Create</option>
|
||||
<option value="Read">Read</option>
|
||||
<option value="Update">Update</option>
|
||||
<option value="Delete">Delete</option>
|
||||
</select>
|
||||
<div class="invalid-feedback"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
|
||||
<button type="submit" class="btn btn-primary" id="submitBtn">Simpan</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
|
||||
@section('js')
|
||||
<script>
|
||||
let table;
|
||||
let isEdit = false;
|
||||
|
||||
$(document).ready(function() {
|
||||
// Initialize Select2
|
||||
initializeSelect2();
|
||||
|
||||
// Re-initialize Select2 when modal is shown
|
||||
$('#permissionModal').on('shown.bs.modal', function() {
|
||||
initializeSelect2();
|
||||
});
|
||||
|
||||
// Initialize DataTable
|
||||
table = $('#permissionTable').DataTable({
|
||||
processing: true,
|
||||
serverSide: true,
|
||||
ajax: {
|
||||
url: "{{ route('manajemen.permission.data') }}"
|
||||
},
|
||||
columns: [
|
||||
{
|
||||
data: 'DT_RowIndex',
|
||||
name: 'DT_RowIndex',
|
||||
orderable: false,
|
||||
searchable: false,
|
||||
className: 'text-center',
|
||||
width: '5%'
|
||||
},
|
||||
{
|
||||
data: 'name',
|
||||
name: 'name'
|
||||
},
|
||||
{
|
||||
data: 'guard_name',
|
||||
name: 'guard_name'
|
||||
},
|
||||
{
|
||||
data: 'type',
|
||||
name: 'type',
|
||||
className: 'text-center'
|
||||
},
|
||||
@if ($canAksi)
|
||||
{
|
||||
data: 'action',
|
||||
name: 'action',
|
||||
orderable: false,
|
||||
searchable: false,
|
||||
className: 'text-center',
|
||||
width: '10%'
|
||||
}
|
||||
@endif
|
||||
],
|
||||
language: {
|
||||
processing: "<div class='spinner'></div> Sedang Memuat...",
|
||||
}
|
||||
});
|
||||
|
||||
// Handle form submission
|
||||
$('#permissionForm').on('submit', function(e) {
|
||||
e.preventDefault();
|
||||
|
||||
let formData = {
|
||||
name: $('#name').val(),
|
||||
guard_name: $('#guard_name').val(),
|
||||
type: $('#type').val(),
|
||||
_token: $('input[name="_token"]').val()
|
||||
};
|
||||
|
||||
if (!formData.name) {
|
||||
showAlert('error', 'Name harus diisi');
|
||||
return;
|
||||
}
|
||||
if (!formData.guard_name) {
|
||||
showAlert('error', 'Guard Name harus diisi');
|
||||
return;
|
||||
}
|
||||
if (!formData.type) {
|
||||
showAlert('error', 'Type harus dipilih');
|
||||
return;
|
||||
}
|
||||
|
||||
submitForm(formData);
|
||||
});
|
||||
|
||||
function submitForm(formData) {
|
||||
let url = isEdit ?
|
||||
"{{ route('manajemen.permission.update', ':id') }}".replace(':id', $('#permission_id').val()) :
|
||||
"{{ route('manajemen.permission.store') }}";
|
||||
|
||||
if (isEdit) {
|
||||
formData._method = 'PUT';
|
||||
}
|
||||
|
||||
$.ajax({
|
||||
url: url,
|
||||
type: 'POST',
|
||||
data: formData,
|
||||
beforeSend: function() {
|
||||
$('#submitBtn').prop('disabled', true).text('Menyimpan...');
|
||||
$('.is-invalid').removeClass('is-invalid');
|
||||
$('.invalid-feedback').text('');
|
||||
},
|
||||
success: function(response) {
|
||||
if (response.success) {
|
||||
bootstrap.Modal.getInstance(document.getElementById('permissionModal')).hide();
|
||||
table.ajax.reload();
|
||||
showAlert('success', response.message);
|
||||
}
|
||||
},
|
||||
error: function(xhr) {
|
||||
if (xhr.status === 422) {
|
||||
let errors = xhr.responseJSON.errors;
|
||||
$.each(errors, function(key, value) {
|
||||
$('#' + key).addClass('is-invalid');
|
||||
$('#' + key).siblings('.invalid-feedback').text(value[0]);
|
||||
});
|
||||
} else {
|
||||
let errorMessage = 'Terjadi kesalahan saat menyimpan data';
|
||||
if (xhr.responseJSON && xhr.responseJSON.message) {
|
||||
errorMessage = xhr.responseJSON.message;
|
||||
}
|
||||
showAlert('error', errorMessage);
|
||||
}
|
||||
},
|
||||
complete: function() {
|
||||
$('#submitBtn').prop('disabled', false).text('Simpan');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Handle delete button
|
||||
$(document).on('click', '.delete-btn', function() {
|
||||
let id = $(this).data('id');
|
||||
let name = $(this).data('name');
|
||||
|
||||
if (confirm('Apakah Anda yakin ingin menghapus permission "' + name + '"?')) {
|
||||
$.ajax({
|
||||
url: "{{ route('manajemen.permission.destroy', ':id') }}".replace(':id', id),
|
||||
type: 'DELETE',
|
||||
data: {
|
||||
_token: '{{ csrf_token() }}'
|
||||
},
|
||||
success: function(response) {
|
||||
if (response.success) {
|
||||
table.ajax.reload();
|
||||
showAlert('success', response.message);
|
||||
}
|
||||
},
|
||||
error: function(xhr) {
|
||||
let message = 'Terjadi kesalahan saat menghapus data';
|
||||
if (xhr.responseJSON && xhr.responseJSON.message) {
|
||||
message = xhr.responseJSON.message;
|
||||
}
|
||||
showAlert('error', message);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Handle edit button
|
||||
$(document).on('click', '.edit-btn', function() {
|
||||
let id = $(this).data('id');
|
||||
isEdit = true;
|
||||
|
||||
$.ajax({
|
||||
url: "{{ route('manajemen.permission.show', ':id') }}".replace(':id', id),
|
||||
type: 'GET',
|
||||
success: function(data) {
|
||||
$('#permission_id').val(data.id);
|
||||
$('#name').val(data.name).prop('readonly', true);
|
||||
$('#guard_name').val(data.guard_name).prop('readonly', true);
|
||||
$('#permissionModalLabel').text('Edit Permission');
|
||||
|
||||
// Show modal first
|
||||
let modal = new bootstrap.Modal(document.getElementById('permissionModal'));
|
||||
modal.show();
|
||||
|
||||
// Set type after modal is shown and select2 is initialized
|
||||
setTimeout(function() {
|
||||
$('#type').val(data.type).trigger('change');
|
||||
}, 300);
|
||||
},
|
||||
error: function(xhr) {
|
||||
console.log('Error loading data:', xhr);
|
||||
showAlert('error', 'Gagal memuat data permission');
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function initializeSelect2() {
|
||||
$('#type').select2({
|
||||
allowClear: true,
|
||||
placeholder: "Pilih Type",
|
||||
width: '100%',
|
||||
dropdownParent: $('#permissionModal')
|
||||
});
|
||||
}
|
||||
|
||||
function resetForm() {
|
||||
isEdit = false;
|
||||
$('#permissionForm')[0].reset();
|
||||
$('#permission_id').val('');
|
||||
$('#name').prop('readonly', false);
|
||||
$('#guard_name').prop('readonly', false);
|
||||
$('#type').val(null).trigger('change');
|
||||
$('#permissionModalLabel').text('Tambah Permission');
|
||||
$('.is-invalid').removeClass('is-invalid');
|
||||
$('.invalid-feedback').text('');
|
||||
}
|
||||
|
||||
function showAlert(type, message) {
|
||||
if (type === 'success') {
|
||||
showSuccess('Berhasil', message);
|
||||
} else if (type === 'error') {
|
||||
showError('Error', message);
|
||||
} else {
|
||||
showInfo('Informasi', message);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
@endsection
|
||||
Reference in New Issue
Block a user