@extends('layouts.app')
@section('title', __('Treatment plans'))

@section('content')
<div class="d-flex flex-wrap justify-content-between align-items-center gap-2 mb-3">
    <h1 class="h4 fw-bold mb-0">📋 {{ __('Treatment plans') }}</h1>
    @can('create', App\Models\TreatmentPlan::class)
        <button class="btn mb-btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#planModal">
            + {{ __('New plan') }}
        </button>
    @endcan
</div>

<div class="d-flex gap-2 mb-3 flex-wrap">
    <a href="{{ route('tenant.treatment-plans.index') }}"
       class="btn btn-sm {{ request('status') ? 'mb-btn-ghost' : 'mb-btn-primary' }}">{{ __('All') }}</a>
    @foreach($statuses as $s)
        <a href="?status={{ $s->value }}"
           class="btn btn-sm {{ request('status') === $s->value ? 'mb-btn-primary' : 'mb-btn-ghost' }}">{{ $s->label() }}</a>
    @endforeach
</div>

<div class="mb-card p-3">
    <table class="table mb-table align-middle mb-0">
        <thead><tr>
            <th>{{ __('Plan') }}</th><th>{{ __('Patient') }}</th><th>{{ __('Dentist') }}</th>
            <th>{{ __('Progress') }}</th><th>{{ __('Total') }}</th><th>{{ __('Status') }}</th>
        </tr></thead>
        <tbody>
        @forelse($plans as $plan)
            @php($pr = $plan->progress())
            <tr>
                <td><a href="{{ route('tenant.treatment-plans.show', $plan) }}">{{ $plan->title }}</a></td>
                <td>{{ $plan->patient->full_name }}</td>
                <td class="mb-muted">{{ $plan->dentist->name }}</td>
                <td class="mb-muted">{{ $pr['done'] }}/{{ $pr['total'] }}</td>
                <td dir="ltr">{{ number_format($plan->totalNet()) }} IQD</td>
                <td><span class="status-chip" style="background: {{ $plan->status->color() }}">{{ $plan->status->label() }}</span></td>
            </tr>
        @empty
            <tr><td colspan="6" class="mb-muted py-4 text-center">{{ __('No treatment plans yet.') }}</td></tr>
        @endforelse
        </tbody>
    </table>
</div>
{{ $plans->links() }}

@can('create', App\Models\TreatmentPlan::class)
<div class="modal fade" id="planModal" tabindex="-1">
    <div class="modal-dialog">
        <form class="modal-content mb-card" method="POST" action="{{ route('tenant.treatment-plans.store') }}">
            @csrf
            <div class="modal-header" style="border-color:var(--mb-line)">
                <h2 class="h6 fw-bold mb-0">{{ __('New plan') }}</h2>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div class="mb-3">
                    <label class="form-label">{{ __('Title') }}</label>
                    <input name="title" class="form-control" placeholder="{{ __('e.g. Upper right restoration') }}" required>
                </div>
                <div class="mb-3">
                    <label class="form-label">{{ __('Patient') }}</label>
                    <select name="patient_id" class="form-select" required>
                        @foreach(\App\Models\Patient::orderBy('full_name')->limit(500)->get(['id','full_name','phone']) as $p)
                            <option value="{{ $p->id }}">{{ $p->full_name }} — {{ $p->phone }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="mb-3">
                    <label class="form-label">{{ __('Dentist') }}</label>
                    <select name="dentist_id" class="form-select" required>
                        @foreach(\App\Models\Dentist::active()->get(['id','name']) as $d)
                            <option value="{{ $d->id }}">{{ $d->name }}</option>
                        @endforeach
                    </select>
                </div>
            </div>
            <div class="modal-footer" style="border-color:var(--mb-line)">
                <button class="btn mb-btn-primary w-100">{{ __('Create plan') }}</button>
            </div>
        </form>
    </div>
</div>
@endcan

@push('page-scripts')
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
@endpush
@endsection
