@extends('layouts.app')

@section('title', 'Authorised Vets')

@section('css')
@include('vetClinic::partials._mobile_responsive')
@endsection

@section('content')
<section class="content-header">
    <h1><i class="fa fa-id-card-alt"></i> Authorised Vets
        <small style="color:#64748b;">Permit holders for controlled-drug dispensing</small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="{{ route('vc.departments.index') }}">@lang('vetClinic::lang.settings')</a></li>
        <li class="active">Authorised Vets</li>
    </ol>
</section>

<section class="content">

    <div class="alert alert-info" style="font-size:12.5px;line-height:1.55;">
        <i class="fa fa-info-circle"></i>
        Only vets listed here may be selected as the issuing vet when dispensing a controlled substance.
        Each row records <strong>which schedules</strong> the vet is permitted to dispense and when the
        permit <strong>expires</strong>. An expired permit blocks dispensing immediately; permits expiring
        within 30 days are flagged on this page and on the register.
    </div>

    <div class="row">
        <div class="col-md-7">
            @component('components.widget', ['class' => 'box-primary'])
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th>Doctor</th>
                            <th>Schedules covered</th>
                            <th>Permit no.</th>
                            <th>Valid until</th>
                            <th>Status</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($rows as $r)
                            @php
                                $isExpired  = $r->valid_until && $r->valid_until->isPast();
                                $isExpiring = $r->is_expiring;
                                $statusCls  = $isExpired ? 'danger' : ($isExpiring ? 'warning' : 'success');
                                $statusTxt  = $isExpired ? 'EXPIRED' : ($isExpiring ? 'Expiring' : 'Valid');
                            @endphp
                            <tr>
                                <td>
                                    <strong>{{ optional($r->doctor)->name ?? '—' }}</strong>
                                    @if(optional($r->doctor)->specialization)
                                        <br><small class="text-muted">{{ $r->doctor->specialization }}</small>
                                    @endif
                                </td>
                                <td>
                                    @foreach($r->schedules ?? [] as $s)
                                        @php $cfg = $schedules[$s] ?? null; @endphp
                                        @if($cfg)
                                            <span class="label label-{{ $cfg['color'] }}" style="font-size:9.5px;">{{ $cfg['label'] }}</span>
                                        @endif
                                    @endforeach
                                </td>
                                <td>{{ $r->permit_no ?: '—' }}</td>
                                <td>{{ $r->valid_until ? $r->valid_until->format('d M Y') : 'No expiry' }}</td>
                                <td>
                                    <span class="label label-{{ $statusCls }}">{{ $statusTxt }}</span>
                                    @if($r->document_path)
                                        <br><a href="{{ \Illuminate\Support\Facades\Storage::disk('public')->url($r->document_path) }}" target="_blank" style="font-size:11px;">
                                            <i class="fa fa-paperclip"></i> Permit
                                        </a>
                                    @endif
                                </td>
                                <td>
                                    <form method="POST" action="{{ url('vet-clinic/authorized-vets/'.$r->id) }}"
                                          onsubmit="return confirm('Remove this authorisation?');" style="display:inline;">
                                        @csrf @method('DELETE')
                                        <button class="btn btn-xs btn-danger"><i class="fa fa-trash"></i></button>
                                    </form>
                                </td>
                            </tr>
                        @empty
                            <tr><td colspan="6" class="text-center text-muted">No authorised vets yet — add one on the right.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            @endcomponent
        </div>

        <div class="col-md-5">
            @component('components.widget', ['class' => 'box-warning'])
                <h4 style="margin-top:0;"><i class="fa fa-plus-circle"></i> Add / update an authorisation</h4>
                <form method="POST" action="{{ route('vc.authorized-vets.store') }}" enctype="multipart/form-data">
                    @csrf
                    <div class="form-group">
                        <label>Doctor <span class="text-danger">*</span></label>
                        <select name="doctor_id" class="form-control" required>
                            <option value="">— Select a doctor —</option>
                            @foreach($doctors as $d)
                                <option value="{{ $d->id }}" {{ isset($rows[$d->id]) ? 'data-existing="1"' : '' }}>
                                    {{ $d->name }}{{ isset($rows[$d->id]) ? ' (already authorised — submit to update)' : '' }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                    <div class="form-group">
                        <label>Schedules covered <span class="text-danger">*</span></label>
                        @foreach($schedules as $key => $cfg)
                            <div class="checkbox" style="margin:2px 0;">
                                <label style="font-weight:normal;">
                                    <input type="checkbox" name="schedules[]" value="{{ $key }}">
                                    <span class="label label-{{ $cfg['color'] }}" style="font-size:9.5px;">{{ $cfg['label'] }}</span>
                                </label>
                            </div>
                        @endforeach
                    </div>
                    <div class="row">
                        <div class="col-md-7"><div class="form-group">
                            <label>Permit number</label>
                            <input type="text" name="permit_no" class="form-control" placeholder="e.g. NRC-2026-001234">
                        </div></div>
                        <div class="col-md-5"><div class="form-group">
                            <label>Valid until</label>
                            <input type="date" name="valid_until" class="form-control">
                        </div></div>
                    </div>
                    <div class="form-group">
                        <label>Permit document <small class="text-muted">(PDF or image, optional)</small></label>
                        <input type="file" name="document" accept=".pdf,.jpg,.jpeg,.png">
                    </div>
                    <div class="form-group">
                        <label>Notes</label>
                        <textarea name="notes" class="form-control" rows="2"></textarea>
                    </div>
                    <div style="text-align:right;">
                        <button class="btn btn-primary"><i class="fa fa-save"></i> Save authorisation</button>
                    </div>
                </form>
            @endcomponent
        </div>
    </div>
</section>
@endsection
