@extends('layouts.app')

@section('title', 'Edit Machine')

@section('content')
    <div class="mb-4">
        <h4 class="mb-0">Edit Machine: {{ $machine->name }}</h4>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb mb-0">
                <li class="breadcrumb-item"><a href="{{ url('/') }}">Dashboard</a></li>
                <li class="breadcrumb-item"><a href="{{ route('machines.index') }}">Machines</a></li>
                <li class="breadcrumb-item active">Edit</li>
            </ol>
        </nav>
    </div>

    <div class="row justify-content-center">
        <div class="col-lg-10">
            <div class="card shadow-sm border-0">
                <div class="card-body p-4">
                    <form action="{{ route('machines.update', $machine->id) }}" method="POST">
                        @csrf
                        @method('PUT')

                        <div class="row g-4">
                            <div class="col-md-6">
                                <label class="form-label fw-bold small text-uppercase text-muted">Machine Name</label>
                                <input type="text" name="name" class="form-control" required value="{{ $machine->name }}">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold small text-uppercase text-muted">Asset ID / Code</label>
                                <input type="text" name="code" class="form-control" required value="{{ $machine->code }}">
                            </div>

                            <div class="col-md-6">
                                <label class="form-label fw-bold small text-uppercase text-muted">Production Line</label>
                                <select name="production_line_id" class="form-select" required>
                                    @foreach($productionLines as $line)
                                        <option value="{{ $line->id }}" {{ $machine->production_line_id == $line->id ? 'selected' : '' }}>
                                            {{ $line->zone->factory->name }} - {{ $line->name }}
                                        </option>
                                    @endforeach
                                </select>
                            </div>

                            <div class="col-md-6">
                                <label class="form-label fw-bold small text-uppercase text-muted">Status</label>
                                <select name="status" class="form-select" required>
                                    <option value="idle" {{ $machine->status == 'idle' ? 'selected' : '' }}>Idle</option>
                                    <option value="running" {{ $machine->status == 'running' ? 'selected' : '' }}>Running
                                    </option>
                                    <option value="under_maintenance" {{ $machine->status == 'under_maintenance' ? 'selected' : '' }}>Under Maintenance</option>
                                    <option value="stopped" {{ $machine->status == 'stopped' ? 'selected' : '' }}>Stopped
                                    </option>
                                </select>
                            </div>

                            <div class="col-md-12">
                                <hr class="my-2">
                                <h6 class="fw-bold mb-3 text-primary"><i class="bi bi-tools me-2"></i>Maintenance Settings
                                </h6>
                            </div>

                            <div class="col-md-6">
                                <label class="form-label fw-bold small text-uppercase text-muted">Last Maintenance</label>
                                <input type="date" name="last_maintenance_at" class="form-control"
                                    value="{{ $machine->last_maintenance_at ? $machine->last_maintenance_at->format('Y-m-d') : '' }}">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold small text-uppercase text-muted">Next Scheduled
                                    Service</label>
                                <input type="date" name="next_maintenance_at" class="form-control"
                                    value="{{ $machine->next_maintenance_at ? $machine->next_maintenance_at->format('Y-m-d') : '' }}">
                            </div>
                        </div>

                        <div class="mt-5 pt-3 border-top d-flex justify-content-end gap-2">
                            <a href="{{ route('machines.index') }}" class="btn btn-light px-4">Cancel</a>
                            <button type="submit" class="btn btn-primary px-5 shadow-sm">Apply Changes</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
@endsection