@extends('layouts.app')

@section('title', $req->code)

@php
  $statusLabel = fn ($s) => $s ? __('bms::lang.mst_'.$s) : '';
  $statusBadge = [
    'submitted'=>'label-default','reviewed'=>'label-info','assigned'=>'label-primary',
    'in_progress'=>'label-warning','resolved'=>'label-success','verified'=>'label-success','cancelled'=>'label-default',
  ];
  $prioBadge = ['low'=>'label-default','medium'=>'label-info','high'=>'label-warning','urgent'=>'label-danger'];
@endphp

@section('content')

<section class="content-header">
    <h1>{{ $req->code }} — {{ $req->title }}
        <small>
            <span class="label {{ $prioBadge[$req->priority] ?? 'label-default' }}">@lang('bms::lang.prio_'.$req->priority)</span>
            <span class="label {{ $statusBadge[$req->status] ?? 'label-default' }}">{{ $statusLabel($req->status) }}</span>
            @if ($req->isOverdue())<span class="label label-danger">@lang('bms::lang.overdue')</span>@endif
        </small>
    </h1>
</section>

<section class="content">
    <a href="{{ route('bms.maintenance.index') }}" class="tw-dw-btn tw-dw-btn-outline tw-dw-btn-neutral" style="margin-bottom:14px;">
        <i class="fa fa-arrow-left"></i> @lang('bms::lang.back_to_maintenance')
    </a>

    <div class="row">
        <div class="col-md-7">
            @component('components.widget', ['class' => 'box-solid', 'title' => __('bms::lang.details')])
                <table class="table table-striped">
                    <tr><th style="width:35%">@lang('bms::lang.target')</th><td>{{ $req->hall_id ? ($halls[$req->hall_id] ?? '—') : ($locations[$req->location_id] ?? __('bms::lang.office')) }}</td></tr>
                    <tr><th>@lang('bms::lang.category')</th><td>@lang('bms::lang.mcat_'.$req->category)</td></tr>
                    @if ($req->asset)<tr><th>@lang('bms::lang.affected_asset')</th><td>{{ $req->asset->code }} · {{ $req->asset->name }}</td></tr>@endif
                    <tr><th>@lang('bms::lang.assignee')</th><td>{{ $req->assigned_to ? ($users[$req->assigned_to] ?? '—') : __('bms::lang.unassigned') }}</td></tr>
                    <tr><th>@lang('bms::lang.reported_by')</th><td>{{ $req->reported_by ? ($users[$req->reported_by] ?? '—') : '—' }}</td></tr>
                    <tr><th>@lang('bms::lang.sla_due')</th><td>{{ optional($req->due_at)->format('Y-m-d H:i') ?? '—' }}</td></tr>
                    <tr><th>@lang('messages.date')</th><td>{{ optional($req->created_at)->format('Y-m-d H:i') }}</td></tr>
                    @if ($req->description)<tr><th>@lang('bms::lang.description')</th><td>{{ $req->description }}</td></tr>@endif
                </table>
            @endcomponent

            @component('components.widget', ['class' => 'box-primary', 'title' => __('bms::lang.history')])
                @forelse ($req->updates as $u)
                    <div style="border-inline-start:3px solid #ddd; padding:6px 14px; margin-bottom:10px;">
                        <div>
                            @if ($u->to_status)
                                <span class="label label-default">{{ $statusLabel($u->from_status) ?: '—' }}</span> →
                                <span class="label {{ $statusBadge[$u->to_status] ?? 'label-default' }}">{{ $statusLabel($u->to_status) }}</span>
                            @else
                                <i class="fa fas fa-comment-dots text-muted"></i> @lang('bms::lang.comment')
                            @endif
                            <small class="text-muted pull-right">{{ optional($u->created_at)->format('Y-m-d H:i') }}</small>
                        </div>
                        @if ($u->comment)<p style="margin:6px 0 0;">{{ $u->comment }}</p>@endif
                        <small class="text-muted">{{ $u->user_id ? ($users[$u->user_id] ?? ('#'.$u->user_id)) : '' }}</small>
                    </div>
                @empty
                    <p class="text-muted">@lang('bms::lang.no_updates')</p>
                @endforelse
            @endcomponent
        </div>

        <div class="col-md-5">
            @can('bms.maintenance.manage')
            @component('components.widget', ['class' => 'box-warning', 'title' => __('bms::lang.update_request')])
                @if (count($next))
                    <div class="form-group">
                        <label>@lang('bms::lang.move_to')</label><br>
                        @foreach ($next as $to)
                            <button class="tw-dw-btn tw-dw-btn-sm tw-dw-btn-primary mnt-move" data-to="{{ $to }}" style="margin:2px;">
                                {{ $statusLabel($to) }}
                            </button>
                        @endforeach
                    </div>
                @else
                    <p class="text-muted">@lang('bms::lang.workflow_closed')</p>
                @endif

                <div class="form-group">
                    <label>@lang('bms::lang.assign_to')</label>
                    <select id="mnt_assign" class="form-control">
                        <option value="">@lang('bms::lang.unassigned')</option>
                        @foreach ($users as $id => $name)<option value="{{ $id }}" @selected($req->assigned_to == $id)>{{ $name }}</option>@endforeach
                    </select>
                </div>
                <div class="form-group">
                    <label>@lang('bms::lang.comment')</label>
                    <textarea id="mnt_comment" class="form-control" rows="2" placeholder="@lang('bms::lang.comment_ph')"></textarea>
                </div>
                <button id="mnt_comment_btn" class="tw-dw-btn tw-dw-btn-outline tw-dw-btn-primary">
                    <i class="fa fas fa-comment"></i> @lang('bms::lang.add_comment')
                </button>
            @endcomponent
            @endcan
        </div>
    </div>
</section>

@endsection

@section('javascript')
<script>
$(function () {
    var csrf = '{{ csrf_token() }}';
    function post(url, data) {
        return $.ajax({ url: url, method: 'POST', data: $.extend({ _token: csrf }, data) });
    }
    $('.mnt-move').on('click', function () {
        var to = $(this).data('to');
        post('{{ url('bms/maintenance/'.$req->id.'/transition') }}', {
            to: to, comment: $('#mnt_comment').val(), assigned_to: $('#mnt_assign').val() || null
        }).done(function (r) {
            if (r && r.success === false) { toastr.error(r.msg); } else { location.reload(); }
        }).fail(function () { toastr.error('{{ __('messages.something_went_wrong') }}'); });
    });
    $('#mnt_comment_btn').on('click', function () {
        var c = $('#mnt_comment').val();
        if (!c) return;
        post('{{ url('bms/maintenance/'.$req->id.'/comment') }}', { comment: c })
            .done(function () { location.reload(); })
            .fail(function () { toastr.error('{{ __('messages.something_went_wrong') }}'); });
    });
});
</script>
@endsection
