@extends('layouts.app')

@section('title', 'Add Biometric Device')

@section('content')
<div class="row justify-content-center">
    <div class="col-md-8">
        <div class="card border-0 shadow-sm">
            <div class="card-header bg-white">
                <h5 class="mb-0 fw-bold">New Device Configuration</h5>
            </div>
            <div class="card-body">
                <form id="createDeviceForm">
                    @csrf
                    <div class="row g-3">
                        <div class="col-md-6">
                            <label class="form-label">Device Name <span class="text-danger">*</span></label>
                            <input type="text" name="name" class="form-control" placeholder="e.g. Main Entrance" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Location</label>
                            <input type="text" name="location" class="form-control" placeholder="e.g. Reception">
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">IP Address <span class="text-danger">*</span></label>
                            <input type="text" name="ip_address" class="form-control" placeholder="192.168.1.201" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Port <span class="text-danger">*</span></label>
                            <input type="number" name="port" class="form-control" value="4370" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Serial Number</label>
                            <input type="text" name="serial_number" class="form-control" placeholder="Optional">
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Protocol <span class="text-danger">*</span></label>
                            <select name="protocol" class="form-select" required>
                                <option value="eSSL">eSSL (ADMS)</option>
                                <option value="ZK">ZKStandAlone</option>
                                <option value="Other">Other</option>
                            </select>
                        </div>
                    </div>
                    
                    <div class="mt-4 d-flex justify-content-end gap-2">
                        <a href="{{ route('biometric-devices.index') }}" class="btn btn-secondary">Cancel</a>
                        <button type="submit" class="btn btn-primary">Save Device</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script>
$('#createDeviceForm').submit(function(e) {
    e.preventDefault();
    $.ajax({
        url: "{{ route('biometric-devices.store') }}",
        type: 'POST',
        data: $(this).serialize(),
        success: function(res) {
            Swal.fire({
                icon: 'success',
                title: 'Success',
                text: res.success,
                showConfirmButton: false,
                timer: 1500
            }).then(() => {
                window.location.href = "{{ route('biometric-devices.index') }}";
            });
        },
        error: function(err) {
            Swal.fire('Error', 'Failed to save device. Check inputs.', 'error');
        }
    });
});
</script>
@endpush
