@extends('layouts.app')

@section('title', isset($pet) ? __('vetClinic::lang.edit') : __('vetClinic::lang.add_pet'))

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

@section('content')
<section class="content-header">
    <h1><i class="fas fa-paw"></i>
        {{ isset($pet) ? __('vetClinic::lang.edit') . ' — ' . $pet->petname : __('vetClinic::lang.add_pet') }}
    </h1>
</section>

<section class="content">
    <form method="POST" action="{{ isset($pet) ? route('vc.pets.update', $pet->id) : route('vc.pets.store') }}"
          enctype="multipart/form-data">
        @csrf
        <div class="row">
            <div class="col-md-8">
                @component('components.widget', ['class' => 'box-primary', 'title' => __('vetClinic::lang.pet') . ' ' . __('vetClinic::lang.description')])
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>@lang('vetClinic::lang.pet_owner') <span class="text-danger">*</span></label>
                                <select name="contact_id" id="contact_id" class="form-control" style="width:100%" required
                                    {{ isset($preselectedContact) ? 'disabled' : '' }}>
                                    @if(isset($pet) && $pet->owner)
                                        <option value="{{ $pet->contact_id }}" selected>{{ $pet->owner->name }}</option>
                                    @elseif(isset($preselectedContact))
                                        <option value="{{ $preselectedContact->id }}" selected>
                                            {{ $preselectedContact->name }}{{ $preselectedContact->mobile ? ' (' . $preselectedContact->mobile . ')' : '' }}
                                        </option>
                                    @endif
                                </select>
                                @if(isset($preselectedContact))
                                    <input type="hidden" name="contact_id" value="{{ $preselectedContact->id }}">
                                    <small class="text-muted">{{ $preselectedContact->name }} — <a href="{{ route('vc.pet-owners.show', optional(\Modules\VetClinic\Entities\VcPetOwnerDetail::where('contact_id', $preselectedContact->id)->first())->id ?? 0) }}">@lang('vetClinic::lang.pet_owner')</a></small>
                                @endif
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>@lang('vetClinic::lang.petname') <span class="text-danger">*</span></label>
                                <input type="text" name="petname" class="form-control"
                                    value="{{ old('petname', isset($pet) ? $pet->petname : '') }}" required>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>@lang('vetClinic::lang.species')</label>
                                <select name="species" id="species" class="form-control" style="width:100%">
                                    @php $selectedSpecies = old('species', isset($pet) ? $pet->species : ''); @endphp
                                    @foreach(['Dog','Cat','Rabbit','Bird','Hamster','Guinea Pig','Tortoise','Fish','Snake'] as $sp)
                                        <option value="{{ $sp }}" {{ $selectedSpecies === $sp ? 'selected' : '' }}>{{ $sp }}</option>
                                    @endforeach
                                    @if($selectedSpecies && !in_array($selectedSpecies, ['Dog','Cat','Rabbit','Bird','Hamster','Guinea Pig','Tortoise','Fish','Snake']))
                                        <option value="{{ $selectedSpecies }}" selected>{{ $selectedSpecies }}</option>
                                    @endif
                                </select>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>@lang('vetClinic::lang.breed')</label>
                                <input type="text" name="breed" class="form-control"
                                    value="{{ old('breed', isset($pet) ? $pet->breed : '') }}">
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>@lang('vetClinic::lang.color')</label>
                                <input type="text" name="color" class="form-control"
                                    value="{{ old('color', isset($pet) ? $pet->color : '') }}">
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-3">
                            <div class="form-group">
                                <label>@lang('vetClinic::lang.gender')</label>
                                <select name="gender" class="form-control">
                                    <option value="unknown" {{ old('gender', isset($pet) ? $pet->gender : '') == 'unknown' ? 'selected' : '' }}>@lang('vetClinic::lang.unknown')</option>
                                    <option value="male" {{ old('gender', isset($pet) ? $pet->gender : '') == 'male' ? 'selected' : '' }}>@lang('vetClinic::lang.male')</option>
                                    <option value="female" {{ old('gender', isset($pet) ? $pet->gender : '') == 'female' ? 'selected' : '' }}>@lang('vetClinic::lang.female')</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label>@lang('vetClinic::lang.dob')</label>
                                <input type="text" id="dob_display" class="form-control" autocomplete="off" placeholder="Date of Birth">
                                <input type="hidden" name="dob" id="dob_iso" value="{{ old('dob', isset($pet) && $pet->dob ? $pet->dob->format('Y-m-d') : '') }}">
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label>@lang('vetClinic::lang.bloodgroup')</label>
                                <input type="text" name="bloodgroup" class="form-control"
                                    value="{{ old('bloodgroup', isset($pet) ? $pet->bloodgroup : '') }}">
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>@lang('vetClinic::lang.microchip_no')</label>
                                <input type="text" name="microchip_no" class="form-control"
                                    value="{{ old('microchip_no', isset($pet) ? $pet->microchip_no : '') }}">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>@lang('vetClinic::lang.serial_no')</label>
                                <input type="text" name="serial_no" class="form-control"
                                    value="{{ old('serial_no', isset($pet) ? $pet->serial_no : '') }}">
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>@lang('vetClinic::lang.status')</label>
                                <select name="status" class="form-control">
                                    <option value="active" {{ old('status', isset($pet) ? $pet->status : 'active') == 'active' ? 'selected' : '' }}>@lang('vetClinic::lang.active')</option>
                                    <option value="inactive" {{ old('status', isset($pet) ? $pet->status : '') == 'inactive' ? 'selected' : '' }}>@lang('vetClinic::lang.inactive')</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group" style="padding-top:25px">
                                <label>
                                    <input type="checkbox" name="neutered" value="1"
                                        {{ old('neutered', isset($pet) && $pet->neutered ? true : false) ? 'checked' : '' }}>
                                    @lang('vetClinic::lang.neutered')
                                </label>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-8">
                            <div class="form-group">
                                <label><i class="fa fa-user-md text-primary"></i> @lang('vetClinic::lang.preferred_doctor')
                                    <small class="text-muted">(@lang('vetClinic::lang.pet_level_override'))</small>
                                </label>
                                <select name="preferred_doctor_id" id="preferred_doctor_id" class="form-control" style="width:100%">
                                    <option value="">— @lang('vetClinic::lang.use_owner_preferred_doctor') —</option>
                                    @foreach(($doctors ?? []) as $doc)
                                        <option value="{{ $doc->id }}"
                                            {{ (string) old('preferred_doctor_id', isset($pet) ? $pet->preferred_doctor_id : '') === (string) $doc->id ? 'selected' : '' }}>
                                            {{ $doc->name }}{{ $doc->specialization ? ' — ' . $doc->specialization : '' }}
                                        </option>
                                    @endforeach
                                </select>
                                <small class="text-muted">@lang('vetClinic::lang.pet_preferred_doctor_hint')</small>
                            </div>
                        </div>
                    </div>
                @endcomponent
            </div>

            <div class="col-md-4">
                @component('components.widget', ['class' => 'box-default', 'title' => __('vetClinic::lang.photo')])
                    <div class="form-group">
                        <input type="file" name="photo" accept="image/*">
                    </div>
                @endcomponent

                <div class="form-group">
                    <button type="submit" class="btn btn-primary btn-block">
                        <i class="fa fa-save"></i>
                        {{ isset($pet) ? __('vetClinic::lang.update') : __('vetClinic::lang.save') }}
                    </button>
                    <a href="{{ route('vc.pets.index') }}" class="btn btn-default btn-block">@lang('vetClinic::lang.cancel')</a>
                </div>
            </div>
        </div>
    </form>
