@extends('layouts.app')
@section('title', $dentist->exists ? __('Edit dentist') : __('Add dentist'))

@php
    $days = [0 => __('Sunday'), 1 => __('Monday'), 2 => __('Tuesday'), 3 => __('Wednesday'),
             4 => __('Thursday'), 5 => __('Friday'), 6 => __('Saturday')];
    $existing = $dentist->exists
        ? $dentist->schedules->groupBy('weekday')->map(
            fn ($rows) => $rows->map(fn ($r) => substr($r->start_time, 0, 5).'-'.substr($r->end_time, 0, 5))->values()
          )->toArray()
        : [];
@endphp

@section('content')
<h1 class="h4 fw-bold mb-3">{{ $dentist->exists ? __('Edit dentist') : __('Add dentist') }}</h1>

<form method="POST"
      action="{{ $dentist->exists ? route('tenant.dentists.update', $dentist) : route('tenant.dentists.store') }}">
    @csrf
    @if($dentist->exists) @method('PUT') @endif

    <div class="row g-3">
        <div class="col-lg-5">
            <div class="mb-card p-4">
                <div class="mb-3">
                    <label class="form-label">{{ __('Name') }}</label>
                    <input name="name" value="{{ old('name', $dentist->name) }}" class="form-control @error('name') is-invalid @enderror" required>
                    @error('name')<div class="invalid-feedback">{{ $message }}</div>@enderror
                </div>
                <div class="mb-3">
                    <label class="form-label">{{ __('Specialty') }}</label>
                    <input name="specialty" value="{{ old('specialty', $dentist->specialty) }}" class="form-control" placeholder="{{ __('e.g. Orthodontist') }}">
                </div>
                <div class="row">
                    <div class="col-4 mb-3">
                        <label class="form-label">{{ __('Color') }}</label>
                        <input type="color" name="color" value="{{ old('color', $dentist->color) }}" class="form-control form-control-color w-100">
                    </div>
                    <div class="col-4 mb-3">
                        <label class="form-label">{{ __('Slot (min)') }}</label>
                        <input type="number" name="slot_duration_min" value="{{ old('slot_duration_min', $dentist->slot_duration_min) }}" min="5" max="240" step="5" class="form-control">
                    </div>
                    <div class="col-4 mb-3">
                        <label class="form-label">{{ __('Buffer (min)') }}</label>
                        <input type="number" name="buffer_min" value="{{ old('buffer_min', $dentist->buffer_min) }}" min="0" max="120" step="5" class="form-control">
                    </div>
                </div>
                <div class="row">
                    <div class="col-6 mb-3">
                        <label class="form-label">{{ __('Status') }}</label>
                        <select name="status" class="form-select">
                            <option value="active" @selected(old('status', $dentist->status) === 'active')>{{ __('Active') }}</option>
                            <option value="inactive" @selected(old('status', $dentist->status) === 'inactive')>{{ __('Inactive') }}</option>
                        </select>
                    </div>
                    <div class="col-6 mb-3 d-flex align-items-end">
                        <div class="form-check">
                            <input type="hidden" name="is_bookable_online" value="0">
                            <input class="form-check-input" type="checkbox" name="is_bookable_online" value="1" id="online"
                                   @checked(old('is_bookable_online', $dentist->is_bookable_online ?? true))>
                            <label class="form-check-label" for="online">{{ __('Online booking') }}</label>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-lg-7">
            <div class="mb-card p-4">
                <h2 class="h6 fw-semibold mb-3">{{ __('Weekly schedule') }}</h2>
                <p class="mb-muted small">{{ __('One or more ranges per day, format 09:00-13:00. Leave blank for days off.') }}</p>
                @error('schedules.*.*')<div class="alert alert-danger small">{{ $message }}</div>@enderror
                @foreach($days as $i => $day)
                    <div class="row align-items-center mb-2">
                        <div class="col-4 col-md-3 fw-semibold">{{ $day }}</div>
                        <div class="col-4">
                            <input name="schedules[{{ $i }}][]" dir="ltr" class="form-control form-control-sm"
                                   placeholder="09:00-13:00"
                                   value="{{ old("schedules.$i.0", $existing[$i][0] ?? '') }}">
                        </div>
                        <div class="col-4">
                            <input name="schedules[{{ $i }}][]" dir="ltr" class="form-control form-control-sm"
                                   placeholder="16:00-20:00"
                                   value="{{ old("schedules.$i.1", $existing[$i][1] ?? '') }}">
                        </div>
                    </div>
                @endforeach
            </div>
        </div>
    </div>

    <div class="mt-3 d-flex gap-2">
        <button class="btn mb-btn-primary">{{ __('Save changes') }}</button>
        <a href="{{ route('tenant.dentists.index') }}" class="btn mb-btn-ghost">{{ __('Cancel') }}</a>
    </div>
</form>
@endsection
