MIF_E31220044/resources/views/admin/alternatif/index.blade.php

120 lines
5.1 KiB
PHP

@extends('layout.app')
@section('content')
<div class="card">
<div class="card-header d-flex justify-content-between align-items-center">
<h3>Tabel Alternatif</h3>
<button class="btn btn-primary" data-toggle="modal" data-target="#alternatifModal" onclick="clearForm()">Tambah Alternatif</button>
</div>
<div class="card-body">
<p>Data-data mengenai pupuk yang akan dievaluasi di representasikan dalam tabel berikut:</p>
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>No</th>
<th>Nama</th>
<th>Jenis</th>
<th>Bahan Utama</th>
<th>Harga</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
@foreach($alternatif as $key => $item)
<tr>
<td>{{ $key + 1 }}</td>
<td>{{ $item->name }}</td>
<td>{{ $item->jenis }}</td>
<td>{{ $item->bahan_utama }}</td>
<td>Rp {{ number_format($item->harga, 0, ',', '.') }}</td>
<td>
<button class="btn btn-warning btn-sm" onclick="editAlternatif({{ $item }})">Edit</button>
<form action="{{ route('admin.alternatif.destroy', $item->id) }}" method="POST" style="display: inline;">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Apakah Anda yakin ingin menghapus data ini?')">Hapus</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
{{-- Modal --}}
<div class="modal fade" id="alternatifModal" tabindex="-1" role="dialog" aria-labelledby="modalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="modalLabel">Tambah/Edit Alternatif</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<form id="alternatifForm" action="{{ route('admin.alternatif.store') }}" method="POST">
@csrf
<input type="hidden" name="_method" value="POST" id="methodInput">
<div class="form-group">
<label>Nama</label>
<input type="text" class="form-control" name="name" id="name" required>
</div>
<div class="form-group">
<label>Jenis</label>
<select class="form-control" name="jenis" id="jenis" required>
<option value="Padat">Padat</option>
<option value="Cair">Cair</option>
</select>
</div>
<div class="form-group">
<label>Bahan Utama</label>
<input type="text" class="form-control" name="bahan_utama" id="bahan_utama" required>
</div>
<div class="form-group">
<label>Harga</label>
<input type="number" class="form-control" name="harga" id="harga" required>
</div>
<div class="form-group mt-3">
<button type="submit" class="btn btn-primary" id="submitButton">Simpan</button>
</div>
</form>
</div>
</div>
</div>
</div>
@endsection
@push('scripts')
<script>
function clearForm() {
document.getElementById('alternatifForm').reset();
document.getElementById('methodInput').value = 'POST';
document.getElementById('modalLabel').innerText = 'Tambah Alternatif';
document.getElementById('submitButton').innerText = 'Simpan';
}
function editAlternatif(item) {
if (!item) {
console.error("Item tidak ditemukan");
return;
}
document.getElementById('name').value = item.name;
document.getElementById('jenis').value = item.jenis;
document.getElementById('bahan_utama').value = item.bahan_utama;
document.getElementById('harga').value = item.harga;
document.getElementById('methodInput').value = 'PUT';
document.getElementById('modalLabel').innerText = 'Edit Alternatif';
document.getElementById('submitButton').innerText = 'Update';
document.getElementById('alternatifForm').action = '/admin/alternatif/' + item.id;
$('#alternatifModal').modal('show');
}
</script>
@endpush