@extends('layouts.app')
@section('title', 'Manajemen User - ISPA Prediction')
@section('content')
@if (session('success'))
    <div class="alert alert-success alert-dismissible fade show" role="alert">
        <strong>Berhasil!</strong> {{ session('success') }}
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>
@endif

@if (session('error'))
    <div class="alert alert-danger alert-dismissible fade show" role="alert">
        <strong>Gagal!</strong> {{ session('error') }}
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>
@endif

<div class="container my-4">
    <div class="card shadow-sm">
        <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
            <h4 class="mb-0">Manajemen Users</h4>
        </div>
        <div class="card-body">
            <form action="{{ route('admin.users.store') }}" method="POST" class="row g-3 mb-4">
                @csrf
                <div class="col-md-4">
                    <input type="text" name="name" class="form-control" placeholder="Nama" required>
                </div>
                <div class="col-md-4">
                    <input type="email" name="email" class="form-control" placeholder="Email" required>
                </div>
                <div class="col-md-4 d-flex">
                    <input type="password" name="password" class="form-control me-2" placeholder="Password" required>
                    <button type="submit" class="btn btn-success">Tambah</button>
                </div>
            </form>

            <div class="table-responsive">
                <table class="table table-hover table-bordered">
                    <thead class="table-light">
                        <tr>
                            <th>Nama</th>
                            <th>Email</th>
                            <th style="width: 150px;">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($users as $user)
    <tr>
        <td>{{ $user->name }}</td>
        <td>{{ $user->email }}</td>
        <td>
            <a href="{{ route('admin.edit', $user->id) }}" class="btn btn-sm btn-warning">Edit</a>
            
            {{-- Jika bukan user default (Admin User), tampilkan tombol Delete --}}
            @if($user->name !== 'Admin User')
                <form action="{{ route('admin.users.destroy', $user->id) }}" method="POST" class="d-inline">
                    @csrf
                    @method('DELETE')
                    <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Yakin ingin menghapus user ini?')">Delete</button>
                </form>
            @endif
        </td>
    </tr>
@empty
    <tr>
        <td colspan="3" class="text-center">Tidak ada data user.</td>
    </tr>
@endforelse

                    </tbody>
                </table>
            </div>

        </div>
    </div>
</div>
@endsection