MIF_E31211908/resources/views/page/dashboard/user/index.blade.php

239 lines
12 KiB
PHP

@extends('layout.app')
@section('content')
<div id="modal" class="modal hidden fixed inset-0 z-50 overflow-auto bg-gray-500 bg-opacity-50 flex justify-center items-center">
<div class="modal-content bg-white w-1/2 p-4 rounded-lg">
<div class="flex justify-between border-b-2 border-[#ee69b1] p-2 mb-2">
<h2 class="text-xl font-bold">Tambah {{$data['title'] }}</h2>
<button id="closeModalButton" class="text-red-500 hover:text-red-700">&times;</button>
</div>
<div class="modal-body">
<form id="formData">
@csrf
<div class="mb-6">
<label for="name" class="block mb-2 text-sm font-medium text-gray-900">Nama Lengkap</label>
<input type="text" id="name" name="name" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5" placeholder="Masukan Nama" required />
<input type="id" id="id" name="id" hidden>
</div>
<div class="mb-6">
<label for="email" class="block mb-2 text-sm font-medium text-gray-900">Email</label>
<input type="text" id="email" name="email" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5" placeholder="Masukan Email" required />
</div>
<div class="mb-6 relative">
<label for="password" class="inline-block">Password</label>
<input type="password" name="password" id="password" placeholder="*****************" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5">
<button type="button" id="togglePassword" class="absolute bottom-1 right-3 transform -translate-y-1/2">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-5 h-5" id="toggleIcon">
<path stroke-linecap="round" stroke-linejoin="round" d="M3.98 8.223A10.477 10.477 0 0 0 1.934 12C3.226 16.338 7.244 19.5 12 19.5c.993 0 1.953-.138 2.863-.395M6.228 6.228A10.451 10.451 0 0 1 12 4.5c4.756 0 8.773 3.162 10.065 7.498a10.522 10.522 0 0 1-4.293 5.774M6.228 6.228 3 3m3.228 3.228 3.65 3.65m7.894 7.894L21 21m-3.228-3.228-3.65-3.65m0 0a3 3 0 1 0-4.243-4.243m4.242 4.242L9.88 9.88" />
</svg>
</button>
</div>
<div class="mb-6 relative">
<label for="password_confirm" class="inline-block">Konfirmasi Password</label>
<input type="password" name="password_confirm" id="password_confirm" placeholder="*****************" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5">
<button type="button" id="togglePasswordConfirm" class="absolute bottom-1 right-3 transform -translate-y-1/2">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-5 h-5" id="toggleIconConfirm">
<path stroke-linecap="round" stroke-linejoin="round" d="M3.98 8.223A10.477 10.477 0 0 0 1.934 12C3.226 16.338 7.244 19.5 12 19.5c.993 0 1.953-.138 2.863-.395M6.228 6.228A10.451 10.451 0 0 1 12 4.5c4.756 0 8.773 3.162 10.065 7.498a10.522 10.522 0 0 1-4.293 5.774M6.228 6.228 3 3m3.228 3.228 3.65 3.65m7.894 7.894L21 21m-3.228-3.228-3.65-3.65m0 0a3 3 0 1 0-4.243-4.243m4.242 4.242L9.88 9.88" />
</svg>
</button>
</div>
<button type="button" onclick="saveData()" class="text-white bg-yellow-500 hover:bg-yellow-400 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm w-full sm:w-auto px-5 py-2.5 text-center">Submit</button>
</form>
</div>
</div>
</div>
<div class="w-full h-full bg-white rounded-lg shadow-lg p-5">
<div class="py-5">
<div class="pb-4 bg-white flex justify-between">
<div class="relative mt-1">
<div class="absolute inset-y-0 rtl:inset-r-0 start-0 flex items-center ps-3 pointer-events-none">
<svg class="w-4 h-4 text-gray-500" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 20 20">
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m19 19-4-4m0-7A7 7 0 1 1 1 8a7 7 0 0 1 14 0Z"/>
</svg>
</div>
<input type="text" id="search" class="block pt-2 ps-10 text-sm text-gray-900 border border-gray-300 rounded-lg w-80 bg-gray-50 focus:ring-blue-500 focus:border-blue-500" placeholder="Cari data">
</div>
<div>
<button id="openModalButton" class="bg-yellow-500 hover:bg-yellow-400 text-white p-2 rounded-lg">
Tambah Data
</button>
</div>
</div>
<table id="table" class="w-full">
<thead class="bg-gray-200">
<tr class="text-left">
<th class="p-3">No</th>
<th class="p-3">Nama</th>
<th class="p-3">Email</th>
<th class="p-3">Dibuat</th>
<th class="p-3"></th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<nav class="flex items-center flex-column flex-wrap md:flex-row justify-between pt-4" aria-label="Table navigation">
<span id="pagination-info" class="text-sm font-normal text-gray-500 mb-4 md:mb-0 block w-full md:inline md:w-auto"></span>
<ul id="pagination" class="inline-flex -space-x-px rtl:space-x-reverse text-sm h-8">
<li>
<a href="#" onclick="previousPage()" class="flex items-center justify-center px-3 h-8 ms-0 leading-tight text-gray-500 bg-white border border-gray-300 rounded-s-lg">Previous</a>
</li>
<li>
<a href="#" onclick="nextPage()" class="flex items-center justify-center px-3 h-8 leading-tight text-gray-500 bg-white border border-gray-300 rounded-e-lg">Next</a>
</li>
</ul>
</nav>
</div>
</div>
@endsection
@section('script')
<script>
window.addEventListener('DOMContentLoaded', function() {
fetchData();
});
function fetchData() {
var searchQuery = $('#search').val();
$.ajax({
url: '{{ route('user')}}',
type: 'GET',
dataType: 'json',
data: {
page: currentPage,
per_page: itemsPerPage,
search: searchQuery
},
success: function(data) {
renderData(data.message.data);
updatePagination(data.message);
},
error: function(xhr, status, error) {
console.error('Error fetching data:', error);
}
});
}
$(document).on("click","#openModalButton",function(){
$("#id").val("");
})
function renderData(data) {
var tableBody = document.querySelector('#table tbody');
tableBody.innerHTML = '';
data.forEach((item, index) => {
let id = '{!! Auth::user()->id !!}';
if(id==item.id){
var button = `<button class="bg-blue-500 text-white px-3 py-1 rounded-lg mr-2" onclick="editData(${item.id})">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-5 h-5">
<path stroke-linecap="round" stroke-linejoin="round" d="m16.862 4.487 1.687-1.688a1.875 1.875 0 1 1 2.652 2.652L10.582 16.07a4.5 4.5 0 0 1-1.897 1.13L6 18l.8-2.685a4.5 4.5 0 0 1 1.13-1.897l8.932-8.931Zm0 0L19.5 7.125M18 14v4.75A2.25 2.25 0 0 1 15.75 21H5.25A2.25 2.25 0 0 1 3 18.75V8.25A2.25 2.25 0 0 1 5.25 6H10" />
</svg>
</button>
<button class="bg-red-500 text-white px-3 py-1 rounded-lg" onclick="deleteData(${item.id})">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-5 h-5">
<path stroke-linecap="round" stroke-linejoin="round" d="m14.74 9-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 0 1-2.244 2.077H8.084a2.25 2.25 0 0 1-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 0 0-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 0 1 3.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 0 0-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 0 0-7.5 0" />
</svg>
</button>`;
}else{
var button = '-';
}
var row = `
<tr class="text-left border-b border-gray-200">
<td class="p-3">${index + 1}</td>
<td class="p-3">${item.name}</td>
<td class="p-3">${item.email}</td>
<td class="p-3">${item.created_at}</td>
<td class="p-3">
`+button+`
</td>
</tr>
`;
tableBody.insertAdjacentHTML('beforeend', row);
});
}
function editData(id) {
var baseUrl = "{{ route('user.update', ['id' => '__ID__']) }}";
$.ajax({
url: baseUrl.replace('__ID__', id),
type: 'GET',
success: function(response) {
$('#id').val(response.message.id);
$('#name').val(response.message.name);
$('#email').val(response.message.email);
// $('#password').prop('disabled', true).removeClass('bg-gray-50').addClass('bg-gray-300');
// $('#password_confirm').prop('disabled', true).removeClass('bg-gray-50').addClass('bg-gray-300');
// $('#togglePassword').prop('disabled', true);
// $('#togglePasswordConfirm').prop('disabled', true);
toggleModal();
},
error: function(error) {
console.error('Error getting data:', error.responseText);
}
});
}
function deleteData(id) {
var baseUrl = "{{ route('user.delete', ['id' => '__ID__']) }}";
if (confirm('Apakah anda yakin ingin menghapus data ini?')) {
$.ajax({
url: baseUrl.replace('__ID__', id),
type: 'GET',
success: function(response) {
alert(response.message);
fetchData();
},
error: function(error) {
console.error('Error getting data:', error.responseText);
}
});
}
}
function saveData() {
var formData = new FormData(document.getElementById("formData"));
var baseUrl = "{{ route('user.update', ['id' => '__ID__']) }}";
var baseUrl2 = "{{ route('user', ['id' => '__ID__']) }}";
var id = $('#id').val();
// console.error("ID",id);
console.error("ID",id);
if(id !== '' && id !== null){
var url = baseUrl.replace('__ID__', id);
}else{
var url = "{{ route('user', ['id' => '__ID__']) }}";
}
var csrfToken = $('meta[name="csrf-token"]').attr('content');
var ajaxSettings = {
url: url,
type: 'POST',
data: formData,
processData: false,
contentType: false,
headers: {
'X-CSRF-TOKEN': csrfToken
},
success: function(response) {
alert(response.message);
fetchData();
closeModal();
$('#id').val("");
$('#formData')[0].reset();
},
error: function(error) {
console.error('Error saving data:', error.responseText);
}
};
$.ajax(ajaxSettings);
}
</script>
@endsection