@extends('layouts.app')

@section('content')
    <div class="space-y-6">
        <div class="flex flex-wrap items-center justify-between gap-4">
            <div>
                <h2 class="text-2xl font-semibold text-slate-900">Pengaturan Aplikasi</h2>
                <p class="text-sm text-slate-500">Ubah nama aplikasi dan logo untuk header dan struk.</p>
            </div>
        </div>

        <x-card>
            <form method="post" action="{{ route('settings.update') }}" enctype="multipart/form-data" class="grid gap-4 md:grid-cols-2">
                @csrf
                @method('put')
                <x-input name="app_name" label="Nama Aplikasi" :value="old('app_name', $appName)" required />
                <div>
                    <label class="mb-2 block text-sm font-semibold text-slate-700">Logo</label>
                    <input type="file" name="app_logo" accept="image/*" class="block w-full rounded-lg border border-slate-200 px-3 py-2 text-sm text-slate-700">
                    @if($appLogo)
                        <div class="mt-3 flex items-center gap-3">
                            <img src="{{ asset('storage/' . $appLogo) }}" alt="Logo" class="h-12 w-12 rounded-lg object-cover">
                            <span class="text-xs text-slate-500">Logo aktif</span>
                        </div>
                    @endif
                </div>
                <div class="md:col-span-2 border-t border-slate-100 pt-4">
                    <h3 class="text-lg font-semibold text-slate-900">Pengaturan Absensi</h3>
                    <p class="mt-1 text-sm text-slate-500">Pengaturan koneksi mesin dan akses menu absensi.</p>
                </div>
                <x-input name="attendance_device_ip" label="IP Mesin Absensi" :value="old('attendance_device_ip', $attendanceDeviceIp)" placeholder="192.168.1.201" />
                <x-input name="attendance_comm_key" label="Comm Key" :value="old('attendance_comm_key', $attendanceCommKey)" placeholder="0" />
                <x-input name="attendance_token" label="Token Sinkronisasi" :value="old('attendance_token', $attendanceToken)" placeholder="Isi token untuk script sync" />
                <div class="md:col-span-2">
                    <label class="mb-2 block text-sm font-semibold text-slate-700">Role yang Bisa Akses Absensi</label>
                    @php($selectedRoles = collect(explode(',', old('attendance_roles', $attendanceRoles)))->map(fn($r) => trim($r))->filter())
                    <div class="flex flex-wrap gap-3">
                        @foreach($roleOptions as $role)
                            <label class="inline-flex items-center gap-2 rounded-lg border border-slate-200 px-3 py-2 text-sm text-slate-700">
                                <input type="checkbox" name="attendance_roles[]" value="{{ $role }}"
                                    {{ $selectedRoles->contains($role) ? 'checked' : '' }}>
                                <span>{{ ucfirst($role) }}</span>
                            </label>
                        @endforeach
                    </div>
                    <p class="mt-2 text-xs text-slate-500">Jika kosong, otomatis hanya admin.</p>
                </div>
                <div class="md:col-span-2">
                    <label class="mb-2 block text-sm font-semibold text-slate-700">Role yang Bisa Akses Gudang</label>
                    @php($selectedWarehouseRoles = collect(explode(',', old('warehouse_roles', $warehouseRoles)))->map(fn($r) => trim($r))->filter())
                    <div class="flex flex-wrap gap-3">
                        @foreach($roleOptions as $role)
                            <label class="inline-flex items-center gap-2 rounded-lg border border-slate-200 px-3 py-2 text-sm text-slate-700">
                                <input type="checkbox" name="warehouse_roles[]" value="{{ $role }}"
                                    {{ $selectedWarehouseRoles->contains($role) ? 'checked' : '' }}>
                                <span>{{ ucfirst($role) }}</span>
                            </label>
                        @endforeach
                    </div>
                    <p class="mt-2 text-xs text-slate-500">Jika kosong, otomatis hanya admin.</p>
                </div>
                <div class="md:col-span-2 flex justify-end gap-3">
                    <x-button type="submit">Simpan</x-button>
                </div>
            </form>
        </x-card>
    </div>
@endsection