</section>
@endsection

@section('javascript')
<script>
$(document).ready(function() {
    $('#dob_display').datepicker({
        todayHighlight: true,
        autoclose: true,
        format: datepicker_date_format,
        endDate: new Date()
    }).on('changeDate', function(e) {
        if (e.dates.length) {
            var d = e.dates[0];
            var iso = d.getFullYear() + '-' +
                      String(d.getMonth() + 1).padStart(2, '0') + '-' +
                      String(d.getDate()).padStart(2, '0');
            $('#dob_iso').val(iso);
        }
    }).on('clearDate', function() {
        $('#dob_iso').val('');
    });

    var dobIso = $('#dob_iso').val();
    if (dobIso) {
        var parts = dobIso.split('-');
        $('#dob_display').datepicker('setDate',
            new Date(+parts[0], +parts[1] - 1, +parts[2])
        );
    }

    $('#species').select2({
        width: '100%',
        tags: true,
        placeholder: 'Dog, Cat, Rabbit...',
        allowClear: true
    });

    $('#preferred_doctor_id').select2({
        width: '100%',
        placeholder: '— Use owner\'s preferred doctor —',
        allowClear: true
    });

    @if(!isset($preselectedContact))
    $('#contact_id').select2({
        width: '100%',
        minimumInputLength: 1,
        minimumResultsForSearch: -1,
        ajax: {
            url: '{{ route('vc.pet-owners.search') }}',
            dataType: 'json',
            delay: 300,
            data: function (params) { return { q: params.term }; },
            processResults: function (data) { return { results: data }; }
        },
        placeholder: '{{ __("vetClinic::lang.search") }} owner...',
        language: {
            inputTooShort: function() { return 'Type to search...'; },
            noResults: function() { return 'No owners found'; },
            searching: function() { return 'Searching...'; }
        }
    });
    @endif
});
</script>
@endsection
