@extends('layouts.app')
@section('title', $line->phone_number)
@section('content')
<div class="d-flex justify-content-between align-items-center mb-3">
    <h1 class="h4 mb-0">{{ $line->phone_number }} <span class="badge text-bg-secondary">{{ $line->status->label() }}</span></h1>
    @can('lines.edit')<a class="btn btn-outline-primary btn-sm" href="{{ route('lines.edit',$line) }}">{{ __('messages.edit') }}</a>@endcan
</div>
<ul class="nav nav-tabs page-tabs mb-3">
    @foreach(['overview' => __('messages.overview'), 'customer' => __('messages.customer'), 'plan' => __('messages.current_plan'), 'billing' => __('nav.billing'), 'invoices' => __('nav.invoices'), 'payments' => __('nav.payments'), 'history' => __('messages.ownership_history'), 'activity' => __('messages.activity')] as $key => $label)
        <li class="nav-item"><a class="nav-link {{ ($tab ?? 'overview') === $key ? 'active' : '' }}" href="{{ route('lines.show', [$line, 'tab' => $key]) }}">{{ $label }}</a></li>
    @endforeach
</ul>

@if(($tab ?? 'overview') === 'overview')
<div class="row g-3">
    <div class="col-md-6">
        <div class="card card-body">
            <div class="row gy-2">
                <div class="col-6"><div class="small text-muted">{{ __('messages.company') }}</div>{{ $line->telecomCompany?->name }}</div>
                <div class="col-6"><div class="small text-muted">{{ __('messages.sim_type') }}</div>{{ $line->sim_type->label() }}</div>
                <div class="col-6"><div class="small text-muted">{{ __('messages.line_price') }}</div>{{ money($line->sale_price) }}</div>
                <div class="col-6"><div class="small text-muted">{{ __('messages.status') }}</div>{{ $line->status->label() }}</div>
                <div class="col-6"><div class="small text-muted">{{ __('messages.iccid') }}</div>{{ $line->iccid ?: '—' }}</div>
                <div class="col-6"><div class="small text-muted">{{ __('messages.activation_date') }}</div>{{ $line->activation_date?->toDateString() ?: '—' }}</div>
            </div>
        </div>
    </div>
    <div class="col-md-6">
        <div class="card card-body">
            <div class="small text-muted">{{ __('messages.customer') }}</div>
            <div class="fs-5">{{ $line->customer?->name ?? '—' }}</div>
            <div class="small text-muted mt-3">{{ __('messages.current_plan') }}</div>
            <div class="fs-5">{{ $line->currentPlan?->name ?? __('messages.no_plan') }}</div>
            @if($line->currentPlan)
                <div>{{ money($line->currentPlan->price) }} · {{ $line->plan_start_date?->toDateString() }}</div>
            @endif
        </div>
    </div>
</div>
@endif

@if(($tab ?? '') === 'customer')
<div class="card card-body">
    <p>{{ $line->customer?->name ?? '—' }} · {{ $line->customer?->phone }}</p>
    @if($line->customer?->customer_type?->value === 'company')
        <p>{{ __('messages.main_account') }}: {{ $line->mainAccount?->name ?: '—' }}</p>
        <p>{{ __('messages.sub_account') }}: {{ $line->subAccount?->name ?: '—' }}</p>
    @endif
    @can('lines.transfer')
    <form method="post" action="{{ route('lines.transfer',$line) }}" class="row g-2 mt-2">
        @csrf
        <div class="col-md-6">
            <select class="form-select" name="customer_id" required>
                @foreach($customers ?? [] as $c)<option value="{{ $c->id }}">{{ $c->name }} — {{ $c->phone }}</option>@endforeach
            </select>
        </div>
        <div class="col-md-4"><input class="form-control" name="reason" placeholder="{{ __('messages.reason') }}" required></div>
        <div class="col-md-2"><button class="btn btn-outline-primary w-100">{{ __('messages.transfer_line') }}</button></div>
    </form>
    @endcan
    @can('lines.cancel')
    <form method="post" action="{{ route('lines.return',$line) }}" class="row g-2 mt-3">
        @csrf
        <div class="col-md-6"><input class="form-control" name="reason" required placeholder="{{ __('messages.reason') }}"></div>
        <div class="col-md-4">
            <select class="form-select" name="target">
                <option value="available">{{ __('status.available') }}</option>
                <option value="returned">{{ __('status.returned') }}</option>
            </select>
        </div>
        <div class="col-md-2"><button class="btn btn-outline-warning w-100">{{ __('messages.return_to_available') }}</button></div>
    </form>
    @endcan
