@extends('layouts.app')

@section('title', __('appointments.details_title'))

@push('styles')
    <link rel="stylesheet" href="{{ asset('css/accounts.css') }}">
    <style>
        .visit-item {
            border: 1px solid rgba(0, 0, 0, 0.08);
            border-radius: 14px;
            padding: 14px 14px;
            background: #fff;
        }
        body.dark-mode .visit-item {
            background: #111827;
            border-color: rgba(255, 255, 255, 0.12);
        }
        .visit-item .meta-label {
            font-size: 12px;
            color: rgba(0, 0, 0, 0.55);
        }
        body.dark-mode .visit-item .meta-label {
            color: rgba(255, 255, 255, 0.6);
        }
        .visit-item .meta-value {
            font-weight: 800;
            color: rgba(0, 0, 0, 0.86);
        }
        body.dark-mode .visit-item .meta-value {
            color: rgba(255, 255, 255, 0.9);
        }
    </style>
@endpush

@section('content')
    <div class="container-fluid py-3">
        <div class="d-flex flex-column flex-md-row align-items-start align-items-md-center justify-content-between gap-2 mb-3">
            <div>
                <h4 class="mb-1">{{ __('appointments.details_title') }}</h4>
                <div class="text-muted small">{{ $appointment->name }}</div>
            </div>
            <div class="d-flex gap-2">
                <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addVisitModal">
                    <i class="bi bi-plus-lg"></i>
                    <span>{{ __('appointments.add_visit') }}</span>
                </button>
                @if($appointment->getAttribute('whatsapp_url'))
                    <a class="btn btn-success" href="{{ $appointment->getAttribute('whatsapp_url') }}" target="_blank" rel="noopener">
                        <i class="bi bi-whatsapp"></i>
                        <span>{{ __('appointments.send_whatsapp') }}</span>
                    </a>
                @endif
                <a href="{{ route('appointments.index') }}" class="btn btn-outline-secondary">
                    <i class="bi bi-arrow-left"></i>
                    <span>{{ __('common.back') }}</span>
                </a>
            </div>
        </div>

        @if(session('success'))
            <div class="alert alert-success">{{ session('success') }}</div>
        @endif

        @if($errors->any())
            <div class="alert alert-danger">{{ __('appointments.validation_error') }}</div>
        @endif

        <div class="card border-0 shadow-sm mb-3">
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-12 col-md-4">
                        <div class="text-muted small">{{ __('appointments.phone') }}</div>
                        <div class="fw-bold">{{ $appointment->phone }}</div>
                    </div>
                    <div class="col-12 col-md-4">
                        <div class="text-muted small">{{ __('appointments.whatsapp_phone') }}</div>
                        <div class="fw-bold">{{ $appointment->whatsapp_phone }}</div>
                    </div>
                    <div class="col-12 col-md-4">
                        <div class="text-muted small">{{ __('appointments.project_type') }}</div>
                        <div class="fw-bold">{{ $appointment->project_type }}</div>
                    </div>
                    <div class="col-12">
                        <div class="text-muted small">{{ __('appointments.address') }}</div>
                        <div class="fw-bold">{{ $appointment->address }}</div>
                    </div>
                    @if($appointment->project_details)
                        <div class="col-12">
                            <div class="text-muted small">{{ __('appointments.project_details') }}</div>
                            <div class="fw-bold">{{ $appointment->project_details }}</div>
                        </div>
                    @endif
                </div>
            </div>
        </div>

        <div class="d-flex align-items-center justify-content-between mb-2">
            <div class="fw-semibold">{{ __('appointments.visits') }}</div>
            <div class="text-muted small">
                {{ __('appointments.next_visit') }}:
                @if($appointment->getAttribute('next_visit')?->visit_datetime)
                    <span class="fw-semibold">{{ $appointment->getAttribute('next_visit')->visit_datetime->translatedFormat('Y-m-d h:i A') }}</span>
                @else
                    <span class="fw-semibold">{{ __('appointments.no_visits') }}</span>
                @endif
            </div>
        </div>

        <div class="d-grid gap-2">
            @forelse($appointment->visits as $visit)
                <div class="visit-item">
                    <div class="d-flex flex-column flex-md-row justify-content-between gap-2">
                        <div>
                            <div class="meta-label">{{ __('appointments.visit_datetime') }}</div>
                            <div class="meta-value">{{ $visit->visit_datetime?->translatedFormat('Y-m-d h:i A') }}</div>
                        </div>
                        <div class="text-md-end">
                            <div class="meta-label">{{ __('appointments.visit_details') }}</div>
                            <div class="meta-value">{{ $visit->details ?: __('appointments.no_details') }}</div>
                        </div>
                    </div>
                </div>
            @empty
                <div class="alert alert-info mb-0">{{ __('appointments.no_visits') }}</div>
            @endforelse
        </div>
    </div>

    <div class="modal fade" id="addVisitModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-lg modal-dialog-scrollable">
            <div class="modal-content">
                <form method="POST" action="{{ route('appointments.visits.store', $appointment) }}">
                    @csrf
                    <div class="modal-header">
                        <h5 class="modal-title">{{ __('appointments.add_visit') }}</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="{{ __('common.cancel') }}"></button>
                    </div>
                    <div class="modal-body">
                        <div class="row g-3">
                            <div class="col-12 col-md-6">
                                <label class="form-label">{{ __('appointments.visit_date') }}</label>
                                <input type="date" class="form-control" name="visit_date" value="{{ old('visit_date') }}" required>
                            </div>
                            <div class="col-12 col-md-6">
                                <label class="form-label">{{ __('appointments.visit_time') }}</label>
                                <input type="time" class="form-control" name="visit_time" value="{{ old('visit_time') }}" required>
                            </div>
                            <div class="col-12">
                                <label class="form-label">{{ __('appointments.visit_details') }}</label>
                                <textarea class="form-control" name="details" rows="3">{{ old('details') }}</textarea>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">{{ __('common.cancel') }}</button>
                        <button type="submit" class="btn btn-primary">{{ __('common.save') }}</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <div id="appointmentsShowConfig" data-open-add-visit="{{ $errors->any() ? 1 : 0 }}"></div>
@endsection

@push('scripts')
    <script>
        (function () {
            const configEl = document.getElementById('appointmentsShowConfig');
            const shouldOpen = configEl && (configEl.getAttribute('data-open-add-visit') === '1');
            if (!shouldOpen) return;
            const modalEl = document.getElementById('addVisitModal');
            if (modalEl) new bootstrap.Modal(modalEl).show();
        })();
    </script>
@endpush
