@extends('layouts.app')

@section('title', __('bms::lang.contract') . ' · ' . ($booking->booking_no ?: ('#' . $booking->id)))

@php
    $st = $booking->status;
    // Lifecycle stepper state.
    $steps = ['request' => $st !== null, 'contract' => in_array($st, ['confirmed','contracted','completed'], true), 'payment' => ($invoice['paid'] ?? 0) > 0, 'done' => $st === 'completed'];
    $money = fn($n) => '<span class="display_currency" data-currency_symbol="true">'.(float)$n.'</span>';
    $erpInv = $booking->erp_invoice_id;
@endphp

@section('content')
<section class="content-header">
    <h1><i class="fa fa-file-contract"></i> @lang('bms::lang.contract') — {{ $booking->booking_no ?: ('#'.$booking->id) }}
        <small>{{ optional($booking->contact)->name }}</small></h1>
    <a href="{{ route('bms.contracts.index') }}" class="tw-dw-btn tw-dw-btn-xs tw-dw-btn-outline" style="margin-top:6px;"><i class="fa fa-arrow-left"></i> @lang('bms::lang.contracts')</a>
    <a href="{{ route('bms.bookings.show', $booking->id) }}" class="tw-dw-btn tw-dw-btn-xs tw-dw-btn-outline" style="margin-top:6px;"><i class="fa fa-cog"></i> @lang('bms::lang.booking_details')</a>
</section>