</div>
@endif

@if(($tab ?? '') === 'plan')
<div class="card card-body mb-3">
    <div class="fs-5">{{ $line->currentPlan?->name ?? __('messages.no_plan') }}</div>
    @if($line->currentPlan)
        <div>{{ __('messages.plan_price') }}: {{ money($line->currentPlan->price) }}</div>
        <div>{{ __('messages.start_date') }}: {{ $line->plan_start_date?->toDateString() ?: '—' }}</div>
    @endif
</div>
<div class="card">
    <table class="table compact-table mb-0">
        <thead><tr><th>{{ __('messages.plan') }}</th><th>{{ __('messages.price') }}</th><th>{{ __('messages.start_date') }}</th><th>{{ __('messages.end_date') }}</th><th>{{ __('messages.status') }}</th></tr></thead>
        <tbody>
        @forelse(($assignments ?? []) as $assignment)
            <tr>
                <td>{{ $assignment->plan?->name }}</td>
                <td>{{ money($assignment->price_at_assignment) }}</td>
                <td>{{ $assignment->start_date?->toDateString() }}</td>
                <td>{{ $assignment->end_date?->toDateString() ?: '—' }}</td>
                <td>{{ $assignment->status->label() }}</td>
            </tr>
        @empty
            <tr><td colspan="5">{{ __('messages.no_results') }}</td></tr>
        @endforelse
        </tbody>
    </table>
    @isset($assignments) @include('partials.pagination', ['paginator' => $assignments]) @endisset
</div>
@endif

@if(($tab ?? '') === 'billing' && isset($snapshot))
    @include('billing.partials.snapshot', ['snapshot' => $snapshot])
@endif

@if(($tab ?? '') === 'invoices')
<div class="card">
    <table class="table compact-table mb-0">
        <thead><tr><th>#</th><th>{{ __('messages.date') }}</th><th>{{ __('messages.total') }}</th><th>{{ __('messages.status') }}</th></tr></thead>
        <tbody>
        @forelse(($invoices ?? []) as $inv)
            <tr><td><a href="{{ route('invoices.show',$inv) }}">{{ $inv->invoice_number }}</a></td><td>{{ $inv->date?->toDateString() }}</td><td>{{ money($inv->total) }}</td><td>{{ $inv->status->label() }}</td></tr>
        @empty<tr><td colspan="4">{{ __('messages.no_results') }}</td></tr>@endforelse
        </tbody>
    </table>
    @isset($invoices) @include('partials.pagination', ['paginator' => $invoices]) @endisset
</div>
@endif

@if(($tab ?? '') === 'payments')
<div class="card">
    <table class="table compact-table mb-0">
        <thead><tr><th>#</th><th>{{ __('messages.date') }}</th><th>{{ __('messages.amount') }}</th><th>{{ __('messages.user') }}</th></tr></thead>
        <tbody>
        @forelse(($payments ?? []) as $p)
            <tr><td><a href="{{ route('payments.show',$p) }}">{{ $p->payment_number }}</a></td><td>{{ $p->date?->toDateString() }}</td><td>{{ money($p->amount) }}</td><td>{{ $p->user?->name }}</td></tr>
        @empty<tr><td colspan="4">{{ __('messages.no_results') }}</td></tr>@endforelse
        </tbody>
    </table>
    @isset($payments) @include('partials.pagination', ['paginator' => $payments]) @endisset
</div>
@endif

@if(($tab ?? '') === 'history')
<div class="card">
    <table class="table compact-table mb-0">
        <thead><tr><th>{{ __('messages.date') }}</th><th>{{ __('messages.customer') }}</th><th>{{ __('messages.status') }}</th><th>{{ __('messages.reason') }}</th><th>{{ __('messages.user') }}</th></tr></thead>
        <tbody>
        @forelse(($ownership ?? []) as $h)
            <tr>
                <td>{{ format_datetime($h->changed_at) }}</td>
                <td>{{ $h->previousCustomer?->name }} → {{ $h->newCustomer?->name }}</td>
                <td>{{ $h->previous_status }} → {{ $h->new_status }}</td>
                <td>{{ $h->reason }}</td>
                <td>{{ $h->user?->name }}</td>
            </tr>
        @empty<tr><td colspan="5">{{ __('messages.no_results') }}</td></tr>@endforelse
        </tbody>
    </table>
    @isset($ownership) @include('partials.pagination', ['paginator' => $ownership]) @endisset
</div>
@endif

@if(($tab ?? '') === 'activity' && isset($activity))
    @include('partials.activity-timeline', ['activity' => $activity])
@endif
@endsection
