@extends('layouts.app')
@section('title', $device->exists ? 'Edit Device' : 'Add Device')

@section('content')
<div class="row justify-content-center">
<div class="col-lg-7">

<div class="d-flex align-items-center mb-4 gap-3">
    <a href="{{ route('lis.devices.index') }}" class="btn btn-outline-secondary btn-sm">
        <i class="bi bi-arrow-left"></i>
    </a>
    <h5 class="mb-0 fw-bold">{{ $device->exists ? 'Edit Device' : 'Add LIS Device' }}</h5>
</div>

<div class="card">
    <div class="card-body p-4">
        <form method="POST" action="{{ $device->exists ? route('lis.devices.update', $device) : route('lis.devices.store') }}">
            @csrf
            @if($device->exists) @method('PUT') @endif

            <div class="row g-3">
                <div class="col-md-8">
                    <label class="form-label">Device Name *</label>
                    <input type="text" name="name" class="form-control @error('name') is-invalid @enderror"
                           value="{{ old('name', $device->name) }}" placeholder="e.g. LIS KM8.1 NB" required>
                    @error('name')<div class="invalid-feedback">{{ $message }}</div>@enderror
                </div>
                <div class="col-md-4">
                    <label class="form-label">Chainage</label>
                    <input type="text" name="chainage" class="form-control"
                           value="{{ old('chainage', $device->chainage) }}" placeholder="e.g. KM8.1">
                </div>

                <div class="col-md-6">
                    <label class="form-label">IP Address *</label>
                    <input type="text" name="ip_address" class="form-control @error('ip_address') is-invalid @enderror"
                           value="{{ old('ip_address', $device->ip_address) }}" placeholder="121.120.39.73" required>
                    @error('ip_address')<div class="invalid-feedback">{{ $message }}</div>@enderror
                </div>
                <div class="col-md-6">
                    <label class="form-label">Port *</label>
                    <input type="number" name="port" class="form-control @error('port') is-invalid @enderror"
                           value="{{ old('port', $device->port ?? 9527) }}" min="1" max="65535" required>
                    @error('port')<div class="invalid-feedback">{{ $message }}</div>@enderror
                </div>

                <div class="col-md-6">
                    <label class="form-label">Width (px) *</label>
                    <input type="number" name="width_px" class="form-control"
                           value="{{ old('width_px', $device->width_px ?? 104) }}" min="1" required>
                </div>
                <div class="col-md-6">
                    <label class="form-label">Height (px) *</label>
                    <input type="number" name="height_px" class="form-control"
                           value="{{ old('height_px', $device->height_px ?? 208) }}" min="1" required>
                </div>

                <div class="col-12">
                    <label class="form-label">Highway / Route</label>
                    <input type="text" name="highway" class="form-control"
                           value="{{ old('highway', $device->highway) }}" placeholder="e.g. LDP Northbound">
                </div>

                <div class="col-12">
                    <label class="form-label">Location Description</label>
                    <input type="text" name="location" class="form-control"
                           value="{{ old('location', $device->location) }}" placeholder="e.g. KM8.1 NB, USJ–Seafield Toll Plaza">
                </div>

                <div class="col-md-6">
                    <label class="form-label">Latitude</label>
                    <input type="number" name="latitude" class="form-control" step="0.000001"
                           value="{{ old('latitude', $device->latitude) }}" placeholder="3.052300">
                </div>
                <div class="col-md-6">
                    <label class="form-label">Longitude</label>
                    <input type="number" name="longitude" class="form-control" step="0.000001"
                           value="{{ old('longitude', $device->longitude) }}" placeholder="101.578500">
                </div>

                <div class="col-12">
                    <label class="form-label">Notes</label>
                    <textarea name="notes" class="form-control" rows="2"
                              placeholder="Optional notes…">{{ old('notes', $device->notes) }}</textarea>
                </div>

                <div class="col-12">
                    <div class="form-check form-switch">
                        <input class="form-check-input" type="checkbox" name="is_active" id="is_active" value="1"
                               {{ old('is_active', $device->is_active ?? true) ? 'checked' : '' }}>
                        <label class="form-check-label text-secondary" for="is_active">Device is active</label>
                    </div>
                </div>
            </div>

            <div class="d-flex gap-2 mt-4">
                <button type="submit" class="btn btn-primary px-4">
                    <i class="bi bi-floppy me-1"></i>{{ $device->exists ? 'Update Device' : 'Add Device' }}
                </button>
                <a href="{{ route('lis.devices.index') }}" class="btn btn-outline-secondary">Cancel</a>
            </div>
        </form>
    </div>
</div>

</div>
</div>
@endsection
