@extends('layouts.admin-simple')

@section('title', 'Edit Maintenance Record')

@section('breadcrumb')
    <li class="breadcrumb-item">Warehouse</li>
    <li class="breadcrumb-item">Fixed Assets</li>
    <li class="breadcrumb-item"><a href="{{ route('warehouse.fixed-assets.maintenance.index') }}">Maintenance</a></li>
    <li class="breadcrumb-item"><a href="{{ route('warehouse.fixed-assets.maintenance.show', $record->id) }}">{{ $record->maintenance_number }}</a></li>
    <li class="breadcrumb-item active">Edit</li>
@endsection

@section('page-title')
    <div class="d-flex justify-content-between align-items-center w-100">
        <div class="page-title-left">
            <h3 class="mb-1">Edit Maintenance Record</h3>
            <p class="text-muted mb-0" style="font-size: 13px;">{{ $record->maintenance_number }}</p>
        </div>
        <div class="page-actions-right">
            <a href="{{ route('warehouse.fixed-assets.maintenance.show', $record->id) }}" class="btn btn-sm btn-outline-secondary"><i class="material-icons mr-1" style="font-size: 16px;">arrow_back</i> Back</a>
        </div>
    </div>
@endsection

@section('content')
    @if($errors->any())
        <div class="alert alert-danger alert-dismissible fade show">
            <ul class="mb-0">@foreach($errors->all() as $e)<li>{{ $e }}</li>@endforeach</ul>
            <button type="button" class="close" data-dismiss="alert"><span>&times;</span></button>
        </div>
    @endif

    <form method="POST" action="{{ route('warehouse.fixed-assets.maintenance.update', $record->id) }}">
        @csrf
        @method('PUT')
        <div class="card mb-3">
            <div class="card-header"><h6 class="mb-0"><i class="material-icons mr-2 align-middle">build</i>Maintenance Details</h6></div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label>Asset <span class="text-danger">*</span></label>
                            <select name="fixed_asset_id" class="form-control select2-dropdown" required>
                                <option value="">Select Asset</option>
                                @foreach($assets as $asset)
                                    <option value="{{ $asset->id }}" {{ old('fixed_asset_id', $record->fixed_asset_id) == $asset->id ? 'selected' : '' }}>
                                        {{ $asset->asset_name }} ({{ $asset->asset_code }})
                                    </option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">
                            <label>Maintenance Type <span class="text-danger">*</span></label>
                            <select name="maintenance_type" class="form-control" required>
                                @foreach(['scheduled','unscheduled','inspection','calibration'] as $t)
                                    <option value="{{ $t }}" {{ old('maintenance_type', $record->maintenance_type) == $t ? 'selected' : '' }}>{{ ucfirst($t) }}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">
                            <label>Priority <span class="text-danger">*</span></label>
                            <select name="priority" class="form-control" required>
                                @foreach(['low','medium','high','critical'] as $p)
                                    <option value="{{ $p }}" {{ old('priority', $record->priority) == $p ? 'selected' : '' }}>{{ ucfirst($p) }}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-3">
                        <div class="form-group">
                            <label>Scheduled Date <span class="text-danger">*</span></label>
                            <input type="date" name="scheduled_date" class="form-control" value="{{ old('scheduled_date', $record->scheduled_date ? $record->scheduled_date->format('Y-m-d') : '') }}" required>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">
                            <label>Vendor / Service Provider</label>
                            <input type="text" name="vendor" class="form-control" value="{{ old('vendor', $record->vendor) }}">
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">
                            <label>Labor Cost (AED)</label>
                            <input type="number" name="labor_cost" class="form-control" value="{{ old('labor_cost', $record->labor_cost ?? '0.00') }}" step="0.01" min="0">
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">
                            <label>Parts Cost (AED)</label>
                            <input type="number" name="parts_cost" class="form-control" value="{{ old('parts_cost', $record->parts_cost ?? '0.00') }}" step="0.01" min="0">
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-4">
                        <div class="form-group">
                            <label>Condition Before</label>
                            <select name="condition_before" class="form-control">
                                <option value="">Select Condition</option>
                                @foreach(['new','good','fair','poor','damaged','non_functional'] as $c)
                                    <option value="{{ $c }}" {{ old('condition_before', $record->condition_before) == $c ? 'selected' : '' }}>{{ ucfirst(str_replace('_', ' ', $c)) }}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="col-md-8">
                        <div class="form-group">
                            <label>Description</label>
                            <input type="text" name="description" class="form-control" value="{{ old('description', $record->description) }}" placeholder="Brief description of maintenance work">
                        </div>
                    </div>
                </div>
                <div class="form-group mb-0">
                    <label>Notes</label>
                    <textarea name="notes" rows="2" class="form-control">{{ old('notes', $record->notes) }}</textarea>
                </div>
            </div>
        </div>

        <div class="card">
            <div class="card-body text-center py-3">
                <button type="submit" class="btn btn-success mr-3"><i class="material-icons mr-1" style="font-size: 16px;">save</i> Update Record</button>
                <a href="{{ route('warehouse.fixed-assets.maintenance.show', $record->id) }}" class="btn btn-outline-secondary">Cancel</a>
            </div>
        </div>
    </form>
@endsection

@push('styles')
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css">
    <style>
        .select2-container { width: 100% !important; }
        .select2-container--default .select2-selection--single { height: 38px; border: 1px solid #ced4da; }
        .select2-container--default .select2-selection--single .select2-selection__rendered { line-height: 36px; }
        .select2-container--default .select2-selection--single .select2-selection__arrow { height: 36px; }
    </style>
@endpush

@push('scripts')
    <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
    <script>
        $(document).ready(function() {
            $('.select2-dropdown').select2({ allowClear: true, placeholder: 'Select Asset' });
        });
    </script>
@endpush
