346 lines
14 KiB
PHP
346 lines
14 KiB
PHP
@extends('layouts.main')
|
|
|
|
@section('css')
|
|
<style>
|
|
#periodeTable thead th {
|
|
background: #2563eb !important; background-color: #2563eb !important;
|
|
color: #fff !important;
|
|
border-color: #2563eb !important;
|
|
}
|
|
|
|
#periodeTable tbody tr {
|
|
transition: background-color 0.15s ease-in-out;
|
|
}
|
|
|
|
#periodeTable tbody tr:hover > * {
|
|
background-color: rgba(13, 110, 253, 0.08) !important;
|
|
box-shadow: none;
|
|
}
|
|
</style>
|
|
@endsection
|
|
|
|
@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 Periode',
|
|
'description' => 'Berikut adalah daftar periode akademik yang terdaftar dalam sistem',
|
|
'actionButton' => $canTambah ? '<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#periodeModal" onclick="resetForm()">
|
|
<i class="fas fa-plus"></i> Tambah Periode
|
|
</button>' : ''
|
|
])
|
|
|
|
<div class="table-responsive">
|
|
<table class="table table-bordered w-100" id="periodeTable">
|
|
<thead>
|
|
<tr>
|
|
<th>No</th>
|
|
<th>Nama</th>
|
|
<th>Status</th>
|
|
@if($canAksi)
|
|
<th>Aksi</th>
|
|
@endif
|
|
</tr>
|
|
</thead>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Modal Form -->
|
|
<div class="modal fade" id="periodeModal" tabindex="-1" role="dialog" aria-labelledby="periodeModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog" role="document">
|
|
<div class="modal-content">
|
|
<form id="periodeForm">
|
|
@csrf
|
|
<div class="modal-header">
|
|
<h5 class="modal-title" id="periodeModalLabel">Tambah Periode</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<input type="hidden" id="periode_id" name="periode_id">
|
|
|
|
<div class="form-group">
|
|
<label for="id_periode" class="form-label">ID Periode <span class="text-danger">*</span></label>
|
|
<input type="text" class="form-control" id="id_periode" name="id_periode" required>
|
|
<div class="invalid-feedback"></div>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="nama" class="form-label">Nama <span class="text-danger">*</span></label>
|
|
<input type="text" class="form-control" id="nama" name="nama" required>
|
|
<div class="invalid-feedback"></div>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="status" class="form-label">Status <span class="text-danger">*</span></label>
|
|
<select class="form-control select2" id="status" name="status" required
|
|
data-placeholder="Pilih Status" data-dropdown-parent="#periodeModal">
|
|
<option value=""></option>
|
|
<option value="1">Aktif</option>
|
|
<option value="0">Non Aktif</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 for status
|
|
initializeSelect2();
|
|
|
|
// Re-initialize Select2 when modal is shown
|
|
$('#periodeModal').on('shown.bs.modal', function() {
|
|
initializeSelect2();
|
|
});
|
|
|
|
// Initialize DataTable
|
|
table = $('#periodeTable').DataTable({
|
|
processing: true,
|
|
serverSide: true,
|
|
ajax: {
|
|
url: "{{ route('manajemen.periode.data') }}"
|
|
},
|
|
columns: [
|
|
{
|
|
data: 'DT_RowIndex',
|
|
name: 'DT_RowIndex',
|
|
orderable: false,
|
|
searchable: false,
|
|
className: 'text-center',
|
|
width: '5%'
|
|
},
|
|
{
|
|
data: 'nama',
|
|
name: 'nama'
|
|
},
|
|
{
|
|
data: 'status_badge',
|
|
name: 'status_badge',
|
|
orderable: false,
|
|
searchable: false,
|
|
className: 'text-center',
|
|
width: '15%'
|
|
},
|
|
@if($canAksi)
|
|
{
|
|
data: 'action',
|
|
name: 'action',
|
|
orderable: false,
|
|
searchable: false,
|
|
className: 'text-center',
|
|
width: '15%'
|
|
}
|
|
@endif
|
|
],
|
|
language: {
|
|
processing: "<div class='spinner'></div> Sedang Memuat...",
|
|
}
|
|
});
|
|
|
|
// Handle form submission
|
|
$('#periodeForm').on('submit', function(e) {
|
|
e.preventDefault();
|
|
console.log('Form submit triggered');
|
|
|
|
// Get form data as object
|
|
let formData = {
|
|
id_periode: $('#id_periode').val(),
|
|
nama: $('#nama').val(),
|
|
status: $('#status').val(),
|
|
_token: $('input[name="_token"]').val()
|
|
};
|
|
|
|
console.log('Form data:', formData);
|
|
console.log('Form validation state:', this.checkValidity());
|
|
|
|
// Check for empty required fields
|
|
if (!formData.id_periode) {
|
|
showAlert('error', 'ID Periode harus diisi');
|
|
return;
|
|
}
|
|
if (!formData.nama) {
|
|
showAlert('error', 'Nama harus diisi');
|
|
return;
|
|
}
|
|
if (!formData.status) {
|
|
showAlert('error', 'Status harus dipilih');
|
|
return;
|
|
}
|
|
|
|
|
|
// Submit form directly without confirmation
|
|
submitForm(formData);
|
|
});
|
|
|
|
function submitForm(formData) {
|
|
let url = isEdit ?
|
|
"{{ route('manajemen.periode.update', ':id') }}".replace(':id', $('#periode_id').val()) :
|
|
"{{ route('manajemen.periode.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('periodeModal')).hide();
|
|
table.ajax.reload();
|
|
let message = response.message;
|
|
if (formData.status === '1') {
|
|
message += ' Periode lain telah dinonaktifkan.';
|
|
}
|
|
showAlert('success', 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');
|
|
}
|
|
});
|
|
}
|
|
|
|
// Debug: Handle submit button click directly
|
|
$('#submitBtn').on('click', function() {
|
|
console.log('Submit button clicked');
|
|
console.log('Button disabled?', $(this).prop('disabled'));
|
|
});
|
|
|
|
// Handle edit button
|
|
$(document).on('click', '.edit-btn', function() {
|
|
let id = $(this).data('id');
|
|
isEdit = true;
|
|
|
|
$.ajax({
|
|
url: "{{ route('manajemen.periode.show', ':id') }}".replace(':id', id),
|
|
type: 'GET',
|
|
success: function(data) {
|
|
$('#periode_id').val(data.id);
|
|
$('#id_periode').val(data.id_periode);
|
|
$('#nama').val(data.nama);
|
|
|
|
// Convert boolean to string for select option
|
|
let statusValue = data.status === true || data.status === 1 || data.status === '1' ? '1' : '0';
|
|
|
|
$('#periodeModalLabel').text('Edit Periode');
|
|
|
|
// Show modal first
|
|
let modal = new bootstrap.Modal(document.getElementById('periodeModal'));
|
|
modal.show();
|
|
|
|
// Set status after modal is shown and select2 is initialized
|
|
setTimeout(function() {
|
|
$('#status').val(statusValue).trigger('change');
|
|
}, 300);
|
|
},
|
|
error: function(xhr) {
|
|
console.log('Error loading data:', xhr);
|
|
showAlert('error', 'Gagal memuat data periode');
|
|
}
|
|
});
|
|
});
|
|
|
|
// Handle delete button
|
|
$(document).on('click', '.delete-btn', function() {
|
|
let id = $(this).data('id');
|
|
|
|
if (confirm('Apakah Anda yakin ingin menghapus periode ini?')) {
|
|
$.ajax({
|
|
url: "{{ route('manajemen.periode.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() {
|
|
showAlert('error', 'Terjadi kesalahan saat menghapus data');
|
|
}
|
|
});
|
|
}
|
|
});
|
|
});
|
|
|
|
function initializeSelect2() {
|
|
$('#status').select2({
|
|
allowClear: true,
|
|
placeholder: "Pilih Status",
|
|
width: '100%',
|
|
dropdownParent: $('#periodeModal')
|
|
});
|
|
}
|
|
|
|
function resetForm() {
|
|
isEdit = false;
|
|
$('#periodeForm')[0].reset();
|
|
$('#periode_id').val('');
|
|
$('#status').val(null).trigger('change'); // Reset select2 properly
|
|
$('#periodeModalLabel').text('Tambah Periode');
|
|
$('.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
|