@extends('layouts.admin-simple')

@section('title', '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 active">{{ $record->maintenance_number }}</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">{{ $record->maintenance_number }}</h3>
            <p class="text-muted mb-0" style="font-size: 13px;">
                {{ $record->fixedAsset ? $record->fixedAsset->asset_name : '-' }}
                -
                @php $sColors = ['scheduled'=>'secondary','in_progress'=>'warning','completed'=>'success','cancelled'=>'danger']; @endphp
                <span class="badge badge-{{ $sColors[$record->status] ?? 'secondary' }}">{{ ucfirst(str_replace('_',' ',$record->status)) }}</span>
            </p>
        </div>
        <div class="page-actions-right d-flex">
            @if($record->status == 'scheduled')
                @can('warehouse.fixed-asset-maintenance.process')
                <form method="POST" action="{{ route('warehouse.fixed-assets.maintenance.start', $record->id) }}" class="mr-2" onsubmit="return confirm('Start this maintenance?')">
                    @csrf
                    <button type="submit" class="btn btn-sm btn-warning"><i class="material-icons mr-1" style="font-size: 16px;">play_arrow</i> Start</button>
                </form>
                @endcan
                @can('warehouse.fixed-asset-maintenance.edit')
                <a href="{{ route('warehouse.fixed-assets.maintenance.edit', $record->id) }}" class="btn btn-sm btn-primary mr-2"><i class="material-icons mr-1" style="font-size: 16px;">edit</i> Edit</a>
                @endcan
            @elseif($record->status == 'in_progress')
                @can('warehouse.fixed-asset-maintenance.process')
                <button type="button" class="btn btn-sm btn-success mr-2" onclick="showCompleteModal()"><i class="material-icons mr-1" style="font-size: 16px;">done</i> Complete</button>
                @endcan
            @endif
            <a href="{{ route('warehouse.fixed-assets.maintenance.index') }}" 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(session('success'))
        <div class="alert alert-success alert-dismissible fade show"><span>{{ session('success') }}</span><button type="button" class="close" data-dismiss="alert"><span>&times;</span></button></div>
    @endif
    @if(session('error'))
        <div class="alert alert-danger alert-dismissible fade show"><span>{{ session('error') }}</span><button type="button" class="close" data-dismiss="alert"><span>&times;</span></button></div>
    @endif

    <div class="row">
        <div class="col-md-6">
            <div class="card mb-3">
                <div class="card-header"><h6 class="mb-0">Maintenance Details</h6></div>
                <div class="card-body">
                    <table class="table table-sm table-borderless mb-0">
                        <tr><td class="text-muted" width="35%">Maintenance #</td><td><code>{{ $record->maintenance_number }}</code></td></tr>
                        <tr><td class="text-muted">Type</td><td>{{ ucfirst($record->maintenance_type) }}</td></tr>
                        <tr><td class="text-muted">Priority</td><td>
                            @php $pColors = ['low'=>'secondary','medium'=>'info','high'=>'warning','critical'=>'danger']; @endphp
                            <span class="badge badge-{{ $pColors[$record->priority] ?? 'secondary' }}">{{ ucfirst($record->priority ?? 'medium') }}</span>
                        </td></tr>
                        <tr><td class="text-muted">Scheduled Date</td><td>{{ $record->scheduled_date ? $record->scheduled_date->format('M d, Y') : '-' }}</td></tr>
                        <tr><td class="text-muted">Start Date</td><td>{{ $record->start_date ? $record->start_date->format('M d, Y') : '-' }}</td></tr>
                        <tr><td class="text-muted">Completion Date</td><td>{{ $record->completion_date ? $record->completion_date->format('M d, Y') : '-' }}</td></tr>
                        <tr><td class="text-muted">Vendor</td><td>{{ $record->vendor ?? '-' }}</td></tr>
                    </table>
                </div>
            </div>
        </div>
        <div class="col-md-6">
            <div class="card mb-3">
                <div class="card-header"><h6 class="mb-0">Asset & Costs</h6></div>
                <div class="card-body">
                    <table class="table table-sm table-borderless mb-0">
                        <tr><td class="text-muted" width="35%">Asset</td><td>
                            @if($record->fixedAsset)
                                <a href="{{ route('warehouse.fixed-assets.catalog.show', $record->fixedAsset->id) }}">
                                    {{ $record->fixedAsset->asset_code }} - {{ $record->fixedAsset->asset_name }}
                                </a>
                            @else - @endif
                        </td></tr>
                        <tr><td class="text-muted">Condition Before</td><td>
                            <span class="badge badge-{{ in_array($record->condition_before, ['new','good']) ? 'success' : 'warning' }}">{{ ucfirst(str_replace('_',' ',$record->condition_before ?? '-')) }}</span>
                        </td></tr>
                        <tr><td class="text-muted">Condition After</td><td>
                            @if($record->condition_after)
                                <span class="badge badge-{{ in_array($record->condition_after, ['new','good']) ? 'success' : 'warning' }}">{{ ucfirst(str_replace('_',' ',$record->condition_after)) }}</span>
                            @else - @endif
                        </td></tr>
                        <tr><td class="text-muted">Labor Cost</td><td>AED {{ number_format($record->labor_cost ?? 0, 2) }}</td></tr>
                        <tr><td class="text-muted">Parts Cost</td><td>AED {{ number_format($record->parts_cost ?? 0, 2) }}</td></tr>
                        <tr><td class="text-muted">Total Cost</td><td class="font-weight-bold text-primary">AED {{ number_format($record->total_cost ?? 0, 2) }}</td></tr>
                    </table>
                </div>
            </div>
        </div>
    </div>

    @if($record->description)
    <div class="card mb-3">
        <div class="card-header"><h6 class="mb-0">Description</h6></div>
        <div class="card-body"><p class="mb-0">{{ $record->description }}</p></div>
    </div>
    @endif

    @if($record->findings || $record->actions_taken)
    <div class="card mb-3">
        <div class="card-header"><h6 class="mb-0">Findings & Actions</h6></div>
        <div class="card-body">
            @if($record->findings)
                <p><strong>Findings:</strong></p>
                <p>{{ $record->findings }}</p>
            @endif
            @if($record->actions_taken)
                <p><strong>Actions Taken:</strong></p>
                <p class="mb-0">{{ $record->actions_taken }}</p>
            @endif
        </div>
    </div>
    @endif

    @if($record->notes)
    <div class="card mb-3">
        <div class="card-header"><h6 class="mb-0">Notes</h6></div>
        <div class="card-body"><p class="mb-0">{{ $record->notes }}</p></div>
    </div>
    @endif

    <!-- Complete Modal -->
    @if($record->status == 'in_progress')
    <div class="modal fade" id="completeModal" tabindex="-1">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <form method="POST" action="{{ route('warehouse.fixed-assets.maintenance.complete', $record->id) }}">
                    @csrf
                    <div class="modal-header">
                        <h5 class="modal-title"><i class="material-icons mr-2 align-middle">done</i> Complete Maintenance</h5>
                        <button type="button" class="close" data-dismiss="modal"><span>&times;</span></button>
                    </div>
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label>Condition After <span class="text-danger">*</span></label>
                                    <select name="condition_after" class="form-control" required>
                                        @foreach(['new','good','fair','poor','damaged','non_functional'] as $c)
                                            <option value="{{ $c }}" {{ $c == 'good' ? 'selected' : '' }}>{{ ucfirst(str_replace('_',' ',$c)) }}</option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label>Total Cost (AED) <span class="text-danger">*</span></label>
                                    <input type="number" name="total_cost" class="form-control" step="0.01" min="0" value="{{ ($record->labor_cost ?? 0) + ($record->parts_cost ?? 0) }}" required>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label>Findings</label>
                            <textarea name="findings" class="form-control" rows="2" placeholder="What was found during maintenance">{{ $record->findings }}</textarea>
                        </div>
                        <div class="form-group">
                            <label>Actions Taken</label>
                            <textarea name="actions_taken" class="form-control" rows="2" placeholder="What actions were performed">{{ $record->actions_taken }}</textarea>
                        </div>
                        <div class="form-group mb-0">
                            <label>Completion Notes</label>
                            <textarea name="completion_notes" class="form-control" rows="2" placeholder="Additional completion notes"></textarea>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn btn-success"><i class="material-icons mr-1" style="font-size: 16px;">done</i> Complete</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    @endif
@endsection

@push('scripts')
<script>
function showCompleteModal() { $('#completeModal').modal('show'); }
</script>
@endpush
