@extends('admin.layouts.app')

@section('content')

<style>
@import url('https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&display=swap');
*, *::before, *::after { box-sizing: border-box; }
body, * { font-family: 'Plus Jakarta Sans', sans-serif; }

.eu-wrap {
    padding: 2rem 2.5rem 3rem;
    max-width: 640px;
    margin: 0 auto;
}
.eu-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 2rem;
}
.eu-title { font-size: 22px; font-weight: 700; color: #0D215C; }
.eu-sub   { font-size: 13px; color: #94a3b8; margin-top: 4px; }
.btn-back {
    padding: 8px 18px;
    border-radius: 10px;
    background: #eef2ff;
    color: #3730a3;
    font-size: 13px;
    font-weight: 600;
    text-decoration: none;
    transition: .2s;
}
.btn-back:hover { background: #dbeafe; color: #1d4ed8; }

.eu-card {
    background: #fff;
    border: 1px solid #edf1f7;
    border-radius: 20px;
    overflow: hidden;
    box-shadow: 0 2px 12px rgba(13,33,92,.05);
}
.eu-card-header {
    padding: 1.2rem 1.5rem;
    border-bottom: 1px solid #f1f5f9;
    background: #f8fafc;
    display: flex;
    align-items: center;
    gap: 10px;
}
.eu-card-header h3 { font-size: 14px; font-weight: 700; color: #0D215C; margin: 0; }
.eu-body { padding: 1.5rem; }

.form-group { margin-bottom: 1.2rem; }
.form-label {
    display: block;
    font-size: 13px;
    font-weight: 700;
    color: #374151;
    margin-bottom: 7px;
}
.form-input, .form-select-field {
    width: 100%;
    padding: 11px 14px;
    border: 1.5px solid #e2e8f0;
    border-radius: 12px;
    font-size: 14px;
    outline: none;
    color: #0f172a;
    background: #fff;
    transition: border .2s, box-shadow .2s;
}
.form-input:focus, .form-select-field:focus {
    border-color: #3b82f6;
    box-shadow: 0 0 0 3px rgba(59,130,246,.1);
}
.form-hint {
    font-size: 11px;
    color: #94a3b8;
    margin-top: 5px;
}
.pw-wrap { position: relative; }
.pw-wrap .form-input { padding-right: 44px; }
.pw-toggle {
    position: absolute;
    right: 12px;
    top: 50%;
    transform: translateY(-50%);
    background: none;
    border: none;
    cursor: pointer;
    color: #94a3b8;
    padding: 4px;
    display: flex;
    align-items: center;
}
.pw-toggle:hover { color: #3b82f6; }

.btn-submit {
    width: 100%;
    padding: 13px;
    margin-top: .5rem;
    background: #0D215C;
    color: #fff;
    border: none;
    border-radius: 12px;
    font-size: 14px;
    font-weight: 700;
    cursor: pointer;
    transition: .2s;
    box-shadow: 0 4px 14px rgba(13,33,92,.2);
}
.btn-submit:hover { background: #1e3a8a; transform: translateY(-1px); }

.alert-error {
    background: #fee2e2;
    color: #b91c1c;
    padding: 12px 16px;
    border-radius: 10px;
    margin-bottom: 1rem;
    font-size: 13px;
}
</style>

<div class="eu-wrap">
    <div class="eu-header">
        <div>
            <div class="eu-title">Edit Pengguna</div>
            <div class="eu-sub">Perbarui informasi akun pengguna</div>
        </div>
        <a href="{{ route('admin.users') }}" class="btn-back">← Kembali</a>
    </div>

    <div class="eu-card">
        <div class="eu-card-header">
            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="#0D215C" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></svg>
            <h3>Form Edit Pengguna</h3>
        </div>
        <div class="eu-body">

            @if($errors->any())
            <div class="alert-error">
                @foreach($errors->all() as $e) <div>• {{ $e }}</div> @endforeach
            </div>
            @endif

            @if(session('error'))
            <div class="alert-error">✗ {{ session('error') }}</div>
            @endif

            <form action="{{ route('admin.users.update', $user->id) }}" method="POST">
                @csrf

                <div class="form-group">
                    <label class="form-label" for="name">Nama Lengkap</label>
                    <input type="text" id="name" name="name" class="form-input"
                           value="{{ old('name', $user->name) }}" placeholder="Nama lengkap pengguna" required>
                </div>

                <div class="form-group">
                    <label class="form-label" for="username">Username</label>
                    <input type="text" id="username" name="username" class="form-input"
                           value="{{ old('username', $user->username) }}" placeholder="Username login" required>
                </div>

                <div class="form-group">
                    <label class="form-label" for="jabatan">Jabatan</label>
                    <input type="text" id="jabatan" name="jabatan" class="form-input"
                           value="{{ old('jabatan', $user->jabatan ?? '') }}" placeholder="Jabatan pengguna (opsional)">
                </div>

                <div class="form-group">
                    <label class="form-label" for="role">Role / Hak Akses</label>
                    <select id="role" name="role" class="form-select-field">
                        <option value="user"       {{ old('role', $user->role) == 'user'       ? 'selected' : '' }}>Petugas Lapangan</option>
                        <option value="admin"      {{ old('role', $user->role) == 'admin'      ? 'selected' : '' }}>Administrator (Kasi)</option>
                        <option value="superadmin" {{ old('role', $user->role) == 'superadmin' ? 'selected' : '' }}>Super Admin (Kabid)</option>
                    </select>
                </div>

                <div class="form-group">
                    <label class="form-label" for="password">Password Baru</label>
                    <div class="pw-wrap">
                        <input type="password" id="password" name="password" class="form-input"
                               placeholder="Kosongkan jika tidak ingin mengubah">
                        <button type="button" class="pw-toggle" onclick="togglePassword('password','eyeIconPw')">
                            <svg id="eyeIconPw" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"/><circle cx="12" cy="12" r="3"/></svg>
                        </button>
                    </div>
                    <div class="form-hint">Minimal 6 karakter. Kosongkan jika tidak ingin mengubah password.</div>
                </div>

                <button type="submit" class="btn-submit">Simpan Perubahan</button>
            </form>
        </div>
    </div>
</div>

<script>
function togglePassword(fieldId, iconId) {
    const field = document.getElementById(fieldId);
    const icon  = document.getElementById(iconId);
    if (field.type === 'password') {
        field.type = 'text';
        icon.innerHTML = '<path d="M17.94 17.94A10.07 10.07 0 0 1 12 20c-7 0-11-8-11-8a18.45 18.45 0 0 1 5.06-5.94M9.9 4.24A9.12 9.12 0 0 1 12 4c7 0 11 8 11 8a18.5 18.5 0 0 1-2.16 3.19m-6.72-1.07a3 3 0 1 1-4.24-4.24"/><line x1="1" y1="1" x2="23" y2="23"/>';
    } else {
        field.type = 'password';
        icon.innerHTML = '<path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"/><circle cx="12" cy="12" r="3"/>';
    }
}
</script>

@endsection
