@extends('layouts.admin')
@section('title','User Klien - '.$client->name)
@section('content')
<div class="d-flex justify-content-between align-items-center mb-3">
  <div>
    <a href="{{ route('admin.clients.show',$client) }}" class="text-decoration-none text-muted small"><i class="fas fa-arrow-left me-1"></i>Kembali ke {{ $client->name }}</a>
    <h5 class="fw-bold mb-0 mt-1">User Klien — {{ $client->name }}</h5>
  </div>
  <button class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#addUserModal">
    <i class="fas fa-plus me-1"></i>Tambah User
  </button>
</div>
<div class="card">
  <div class="table-responsive">
    <table class="table table-hover mb-0">
      <thead><tr><th>Nama</th><th>Dept / Jabatan</th><th>Email</th><th>No. HP</th><th>Login Portal</th><th></th></tr></thead>
      <tbody>
      @forelse($client->users as $u)
      <tr>
        <td class="fw-semibold small">{{ $u->name }}</td>
        <td class="text-muted small">{{ $u->department ?? '—' }} / {{ $u->position ?? '—' }}</td>
        <td class="small">{{ $u->email ?? '—' }}</td>
        <td class="small">{{ $u->phone ?? '—' }}</td>
        <td>
          @if($u->can_login)<span class="badge badge-ok"><i class="fas fa-check me-1"></i>Aktif</span>
          @else<span class="badge bg-secondary">Tidak</span>@endif
        </td>
        <td>
          @if(auth()->user()->role === 'admin')
          <form action="{{ route('admin.clients.users.destroy',[$client,$u]) }}" method="POST" onsubmit="return confirm('Hapus user ini?')">
            @csrf @method('DELETE')
            <button class="btn btn-sm btn-outline-danger"><i class="fas fa-trash"></i></button>
          </form>
          @endif
        </td>
      </tr>
      @empty
      <tr><td colspan="6" class="text-center text-muted py-4">Belum ada user</td></tr>
      @endforelse
      </tbody>
    </table>
  </div>
</div>

<div class="modal fade" id="addUserModal" tabindex="-1">
  <div class="modal-dialog"><div class="modal-content">
    <div class="modal-header"><h6 class="modal-title fw-bold">Tambah User Klien</h6><button class="btn-close" data-bs-dismiss="modal"></button></div>
    <form action="{{ route('admin.clients.users.store',$client) }}" method="POST">@csrf
    <div class="modal-body">
      <div class="row g-2">
        <div class="col-12"><label class="form-label small fw-semibold">Nama *</label><input type="text" name="name" class="form-control form-control-sm" required></div>
        <div class="col-6"><label class="form-label small fw-semibold">Email</label><input type="email" name="email" class="form-control form-control-sm"></div>
        <div class="col-6"><label class="form-label small fw-semibold">No. HP</label><input type="text" name="phone" class="form-control form-control-sm"></div>
        <div class="col-6"><label class="form-label small fw-semibold">Departemen</label><input type="text" name="department" class="form-control form-control-sm"></div>
        <div class="col-6"><label class="form-label small fw-semibold">Jabatan</label><input type="text" name="position" class="form-control form-control-sm"></div>
        <div class="col-12 mt-1 form-check">
          <input type="checkbox" name="can_login" value="1" class="form-check-input" id="canLogin">
          <label class="form-check-label small" for="canLogin">Aktifkan Login Portal Klien</label>
        </div>
        <div class="col-12"><label class="form-label small fw-semibold">Password Portal</label><input type="password" name="password" class="form-control form-control-sm" placeholder="Min 6 karakter"></div>
      </div>
    </div>
    <div class="modal-footer"><button type="submit" class="btn btn-primary btn-sm">Simpan</button></div>
    </form>
  </div></div>
</div>
@endsection