<section class="content">
    @if (session('status'))<div class="alert alert-success">{{ session('status') }}</div>@endif
    @if (session('error'))<div class="alert alert-danger">{{ session('error') }}</div>@endif

    {{-- Lifecycle stepper --}}
    <div class="box box-solid"><div class="box-body">
        <ul class="bms-steps">
            <li class="{{ $steps['request'] ? 'on' : '' }}">1 · @lang('bms::lang.cstage_request')</li>
            <li class="{{ $steps['contract'] ? 'on' : '' }}">2 · @lang('bms::lang.cstage_contract')</li>
            <li class="{{ $steps['payment'] ? 'on' : '' }}">3 · @lang('bms::lang.cstage_payment')</li>
            <li class="{{ $steps['done'] ? 'on' : '' }}">4 · @lang('bms::lang.cstage_completed')</li>
        </ul>
    </div></div>

    <div class="row">
        {{-- Event details --}}
        <div class="col-md-5">
            @component('components.widget', ['class' => 'box-primary', 'title' => __('bms::lang.event_details')])
                <table class="table table-condensed">
                    <tr><td>@lang('bms::lang.client')</td><td>{{ optional($booking->contact)->name }} <small class="text-muted">{{ optional($booking->contact)->mobile }}</small></td></tr>
                    <tr><td>@lang('bms::lang.event_type')</td><td>{{ $booking->event_type }} · {{ $booking->attendees }} {{ __('bms::lang.attendees') }}</td></tr>
                    <tr><td>@lang('bms::lang.event_date')</td><td>{{ optional($booking->event_date)->format('Y-m-d') }}</td></tr>
                    <tr><td>@lang('bms::lang.halls')</td><td>@foreach($booking->halls as $bh)<span class="label label-default">{{ $hallNames[$bh->hall_id] ?? ('#'.$bh->hall_id) }}</span> @endforeach</td></tr>
                    <tr><td>@lang('bms::lang.total')</td><td>{!! $money($booking->total_amount) !!}</td></tr>
                </table>
            @endcomponent

            {{-- Documents (proposal + contract) --}}
            @component('components.widget', ['class' => 'box-default', 'title' => __('bms::lang.documents')])
                @if ($canManage)
                <div style="margin-bottom:10px;">
                    <form method="POST" action="{{ route('bms.bookings.document', [$booking->id, 'contract']) }}" style="display:inline">@csrf<button class="tw-dw-btn tw-dw-btn-xs tw-dw-btn-outline">+ @lang('bms::lang.doc_contract')</button></form>
                    <form method="POST" action="{{ route('bms.bookings.document', [$booking->id, 'beo']) }}" style="display:inline">@csrf<button class="tw-dw-btn tw-dw-btn-xs tw-dw-btn-outline">+ @lang('bms::lang.doc_beo')</button></form>
                </div>
                @endif
                @forelse ($documents as $d)
                    <div style="padding:5px 0;border-bottom:1px dashed #eee;">
                        @lang('bms::lang.doc_'.$d->type) <small class="text-muted">v{{ $d->version }} @if($d->accepted_at) · <span class="text-green">@lang('bms::lang.accepted')</span> @endif</small>
                        <span class="pull-right">
                            <a href="{{ route('bms.documents.view', [$d->id, 'en']) }}" target="_blank" class="tw-dw-btn tw-dw-btn-xs tw-dw-btn-outline">EN</a>
                            <a href="{{ route('bms.documents.view', [$d->id, 'ar']) }}" target="_blank" class="tw-dw-btn tw-dw-btn-xs tw-dw-btn-outline">ع</a>
                        </span>
                    </div>
                @empty <p class="text-muted">@lang('bms::lang.no_documents')</p> @endforelse
                @if ($proposals->count())
                    <hr><p class="text-muted" style="margin:0 0 4px;">@lang('bms::lang.cstage_request')</p>
                    @foreach ($proposals as $p)<div>@lang('bms::lang.doc_proposal') <small class="text-muted">v{{ $p->version }}</small></div>@endforeach
                @endif
            @endcomponent
        </div>

        {{-- Invoice + payment (default-script) --}}
        <div class="col-md-4">
            @component('components.widget', ['class' => 'box-primary', 'title' => __('bms::lang.invoice')])
                @if (! $erpInv)
                    <p class="text-muted">@lang('bms::lang.confirm_to_invoice')</p>
                @else
                    <table class="table table-condensed">
                        <tr><td>@lang('bms::lang.invoice_no')</td><td class="text-right">{{ $invoice['invoice_no'] ?? '—' }}</td></tr>
                        <tr><td>@lang('bms::lang.total')</td><td class="text-right">{!! $money($invoice['total'] ?? 0) !!}</td></tr>
                        <tr><td>@lang('bms::lang.paid')</td><td class="text-right text-green">{!! $money($invoice['paid'] ?? 0) !!}</td></tr>
                        <tr><td>@lang('bms::lang.due')</td><td class="text-right">{!! $money($invoice['due'] ?? 0) !!}</td></tr>
                    </table>
                    <a href="{{ route('sell.printInvoice', $erpInv) }}" target="_blank" class="tw-dw-btn tw-dw-btn-xs tw-dw-btn-outline"><i class="fa fa-print"></i> @lang('bms::lang.view_invoice')</a>
                    @if ($canManage)
                        <a href="{{ url('/payments/add_payment/'.$erpInv) }}" class="tw-dw-btn tw-dw-btn-xs tw-dw-btn-outline"><i class="fa fa-money-bill"></i> @lang('bms::lang.add_payment_erp')</a>
                    @endif
                    <hr>
                    <p class="text-muted" style="margin:0 0 6px;">@lang('bms::lang.payments')</p>
                    @forelse ($payments as $p)
                        <div style="font-size:.9em;">{{ substr((string)$p['paid_on'],0,10) }} · {{ $p['method'] }} <span class="pull-right">{!! $money($p['amount']) !!}</span></div>
                    @empty <p class="text-muted">—</p> @endforelse
                @endif
            @endcomponent
        </div>

        {{-- Tasks (the contract converted into work) --}}
        <div class="col-md-3">
            @component('components.widget', ['class' => 'box-default', 'title' => __('bms::lang.tasks')])
                @forelse ($tasks as $t)
                    <div style="padding:5px 0;border-bottom:1px dashed #eee;">
                        <small class="text-muted text-uppercase">{{ $t->department }}</small><br>{{ $t->title }}
                        <span class="pull-right label label-{{ $t->status==='done'?'success':($t->status==='cancelled'?'default':'warning') }}">{{ $t->status }}</span>
                    </div>
                @empty <p class="text-muted">@lang('bms::lang.no_tasks')</p> @endforelse
            @endcomponent
        </div>
    </div>
</section>

<style>
.bms-steps { list-style:none; margin:0; padding:0; display:flex; gap:8px; flex-wrap:wrap; }
.bms-steps li { flex:1; min-width:120px; text-align:center; padding:10px; border-radius:8px; background:#f4f4f4; color:#999; font-weight:600; }
.bms-steps li.on { background:linear-gradient(135deg,#c5a44f,#8a6d33); color:#fff; }
</style>
@endsection
