509 lines
18 KiB
PHP
509 lines
18 KiB
PHP
@extends('layout.main')
|
|
|
|
@section('content')
|
|
<div class="container-xxl flex-grow-1 container-p-y">
|
|
|
|
<div class="row g-4 mb-4">
|
|
<div class="col-sm-6 col-md-4 col-xl-3">
|
|
<x-summary-card
|
|
title="Total Jenis Komoditas"
|
|
count="{{ $totalJenisKomoditas ?? 0 }}"
|
|
subtitle="Keseluruhan jenis komoditas"
|
|
icon="box-multiple"
|
|
color="primary"
|
|
/>
|
|
</div>
|
|
|
|
<div class="col-sm-6 col-md-4 col-xl-3">
|
|
<x-summary-card
|
|
title="Total Induk Komoditas"
|
|
count="{{ $totalIndukKomoditas ?? 0 }}"
|
|
subtitle="Kategori induk terpakai"
|
|
icon="category"
|
|
color="success"
|
|
/>
|
|
</div>
|
|
</div>
|
|
<div class="card">
|
|
<div class="card-header border-bottom">
|
|
<div class="d-flex flex-column flex-md-row justify-content-between align-items-md-center gap-3">
|
|
<div>
|
|
<h5 class="card-title mb-0">Jenis Komoditas</h5>
|
|
<small class="text-muted">Kelola data jenis komoditas</small>
|
|
</div>
|
|
|
|
<button
|
|
type="button"
|
|
class="btn btn-primary"
|
|
data-bs-toggle="offcanvas"
|
|
data-bs-target="#offcanvasAddJenisKomoditas">
|
|
<i class="ti tabler-plus me-1"></i>
|
|
Tambah Jenis Komoditas
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card-body border-bottom">
|
|
<form
|
|
method="GET"
|
|
action="{{ route('jenis-komoditas.index') }}"
|
|
class="row g-3 align-items-center justify-content-between">
|
|
|
|
<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()">
|
|
@foreach([10, 25, 50, 100] as $limit)
|
|
<option value="{{ $limit }}" {{ (int) request('per_page', 10) === $limit ? 'selected' : '' }}>
|
|
{{ $limit }}
|
|
</option>
|
|
@endforeach
|
|
</select>
|
|
<label class="ms-2 text-nowrap">entries</label>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="col-md-auto">
|
|
<div class="input-group input-group-sm">
|
|
<input
|
|
type="text"
|
|
name="search"
|
|
class="form-control"
|
|
placeholder="Cari jenis komoditas..."
|
|
value="{{ request('search') }}">
|
|
|
|
@if(request('search'))
|
|
<a href="{{ route('jenis-komoditas.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>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
|
|
<div class="table-responsive text-nowrap">
|
|
<table class="table table-hover"></table>
|
|
|
|
<div class="table-responsive text-nowrap">
|
|
<table class="table table-hover">
|
|
<thead class="border-top">
|
|
<tr>
|
|
<th style="width: 70px;">No</th>
|
|
<th>Nama Jenis Komoditas</th>
|
|
<th>Komoditas</th>
|
|
<th class="text-center" style="width: 160px;">Aksi</th>
|
|
</tr>
|
|
</thead>
|
|
|
|
<tbody>
|
|
@forelse($jenisKomoditas as $index => $jenis)
|
|
@php
|
|
$komoditasNama = $jenis->komoditas->nama ?? '-';
|
|
$words = preg_split('/\s+/', trim($komoditasNama));
|
|
$initials = collect($words)
|
|
->filter()
|
|
->map(fn($word) => strtoupper(substr($word, 0, 1)))
|
|
->take(2)
|
|
->implode('');
|
|
|
|
$states = ['success', 'danger', 'warning', 'info', 'primary', 'secondary'];
|
|
$state = $states[$jenis->id % count($states)];
|
|
@endphp
|
|
|
|
<tr>
|
|
<td>{{ $jenisKomoditas->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 ?: 'JK' }}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="d-flex flex-column">
|
|
<span class="fw-medium text-heading">{{ $jenis->nama }}</span>
|
|
<small class="text-muted">ID: {{ $jenis->id }}</small>
|
|
</div>
|
|
</div>
|
|
</td>
|
|
|
|
<td>
|
|
<span class="badge bg-label-secondary">
|
|
{{ $komoditasNama }}
|
|
</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="{{ $jenis->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="{{ $jenis->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="{{ $jenis->id }}"
|
|
data-nama="{{ $jenis->nama }}"
|
|
title="Hapus">
|
|
<i class="ti tabler-trash ti-md"></i>
|
|
</button>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
@empty
|
|
<tr>
|
|
<td colspan="5" class="text-center py-5">
|
|
<div class="d-flex flex-column align-items-center">
|
|
<i class="ti tabler-database-off ti-lg mb-2 text-muted"></i>
|
|
<span class="text-muted">Tidak ada data ditemukan.</span>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
@endforelse
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
@if($jenisKomoditas->hasPages())
|
|
<div class="card-footer d-flex justify-content-center justify-content-md-end border-top">
|
|
{{ $jenisKomoditas->appends(request()->query())->links('pagination::bootstrap-5') }}
|
|
</div>
|
|
@endif
|
|
</div>
|
|
</div>
|
|
|
|
@include('master.jeniskomoditas.add')
|
|
@include('master.jeniskomoditas.edit')
|
|
@include('master.jeniskomoditas.view')
|
|
@endsection
|
|
|
|
|
|
@push('scripts')
|
|
<script>
|
|
'use strict';
|
|
|
|
$(function () {
|
|
const csrfToken = "{{ csrf_token() }}";
|
|
const baseUrl = "{{ url('jenis-komoditas') }}";
|
|
|
|
const offcanvasAdd = new bootstrap.Offcanvas(document.getElementById('offcanvasAddJenisKomoditas'));
|
|
const offcanvasEdit = new bootstrap.Offcanvas(document.getElementById('offcanvasEditJenisKomoditas'));
|
|
const offcanvasView = new bootstrap.Offcanvas(document.getElementById('offcanvasViewJenisKomoditas'));
|
|
|
|
$.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.data('validator')) {
|
|
$(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 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 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'));
|
|
}
|
|
}
|
|
|
|
$('#addNewJenisKomoditasForm').validate({
|
|
rules: {
|
|
komoditas_id: {
|
|
required: true
|
|
},
|
|
nama: {
|
|
required: true,
|
|
maxlength: 255
|
|
}
|
|
},
|
|
messages: {
|
|
komoditas_id: {
|
|
required: 'Komoditas wajib dipilih'
|
|
},
|
|
nama: {
|
|
required: 'Nama jenis komoditas wajib diisi',
|
|
maxlength: 'Nama maksimal 255 karakter'
|
|
}
|
|
},
|
|
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 jenis komoditas berhasil disimpan'
|
|
).then(() => window.location.reload());
|
|
},
|
|
error: function (xhr) {
|
|
showError(xhr, 'Gagal menyimpan data jenis komoditas');
|
|
},
|
|
complete: function () {
|
|
setButtonLoading($button, false);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
$('#offcanvasAddJenisKomoditas').on('hidden.bs.offcanvas', function () {
|
|
resetForm($('#addNewJenisKomoditasForm'));
|
|
});
|
|
|
|
$(document).on('click', '.edit-record', function () {
|
|
const id = $(this).data('id');
|
|
const $form = $('#editJenisKomoditasForm');
|
|
|
|
resetForm($form);
|
|
|
|
$.ajax({
|
|
url: `${baseUrl}/${id}`,
|
|
type: 'GET',
|
|
success: function (res) {
|
|
const data = res.data || res;
|
|
|
|
$('#edit-jenis-komoditas-id').val(data.id);
|
|
$('#edit-jenis-komoditas-komoditas').val(data.komoditas_id).trigger('change');
|
|
$('#edit-jenis-komoditas-name').val(data.nama);
|
|
|
|
offcanvasEdit.show();
|
|
},
|
|
error: function (xhr) {
|
|
showError(xhr, 'Terjadi kesalahan saat mengambil data edit');
|
|
}
|
|
});
|
|
});
|
|
|
|
$('#editJenisKomoditasForm').validate({
|
|
rules: {
|
|
komoditas_id: {
|
|
required: true
|
|
},
|
|
nama: {
|
|
required: true,
|
|
maxlength: 255
|
|
}
|
|
},
|
|
messages: {
|
|
komoditas_id: {
|
|
required: 'Komoditas wajib dipilih'
|
|
},
|
|
nama: {
|
|
required: 'Nama jenis komoditas wajib diisi',
|
|
maxlength: 'Nama maksimal 255 karakter'
|
|
}
|
|
},
|
|
submitHandler: function (form, event) {
|
|
event.preventDefault();
|
|
|
|
const $form = $(form);
|
|
const id = $('#edit-jenis-komoditas-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 jenis komoditas berhasil diperbarui'
|
|
).then(() => window.location.reload());
|
|
},
|
|
error: function (xhr) {
|
|
showError(xhr, 'Gagal mengupdate data jenis komoditas');
|
|
},
|
|
complete: function () {
|
|
setButtonLoading($button, false);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
$('#offcanvasEditJenisKomoditas').on('hidden.bs.offcanvas', function () {
|
|
resetForm($('#editJenisKomoditasForm'));
|
|
});
|
|
|
|
$(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-jenis-komoditas-komoditas').val(data.komoditas?.nama || data.nama_komoditas || '-');
|
|
$('#view-jenis-komoditas-name').val(data.nama || '-');
|
|
|
|
offcanvasView.show();
|
|
},
|
|
error: function (xhr) {
|
|
showError(xhr, 'Terjadi kesalahan saat mengambil detail data');
|
|
}
|
|
});
|
|
});
|
|
|
|
$(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: `Jenis Komoditas "${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 jenis komoditas berhasil dihapus'
|
|
).then(() => window.location.reload());
|
|
},
|
|
error: function (xhr) {
|
|
showError(xhr, 'Gagal menghapus data jenis komoditas');
|
|
}
|
|
});
|
|
});
|
|
});
|
|
});
|
|
</script>
|
|
@endpush |