547 lines
19 KiB
PHP
547 lines
19 KiB
PHP
@extends('layout.main')
|
|
|
|
@section('content')
|
|
<div class="container-xxl flex-grow-1 container-p-y">
|
|
<div class="card">
|
|
<div class="card-body border-bottom">
|
|
<form method="GET" action="{{ route('varietas.index') }}" class="d-flex justify-content-between align-items-center row gap-3 gap-md-0">
|
|
<div class="col-md-auto">
|
|
<div class="d-flex align-items-center">
|
|
<label class="me-2 text-nowrap">Show</label>
|
|
<select name="per_page" class="form-select form-select-sm w-auto" onchange="this.form.submit()">
|
|
<option value="10" {{ request('per_page') == 10 ? 'selected' : '' }}>10</option>
|
|
<option value="25" {{ request('per_page') == 25 ? 'selected' : '' }}>25</option>
|
|
<option value="50" {{ request('per_page') == 50 ? 'selected' : '' }}>50</option>
|
|
<option value="100" {{ request('per_page') == 100 ? 'selected' : '' }}>100</option>
|
|
</select>
|
|
<label class="ms-2 text-nowrap">entries</label>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="col-md-auto d-flex gap-2">
|
|
<div class="input-group input-group-sm w-auto">
|
|
<input
|
|
type="text"
|
|
name="search"
|
|
class="form-control"
|
|
placeholder="Cari Varietas..."
|
|
value="{{ request('search') }}">
|
|
|
|
@if(request('search'))
|
|
<a href="{{ route('varietas.index', ['per_page' => request('per_page', 10)]) }}" class="btn btn-outline-secondary">
|
|
<i class="ti tabler-x"></i>
|
|
</a>
|
|
@endif
|
|
|
|
<button type="submit" class="btn btn-outline-primary">
|
|
<i class="ti tabler-search"></i>
|
|
</button>
|
|
</div>
|
|
|
|
<button
|
|
type="button"
|
|
class="btn btn-sm btn-primary add-new"
|
|
data-bs-toggle="offcanvas"
|
|
data-bs-target="#offcanvasAddVarietas">
|
|
<i class="ti tabler-plus me-1"></i> Tambah
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
|
|
<div class="table-responsive text-nowrap">
|
|
<table class="table table-hover">
|
|
<thead class="border-top">
|
|
<tr>
|
|
<th>No</th>
|
|
<th>Nama Varietas</th>
|
|
<th>Komoditas</th>
|
|
<th>Jenis Komoditas</th>
|
|
<th class="text-center">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
|
|
<tbody>
|
|
@forelse($varietas as $index => $item)
|
|
@php
|
|
$namaVarietas = $item->nama ?? '-';
|
|
$words = preg_split('/\s+/', trim($namaVarietas));
|
|
$initials = collect($words)
|
|
->filter()
|
|
->map(fn($word) => strtoupper(substr($word, 0, 1)))
|
|
->take(2)
|
|
->implode('');
|
|
|
|
$states = ['success', 'danger', 'warning', 'info', 'primary', 'secondary'];
|
|
$state = $states[$item->id % count($states)];
|
|
@endphp
|
|
|
|
<tr>
|
|
<td>{{ $varietas->firstItem() + $index }}</td>
|
|
|
|
<td>
|
|
<div class="d-flex justify-content-start align-items-center">
|
|
<div class="avatar-wrapper me-3">
|
|
<div class="avatar avatar-sm">
|
|
<span class="avatar-initial rounded-circle bg-label-{{ $state }}">
|
|
{{ $initials ?: 'VR' }}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="d-flex flex-column">
|
|
<span class="fw-medium text-heading">{{ $item->nama }}</span>
|
|
<small class="text-muted">{{ $item->deskripsi }}</small>
|
|
</div>
|
|
</div>
|
|
</td>
|
|
|
|
<td>
|
|
<span class="badge bg-label-secondary">
|
|
{{ $item->komoditas->nama ?? '-' }}
|
|
</span>
|
|
</td>
|
|
|
|
<td>
|
|
<span class="badge bg-label-info">
|
|
{{ $item->jenisKomoditas->nama ?? '-' }}
|
|
</span>
|
|
</td>
|
|
|
|
<td>
|
|
<div class="d-flex justify-content-center align-items-center">
|
|
<button
|
|
type="button"
|
|
class="btn btn-icon btn-text-secondary rounded-pill view-record"
|
|
data-id="{{ $item->id }}"
|
|
title="Lihat">
|
|
<i class="ti tabler-eye ti-md"></i>
|
|
</button>
|
|
|
|
<button
|
|
type="button"
|
|
class="btn btn-icon btn-text-secondary rounded-pill edit-record"
|
|
data-id="{{ $item->id }}"
|
|
title="Edit">
|
|
<i class="ti tabler-edit ti-md"></i>
|
|
</button>
|
|
|
|
<button
|
|
type="button"
|
|
class="btn btn-icon btn-text-secondary rounded-pill delete-record"
|
|
data-id="{{ $item->id }}"
|
|
data-nama="{{ $item->nama }}"
|
|
title="Hapus">
|
|
<i class="ti tabler-trash ti-md"></i>
|
|
</button>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
@empty
|
|
<tr>
|
|
<td colspan="6" class="text-center py-4">
|
|
Tidak ada data ditemukan.
|
|
</td>
|
|
</tr>
|
|
@endforelse
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
@if($varietas->hasPages())
|
|
<div class="card-footer d-flex justify-content-center justify-content-md-end border-top">
|
|
{{ $varietas->appends(request()->query())->links('pagination::bootstrap-5') }}
|
|
</div>
|
|
@endif
|
|
</div>
|
|
</div>
|
|
|
|
@include('master.varietas.add')
|
|
@include('master.varietas.edit')
|
|
@include('master.varietas.view')
|
|
@endsection
|
|
|
|
@push('scripts')
|
|
<script>
|
|
'use strict';
|
|
|
|
$(function () {
|
|
const csrfToken = "{{ csrf_token() }}";
|
|
const baseUrl = "{{ url('varietas') }}";
|
|
|
|
const offcanvasAdd = new bootstrap.Offcanvas(document.getElementById('offcanvasAddVarietas'));
|
|
const offcanvasEdit = new bootstrap.Offcanvas(document.getElementById('offcanvasEditVarietas'));
|
|
const offcanvasView = new bootstrap.Offcanvas(document.getElementById('offcanvasViewVarietas'));
|
|
|
|
$.ajaxSetup({
|
|
headers: {
|
|
'X-CSRF-TOKEN': csrfToken
|
|
}
|
|
});
|
|
|
|
$('.select2').each(function () {
|
|
const $this = $(this);
|
|
|
|
$this.wrap('<div class="position-relative"></div>').select2({
|
|
dropdownParent: $this.parent(),
|
|
placeholder: $this.data('placeholder') || 'Pilih data',
|
|
allowClear: true,
|
|
width: '100%'
|
|
});
|
|
});
|
|
|
|
$.validator.setDefaults({
|
|
errorElement: 'div',
|
|
errorClass: 'invalid-feedback',
|
|
highlight: function (element) {
|
|
$(element).addClass('is-invalid');
|
|
},
|
|
unhighlight: function (element) {
|
|
$(element).removeClass('is-invalid');
|
|
},
|
|
errorPlacement: function (error, element) {
|
|
if (element.hasClass('select2-hidden-accessible')) {
|
|
error.insertAfter(element.next('.select2-container'));
|
|
} else {
|
|
error.appendTo(element.closest('.form-control-validation'));
|
|
}
|
|
}
|
|
});
|
|
|
|
$('.select2').on('change', function () {
|
|
const $form = $(this).closest('form');
|
|
|
|
if (!$form.length) return;
|
|
if (!$form.data('validator')) return;
|
|
|
|
$(this).valid();
|
|
});
|
|
|
|
function showSuccess(title = 'Berhasil!', text = 'Data berhasil diproses') {
|
|
return Swal.fire({
|
|
icon: 'success',
|
|
title,
|
|
text,
|
|
showConfirmButton: false,
|
|
timer: 1500,
|
|
customClass: {
|
|
confirmButton: 'btn btn-primary'
|
|
},
|
|
buttonsStyling: false
|
|
});
|
|
}
|
|
|
|
function showError(xhr, fallback = 'Terjadi kesalahan sistem.') {
|
|
let message = fallback;
|
|
|
|
if (xhr.responseJSON?.message) {
|
|
message = xhr.responseJSON.message;
|
|
}
|
|
|
|
if (xhr.responseJSON?.errors) {
|
|
message = Object.values(xhr.responseJSON.errors).flat().join('<br>');
|
|
}
|
|
|
|
return Swal.fire({
|
|
icon: 'error',
|
|
title: 'Gagal!',
|
|
html: message,
|
|
customClass: {
|
|
confirmButton: 'btn btn-primary'
|
|
},
|
|
buttonsStyling: false
|
|
});
|
|
}
|
|
|
|
function setButtonLoading($button, isLoading, text = 'Menyimpan...') {
|
|
if (isLoading) {
|
|
$button
|
|
.data('original-text', $button.html())
|
|
.prop('disabled', true)
|
|
.html(`<span class="spinner-border spinner-border-sm me-1"></span>${text}`);
|
|
} else {
|
|
$button
|
|
.prop('disabled', false)
|
|
.html($button.data('original-text'));
|
|
}
|
|
}
|
|
|
|
function resetForm($form) {
|
|
if (!$form.length) return;
|
|
|
|
$form[0].reset();
|
|
|
|
if ($form.data('validator')) {
|
|
$form.validate().resetForm();
|
|
}
|
|
|
|
$form.find('.is-invalid').removeClass('is-invalid');
|
|
|
|
$form.find('.select2').each(function () {
|
|
$(this).val('').trigger('change.select2');
|
|
});
|
|
}
|
|
|
|
function loadJenisKomoditas(komoditasId, targetSelector, selectedId = null) {
|
|
const $target = $(targetSelector);
|
|
|
|
$target.empty().append('<option value=""></option>').trigger('change.select2');
|
|
|
|
if (!komoditasId) return;
|
|
|
|
$.ajax({
|
|
url: `/jenis-komoditas-by-komoditas/${komoditasId}`,
|
|
type: 'GET',
|
|
success: function (res) {
|
|
const data = res.data || [];
|
|
|
|
data.forEach(function (item) {
|
|
const option = new Option(item.nama, item.id, false, false);
|
|
$target.append(option);
|
|
});
|
|
|
|
if (selectedId) {
|
|
$target.val(selectedId);
|
|
}
|
|
|
|
$target.trigger('change.select2');
|
|
},
|
|
error: function (xhr) {
|
|
showError(xhr, 'Gagal mengambil data jenis komoditas.');
|
|
}
|
|
});
|
|
}
|
|
|
|
$('#add-varietas-komoditas').on('change', function () {
|
|
const komoditasId = $(this).val();
|
|
loadJenisKomoditas(komoditasId, '#add-varietas-jenis-komoditas');
|
|
});
|
|
|
|
$('#edit-varietas-komoditas').on('change', function () {
|
|
const komoditasId = $(this).val();
|
|
loadJenisKomoditas(komoditasId, '#edit-varietas-jenis-komoditas');
|
|
});
|
|
|
|
$('#addNewVarietasForm').validate({
|
|
rules: {
|
|
komoditas_id: {
|
|
required: true
|
|
},
|
|
jenis_komoditas_id: {
|
|
required: true
|
|
},
|
|
nama: {
|
|
required: true,
|
|
maxlength: 255
|
|
},
|
|
deskripsi: {
|
|
maxlength: 10000
|
|
}
|
|
},
|
|
messages: {
|
|
komoditas_id: {
|
|
required: 'Komoditas wajib dipilih'
|
|
},
|
|
jenis_komoditas_id: {
|
|
required: 'Jenis komoditas wajib dipilih'
|
|
},
|
|
nama: {
|
|
required: 'Nama varietas wajib diisi',
|
|
maxlength: 'Nama maksimal 255 karakter'
|
|
},
|
|
deskripsi: {
|
|
maxlength: 'Deskripsi terlalu panjang'
|
|
}
|
|
},
|
|
submitHandler: function (form, event) {
|
|
event.preventDefault();
|
|
|
|
const $form = $(form);
|
|
const $button = $form.find('button[type="submit"]');
|
|
|
|
setButtonLoading($button, true);
|
|
|
|
$.ajax({
|
|
url: $form.attr('action'),
|
|
type: 'POST',
|
|
data: $form.serialize(),
|
|
success: function (res) {
|
|
offcanvasAdd.hide();
|
|
|
|
showSuccess(
|
|
'Berhasil!',
|
|
res.message || 'Data varietas berhasil disimpan'
|
|
).then(() => window.location.reload());
|
|
},
|
|
error: function (xhr) {
|
|
showError(xhr, 'Gagal menyimpan data varietas');
|
|
},
|
|
complete: function () {
|
|
setButtonLoading($button, false);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
$('#offcanvasAddVarietas').on('hidden.bs.offcanvas', function () {
|
|
resetForm($('#addNewVarietasForm'));
|
|
$('#add-varietas-jenis-komoditas').empty().append('<option value=""></option>').trigger('change.select2');
|
|
});
|
|
|
|
$(document).on('click', '.view-record', function () {
|
|
const id = $(this).data('id');
|
|
|
|
$.ajax({
|
|
url: `${baseUrl}/${id}`,
|
|
type: 'GET',
|
|
success: function (res) {
|
|
const data = res.data || res;
|
|
|
|
$('#view-varietas-komoditas').val(data.komoditas?.nama || '-');
|
|
$('#view-varietas-jenis-komoditas').val(data.jenis_komoditas?.nama || data.jenis_komoditas?.nama || data.jenisKomoditas?.nama || '-');
|
|
$('#view-varietas-name').val(data.nama || '-');
|
|
$('#view-varietas-description').val(data.deskripsi || '-');
|
|
|
|
offcanvasView.show();
|
|
},
|
|
error: function (xhr) {
|
|
showError(xhr, 'Terjadi kesalahan saat mengambil detail data.');
|
|
}
|
|
});
|
|
});
|
|
|
|
$(document).on('click', '.edit-record', function () {
|
|
const id = $(this).data('id');
|
|
const $form = $('#editVarietasForm');
|
|
|
|
resetForm($form);
|
|
|
|
$.ajax({
|
|
url: `${baseUrl}/${id}`,
|
|
type: 'GET',
|
|
success: function (res) {
|
|
const data = res.data || res;
|
|
|
|
$('#edit-varietas-id').val(data.id);
|
|
$('#edit-varietas-komoditas').val(data.komoditas_id).trigger('change.select2');
|
|
$('#edit-varietas-name').val(data.nama || '');
|
|
$('#edit-varietas-description').val(data.deskripsi || '');
|
|
|
|
loadJenisKomoditas(
|
|
data.komoditas_id,
|
|
'#edit-varietas-jenis-komoditas',
|
|
data.jenis_komoditas_id
|
|
);
|
|
|
|
offcanvasEdit.show();
|
|
},
|
|
error: function (xhr) {
|
|
showError(xhr, 'Terjadi kesalahan saat mengambil data edit.');
|
|
}
|
|
});
|
|
});
|
|
|
|
$('#editVarietasForm').validate({
|
|
rules: {
|
|
komoditas_id: {
|
|
required: true
|
|
},
|
|
jenis_komoditas_id: {
|
|
required: true
|
|
},
|
|
nama: {
|
|
required: true,
|
|
maxlength: 255
|
|
},
|
|
deskripsi: {
|
|
maxlength: 10000
|
|
}
|
|
},
|
|
messages: {
|
|
komoditas_id: {
|
|
required: 'Komoditas wajib dipilih'
|
|
},
|
|
jenis_komoditas_id: {
|
|
required: 'Jenis komoditas wajib dipilih'
|
|
},
|
|
nama: {
|
|
required: 'Nama varietas wajib diisi',
|
|
maxlength: 'Nama maksimal 255 karakter'
|
|
},
|
|
deskripsi: {
|
|
maxlength: 'Deskripsi terlalu panjang'
|
|
}
|
|
},
|
|
submitHandler: function (form, event) {
|
|
event.preventDefault();
|
|
|
|
const $form = $(form);
|
|
const id = $('#edit-varietas-id').val();
|
|
const $button = $form.find('button[type="submit"]');
|
|
|
|
setButtonLoading($button, true, 'Mengupdate...');
|
|
|
|
$.ajax({
|
|
url: `${baseUrl}/${id}`,
|
|
type: 'POST',
|
|
data: $form.serialize(),
|
|
success: function (res) {
|
|
offcanvasEdit.hide();
|
|
|
|
showSuccess(
|
|
'Updated!',
|
|
res.message || 'Data varietas berhasil diperbarui'
|
|
).then(() => window.location.reload());
|
|
},
|
|
error: function (xhr) {
|
|
showError(xhr, 'Gagal mengupdate data varietas');
|
|
},
|
|
complete: function () {
|
|
setButtonLoading($button, false);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
$('#offcanvasEditVarietas').on('hidden.bs.offcanvas', function () {
|
|
resetForm($('#editVarietasForm'));
|
|
$('#edit-varietas-jenis-komoditas').empty().append('<option value=""></option>').trigger('change.select2');
|
|
});
|
|
|
|
$(document).on('click', '.delete-record', function () {
|
|
const id = $(this).data('id');
|
|
const nama = $(this).data('nama') || 'data ini';
|
|
|
|
Swal.fire({
|
|
title: 'Apakah Anda yakin?',
|
|
text: `Varietas "${nama}" akan dihapus secara permanen.`,
|
|
icon: 'warning',
|
|
showCancelButton: true,
|
|
confirmButtonText: 'Ya, Hapus!',
|
|
cancelButtonText: 'Batal',
|
|
customClass: {
|
|
confirmButton: 'btn btn-primary me-3',
|
|
cancelButton: 'btn btn-label-secondary'
|
|
},
|
|
buttonsStyling: false
|
|
}).then(function (result) {
|
|
if (!result.isConfirmed) return;
|
|
|
|
$.ajax({
|
|
url: `${baseUrl}/${id}`,
|
|
type: 'DELETE',
|
|
success: function (res) {
|
|
showSuccess(
|
|
'Terhapus!',
|
|
res.message || 'Data varietas berhasil dihapus'
|
|
).then(() => window.location.reload());
|
|
},
|
|
error: function (xhr) {
|
|
showError(xhr, 'Gagal menghapus data varietas');
|
|
}
|
|
});
|
|
});
|
|
});
|
|
});
|
|
</script>
|
|
@endpush |