SPK-Lahan-Tanaman-Cabai-SAW/resources/views/admin/usersetting.blade.php

323 lines
19 KiB
PHP

<x-layout>
<x-slot:title>{{ $title }}</x-slot>
<button data-modal-target="tambahUserModal" data-modal-toggle="tambahUserModal" class="text-white bg-[#388E3C] hover:bg-green-900 focus:ring-4 focus:ring-green-300 font-medium rounded-lg text-sm px-5 py-2.5 me-2 mb-5">
Tambah User
</button>
@if(auth()->user()->role === 'super_admin')
<a href="{{ route('kelompoktani.index') }}"
class="text-white bg-[#388E3C] hover:bg-green-900 focus:ring-4 focus:ring-green-300 font-medium rounded-lg text-sm px-5 py-2.5 me-2 mb-5 inline-block">
Tambah Kelompok Tani
</a>
@endif
@if ($errors->any())
<div class="text-red-500">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<div class="flex items-center justify-center">
{{-- Tabel Data Kriteria --}}
<div class="overflow-x-auto w-full">
<table class="w-full text-sm text-left text-[#1B5E20] border border-[#C8E6C9]">
<thead class="text-xs text-white uppercase bg-[#388E3C]">
<tr>
<th class="px-6 py-3" scope="col">Kelompok Tani</th>
<th class="px-6 py-3" scope="col">ID</th>
<th class="px-6 py-3" scope="col">Name</th>
<th class="px-6 py-3" scope="col">Username</th>
<th class="px-6 py-3" scope="col">Password</th>
<th class="px-6 py-3" scope="col">Role</th>
<th class="px-6 py-3" scope="col">Aksi</th>
</tr>
</thead>
<tbody>
@forelse ($users as $index => $user)
<tr class="bg-[#f9f9f9] border-b border-[#ddd] hover:bg-[#e2e2e2]">
<td class="px-6 py-4">
{{ $user->kelompokTani->isNotEmpty() ? $user->kelompokTani->pluck('nama')->join(', ') : '-' }}
</td>
<td class="px-6 py-4">{{ $user->id }}</td>
<td class="px-6 py-4">{{ $user->name }}</td>
<td class="px-6 py-4">{{ $user->username }}</td>
<td class="px-6 py-4">••••••</td>
<td class="px-6 py-4">{{ $user->role }}</td>
<td class="px-4 py-3">
<!-- Tombol Edit -->
<button type="button"
data-modal-target="editUserModal{{ $user->id }}"
data-modal-toggle="editUserModal{{ $user->id }}"
class="bg-white border border-blue-600 p-1 rounded cursor-pointer
text-blue-600 hover:bg-blue-600 hover:text-white transition-colors
inline-flex items-center justify-center">
<svg class="w-6 h-6" aria-hidden="true" fill="currentColor" viewBox="0 0 24 24">
<path fill-rule="evenodd"
d="M11.32 6.176H5c-1.105 0-2 .949-2 2.118v10.588C3 20.052 3.895 21 5 21h11c1.105 0 2-.948 2-2.118v-7.75l-3.914 4.144A2.46 2.46 0 0 1 12.81 16l-2.681.568c-1.75.37-3.292-1.263-2.942-3.115l.536-2.839c.097-.512.335-.983.684-1.352l2.914-3.086Z"
clip-rule="evenodd" />
<path fill-rule="evenodd"
d="M19.846 4.318a2.148 2.148 0 0 0-.437-.692 2.014 2.014 0 0 0-.654-.463 1.92 1.92 0 0 0-1.544 0 2.014 2.014 0 0 0-.654.463l-.546.578 2.852 3.02.546-.579a2.14 2.14 0 0 0 .437-.692 2.244 2.244 0 0 0 0-1.635ZM17.45 8.721 14.597 5.7 9.82 10.76a.54.54 0 0 0-.137.27l-.536 2.84c-.07.37.239.696.588.622l2.682-.567a.492.492 0 0 0 .255-.145l4.778-5.06Z"
clip-rule="evenodd" />
</svg>
</button>
@php
$auth = auth()->user();
@endphp
@if (
$auth->id !== $user->id && // Tidak bisa hapus diri sendiri
(
$auth->role === 'super_admin' || // super_admin bisa hapus siapa saja kecuali dirinya
($auth->role === 'admin' && $user->role !== 'admin') // admin hanya boleh hapus user biasa
)
)
<!-- Tombol Hapus -->
<button type="button"
data-modal-target="hapusUserModal{{ $user->id }}"
data-modal-toggle="hapusUserModal{{ $user->id }}"
class="bg-white border border-red-600 p-1 rounded cursor-pointer
text-red-600 hover:bg-red-600 hover:text-white transition-colors">
<svg class="w-6 h-6" aria-hidden="true" fill="currentColor" viewBox="0 0 24 24">
<path fill-rule="evenodd"
d="M8.586 2.586A2 2 0 0 1 10 2h4a2 2 0 0 1 2 2v2h3a1 1 0 1 1 0 2v12a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V8a1 1 0 0 1 0-2h3V4a2 2 0 0 1 .586-1.414ZM10 6h4V4h-4v2Zm1 4a1 1 0 1 0-2 0v8a1 1 0 1 0 2 0v-8Zm4 0a1 1 0 1 0-2 0v8a1 1 0 1 0 2 0v-8Z"
clip-rule="evenodd" />
</svg>
</button>
@endif
</td>
</tr>
@empty
<tr>
<td colspan="7" class="text-center py-4 text-gray-500">Tidak ada data User.</td>
</tr>
@endforelse
</tbody>
</table>
</div>
</div>
@if(session('success'))
<div id="success-alert" class="flex items-center p-4 mb-4 text-sm text-green-800 border border-green-300 rounded-lg bg-green-50 dark:bg-gray-800 dark:text-green-400 dark:border-green-800 mt-4" role="alert">
<svg class="shrink-0 inline w-4 h-4 me-3" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 20 20">
<path d="M10 .5a9.5 9.5 0 1 0 9.5 9.5A9.51 9.51 0 0 0 10 .5ZM9.5 4a1.5 1.5 0 1 1 0 3 1.5 1.5 0 0 1 0-3ZM12 15H8a1 1 0 0 1 0-2h1v-3H8a1 1 0 0 1 0-2h2a1 1 0 0 1 1 1v4h1a1 1 0 0 1 0 2Z"/>
</svg>
<span class="sr-only">Info</span>
<div>
<span class="font-medium">{{ session('success') }}</span>
</div>
</div>
<script>
setTimeout(function() {
// Fade out the success alert after 3 seconds
document.getElementById('success-alert').style.transition = 'opacity 1s';
document.getElementById('success-alert').style.opacity = '0';
// After fade out, remove the alert from the DOM
setTimeout(function() {
document.getElementById('success-alert').remove();
}, 1000); // Wait for fade-out transition
}, 2000); // 3 seconds delay before fading out
</script>
@endif
<!-- Modal Tambah User -->
<div id="tambahUserModal" tabindex="-1" aria-hidden="true" class="hidden overflow-y-auto overflow-x-hidden fixed top-0 right-0 left-0 z-50 w-full inset-0 h-[calc(100%-1rem)] max-h-full items-center justify-center bg-black/30">
<div class="relative p-4 w-full max-w-2xl max-h-full">
<!-- Modal content -->
<div class="relative bg-[#388E3C] rounded-lg shadow">
<!-- Header -->
<div class="flex items-center justify-between p-4 border-b rounded-t border-gray-600">
<h3 class="text-xl font-semibold text-white">
Tambah Pengguna
</h3>
<button type="button" class="text-gray-400 bg-transparent hover:text-white" data-modal-hide="tambahUserModal">
<svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20">
<path fill-rule="evenodd"
d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z"
clip-rule="evenodd" />
</svg>
</button>
</div>
<!-- Form -->
<form action="{{ route('usersetting.store') }}" method="POST" class="p-6 space-y-6">
@csrf
<div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
<div>
<label for="name" class="block mb-2 text-sm font-medium text-white">Nama</label>
<input type="text" id="name" name="name" required
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg
focus:ring-green-500 focus:border-green-500 block w-full p-2.5" placeholder="Budi">
</div>
<div>
<label for="username" class="block mb-2 text-sm font-medium text-white">Username</label>
<input type="text" id="username" name="username" required
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg
focus:ring-green-500 focus:border-green-500 block w-full p-2.5" placeholder="budi123">
</div>
</div>
<div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
<div>
<label for="password" class="block mb-2 text-sm font-medium text-white">Password</label>
<input type="text" id="password" name="password" required
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg
focus:ring-green-500 focus:border-green-500 block w-full p-2.5" placeholder="bud1b3b3k">
</div>
<div>
<label for="role" class="block mb-2 text-sm font-medium text-white">Role</label>
<select id="role" name="role" class="bg-white border border-gray-300 text-black text-sm rounded-lg block w-full p-2.5">
<option value="admin">admin</option>
<option value="user">user</option>
</select>
</div>
</div>
@if(auth()->user()->role === 'super_admin')
<div>
<label for="kelompok_tani_id" class="block mb-2 text-sm font-medium text-white">Kelompok Tani</label>
<select id="kelompok_tani_id" name="kelompok_tani_id" required
class="bg-white border border-gray-300 text-black text-sm rounded-lg block w-full p-2.5">
<option value="">-- Pilih Kelompok Tani --</option>
@foreach ($kelompokTani as $kt)
<option value="{{ $kt->id }}">{{ $kt->nama }}</option>
@endforeach
</select>
</div>
@endif
<button type="submit"
class="text-white bg-green-900 hover:bg-green-950 font-medium rounded-lg text-sm px-5 py-2.5 text-center">
Simpan
</button>
</form>
</div>
</div>
</div>
@foreach ($users as $user)
<div id="hapusUserModal{{ $user->id }}" tabindex="-1" aria-hidden="true"
class="hidden fixed top-0 left-0 right-0 z-50 w-full p-4 overflow-x-hidden overflow-y-auto md:inset-0 h-modal md:h-full bg-black/30 flex items-center justify-center">
<div class="relative w-full max-w-md h-full md:h-auto">
<div class="relative bg-[#388E3C] rounded-lg shadow">
<div class="p-6 text-center">
<svg class="mx-auto mb-4 w-12 h-12 text-gray-300"
fill="none" stroke="currentColor" viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M6 18L18 6M6 6l12 12"></path>
</svg>
<h3 class="mb-5 text-lg font-normal text-white">
Yakin ingin menghapus <strong>{{ $user->name }}</strong>?
</h3>
<form action="{{ route('usersetting.destroy', $user->id) }}" method="POST">
@csrf
@method('DELETE')
<button type="submit"
class="text-white bg-red-600 hover:bg-red-800 focus:ring-4 focus:outline-none focus:ring-red-300
font-medium rounded-lg text-sm inline-flex items-center px-5 py-2.5 text-center mr-2">
Ya, Hapus
</button>
<button type="button"
class="text-black bg-white hover:bg-gray-300 focus:ring-4 focus:outline-none focus:ring-gray-200
rounded-lg border border-gray-200 text-sm font-medium px-5 py-2.5 hover:text-gray-900
focus:z-10"
data-modal-hide="hapusUserModal{{ $user->id }}">
Batal
</button>
</form>
</div>
</div>
</div>
</div>
@endforeach
@foreach ($users as $user)
<div id="editUserModal{{ $user->id }}" tabindex="-1" aria-hidden="true"
class="hidden overflow-y-auto overflow-x-hidden fixed top-0 right-0 left-0 z-50 w-full inset-0 h-[calc(100%-1rem)] max-h-full flex items-center justify-center bg-black/30">
<div class="relative p-4 w-full max-w-2xl max-h-full">
<div class="relative bg-[#388E3C] rounded-lg shadow">
<!-- Header -->
<div class="flex items-center justify-between p-4 border-b rounded-t border-gray-600">
<h3 class="text-xl font-semibold text-white">
Edit Pengguna
</h3>
<button type="button" class="text-gray-400 bg-transparent hover:text-white"
data-modal-hide="editUserModal{{ $user->id }}">
<svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20">
<path fill-rule="evenodd"
d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z"
clip-rule="evenodd" />
</svg>
</button>
</div>
<!-- Form -->
<form action="{{ route('usersetting.update', $user->id) }}" method="POST" class="p-6 space-y-6">
@csrf
@method('PUT')
<div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
<div>
<label for="name{{ $user->id }}" class="block mb-2 text-sm font-medium text-white">Nama</label>
<input type="text" id="name{{ $user->id }}" name="name" value="{{ $user->name }}" required
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg
focus:ring-green-500 focus:border-green-500 block w-full p-2.5">
</div>
<div>
<label for="username{{ $user->id }}" class="block mb-2 text-sm font-medium text-white">Username</label>
<input type="text" id="username{{ $user->id }}" name="username" value="{{ $user->username }}" required
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg
focus:ring-green-500 focus:border-green-500 block w-full p-2.5">
</div>
<div>
<label for="password{{ $user->id }}" class="block mb-2 text-sm font-medium text-white">Password (Opsional)</label>
<input type="password" id="password{{ $user->id }}" name="password"
placeholder="Kosongkan jika tidak ingin mengubah"
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg
focus:ring-green-500 focus:border-green-500 block w-full p-2.5">
</div>
<div>
<label for="role{{ $user->id }}" class="block mb-2 text-sm font-medium text-white">Role</label>
<select id="role{{ $user->id }}" name="role" required
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg
focus:ring-green-500 focus:border-green-500 block w-full p-2.5">
<option value="admin" {{ $user->role == 'admin' ? 'selected' : '' }}>admin</option>
<option value="user" {{ $user->role == 'user' ? 'selected' : '' }}>user</option>
</select>
</div>
</div>
<div>
<label for="kelompok_tani_id{{ $user->id }}" class="block mb-2 text-sm font-medium text-white">Kelompok Tani</label>
<select id="kelompok_tani_id{{ $user->id }}" name="kelompok_tani_id" required
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg
focus:ring-green-500 focus:border-green-500 block w-full p-2.5">
<option disabled selected>Pilih Kelompok Tani</option>
@foreach($kelompokTani as $kelompok)
<option value="{{ $kelompok->id }}"
@php
$userKelompokId = $user->memberKelompok->kelompok_tani_id ?? null;
@endphp
{{ $userKelompokId == $kelompok->id ? 'selected' : '' }}>
{{ $kelompok->nama }}
</option>
@endforeach
</select>
</div>
<button type="submit"
class="text-white bg-green-900 hover:bg-green-950 font-medium rounded-lg text-sm px-5 py-2.5 text-center">
Simpan Perubahan
</button>
</form>
</div>
</div>
</div>
@endforeach
</x-layout>