@extends('layouts.app')
@section('title', 'Discharge · ' . ($admission->pet->petname ?? 'Patient'))

@section('content')
<section class="content-header">
    <h1><i class="fa fa-door-open"></i> Discharge wizard
        <small>{{ optional($admission->pet)->petname }} · {{ $admission->admission_no }}</small>
    </h1>
</section>

<section class="content">

<div class="row">
    <div class="col-md-7">
        <div class="box box-solid">
            <div class="box-header with-border"><h3 class="box-title">Take-home instructions</h3></div>
            <div class="box-body">
                <div class="form-group">
                    <label>Template</label>
                    <select id="template_id" class="form-control">
                        <option value="">— pick —</option>
                        @foreach($templates as $t)<option value="{{ $t->id }}">{{ $t->name }}</option>@endforeach
                    </select>
                </div>
                <div class="row">
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label>Follow-up date</label>
                            <input type="date" id="follow_up_date" class="form-control">
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label>Discharge status</label>
                            <select id="discharge_status" class="form-control">
                                <option value="discharged">Discharged</option>
                                <option value="transferred">Transferred</option>
                                <option value="deceased">Deceased</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label>Clinical summary</label>
                    <textarea id="summary" class="form-control" rows="3"></textarea>
                </div>
                <div class="form-group">
                    <label>Rendered take-home text (preview)</label>
                    <textarea id="rendered" class="form-control" rows="8" readonly style="background:#f8fafc;"></textarea>
                </div>
                <button id="btn-preview" class="btn btn-default"><i class="fa fa-eye"></i> Preview</button>
                <button id="btn-finalise" class="btn btn-success"><i class="fa fa-check-circle"></i> Finalise discharge</button>
                <a href="{{ route('va.admissions.show', $admission->id) }}" class="btn btn-link">Cancel</a>
            </div>
        </div>
    </div>

    <div class="col-md-5">
        <div class="box box-solid">
            <div class="box-header with-border"><h3 class="box-title">Meds to go ({{ $medications->count() }})</h3></div>
            <div class="box-body">
                @forelse($medications as $m)
                    <div style="border-bottom:1px solid #eef;padding:6px 0;">
                        <strong>{{ $m->medicine_name }}</strong>
                        <span class="text-muted">· {{ $m->route }} · q{{ $m->frequency_hours }}h
                            @if($m->dose_total_mg) · {{ $m->dose_total_mg }} mg @endif
                        </span>
                    </div>
                @empty
                    <p class="text-muted">No medications ordered.</p>
                @endforelse
            </div>
        </div>

        <div class="box box-solid">
            <div class="box-header with-border"><h3 class="box-title">Fluids on file ({{ $fluids->count() }})</h3></div>
            <div class="box-body">
                @forelse($fluids as $f)
                    <div style="border-bottom:1px solid #eef;padding:6px 0;">
                        <strong>{{ $f->fluid_type }}</strong>
                        <span class="text-muted">· {{ $f->rate_ml_per_hr }} mL/h · {{ $f->total_volume_given_ml }} mL total</span>
                    </div>
                @empty
                    <p class="text-muted">No IV fluids.</p>
                @endforelse
            </div>
        </div>
    </div>
</div>

</section>
@endsection

@section('javascript')
<script>
$(function () {
    var TOKEN = $('meta[name="csrf-token"]').attr('content');
    var PREVIEW_URL  = '{{ route('va.discharge.preview', $admission->id) }}';
    var FINALISE_URL = '{{ route('va.discharge.finalise', $admission->id) }}';

    $('#btn-preview').on('click', function () {
        var tid = $('#template_id').val(); if (!tid) { toastr.warning('Pick a template.'); return; }
        $.post(PREVIEW_URL, {
            _token: TOKEN,
            template_id: tid,
            follow_up_date: $('#follow_up_date').val(),
            summary: $('#summary').val(),
        }).done(function (res) { $('#rendered').val(res.rendered); })
          .fail(function (xhr) { toastr.error((xhr.responseJSON && xhr.responseJSON.msg) || 'Preview failed.'); });
    });

    $('#btn-finalise').on('click', function () {
        if (!confirm('Finalise discharge? The invoice will be locked.')) return;
        $.post(FINALISE_URL, {
            _token: TOKEN,
            discharged_at: new Date().toISOString().slice(0, 19).replace('T', ' '),
            discharge_summary: $('#summary').val(),
            status: $('#discharge_status').val(),
        }).done(function (res) {
            toastr.success(res.msg);
            setTimeout(function () { window.location = '{{ route('va.admissions.show', $admission->id) }}'; }, 800);
        }).fail(function (xhr) { toastr.error((xhr.responseJSON && xhr.responseJSON.msg) || 'Discharge failed.'); });
    });
});
</script>
@endsection
